Browse Source

WIP: Add tests for Executor.

wip
Titouan Rigoudy 6 years ago
parent
commit
aceccc72f2
2 changed files with 58 additions and 4 deletions
  1. +2
    -1
      src/context.rs
  2. +56
    -3
      src/executor.rs

+ 2
- 1
src/context.rs View File

@ -9,6 +9,7 @@ use crate::user::UserMap;
/// Contains all the different bits of client state.
///
/// Implements `Sync`.
#[derive(Debug)]
pub struct Context {
pub rooms: Mutex<RoomMap>,
pub users: Mutex<UserMap>,
@ -26,8 +27,8 @@ impl Context {
#[cfg(test)]
mod tests {
use std::thread;
use std::sync::Arc;
use std::thread;
use super::Context;


+ 56
- 3
src/executor.rs View File

@ -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.
}

Loading…
Cancel
Save