-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.rs
107 lines (91 loc) · 2.45 KB
/
local.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::future::pending;
use std::rc::Rc;
use futures::channel::oneshot::channel;
use futures::executor::block_on;
use futures::{select_biased, FutureExt};
use futures_scopes::local::LocalSpawnScope;
#[test]
fn test_mutate_outer() {
let mut called = true;
{
let mut scope = LocalSpawnScope::new();
scope
.spawner()
.spawn_local_scoped(async {
called = true;
})
.unwrap();
block_on(scope.until_empty());
}
assert!(called);
}
#[test]
fn test_drop_without_spawner() {
let counter = Rc::new(());
{
let scope = LocalSpawnScope::new();
for _ in 0..50 {
let counter = counter.clone();
scope
.spawner()
.spawn_local_scoped(async move {
let _counter = counter;
pending::<()>().await
})
.unwrap();
}
}
assert_eq!(1, Rc::strong_count(&counter));
}
#[test]
fn test_spawn_outside_until_empty() {
let mut scope = LocalSpawnScope::new();
let spawner = scope.spawner();
let (sx, rx) = channel();
let f = async {
// at this point scope.until_empty().fuse() should have been polled
// and returned pending
// Let's test if it can wake up when spawning a new future
spawner
.spawn_local_scoped(async {
sx.send(()).unwrap();
})
.unwrap();
pending::<()>().await;
};
block_on(async {
spawner
.spawn_local_scoped(async {
rx.await.unwrap();
})
.unwrap();
scope.until_stalled().await;
select_biased! {
_ = scope.until_empty().fuse() => (),
_ = f.fuse() => (),
};
});
}
#[test]
fn test_spawn_outside_until() {
let mut scope = LocalSpawnScope::new();
let spawner = scope.spawner();
let (sx, rx) = channel();
let f = async {
// at this point scope.until(rx).fuse() should have been polled
// and returned pending
// Let's test if it can wake up when spawning a new future
spawner
.spawn_local_scoped(async {
sx.send(()).unwrap();
})
.unwrap();
pending::<()>().await;
};
block_on(async {
select_biased! {
_ = scope.until(rx).fuse() => (),
_ = f.fuse() => (),
};
});
}