rt: improve "no runtime" panic messages (#2145)

This commit is contained in:
Avery Harnish
2020-01-24 15:10:11 -08:00
committed by Carl Lerche
parent a16c9a5a01
commit 9eca96aa21
4 changed files with 32 additions and 3 deletions
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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
+1 -1
View File
@@ -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)
+27
View File
@@ -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;
}