File: io_timeout.rs

package info (click to toggle)
rust-async-std 1.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,992 kB
  • sloc: sh: 13; makefile: 8
file content (48 lines) | stat: -rw-r--r-- 1,253 bytes parent folder | download | duplicates (3)
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
use std::time::Duration;

use async_std::io;
use async_std::task;

#[test]
#[should_panic(expected = "timed out")]
#[cfg(not(any(
    target_os = "unknown",
    target_arch = "arm",
    target_arch = "mips",
    target_arch = "powerpc",
    target_arch = "powerpc64",
    target_arch = "x86",
)))] // stdin tests fail when running through cross
fn io_timeout_timedout() {
    task::block_on(async {
        io::timeout(Duration::from_secs(1), async {
            let stdin = io::stdin();
            let mut line = String::new();
            let _n = stdin.read_line(&mut line).await?;
            Ok(())
        })
        .await
        .unwrap(); // We should panic with a timeout error
    });
}

#[test]
#[should_panic(expected = "My custom error")]
fn io_timeout_future_err() {
    task::block_on(async {
        io::timeout(Duration::from_secs(1), async {
            Err::<(), io::Error>(io::Error::new(io::ErrorKind::Other, "My custom error"))
        })
        .await
        .unwrap(); // We should panic with our own error
    });
}

#[test]
fn io_timeout_future_ok() {
    task::block_on(async {
        io::timeout(Duration::from_secs(1), async { Ok(()) })
            .await
            .unwrap(); // We shouldn't panic at all
    });
}