Skip to content

Commit fb2f31b

Browse files
committed
add tests for io::timeout
1 parent bcb4910 commit fb2f31b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/io_timeout.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::time::Duration;
2+
3+
use async_std::io;
4+
use async_std::task;
5+
6+
#[test]
7+
#[should_panic(expected = "timed out")]
8+
fn io_timeout_timedout() {
9+
task::block_on(async {
10+
io::timeout(Duration::from_secs(1), async {
11+
let stdin = io::stdin();
12+
let mut line = String::new();
13+
let _n = stdin.read_line(&mut line).await?;
14+
Ok(())
15+
})
16+
.await.unwrap(); // We should panic with a timeout error
17+
});
18+
}
19+
20+
#[test]
21+
#[should_panic(expected = "My custom error")]
22+
fn io_timeout_future_err() {
23+
task::block_on(async {
24+
io::timeout(Duration::from_secs(1), async {
25+
Err::<(), io::Error>(io::Error::new(io::ErrorKind::Other, "My custom error"))
26+
})
27+
.await.unwrap(); // We should panic with our own error
28+
});
29+
}
30+
31+
#[test]
32+
fn io_timeout_future_ok() {
33+
task::block_on(async {
34+
io::timeout(Duration::from_secs(1), async {
35+
Ok(())
36+
})
37+
.await.unwrap(); // We shouldn't panic at all
38+
});
39+
}

0 commit comments

Comments
 (0)