From 8ef93d1470cb509e3ed6d4aa155185d9886c1328 Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Mon, 30 Oct 2023 22:01:54 +0100 Subject: [PATCH] [#3] Finalize service tests Signed-off-by: Christian Eltzschig --- elkodon/tests/service_event_tests.rs | 132 ++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 2 deletions(-) diff --git a/elkodon/tests/service_event_tests.rs b/elkodon/tests/service_event_tests.rs index 3409dea..7a0b1c5 100644 --- a/elkodon/tests/service_event_tests.rs +++ b/elkodon/tests/service_event_tests.rs @@ -157,20 +157,148 @@ mod service_event { #[test] fn simple_communication_works_listener_created_first() { let service_name = generate_name(); + let event_id = EventId::new(432); let sut = Sut::new(&service_name).event().create().unwrap(); let sut2 = Sut::new(&service_name).event().open().unwrap(); - let listener = sut.listener().create().unwrap(); + let mut listener = sut.listener().create().unwrap(); let notifier = sut2 .notifier() - .default_trigger_id(EventId::new(432)) + .default_trigger_id(event_id) .create() .unwrap(); assert_that!(notifier.notify(), is_ok); + + let mut received_events = 0; + for event in listener.try_wait().unwrap().iter() { + assert_that!(*event, eq event_id); + received_events += 1; + } + assert_that!(received_events, eq 1); + } + + #[test] + fn simple_communication_works_notifier_created_first() { + let service_name = generate_name(); + let event_id = EventId::new(43212); + + let sut = Sut::new(&service_name).event().create().unwrap(); + + let sut2 = Sut::new(&service_name).event().open().unwrap(); + + let notifier = sut2 + .notifier() + .default_trigger_id(event_id) + .create() + .unwrap(); + let mut listener = sut.listener().create().unwrap(); + assert_that!(notifier.notify(), is_ok); + + let mut received_events = 0; + for event in listener.try_wait().unwrap().iter() { + assert_that!(*event, eq event_id); + received_events += 1; + } + assert_that!(received_events, eq 1); + } + + #[test] + fn communication_with_max_notifiers_and_listeners_single_notification() { + const MAX_LISTENERS: usize = 4; + const MAX_NOTIFIERS: usize = 6; + const NUMBER_OF_ITERATIONS: u64 = 128; + let service_name = generate_name(); + + let sut = Sut::new(&service_name) + .event() + .max_notifiers(MAX_NOTIFIERS) + .max_listeners(MAX_LISTENERS) + .create() + .unwrap(); + + let mut listeners = vec![]; + let mut notifiers = vec![]; + + for _ in 0..MAX_LISTENERS { + listeners.push(sut.listener().create().unwrap()); + } + + for i in 0..MAX_NOTIFIERS { + notifiers.push( + sut.notifier() + .default_trigger_id(EventId::new((4 * i + 3) as u64)) + .create() + .unwrap(), + ); + } + + for _ in 0..NUMBER_OF_ITERATIONS { + for (i, notifier) in notifiers.iter().enumerate() { + assert_that!(notifier.notify(), is_ok); + + for listener in &mut listeners { + let mut received_events = 0; + for event in listener.try_wait().unwrap().iter() { + assert_that!(*event, eq EventId::new((4*i + 3) as u64)); + received_events += 1; + } + assert_that!(received_events, eq 1); + } + } + } + } + + #[test] + fn communication_with_max_notifiers_and_listeners_multi_notification() { + const MAX_LISTENERS: usize = 5; + const MAX_NOTIFIERS: usize = 7; + const NUMBER_OF_ITERATIONS: u64 = 128; + let service_name = generate_name(); + + let sut = Sut::new(&service_name) + .event() + .max_notifiers(MAX_NOTIFIERS) + .max_listeners(MAX_LISTENERS) + .create() + .unwrap(); + + let mut listeners = vec![]; + let mut notifiers = vec![]; + + for _ in 0..MAX_LISTENERS { + listeners.push(sut.listener().create().unwrap()); + } + + for i in 0..MAX_NOTIFIERS { + notifiers.push( + sut.notifier() + .default_trigger_id(EventId::new((i) as u64)) + .create() + .unwrap(), + ); + } + + for _ in 0..NUMBER_OF_ITERATIONS { + for notifier in ¬ifiers { + assert_that!(notifier.notify(), is_ok); + } + + for listener in &mut listeners { + let mut received_events = 0; + + let mut received_event_ids = [false; MAX_NOTIFIERS]; + for event in listener.try_wait().unwrap().iter() { + assert_that!(received_event_ids[event.as_u64() as usize], eq false); + received_event_ids[event.as_u64() as usize] = true; + received_events += 1; + } + assert_that!(received_events, eq MAX_NOTIFIERS); + } + } } #[instantiate_tests()]