Skip to content

Commit

Permalink
Add Runtime::enter (#25)
Browse files Browse the repository at this point in the history
* Add `Runtime::enter`

Add a `Runtime::enter` function that allows the caller to enter the
runtime context without calling `run`, `spawn`, or `block_on`. This
mirrors the `enter` function from Tokio 0.2's runtime.

* Fix tests

Fix the current_thread test that would fail all the time. Also fix the
threadpool test which was failing intermittently.

* Clean up unnecessary code
  • Loading branch information
bryanburgers authored and hawkw committed Jan 22, 2020
1 parent f76cee7 commit 1d7f57a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/runtime/current_thread/runtime.rs
Expand Up @@ -409,4 +409,26 @@ impl Runtime {
.try_with(|c| c.borrow().is_some())
.unwrap_or(false)
}

/// Enter the runtime context
pub fn enter<F, R>(&self, f: F) -> R
where
F: FnOnce() -> R,
{
let handle = self.inner.handle().clone();
let Runtime {
ref inner,
ref compat,
ref idle,
..
} = *self;
let mut spawner = compat::CompatSpawner::new(handle, &idle);
let mut enter = executor_01::enter().unwrap();
// Set the default tokio 0.1 reactor to the background compat reactor.
let _reactor = reactor_01::set_default(compat.reactor());
let _timer = timer_02::timer::set_default(compat.timer());
executor_01::with_default(&mut spawner, &mut enter, |_enter| {
Self::with_idle(idle, || inner.enter(f))
})
}
}
14 changes: 14 additions & 0 deletions src/runtime/threadpool/mod.rs
Expand Up @@ -554,6 +554,20 @@ impl Runtime {
fn inner_mut(&mut self) -> &mut Inner {
self.inner.as_mut().unwrap()
}

/// Enter the runtime context
pub fn enter<F, R>(&self, f: F) -> R
where
F: FnOnce() -> R,
{
let spawner = self.spawner();
let inner = self.inner();
let compat = &inner.compat_bg;
let _timer = timer_02::timer::set_default(compat.timer());
let _reactor = reactor_01::set_default(compat.reactor());
let _executor = executor_01::set_default(spawner);
inner.runtime.enter(f)
}
}

impl Drop for Runtime {
Expand Down
24 changes: 24 additions & 0 deletions tests/rt_current_thread.rs
Expand Up @@ -164,3 +164,27 @@ fn idle_after_block_on() {
rt.run().unwrap();
assert!(ran.load(Ordering::SeqCst));
}

#[test]
fn enter_exposed() {
let rt = current_thread::Runtime::new().unwrap();
rt.enter(|| {
let _handle = tokio_02::runtime::Handle::current();
});
}

#[test]
fn enter_can_spawn_01_futures() {
let future_ran = Arc::new(AtomicBool::new(false));
let ran = future_ran.clone();
let mut rt = current_thread::Runtime::new().unwrap();
rt.enter(|| {
tokio_01::spawn(futures_01::future::lazy(move || {
future_ran.store(true, Ordering::SeqCst);
Ok(())
}));
});

rt.run();
assert!(ran.load(Ordering::SeqCst));
}
24 changes: 24 additions & 0 deletions tests/rt_threadpool.rs
Expand Up @@ -182,3 +182,27 @@ fn idle_after_block_on() {
rt.shutdown_on_idle();
assert!(ran.load(Ordering::SeqCst));
}

#[test]
fn enter_exposed() {
let rt = runtime::Runtime::new().unwrap();
rt.enter(|| {
let _handle = tokio_02::runtime::Handle::current();
});
}

#[test]
fn enter_can_spawn_01_futures() {
let future_ran = Arc::new(AtomicBool::new(false));
let ran = future_ran.clone();
let mut rt = runtime::Runtime::new().unwrap();
rt.enter(|| {
tokio_01::spawn(futures_01::future::lazy(move || {
future_ran.store(true, Ordering::SeqCst);
Ok(())
}))
});

rt.shutdown_on_idle();
assert!(ran.load(Ordering::SeqCst));
}

0 comments on commit 1d7f57a

Please sign in to comment.