1 | #![warn (rust_2018_idioms)] |
2 | |
3 | use std::io; |
4 | use tokio::io::{AsyncReadExt, AsyncWriteExt}; |
5 | use tokio::time::{Duration, Instant}; |
6 | use tokio_test::io::Builder; |
7 | |
8 | #[tokio::test ] |
9 | async fn read() { |
10 | let mut mock = Builder::new().read(b"hello " ).read(b"world!" ).build(); |
11 | |
12 | let mut buf = [0; 256]; |
13 | |
14 | let n = mock.read(&mut buf).await.expect("read 1" ); |
15 | assert_eq!(&buf[..n], b"hello " ); |
16 | |
17 | let n = mock.read(&mut buf).await.expect("read 2" ); |
18 | assert_eq!(&buf[..n], b"world!" ); |
19 | } |
20 | |
21 | #[tokio::test ] |
22 | async fn read_error() { |
23 | let error = io::Error::new(io::ErrorKind::Other, "cruel" ); |
24 | let mut mock = Builder::new() |
25 | .read(b"hello " ) |
26 | .read_error(error) |
27 | .read(b"world!" ) |
28 | .build(); |
29 | let mut buf = [0; 256]; |
30 | |
31 | let n = mock.read(&mut buf).await.expect("read 1" ); |
32 | assert_eq!(&buf[..n], b"hello " ); |
33 | |
34 | match mock.read(&mut buf).await { |
35 | Err(error) => { |
36 | assert_eq!(error.kind(), io::ErrorKind::Other); |
37 | assert_eq!("cruel" , format!("{}" , error)); |
38 | } |
39 | Ok(_) => panic!("error not received" ), |
40 | } |
41 | |
42 | let n = mock.read(&mut buf).await.expect("read 1" ); |
43 | assert_eq!(&buf[..n], b"world!" ); |
44 | } |
45 | |
46 | #[tokio::test ] |
47 | async fn write() { |
48 | let mut mock = Builder::new().write(b"hello " ).write(b"world!" ).build(); |
49 | |
50 | mock.write_all(b"hello " ).await.expect("write 1" ); |
51 | mock.write_all(b"world!" ).await.expect("write 2" ); |
52 | } |
53 | |
54 | #[tokio::test ] |
55 | async fn write_with_handle() { |
56 | let (mut mock, mut handle) = Builder::new().build_with_handle(); |
57 | handle.write(b"hello " ); |
58 | handle.write(b"world!" ); |
59 | |
60 | mock.write_all(b"hello " ).await.expect("write 1" ); |
61 | mock.write_all(b"world!" ).await.expect("write 2" ); |
62 | } |
63 | |
64 | #[tokio::test ] |
65 | async fn read_with_handle() { |
66 | let (mut mock, mut handle) = Builder::new().build_with_handle(); |
67 | handle.read(b"hello " ); |
68 | handle.read(b"world!" ); |
69 | |
70 | let mut buf = vec![0; 6]; |
71 | mock.read_exact(&mut buf).await.expect("read 1" ); |
72 | assert_eq!(&buf[..], b"hello " ); |
73 | mock.read_exact(&mut buf).await.expect("read 2" ); |
74 | assert_eq!(&buf[..], b"world!" ); |
75 | } |
76 | |
77 | #[tokio::test ] |
78 | async fn write_error() { |
79 | let error = io::Error::new(io::ErrorKind::Other, "cruel" ); |
80 | let mut mock = Builder::new() |
81 | .write(b"hello " ) |
82 | .write_error(error) |
83 | .write(b"world!" ) |
84 | .build(); |
85 | mock.write_all(b"hello " ).await.expect("write 1" ); |
86 | |
87 | match mock.write_all(b"whoa" ).await { |
88 | Err(error) => { |
89 | assert_eq!(error.kind(), io::ErrorKind::Other); |
90 | assert_eq!("cruel" , format!("{}" , error)); |
91 | } |
92 | Ok(_) => panic!("error not received" ), |
93 | } |
94 | |
95 | mock.write_all(b"world!" ).await.expect("write 2" ); |
96 | } |
97 | |
98 | #[tokio::test ] |
99 | #[should_panic ] |
100 | async fn mock_panics_read_data_left() { |
101 | use tokio_test::io::Builder; |
102 | Builder::new().read(b"read" ).build(); |
103 | } |
104 | |
105 | #[tokio::test ] |
106 | #[should_panic ] |
107 | async fn mock_panics_write_data_left() { |
108 | use tokio_test::io::Builder; |
109 | Builder::new().write(b"write" ).build(); |
110 | } |
111 | |
112 | #[tokio::test (start_paused = true)] |
113 | async fn wait() { |
114 | const FIRST_WAIT: Duration = Duration::from_secs(1); |
115 | |
116 | let mut mock = Builder::new() |
117 | .wait(FIRST_WAIT) |
118 | .read(b"hello " ) |
119 | .read(b"world!" ) |
120 | .build(); |
121 | |
122 | let mut buf = [0; 256]; |
123 | |
124 | let start = Instant::now(); // record the time the read call takes |
125 | // |
126 | let n = mock.read(&mut buf).await.expect("read 1" ); |
127 | assert_eq!(&buf[..n], b"hello " ); |
128 | println!("time elapsed after first read {:?}" , start.elapsed()); |
129 | |
130 | let n = mock.read(&mut buf).await.expect("read 2" ); |
131 | assert_eq!(&buf[..n], b"world!" ); |
132 | println!("time elapsed after second read {:?}" , start.elapsed()); |
133 | |
134 | // make sure the .wait() instruction worked |
135 | assert!( |
136 | start.elapsed() >= FIRST_WAIT, |
137 | "consuming the whole mock only took {}ms" , |
138 | start.elapsed().as_millis() |
139 | ); |
140 | } |
141 | |
142 | #[tokio::test (start_paused = true)] |
143 | async fn multiple_wait() { |
144 | const FIRST_WAIT: Duration = Duration::from_secs(1); |
145 | const SECOND_WAIT: Duration = Duration::from_secs(1); |
146 | |
147 | let mut mock = Builder::new() |
148 | .wait(FIRST_WAIT) |
149 | .read(b"hello " ) |
150 | .wait(SECOND_WAIT) |
151 | .read(b"world!" ) |
152 | .build(); |
153 | |
154 | let mut buf = [0; 256]; |
155 | |
156 | let start = Instant::now(); // record the time it takes to consume the mock |
157 | |
158 | let n = mock.read(&mut buf).await.expect("read 1" ); |
159 | assert_eq!(&buf[..n], b"hello " ); |
160 | println!("time elapsed after first read {:?}" , start.elapsed()); |
161 | |
162 | let n = mock.read(&mut buf).await.expect("read 2" ); |
163 | assert_eq!(&buf[..n], b"world!" ); |
164 | println!("time elapsed after second read {:?}" , start.elapsed()); |
165 | |
166 | // make sure the .wait() instruction worked |
167 | assert!( |
168 | start.elapsed() >= FIRST_WAIT + SECOND_WAIT, |
169 | "consuming the whole mock only took {}ms" , |
170 | start.elapsed().as_millis() |
171 | ); |
172 | } |
173 | |