From 9eca96aa214cb8e2fd695cbed179f93826b3ef46 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Fri, 24 Jan 2020 17:10:12 -0600 Subject: [PATCH] rt: improve "no runtime" panic messages (#2145) --- tokio/src/io/driver/mod.rs | 3 ++- tokio/src/time/driver/handle.rs | 3 ++- tokio/src/time/error.rs | 2 +- tokio/tests/no_rt.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tokio/tests/no_rt.rs diff --git a/tokio/src/io/driver/mod.rs b/tokio/src/io/driver/mod.rs index fb3104f157a..8385448c186 100644 --- a/tokio/src/io/driver/mod.rs +++ b/tokio/src/io/driver/mod.rs @@ -198,7 +198,8 @@ impl Handle { /// /// This function panics if there is no current reactor set. pub(super) fn current() -> Self { - context::io_handle().expect("no current reactor") + context::io_handle() + .expect("there is no reactor running, must be called from the context of Tokio runtime") } /// Forces a reactor blocked in a call to `turn` to wakeup, or otherwise diff --git a/tokio/src/time/driver/handle.rs b/tokio/src/time/driver/handle.rs index 3a424800b85..38b1761c8e4 100644 --- a/tokio/src/time/driver/handle.rs +++ b/tokio/src/time/driver/handle.rs @@ -21,7 +21,8 @@ impl Handle { /// /// This function panics if there is no current timer set. pub(crate) fn current() -> Self { - context::time_handle().expect("no current timer") + context::time_handle() + .expect("there is no timer running, must be called from the context of Tokio runtime") } /// Tries to return a strong ref to the inner diff --git a/tokio/src/time/error.rs b/tokio/src/time/error.rs index 82a17275f50..0667b97ac1e 100644 --- a/tokio/src/time/error.rs +++ b/tokio/src/time/error.rs @@ -64,7 +64,7 @@ impl fmt::Display for Error { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { use self::Kind::*; let descr = match self.0 { - Shutdown => "timer is shutdown", + Shutdown => "the timer is shutdown, must be called from the context of Tokio runtime", AtCapacity => "timer is at capacity and cannot create a new entry", }; write!(fmt, "{}", descr) diff --git a/tokio/tests/no_rt.rs b/tokio/tests/no_rt.rs new file mode 100644 index 00000000000..962eed7952d --- /dev/null +++ b/tokio/tests/no_rt.rs @@ -0,0 +1,27 @@ +use tokio::net::TcpStream; +use tokio::sync::oneshot; +use tokio::time::{timeout, Duration}; + +use futures::executor::block_on; + +use std::net::TcpListener; + +#[test] +#[should_panic(expected = "no timer running")] +fn panics_when_no_timer() { + block_on(timeout_value()); +} + +#[test] +#[should_panic(expected = "no reactor running")] +fn panics_when_no_reactor() { + let srv = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = srv.local_addr().unwrap(); + block_on(TcpStream::connect(&addr)).unwrap(); +} + +async fn timeout_value() { + let (_tx, rx) = oneshot::channel::<()>(); + let dur = Duration::from_millis(20); + let _ = timeout(dur, rx).await; +}