|
|
|
@ -50,8 +50,61 @@ impl Executor { |
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Blocks until all scheduled jobs are executed.
|
|
|
|
pub fn join(self) {
|
|
|
|
self.pool.join()
|
|
|
|
/// Blocks until all scheduled jobs are executed, then returns the context.
|
|
|
|
pub fn join(self) -> Context {
|
|
|
|
self.pool.join();
|
|
|
|
|
|
|
|
// Once the pool is joined, no-one should be holding on to copies of
|
|
|
|
// `self.context` anymore, so we unwrap() here.
|
|
|
|
Arc::try_unwrap(self.context).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::io;
|
|
|
|
use std::sync::{Arc, Barrier, Mutex};
|
|
|
|
|
|
|
|
use super::{Context, Executor, Job};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn immediate_join_returns_empty_context() {
|
|
|
|
let context = Executor::new().join();
|
|
|
|
assert_eq!(context.users.lock().get_list(), vec![]);
|
|
|
|
assert_eq!(context.rooms.lock().get_room_list(), vec![]);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Waiter {
|
|
|
|
pub barrier: Arc<Barrier>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Job for Waiter {
|
|
|
|
fn execute(self: Box<Self>, context: &Context) -> io::Result<()> {
|
|
|
|
self.barrier.wait();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn join_waits_for_all_jobs() {
|
|
|
|
let executor = Executor::new();
|
|
|
|
|
|
|
|
let barrier = Arc::new(Barrier::new(2));
|
|
|
|
|
|
|
|
let waiter1 = Box::new(Waiter {
|
|
|
|
barrier: barrier.clone(),
|
|
|
|
});
|
|
|
|
let waiter2 = Box::new(Waiter {
|
|
|
|
barrier: barrier.clone(),
|
|
|
|
});
|
|
|
|
|
|
|
|
executor.schedule(waiter1);
|
|
|
|
executor.schedule(waiter2);
|
|
|
|
|
|
|
|
let context = executor.join();
|
|
|
|
assert_eq!(context.users.lock().get_list(), vec![]);
|
|
|
|
assert_eq!(context.rooms.lock().get_room_list(), vec![]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add a test that exercises modifying Context.
|
|
|
|
}
|