Skip to content

Core::run_until_finished #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/reactor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@ impl Core {
self.poll(max_wait);
}

/// Run the event loop until all spawned futures complete.
pub fn run_until_finished(&mut self) {
while self.inner.borrow().live() {
self.turn(None);
}
}

fn poll(&mut self, max_wait: Option<Duration>) -> bool {
// Given the `max_wait` variable specified, figure out the actual
// timeout that we're going to pass to `poll`. This involves taking a
Expand Down Expand Up @@ -570,6 +577,11 @@ impl Inner {
_registration: pair.0,
});
}

/// Deterimne whether the core has work scheduled
fn live(&self) -> bool {
!self.io_dispatch.is_empty() || !self.task_dispatch.is_empty() || !self.timeouts.is_empty()
}
}

impl Remote {
Expand Down
15 changes: 15 additions & 0 deletions tests/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::any::Any;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use std::rc::Rc;
use std::cell::Cell;

use futures::{Future, Poll};
use futures::future;
Expand Down Expand Up @@ -145,3 +147,16 @@ fn spawn_in_drop() {

lp.run(rx).unwrap();
}

#[test]
fn run_until_finished() {
drop(env_logger::init());
let mut lp = Core::new().unwrap();
let ran = Rc::new(Cell::new(false));
{
let ran = ran.clone();
lp.handle().spawn(future::lazy(move || { ran.set(true); Ok(()) }));
}
lp.run_until_finished();
assert!(ran.get());
}