diff --git a/iceoryx2-ffi/cxx/tests/src/waitset_tests.cpp b/iceoryx2-ffi/cxx/tests/src/waitset_tests.cpp index 3b2b91dfb..7264296b4 100644 --- a/iceoryx2-ffi/cxx/tests/src/waitset_tests.cpp +++ b/iceoryx2-ffi/cxx/tests/src/waitset_tests.cpp @@ -190,6 +190,49 @@ TYPED_TEST(WaitSetTest, deadline_attachment_blocks_for_at_least_timeout) { ASSERT_THAT(elapsed, Ge(TIMEOUT.toMilliseconds())); } +TYPED_TEST(WaitSetTest, does_not_block_longer_than_provided_timeout) { + auto sut = this->create_sut(); + + auto begin = std::chrono::steady_clock::now(); + auto guard = sut.attach_interval(Duration::max()).expect(""); + + auto callback_called = false; + auto result = sut.wait_and_process_once( + [&](auto attachment_id) -> CallbackProgression { + callback_called = true; + return CallbackProgression::Stop; + }, + TIMEOUT); + + auto end = std::chrono::steady_clock::now(); + auto elapsed = std::chrono::duration_cast(end - begin).count(); + + ASSERT_THAT(callback_called, Eq(false)); + ASSERT_THAT(elapsed, Ge(TIMEOUT.toMilliseconds())); +} + +TYPED_TEST(WaitSetTest, blocks_until_interval_when_user_timeout_is_larger) { + auto sut = this->create_sut(); + + auto begin = std::chrono::steady_clock::now(); + auto guard = sut.attach_interval(TIMEOUT).expect(""); + + auto callback_called = false; + auto result = sut.wait_and_process_once( + [&](auto attachment_id) -> CallbackProgression { + callback_called = true; + return CallbackProgression::Stop; + }, + Duration::max()); + + auto end = std::chrono::steady_clock::now(); + auto elapsed = std::chrono::duration_cast(end - begin).count(); + + ASSERT_THAT(callback_called, Eq(true)); + ASSERT_THAT(elapsed, Ge(TIMEOUT.toMilliseconds())); +} + + TYPED_TEST(WaitSetTest, deadline_attachment_wakes_up_when_notified) { auto sut = this->create_sut(); auto listener = this->create_listener(); diff --git a/iceoryx2/src/port/waitset.rs b/iceoryx2/src/port/waitset.rs index 78bd4a0eb..57db564ce 100644 --- a/iceoryx2/src/port/waitset.rs +++ b/iceoryx2/src/port/waitset.rs @@ -795,13 +795,13 @@ impl WaitSet { /// CallbackProgression::Continue /// }; /// - /// const PROCESS_TIMEOUT: Duration = Duration::MAX; + /// const TIMEOUT: Duration = Duration::MAX; /// /// // main event loop /// loop { - /// // blocks until an event arrives, handles all arrived events and then - /// // returns. - /// waitset.wait_and_process_once(on_event)?; + /// // blocks until an event arrives or TIMEOUT was reached, handles all arrived events + /// // and then returns. + /// waitset.wait_and_process_once(on_event, TIMEOUT)?; /// // do some event post processing /// println!("handled events"); /// } diff --git a/iceoryx2/tests/waitset_tests.rs b/iceoryx2/tests/waitset_tests.rs index b2c016f5f..83ad05289 100644 --- a/iceoryx2/tests/waitset_tests.rs +++ b/iceoryx2/tests/waitset_tests.rs @@ -283,6 +283,56 @@ mod waitset { assert_that!(start.elapsed(), time_at_least TIMEOUT); } + #[test] + fn run_does_not_block_longer_than_provided_timeout() + where + ::Listener: SynchronousMultiplexing, + { + let _watchdog = Watchdog::new(); + let sut = WaitSetBuilder::new().create::().unwrap(); + + let _tick_guard = sut.attach_interval(Duration::MAX).unwrap(); + + let mut callback_called = false; + let start = Instant::now(); + sut.wait_and_process_once( + |_| { + callback_called = true; + CallbackProgression::Continue + }, + TIMEOUT, + ) + .unwrap(); + + assert_that!(callback_called, eq false); + assert_that!(start.elapsed(), time_at_least TIMEOUT); + } + + #[test] + fn run_does_block_until_interval_when_user_timeout_is_larger() + where + ::Listener: SynchronousMultiplexing, + { + let _watchdog = Watchdog::new(); + let sut = WaitSetBuilder::new().create::().unwrap(); + + let _tick_guard = sut.attach_interval(TIMEOUT).unwrap(); + + let mut callback_called = false; + let start = Instant::now(); + sut.wait_and_process_once( + |_| { + callback_called = true; + CallbackProgression::Continue + }, + Duration::MAX, + ) + .unwrap(); + + assert_that!(callback_called, eq true); + assert_that!(start.elapsed(), time_at_least TIMEOUT); + } + #[test] fn run_lists_all_deadlines() where