From b95dd13d3bf760ba23861e561c172958404749f7 Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Wed, 21 Aug 2019 12:57:00 +0300 Subject: [PATCH] add tests for io::timeout --- tests/io_timeout.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/io_timeout.rs diff --git a/tests/io_timeout.rs b/tests/io_timeout.rs new file mode 100644 index 00000000..85a17ab7 --- /dev/null +++ b/tests/io_timeout.rs @@ -0,0 +1,40 @@ +use std::time::Duration; + +use async_std::io; +use async_std::task; + +#[test] +#[should_panic(expected = "timed out")] +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 + }); +}