Skip to content

Commit 87e55b5

Browse files
committed
Test futures with spawn_local
1 parent 77bbf31 commit 87e55b5

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ tokio = { version = "=1.38.1", default-features = false, features = ["sync"] }
1414

1515
[dev-dependencies]
1616
tokio = { version = "=1.38.1", features = ["full"] }
17+
futures = "0.3"

tests/futures_tests.rs

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use futures::StreamExt;
2+
use ticked_async_executor::TickedAsyncExecutor;
3+
4+
const DELTA: f64 = 1000.0 / 60.0;
5+
6+
#[test]
7+
fn test_futures_join() {
8+
let executor = TickedAsyncExecutor::default();
9+
10+
let (mut tx1, mut rx1) = futures::channel::mpsc::channel::<usize>(1);
11+
let (mut tx2, mut rx2) = futures::channel::mpsc::channel::<usize>(1);
12+
executor
13+
.spawn_local("ThreadedFuture", async move {
14+
let (a, b) = futures::join!(rx1.next(), rx2.next());
15+
assert_eq!(a.unwrap(), 10);
16+
assert_eq!(b.unwrap(), 20);
17+
})
18+
.detach();
19+
20+
let (mut tx3, mut rx3) = futures::channel::mpsc::channel::<usize>(1);
21+
let (mut tx4, mut rx4) = futures::channel::mpsc::channel::<usize>(1);
22+
executor
23+
.spawn_local("LocalFuture", async move {
24+
let (a, b) = futures::join!(rx3.next(), rx4.next());
25+
assert_eq!(a.unwrap(), 10);
26+
assert_eq!(b.unwrap(), 20);
27+
})
28+
.detach();
29+
30+
tx1.try_send(10).unwrap();
31+
tx3.try_send(10).unwrap();
32+
for _ in 0..10 {
33+
executor.tick(DELTA);
34+
}
35+
tx2.try_send(20).unwrap();
36+
tx4.try_send(20).unwrap();
37+
38+
while executor.num_tasks() != 0 {
39+
executor.tick(DELTA);
40+
}
41+
}
42+
43+
#[test]
44+
fn test_futures_select() {
45+
let executor = TickedAsyncExecutor::default();
46+
47+
let (mut tx1, mut rx1) = futures::channel::mpsc::channel::<usize>(1);
48+
let (_tx2, mut rx2) = futures::channel::mpsc::channel::<usize>(1);
49+
executor
50+
.spawn_local("ThreadedFuture", async move {
51+
futures::select! {
52+
data = rx1.next() => {
53+
assert_eq!(data.unwrap(), 10);
54+
}
55+
_ = rx2.next() => {}
56+
}
57+
})
58+
.detach();
59+
60+
let (mut tx3, mut rx3) = futures::channel::mpsc::channel::<usize>(1);
61+
let (_tx4, mut rx4) = futures::channel::mpsc::channel::<usize>(1);
62+
executor
63+
.spawn_local("LocalFuture", async move {
64+
futures::select! {
65+
data = rx3.next() => {
66+
assert_eq!(data.unwrap(), 10);
67+
}
68+
_ = rx4.next() => {}
69+
}
70+
})
71+
.detach();
72+
73+
for _ in 0..10 {
74+
executor.tick(DELTA);
75+
}
76+
77+
tx1.try_send(10).unwrap();
78+
tx3.try_send(10).unwrap();
79+
while executor.num_tasks() != 0 {
80+
executor.tick(DELTA);
81+
}
82+
}

0 commit comments

Comments
 (0)