Skip to content

Commit

Permalink
fix: improves panic messages when there is no reactor/timer
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Jan 22, 2020
1 parent 5d82ac2 commit fbc3439
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion tokio/src/io/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tokio/src/time/driver/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

/// Try to return a strong ref to the inner
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/time/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions tokio/tests/no_rt.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit fbc3439

Please sign in to comment.