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
|
//! Tests for the never channel flavor.
use std::thread;
use std::time::{Duration, Instant};
use crossbeam_channel::{never, select, tick, unbounded};
fn ms(ms: u64) -> Duration {
Duration::from_millis(ms)
}
#[test]
fn smoke() {
select! {
recv(never::<i32>()) -> _ => panic!(),
default => {}
}
}
#[test]
fn optional() {
let (s, r) = unbounded::<i32>();
s.send(1).unwrap();
s.send(2).unwrap();
let mut r = Some(&r);
select! {
recv(r.unwrap_or(&never())) -> _ => {}
default => panic!(),
}
r = None;
select! {
recv(r.unwrap_or(&never())) -> _ => panic!(),
default => {}
}
}
#[test]
fn tick_n() {
let mut r = tick(ms(100));
let mut step = 0;
loop {
select! {
recv(r) -> _ => step += 1,
default(ms(500)) => break,
}
if step == 10 {
r = never();
}
}
assert_eq!(step, 10);
}
#[test]
fn capacity() {
let r = never::<i32>();
assert_eq!(r.capacity(), Some(0));
}
#[test]
fn len_empty_full() {
let r = never::<i32>();
assert_eq!(r.len(), 0);
assert!(r.is_empty());
assert!(r.is_full());
}
#[test]
fn try_recv() {
let r = never::<i32>();
assert!(r.try_recv().is_err());
thread::sleep(ms(100));
assert!(r.try_recv().is_err());
}
#[test]
fn recv_timeout() {
let start = Instant::now();
let r = never::<i32>();
assert!(r.recv_timeout(ms(100)).is_err());
let now = Instant::now();
assert!(now - start >= ms(100));
assert!(now - start <= ms(150));
assert!(r.recv_timeout(ms(100)).is_err());
let now = Instant::now();
assert!(now - start >= ms(200));
assert!(now - start <= ms(250));
}
|