use std::thread::{sleep, spawn}; use std::time::Duration; use win_events::{wait_all, wait_first, ManualResetEventPair}; #[test] fn mep_test_init_false() { let event = ManualResetEventPair::new(false); assert!(!event.is_set()); } #[test] fn mep_test_init_true() { let event = ManualResetEventPair::new(true); assert!(event.is_set()); } #[test] fn mep_test_set_event() { let event = ManualResetEventPair::new(false); event.set(); assert!(event.is_set()); } #[test] fn mep_test_clear_event() { let event = ManualResetEventPair::new(true); event.clear(); assert!(!event.is_set()); } #[test] fn mep_test_thread_wait() { let event = ManualResetEventPair::new(false); let event2 = event.clone(); let worker = spawn(move || { sleep(Duration::from_millis(3)); event2.set(); }); let wait_result = event.wait(Duration::from_millis(100)).unwrap(); assert!(wait_result); worker.join().unwrap(); } #[test] fn mep_test_wait_already_set() { let event = ManualResetEventPair::new(true); assert!(event.wait(Duration::from_millis(100)).unwrap()) } #[test] fn mep_test_wait_timeout() { let event = ManualResetEventPair::new(false); assert!(!event.wait(Duration::from_millis(0)).unwrap()) } #[test] fn mep_test_wait_first() { let set_event0 = ManualResetEventPair::new(false); let set_event1 = ManualResetEventPair::new(false); let set_event2 = ManualResetEventPair::new(false); let inner_set = set_event2.clone(); let worker = spawn(move || { sleep(Duration::from_millis(10)); inner_set.set() }); let wait_result = wait_first( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(100), ) .unwrap(); assert_eq!(2, wait_result); worker.join().unwrap(); } #[test] fn mep_test_wait_first_already_set() { let set_event0 = ManualResetEventPair::new(false); let set_event1 = ManualResetEventPair::new(false); let set_event2 = ManualResetEventPair::new(true); let wait_result = wait_first( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(10), ) .unwrap(); assert_eq!(2, wait_result); } #[test] fn mep_test_wait_first_timeout() { let set_event0 = ManualResetEventPair::new(false); let set_event1 = ManualResetEventPair::new(false); let set_event2 = ManualResetEventPair::new(false); let wait_result = wait_first( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(3), ) .unwrap(); assert_eq!(-1, wait_result); } #[test] fn mep_test_wait_all() { let set_event0 = ManualResetEventPair::new(false); let set_event1 = ManualResetEventPair::new(false); let set_event2 = ManualResetEventPair::new(false); let inner_set0 = set_event0.clone(); let inner_set1 = set_event1.clone(); let inner_set2 = set_event2.clone(); let worker = spawn(move || { sleep(Duration::from_millis(10)); inner_set0.set(); inner_set1.set(); inner_set2.set(); }); let wait_result = wait_all( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(100), ) .unwrap(); assert_eq!(true, wait_result); worker.join().unwrap(); } #[test] fn mep_test_wait_all_already_set() { let set_event0 = ManualResetEventPair::new(true); let set_event1 = ManualResetEventPair::new(true); let set_event2 = ManualResetEventPair::new(true); let wait_result = wait_all( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(10), ) .unwrap(); assert_eq!(true, wait_result); } #[test] fn mep_test_wait_all_partial_set() { let set_event0 = ManualResetEventPair::new(true); let set_event1 = ManualResetEventPair::new(true); let set_event2 = ManualResetEventPair::new(false); let inner_set = set_event2.clone(); let worker = spawn(move || { sleep(Duration::from_millis(10)); inner_set.set(); }); let wait_result = wait_first( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(100), ) .unwrap(); assert_eq!(0, wait_result); worker.join().unwrap(); } #[test] fn mep_test_wait_all_timeout() { let set_event0 = ManualResetEventPair::new(false); let set_event1 = ManualResetEventPair::new(false); let set_event2 = ManualResetEventPair::new(false); let wait_result = wait_all( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(0), ) .unwrap(); assert_eq!(false, wait_result); } #[test] fn mep_test_wait_all_cleared_timeout() { let set_event0 = ManualResetEventPair::new(true); let set_event1 = ManualResetEventPair::new(true); let set_event2 = ManualResetEventPair::new(false); set_event0.clear(); set_event2.set(); let wait_result = wait_all( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(0), ) .unwrap(); assert_eq!(false, wait_result); } #[test] fn mep_test_wait_not() { let evt = ManualResetEventPair::new(true); let event_not_set = evt.event_not_set(); let worker = spawn(move || { sleep(Duration::from_millis(500)); evt.clear() }); let wait_result = event_not_set.wait(Duration::from_millis(5000)).unwrap(); assert!(wait_result); worker.join().unwrap(); }