mod common; use std::time::Duration; use common::{Put, Reply, Request, ThreadedServer}; // Terminate the dispatcher loop by dropping the only client. #[test] fn no_clients() { let (clnt, jh) = ump_ng_server::spawn_thread(|_| Ok(ThreadedServer::default())).unwrap(); // Drop the (only) client, which should cause dispatch loop to terminate. drop(clnt); // Termination by clients disappearing should return None assert_eq!(jh.join().unwrap(), None); } // Terminate the dispatcher loop by explicitly requesting it to terminate from // its handler. #[test] fn handler_req_term() { let (clnt, jh) = ump_ng_server::spawn_thread(|_| Ok(ThreadedServer::default())).unwrap(); assert_eq!(clnt.req(Request::Add(2, 4)).unwrap(), Reply::Sum(6)); assert_eq!(clnt.req(Request::Croak).unwrap(), Reply::OkIWillCroak); let rv = jh.join().unwrap(); assert_eq!(rv, Some(42)); } #[test] fn handler_put_term() { let (clnt, jh) = ump_ng_server::spawn_thread(|_| Ok(ThreadedServer::default())).unwrap(); clnt.post(Put::Signal).unwrap(); clnt.post(Put::Croak).unwrap(); let rv = jh.join().unwrap(); assert_eq!(rv, Some(42)); } // Populate the queue with a bunch of nodes, then drop the client while there // are still nodes in the queue #[test] fn handler_delay_term() { let (clnt, jh) = ump_ng_server::spawn_thread(|_| Ok(ThreadedServer::default())).unwrap(); // Push a bunch of sleep nodes on the queue for _ in 0..10 { clnt.post(Put::Sleep(Duration::from_millis(100))).unwrap(); } // Drop the client drop(clnt); let rv = jh.join().unwrap(); assert_eq!(rv, None); } // Make sure all the sleepers are processed. // The explicit termination request should take precedence over terminating // over all clients disappearing. #[test] fn handler_delay_term_count() { let (clnt, jh) = ump_ng_server::spawn_thread(|_| Ok(ThreadedServer::default())).unwrap(); for _ in 0..10 { clnt.post(Put::Sleep(Duration::from_millis(100))).unwrap(); } clnt.post(Put::CroakSleepCount).unwrap(); drop(clnt); let rv = jh.join().unwrap(); // Should have received the sleep count assert_eq!(rv, Some(10)); } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :