blob: 104e75798d4e1d89462ec32cca28c192c6b37460 [file] [log] [blame]
<!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."><meta name="keywords" content="rust, rustlang, rust-lang, test"><title>test in tokio - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../tokio/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../tokio/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2><a href="index.html">In tokio</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Attribute Macro <a href="index.html">tokio</a>::<wbr><a class="attr" href="#">test</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust attr"><code>#[test]</code></pre></div><details class="rustdoc-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 href="#multi-threaded-runtime">Multi-threaded runtime</a></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="attribute">#[tokio::test(flavor = <span class="string">&quot;multi_thread&quot;</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. This is the default
flavor.</p>
<p>Note: The multi-threaded runtime requires the <code>rt-multi-thread</code> feature
flag.</p>
<h2 id="current-thread-runtime"><a href="#current-thread-runtime">Current thread runtime</a></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="attribute">#[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 href="#usage">Usage</a></h3><h4 id="using-the-multi-thread-runtime"><a href="#using-the-multi-thread-runtime">Using the multi-thread runtime</a></h4>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[tokio::test(flavor = <span class="string">&quot;multi_thread&quot;</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="attribute">#[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 href="#using-current-thread-runtime">Using current thread runtime</a></h4>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[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="attribute">#[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 href="#set-number-of-worker-threads">Set number of worker threads</a></h4>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[tokio::test(flavor =<span class="string">&quot;multi_thread&quot;</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="attribute">#[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 href="#configure-the-runtime-to-start-with-time-paused">Configure the runtime to start with time paused</a></h4>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[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="attribute">#[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 href="#rename-package">Rename package</a></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="attribute">#[tokio1::test(<span class="kw">crate </span>= <span class="string">&quot;tokio1&quot;</span>)]
</span><span class="kw">async fn </span>my_test() {
<span class="macro">println!</span>(<span class="string">&quot;Hello world&quot;</span>);
}</code></pre></div>
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="tokio" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>