Skip to content

Commit

Permalink
[#533] Add tests to verify user provided timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Nov 30, 2024
1 parent 8484a89 commit eee8fa0
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
43 changes: 43 additions & 0 deletions iceoryx2-ffi/cxx/tests/src/waitset_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::chrono::milliseconds>(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<std::chrono::milliseconds>(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();
Expand Down
8 changes: 4 additions & 4 deletions iceoryx2/src/port/waitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,13 +795,13 @@ impl<Service: crate::service::Service> WaitSet<Service> {
/// 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");
/// }
Expand Down
50 changes: 50 additions & 0 deletions iceoryx2/tests/waitset_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,56 @@ mod waitset {
assert_that!(start.elapsed(), time_at_least TIMEOUT);
}

#[test]
fn run_does_not_block_longer_than_provided_timeout<S: Service>()
where
<S::Event as Event>::Listener: SynchronousMultiplexing,
{
let _watchdog = Watchdog::new();
let sut = WaitSetBuilder::new().create::<S>().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<S: Service>()
where
<S::Event as Event>::Listener: SynchronousMultiplexing,
{
let _watchdog = Watchdog::new();
let sut = WaitSetBuilder::new().create::<S>().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<S: Service>()
where
Expand Down

0 comments on commit eee8fa0

Please sign in to comment.