1 //! Tests for the never channel flavor.
2
3 use std::thread;
4 use std::time::{Duration, Instant};
5
6 use crossbeam_channel::{never, select, tick, unbounded};
7
ms(ms: u64) -> Duration8 fn ms(ms: u64) -> Duration {
9 Duration::from_millis(ms)
10 }
11
12 #[test]
smoke()13 fn smoke() {
14 select! {
15 recv(never::<i32>()) -> _ => panic!(),
16 default => {}
17 }
18 }
19
20 #[test]
optional()21 fn optional() {
22 let (s, r) = unbounded::<i32>();
23 s.send(1).unwrap();
24 s.send(2).unwrap();
25
26 let mut r = Some(&r);
27 select! {
28 recv(r.unwrap_or(&never())) -> _ => {}
29 default => panic!(),
30 }
31
32 r = None;
33 select! {
34 recv(r.unwrap_or(&never())) -> _ => panic!(),
35 default => {}
36 }
37 }
38
39 #[test]
tick_n()40 fn tick_n() {
41 let mut r = tick(ms(100));
42 let mut step = 0;
43
44 loop {
45 select! {
46 recv(r) -> _ => step += 1,
47 default(ms(500)) => break,
48 }
49
50 if step == 10 {
51 r = never();
52 }
53 }
54
55 assert_eq!(step, 10);
56 }
57
58 #[test]
capacity()59 fn capacity() {
60 let r = never::<i32>();
61 assert_eq!(r.capacity(), Some(0));
62 }
63
64 #[test]
len_empty_full()65 fn len_empty_full() {
66 let r = never::<i32>();
67 assert_eq!(r.len(), 0);
68 assert_eq!(r.is_empty(), true);
69 assert_eq!(r.is_full(), true);
70 }
71
72 #[test]
try_recv()73 fn try_recv() {
74 let r = never::<i32>();
75 assert!(r.try_recv().is_err());
76
77 thread::sleep(ms(100));
78 assert!(r.try_recv().is_err());
79 }
80
81 #[test]
recv_timeout()82 fn recv_timeout() {
83 let start = Instant::now();
84 let r = never::<i32>();
85
86 assert!(r.recv_timeout(ms(100)).is_err());
87 let now = Instant::now();
88 assert!(now - start >= ms(100));
89 assert!(now - start <= ms(150));
90
91 assert!(r.recv_timeout(ms(100)).is_err());
92 let now = Instant::now();
93 assert!(now - start >= ms(200));
94 assert!(now - start <= ms(250));
95 }
96