Skip to content

Commit

Permalink
fix reactor drop panic
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Apr 29, 2019
1 parent d2c1791 commit f4e1205
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,25 @@ use crate::service::{ServiceRequest, ServiceResponse};
use crate::{Error, HttpRequest, HttpResponse};

thread_local! {
static RT: RefCell<Runtime> = {
RefCell::new(Runtime::new().unwrap())
static RT: RefCell<Inner> = {
RefCell::new(Inner(Some(Runtime::new().unwrap())))
};
}

struct Inner(Option<Runtime>);

impl Inner {
fn get_mut(&mut self) -> &mut Runtime {
self.0.as_mut().unwrap()
}
}

impl Drop for Inner {
fn drop(&mut self) {
std::mem::forget(self.0.take().unwrap())
}
}

/// Runs the provided future, blocking the current thread until the future
/// completes.
///
Expand All @@ -47,7 +61,7 @@ pub fn block_on<F>(f: F) -> Result<F::Item, F::Error>
where
F: Future,
{
RT.with(move |rt| rt.borrow_mut().block_on(f))
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(f))
}

/// Runs the provided function, blocking the current thread until the resul
Expand All @@ -65,7 +79,7 @@ where
F: FnOnce() -> R,
R: IntoFuture,
{
RT.with(move |rt| rt.borrow_mut().block_on(f().into_future()))
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(lazy(|| f())))
}

#[doc(hidden)]
Expand All @@ -77,8 +91,12 @@ pub fn run_on<F, R>(f: F) -> R
where
F: FnOnce() -> R,
{
RT.with(move |rt| rt.borrow_mut().block_on(lazy(|| Ok::<_, ()>(f()))))
.unwrap()
RT.with(move |rt| {
rt.borrow_mut()
.get_mut()
.block_on(lazy(|| Ok::<_, ()>(f())))
})
.unwrap()
}

/// Create service that always responds with `HttpResponse::Ok()`
Expand Down Expand Up @@ -657,7 +675,7 @@ mod tests {
);

let req = TestRequest::post().uri("/index.html").to_request();
let res = block_on(app.call(req)).unwrap();
let res = block_fn(|| app.call(req)).unwrap();
assert!(res.status().is_success());
}
}

0 comments on commit f4e1205

Please sign in to comment.