Skip to content

Commit 4041e8d

Browse files
committed
add tokio example/test
1 parent 9c8246b commit 4041e8d

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ version = "^0.7"
5454
default-features = false
5555
features = ["compat"]
5656

57+
[dev-dependencies.tokio]
58+
version = "^1.47"
59+
features = ["macros", "rt-multi-thread"]
60+
61+
[[example]]
62+
name = "tokio"
63+
required-features = ["tokio"]
64+
5765
[package.metadata.docs.rs]
5866
all-features = true
5967

examples/tokio.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use async_rs::{Executor, Reactor, Runtime, TokioRuntime};
2+
use std::{io, sync::Arc, time::Duration};
3+
4+
async fn get_a(rt: Arc<TokioRuntime>) -> io::Result<u32> {
5+
rt.clone()
6+
.spawn_blocking(move || rt.block_on(async { Ok(12) }))
7+
.await
8+
}
9+
10+
async fn get_b(rt: Arc<TokioRuntime>) -> io::Result<u32> {
11+
rt.spawn(async { Ok(30) }).await
12+
}
13+
14+
async fn tokio_main() -> io::Result<()> {
15+
let rt = Arc::new(Runtime::tokio());
16+
let a = get_a(rt.clone()).await?;
17+
let b = get_b(rt.clone()).await?;
18+
rt.sleep(Duration::from_millis(500)).await;
19+
assert_eq!(a + b, 42);
20+
Ok(())
21+
}
22+
23+
#[tokio::main]
24+
async fn main() -> io::Result<()> {
25+
tokio_main().await
26+
}
27+
28+
#[tokio::test]
29+
async fn tokio() -> io::Result<()> {
30+
tokio_main().await
31+
}

tests/tokio.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../examples/tokio.rs

0 commit comments

Comments
 (0)