File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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
17+ . unwrap ( ) ; // We should panic with a timeout error
18+ } ) ;
19+ }
20+
21+ #[ test]
22+ #[ should_panic( expected = "My custom error" ) ]
23+ fn io_timeout_future_err ( ) {
24+ task:: block_on ( async {
25+ io:: timeout ( Duration :: from_secs ( 1 ) , async {
26+ Err :: < ( ) , io:: Error > ( io:: Error :: new ( io:: ErrorKind :: Other , "My custom error" ) )
27+ } )
28+ . await
29+ . unwrap ( ) ; // We should panic with our own error
30+ } ) ;
31+ }
32+
33+ #[ test]
34+ fn io_timeout_future_ok ( ) {
35+ task:: block_on ( async {
36+ io:: timeout ( Duration :: from_secs ( 1 ) , async { Ok ( ( ) ) } )
37+ . await
38+ . unwrap ( ) ; // We shouldn't panic at all
39+ } ) ;
40+ }
You can’t perform that action at this time.
0 commit comments