• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::sync::CancellationToken;
2 
3 use loom::{future::block_on, thread};
4 use tokio_test::assert_ok;
5 
6 #[test]
cancel_token()7 fn cancel_token() {
8     loom::model(|| {
9         let token = CancellationToken::new();
10         let token1 = token.clone();
11 
12         let th1 = thread::spawn(move || {
13             block_on(async {
14                 token1.cancelled().await;
15             });
16         });
17 
18         let th2 = thread::spawn(move || {
19             token.cancel();
20         });
21 
22         assert_ok!(th1.join());
23         assert_ok!(th2.join());
24     });
25 }
26 
27 #[test]
cancel_token_owned()28 fn cancel_token_owned() {
29     loom::model(|| {
30         let token = CancellationToken::new();
31         let token1 = token.clone();
32 
33         let th1 = thread::spawn(move || {
34             block_on(async {
35                 token1.cancelled_owned().await;
36             });
37         });
38 
39         let th2 = thread::spawn(move || {
40             token.cancel();
41         });
42 
43         assert_ok!(th1.join());
44         assert_ok!(th2.join());
45     });
46 }
47 
48 #[test]
cancel_with_child()49 fn cancel_with_child() {
50     loom::model(|| {
51         let token = CancellationToken::new();
52         let token1 = token.clone();
53         let token2 = token.clone();
54         let child_token = token.child_token();
55 
56         let th1 = thread::spawn(move || {
57             block_on(async {
58                 token1.cancelled().await;
59             });
60         });
61 
62         let th2 = thread::spawn(move || {
63             token2.cancel();
64         });
65 
66         let th3 = thread::spawn(move || {
67             block_on(async {
68                 child_token.cancelled().await;
69             });
70         });
71 
72         assert_ok!(th1.join());
73         assert_ok!(th2.join());
74         assert_ok!(th3.join());
75     });
76 }
77 
78 #[test]
drop_token_no_child()79 fn drop_token_no_child() {
80     loom::model(|| {
81         let token = CancellationToken::new();
82         let token1 = token.clone();
83         let token2 = token.clone();
84 
85         let th1 = thread::spawn(move || {
86             drop(token1);
87         });
88 
89         let th2 = thread::spawn(move || {
90             drop(token2);
91         });
92 
93         let th3 = thread::spawn(move || {
94             drop(token);
95         });
96 
97         assert_ok!(th1.join());
98         assert_ok!(th2.join());
99         assert_ok!(th3.join());
100     });
101 }
102 
103 #[test]
drop_token_with_children()104 fn drop_token_with_children() {
105     loom::model(|| {
106         let token1 = CancellationToken::new();
107         let child_token1 = token1.child_token();
108         let child_token2 = token1.child_token();
109 
110         let th1 = thread::spawn(move || {
111             drop(token1);
112         });
113 
114         let th2 = thread::spawn(move || {
115             drop(child_token1);
116         });
117 
118         let th3 = thread::spawn(move || {
119             drop(child_token2);
120         });
121 
122         assert_ok!(th1.join());
123         assert_ok!(th2.join());
124         assert_ok!(th3.join());
125     });
126 }
127 
128 #[test]
drop_and_cancel_token()129 fn drop_and_cancel_token() {
130     loom::model(|| {
131         let token1 = CancellationToken::new();
132         let token2 = token1.clone();
133         let child_token = token1.child_token();
134 
135         let th1 = thread::spawn(move || {
136             drop(token1);
137         });
138 
139         let th2 = thread::spawn(move || {
140             token2.cancel();
141         });
142 
143         let th3 = thread::spawn(move || {
144             drop(child_token);
145         });
146 
147         assert_ok!(th1.join());
148         assert_ok!(th2.join());
149         assert_ok!(th3.join());
150     });
151 }
152 
153 #[test]
cancel_parent_and_child()154 fn cancel_parent_and_child() {
155     loom::model(|| {
156         let token1 = CancellationToken::new();
157         let token2 = token1.clone();
158         let child_token = token1.child_token();
159 
160         let th1 = thread::spawn(move || {
161             drop(token1);
162         });
163 
164         let th2 = thread::spawn(move || {
165             token2.cancel();
166         });
167 
168         let th3 = thread::spawn(move || {
169             child_token.cancel();
170         });
171 
172         assert_ok!(th1.join());
173         assert_ok!(th2.join());
174         assert_ok!(th3.join());
175     });
176 }
177