-
Notifications
You must be signed in to change notification settings - Fork 456
/
tick.rs
350 lines (289 loc) · 8.39 KB
/
tick.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! Tests for the tick channel flavor.
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::thread;
use std::time::{Duration, Instant};
use crossbeam_channel::{after, select, tick, Select, TryRecvError};
use crossbeam_utils::thread::scope;
fn ms(ms: u64) -> Duration {
Duration::from_millis(ms)
}
#[test]
fn fire() {
let start = Instant::now();
let r = tick(ms(50));
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
thread::sleep(ms(100));
let fired = r.try_recv().unwrap();
assert!(start < fired);
assert!(fired - start >= ms(50));
let now = Instant::now();
assert!(fired < now);
assert!(now - fired >= ms(50));
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
select! {
recv(r) -> _ => panic!(),
default => {}
}
select! {
recv(r) -> _ => {}
recv(tick(ms(200))) -> _ => panic!(),
}
}
#[test]
fn intervals() {
let start = Instant::now();
let r = tick(ms(50));
let t1 = r.recv().unwrap();
assert!(start + ms(50) <= t1);
assert!(start + ms(100) > t1);
thread::sleep(ms(300));
let t2 = r.try_recv().unwrap();
assert!(start + ms(100) <= t2);
assert!(start + ms(150) > t2);
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
let t3 = r.recv().unwrap();
assert!(start + ms(400) <= t3);
assert!(start + ms(450) > t3);
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
}
#[test]
fn capacity() {
const COUNT: usize = 10;
for i in 0..COUNT {
let r = tick(ms(i as u64));
assert_eq!(r.capacity(), Some(1));
}
}
#[test]
fn len_empty_full() {
let r = tick(ms(50));
assert_eq!(r.len(), 0);
assert_eq!(r.is_empty(), true);
assert_eq!(r.is_full(), false);
thread::sleep(ms(100));
assert_eq!(r.len(), 1);
assert_eq!(r.is_empty(), false);
assert_eq!(r.is_full(), true);
r.try_recv().unwrap();
assert_eq!(r.len(), 0);
assert_eq!(r.is_empty(), true);
assert_eq!(r.is_full(), false);
}
#[test]
fn try_recv() {
let r = tick(ms(200));
assert!(r.try_recv().is_err());
thread::sleep(ms(100));
assert!(r.try_recv().is_err());
thread::sleep(ms(200));
assert!(r.try_recv().is_ok());
assert!(r.try_recv().is_err());
thread::sleep(ms(200));
assert!(r.try_recv().is_ok());
assert!(r.try_recv().is_err());
}
#[test]
fn recv() {
let start = Instant::now();
let r = tick(ms(50));
let fired = r.recv().unwrap();
assert!(start < fired);
assert!(fired - start >= ms(50));
let now = Instant::now();
assert!(fired < now);
assert!(now - fired < fired - start);
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
}
#[cfg(not(feature = "sanitize"))] // TODO: assertions failed due to tsan is slow
#[test]
fn recv_timeout() {
let start = Instant::now();
let r = tick(ms(200));
assert!(r.recv_timeout(ms(100)).is_err());
let now = Instant::now();
assert!(now - start >= ms(100));
assert!(now - start <= ms(150));
let fired = r.recv_timeout(ms(200)).unwrap();
assert!(fired - start >= ms(200));
assert!(fired - start <= ms(250));
assert!(r.recv_timeout(ms(100)).is_err());
let now = Instant::now();
assert!(now - start >= ms(300));
assert!(now - start <= ms(350));
let fired = r.recv_timeout(ms(200)).unwrap();
assert!(fired - start >= ms(400));
assert!(fired - start <= ms(450));
}
#[test]
fn recv_two() {
let r1 = tick(ms(50));
let r2 = tick(ms(50));
scope(|scope| {
scope.spawn(|_| {
for _ in 0..10 {
select! {
recv(r1) -> _ => {}
recv(r2) -> _ => {}
}
}
});
scope.spawn(|_| {
for _ in 0..10 {
select! {
recv(r1) -> _ => {}
recv(r2) -> _ => {}
}
}
});
})
.unwrap();
}
#[test]
fn recv_race() {
select! {
recv(tick(ms(50))) -> _ => {}
recv(tick(ms(100))) -> _ => panic!(),
}
select! {
recv(tick(ms(100))) -> _ => panic!(),
recv(tick(ms(50))) -> _ => {}
}
}
#[test]
fn stress_default() {
const COUNT: usize = 10;
for _ in 0..COUNT {
select! {
recv(tick(ms(0))) -> _ => {}
default => panic!(),
}
}
for _ in 0..COUNT {
select! {
recv(tick(ms(100))) -> _ => panic!(),
default => {}
}
}
}
#[test]
fn select() {
const THREADS: usize = 4;
let hits = AtomicUsize::new(0);
let r1 = tick(ms(200));
let r2 = tick(ms(300));
scope(|scope| {
for _ in 0..THREADS {
scope.spawn(|_| {
let timeout = after(ms(1100));
loop {
let mut sel = Select::new();
let oper1 = sel.recv(&r1);
let oper2 = sel.recv(&r2);
let oper3 = sel.recv(&timeout);
let oper = sel.select();
match oper.index() {
i if i == oper1 => {
oper.recv(&r1).unwrap();
hits.fetch_add(1, Ordering::SeqCst);
}
i if i == oper2 => {
oper.recv(&r2).unwrap();
hits.fetch_add(1, Ordering::SeqCst);
}
i if i == oper3 => {
oper.recv(&timeout).unwrap();
break;
}
_ => unreachable!(),
}
}
});
}
})
.unwrap();
assert_eq!(hits.load(Ordering::SeqCst), 8);
}
#[cfg(not(feature = "sanitize"))] // TODO: assertions failed due to tsan is slow
#[test]
fn ready() {
const THREADS: usize = 4;
let hits = AtomicUsize::new(0);
let r1 = tick(ms(200));
let r2 = tick(ms(300));
scope(|scope| {
for _ in 0..THREADS {
scope.spawn(|_| {
let timeout = after(ms(1100));
'outer: loop {
let mut sel = Select::new();
sel.recv(&r1);
sel.recv(&r2);
sel.recv(&timeout);
loop {
match sel.ready() {
0 => {
if r1.try_recv().is_ok() {
hits.fetch_add(1, Ordering::SeqCst);
break;
}
}
1 => {
if r2.try_recv().is_ok() {
hits.fetch_add(1, Ordering::SeqCst);
break;
}
}
2 => {
if timeout.try_recv().is_ok() {
break 'outer;
}
}
_ => unreachable!(),
}
}
}
});
}
})
.unwrap();
assert_eq!(hits.load(Ordering::SeqCst), 8);
}
#[test]
fn fairness() {
const COUNT: usize = 30;
for &dur in &[0, 1] {
let mut hits = [0usize; 2];
for _ in 0..COUNT {
let r1 = tick(ms(dur));
let r2 = tick(ms(dur));
for _ in 0..COUNT {
select! {
recv(r1) -> _ => hits[0] += 1,
recv(r2) -> _ => hits[1] += 1,
}
}
}
assert!(hits.iter().all(|x| *x >= COUNT / hits.len() / 2));
}
}
#[test]
fn fairness_duplicates() {
const COUNT: usize = 30;
for &dur in &[0, 1] {
let mut hits = [0usize; 5];
for _ in 0..COUNT {
let r = tick(ms(dur));
for _ in 0..COUNT {
select! {
recv(r) -> _ => hits[0] += 1,
recv(r) -> _ => hits[1] += 1,
recv(r) -> _ => hits[2] += 1,
recv(r) -> _ => hits[3] += 1,
recv(r) -> _ => hits[4] += 1,
}
}
}
assert!(hits.iter().all(|x| *x >= COUNT / hits.len() / 2));
}
}