use std::thread::{sleep, spawn}; use std::time::Duration; use win_events::{wait_all, wait_first, PulseEvent}; #[test] fn pe_test_init() { let event = PulseEvent::new(); assert!(!event.is_set()); } #[test] fn pe_test_set_event() { let event = PulseEvent::new(); event.set(); assert!(!event.is_set()); } #[test] fn pe_test_clear_event() { let event = PulseEvent::new(); event.set(); event.clear(); assert!(!event.is_set()); } #[test] fn pe_test_thread_wait() { let event = PulseEvent::new(); let event2 = event.clone(); let worker = spawn(move || { sleep(Duration::from_millis(5)); event2.set(); }); let wait_result = event.wait(Duration::from_millis(100)).unwrap(); assert!(wait_result); worker.join().unwrap(); } #[test] fn pe_test_wait_already_set() { let event = PulseEvent::new(); assert!(!event.wait(Duration::from_millis(1)).unwrap()) } #[test] fn pe_test_wait_timeout() { let event = PulseEvent::new(); assert!(!event.wait(Duration::from_millis(0)).unwrap()) } #[test] fn pe_test_wait_first() { let set_event0 = PulseEvent::new(); let set_event1 = PulseEvent::new(); let set_event2 = PulseEvent::new(); 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 pe_test_wait_first_timeout() { let set_event0 = PulseEvent::new(); let set_event1 = PulseEvent::new(); let set_event2 = PulseEvent::new(); let wait_result = wait_first( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(3), ) .unwrap(); assert_eq!(-1, wait_result); } #[test] fn pe_test_wait_all() { let set_event0 = PulseEvent::new(); let set_event1 = PulseEvent::new(); let set_event2 = PulseEvent::new(); 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 pe_test_wait_all_partial_set() { let set_event0 = PulseEvent::new(); let set_event1 = PulseEvent::new(); let set_event2 = PulseEvent::new(); let inner_set = set_event2.clone(); let worker = spawn(move || { sleep(Duration::from_millis(1)); inner_set.set(); }); let wait_result = wait_first( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(10), ) .unwrap(); assert_eq!(2, wait_result); worker.join().unwrap(); } #[test] fn pe_test_wait_all_timeout() { let set_event0 = PulseEvent::new(); let set_event1 = PulseEvent::new(); let set_event2 = PulseEvent::new(); let wait_result = wait_all( vec![&set_event0, &set_event1, &set_event2], Duration::from_millis(0), ) .unwrap(); assert_eq!(false, wait_result); }