135 lines
12 KiB
HTML
135 lines
12 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Marks async function to be executed by runtime, suitable to test environment. This macro helps set up a `Runtime` without requiring the user to use Runtime or Builder directly."><title>test in tokio_macros - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="tokio_macros" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc attr"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">test</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../tokio_macros/index.html">tokio_<wbr>macros</a><span class="version">2.6.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">test</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#multi-threaded-runtime" title="Multi-threaded runtime">Multi-threaded runtime</a></li><li><a href="#current-thread-runtime" title="Current thread runtime">Current thread runtime</a><ul><li><a href="#usage" title="Usage">Usage</a></li></ul></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate tokio_<wbr>macros</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="index.html">tokio_macros</a></div><h1>Attribute Macro <span class="attr">test</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/tokio_macros/lib.rs.html#574-576">Source</a> </span></div><pre class="rust item-decl"><code>#[test]</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Marks async function to be executed by runtime, suitable to test environment.
|
||
This macro helps set up a <code>Runtime</code> without requiring the user to use
|
||
<a href="../tokio/runtime/struct.Runtime.html">Runtime</a> or
|
||
<a href="../tokio/runtime/struct.Builder.html">Builder</a> directly.</p>
|
||
<p>Note: This macro is designed to be simplistic and targets applications that
|
||
do not require a complex setup. If the provided functionality is not
|
||
sufficient, you may be interested in using
|
||
<a href="../tokio/runtime/struct.Builder.html">Builder</a>, which provides a more
|
||
powerful interface.</p>
|
||
<h2 id="multi-threaded-runtime"><a class="doc-anchor" href="#multi-threaded-runtime">§</a>Multi-threaded runtime</h2>
|
||
<p>To use the multi-threaded runtime, the macro can be configured using</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::test(flavor = <span class="string">"multi_thread"</span>, worker_threads = <span class="number">1</span>)]
|
||
</span><span class="kw">async fn </span>my_test() {
|
||
<span class="macro">assert!</span>(<span class="bool-val">true</span>);
|
||
}</code></pre></div>
|
||
<p>The <code>worker_threads</code> option configures the number of worker threads, and
|
||
defaults to the number of cpus on the system.</p>
|
||
<p>Note: The multi-threaded runtime requires the <code>rt-multi-thread</code> feature
|
||
flag.</p>
|
||
<h2 id="current-thread-runtime"><a class="doc-anchor" href="#current-thread-runtime">§</a>Current thread runtime</h2>
|
||
<p>The default test runtime is single-threaded. Each test gets a
|
||
separate current-thread runtime.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::test]
|
||
</span><span class="kw">async fn </span>my_test() {
|
||
<span class="macro">assert!</span>(<span class="bool-val">true</span>);
|
||
}</code></pre></div><h3 id="usage"><a class="doc-anchor" href="#usage">§</a>Usage</h3><h4 id="using-the-multi-thread-runtime"><a class="doc-anchor" href="#using-the-multi-thread-runtime">§</a>Using the multi-thread runtime</h4>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::test(flavor = <span class="string">"multi_thread"</span>)]
|
||
</span><span class="kw">async fn </span>my_test() {
|
||
<span class="macro">assert!</span>(<span class="bool-val">true</span>);
|
||
}</code></pre></div>
|
||
<p>Equivalent code not using <code>#[tokio::test]</code></p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[test]
|
||
</span><span class="kw">fn </span>my_test() {
|
||
tokio::runtime::Builder::new_multi_thread()
|
||
.enable_all()
|
||
.build()
|
||
.unwrap()
|
||
.block_on(<span class="kw">async </span>{
|
||
<span class="macro">assert!</span>(<span class="bool-val">true</span>);
|
||
})
|
||
}</code></pre></div><h4 id="using-current-thread-runtime"><a class="doc-anchor" href="#using-current-thread-runtime">§</a>Using current thread runtime</h4>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::test]
|
||
</span><span class="kw">async fn </span>my_test() {
|
||
<span class="macro">assert!</span>(<span class="bool-val">true</span>);
|
||
}</code></pre></div>
|
||
<p>Equivalent code not using <code>#[tokio::test]</code></p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[test]
|
||
</span><span class="kw">fn </span>my_test() {
|
||
tokio::runtime::Builder::new_current_thread()
|
||
.enable_all()
|
||
.build()
|
||
.unwrap()
|
||
.block_on(<span class="kw">async </span>{
|
||
<span class="macro">assert!</span>(<span class="bool-val">true</span>);
|
||
})
|
||
}</code></pre></div><h4 id="set-number-of-worker-threads"><a class="doc-anchor" href="#set-number-of-worker-threads">§</a>Set number of worker threads</h4>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::test(flavor = <span class="string">"multi_thread"</span>, worker_threads = <span class="number">2</span>)]
|
||
</span><span class="kw">async fn </span>my_test() {
|
||
<span class="macro">assert!</span>(<span class="bool-val">true</span>);
|
||
}</code></pre></div>
|
||
<p>Equivalent code not using <code>#[tokio::test]</code></p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[test]
|
||
</span><span class="kw">fn </span>my_test() {
|
||
tokio::runtime::Builder::new_multi_thread()
|
||
.worker_threads(<span class="number">2</span>)
|
||
.enable_all()
|
||
.build()
|
||
.unwrap()
|
||
.block_on(<span class="kw">async </span>{
|
||
<span class="macro">assert!</span>(<span class="bool-val">true</span>);
|
||
})
|
||
}</code></pre></div><h4 id="configure-the-runtime-to-start-with-time-paused"><a class="doc-anchor" href="#configure-the-runtime-to-start-with-time-paused">§</a>Configure the runtime to start with time paused</h4>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tokio::test(start_paused = <span class="bool-val">true</span>)]
|
||
</span><span class="kw">async fn </span>my_test() {
|
||
<span class="macro">assert!</span>(<span class="bool-val">true</span>);
|
||
}</code></pre></div>
|
||
<p>Equivalent code not using <code>#[tokio::test]</code></p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[test]
|
||
</span><span class="kw">fn </span>my_test() {
|
||
tokio::runtime::Builder::new_current_thread()
|
||
.enable_all()
|
||
.start_paused(<span class="bool-val">true</span>)
|
||
.build()
|
||
.unwrap()
|
||
.block_on(<span class="kw">async </span>{
|
||
<span class="macro">assert!</span>(<span class="bool-val">true</span>);
|
||
})
|
||
}</code></pre></div>
|
||
<p>Note that <code>start_paused</code> requires the <code>test-util</code> feature to be enabled.</p>
|
||
<h4 id="rename-package"><a class="doc-anchor" href="#rename-package">§</a>Rename package</h4>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio <span class="kw">as </span>tokio1;
|
||
|
||
<span class="attr">#[tokio1::test(<span class="kw">crate </span>= <span class="string">"tokio1"</span>)]
|
||
</span><span class="kw">async fn </span>my_test() {
|
||
<span class="macro">println!</span>(<span class="string">"Hello world"</span>);
|
||
}</code></pre></div><h4 id="configure-unhandled-panic-behavior"><a class="doc-anchor" href="#configure-unhandled-panic-behavior">§</a>Configure unhandled panic behavior</h4>
|
||
<p>Available options are <code>shutdown_runtime</code> and <code>ignore</code>. For more details, see
|
||
<a href="../tokio/runtime/struct.Builder.html#method.unhandled_panic"><code>Builder::unhandled_panic</code></a>.</p>
|
||
<p>This option is only compatible with the <code>current_thread</code> runtime.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[cfg(tokio_unstable)]
|
||
#[tokio::test(flavor = <span class="string">"current_thread"</span>, unhandled_panic = <span class="string">"shutdown_runtime"</span>)]
|
||
</span><span class="kw">async fn </span>my_test() {
|
||
<span class="kw">let _ </span>= tokio::spawn(<span class="kw">async </span>{
|
||
<span class="macro">panic!</span>(<span class="string">"This panic will shutdown the runtime."</span>);
|
||
}).<span class="kw">await</span>;
|
||
}
|
||
</code></pre></div>
|
||
<p>Equivalent code not using <code>#[tokio::test]</code></p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[cfg(tokio_unstable)]
|
||
#[test]
|
||
</span><span class="kw">fn </span>my_test() {
|
||
tokio::runtime::Builder::new_current_thread()
|
||
.enable_all()
|
||
.unhandled_panic(UnhandledPanic::ShutdownRuntime)
|
||
.build()
|
||
.unwrap()
|
||
.block_on(<span class="kw">async </span>{
|
||
<span class="kw">let _ </span>= tokio::spawn(<span class="kw">async </span>{
|
||
<span class="macro">panic!</span>(<span class="string">"This panic will shutdown the runtime."</span>);
|
||
}).<span class="kw">await</span>;
|
||
})
|
||
}
|
||
</code></pre></div>
|
||
<p><strong>Note</strong>: This option depends on Tokio’s <a href="../tokio/index.html#unstable-features">unstable API</a>. See <a href="../tokio/index.html#unstable-features">the
|
||
documentation on unstable features</a> for details on how to enable
|
||
Tokio’s unstable features.</p>
|
||
</div></details></section></div></main></body></html> |