Skip to content

Commit bed825f

Browse files
committed
Add tests for signal mechanism
1 parent 7fbb772 commit bed825f

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

iceoryx2-cal/src/event/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
pub mod id_tracker;
1414
pub mod process_local;
15-
pub mod signal;
15+
pub mod signal_mechanism;
1616
pub mod unix_datagram_socket;
1717

1818
use std::{fmt::Debug, time::Duration};

iceoryx2-cal/src/event/signal/mod.rs renamed to iceoryx2-cal/src/event/signal_mechanism/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub trait SignalMechanism {
2121
fn init(&mut self) -> Result<(), ListenerCreateError>;
2222

2323
fn notify(&self) -> Result<(), NotifierNotifyError>;
24-
fn try_wait(&self) -> Result<(), ListenerWaitError>;
25-
fn timed_wait(&self, timeout: Duration) -> Result<(), ListenerWaitError>;
24+
fn try_wait(&self) -> Result<bool, ListenerWaitError>;
25+
fn timed_wait(&self, timeout: Duration) -> Result<bool, ListenerWaitError>;
2626
fn blocking_wait(&self) -> Result<(), ListenerWaitError>;
2727
}

iceoryx2-cal/src/event/signal/semaphore.rs renamed to iceoryx2-cal/src/event/signal_mechanism/semaphore.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,24 @@ impl SignalMechanism for Semaphore {
5959
Ok(())
6060
}
6161

62-
fn try_wait(&self) -> Result<(), ListenerWaitError> {
63-
fail!(from self, when self.semaphore().try_wait(),
62+
fn try_wait(&self) -> Result<bool, ListenerWaitError> {
63+
Ok(fail!(from self, when self.semaphore().try_wait(),
6464
with ListenerWaitError::InternalFailure,
65-
"Failed to dedcrement underlying semaphore.");
66-
Ok(())
65+
"Failed to dedcrement underlying semaphore."))
6766
}
6867

6968
fn timed_wait(
7069
&self,
7170
timeout: std::time::Duration,
72-
) -> Result<(), crate::event::ListenerWaitError> {
73-
fail!(from self, when self.semaphore().timed_wait(timeout),
71+
) -> Result<bool, crate::event::ListenerWaitError> {
72+
Ok(fail!(from self, when self.semaphore().timed_wait(timeout),
7473
with ListenerWaitError::InternalFailure,
75-
"Failed to dedcrement underlying semaphore.");
76-
Ok(())
74+
"Failed to dedcrement underlying semaphore."))
7775
}
7876

7977
fn blocking_wait(&self) -> Result<(), crate::event::ListenerWaitError> {
80-
fail!(from self, when self.semaphore().wait(),
78+
Ok(fail!(from self, when self.semaphore().wait(),
8179
with ListenerWaitError::InternalFailure,
82-
"Failed to dedcrement underlying semaphore.");
83-
Ok(())
80+
"Failed to dedcrement underlying semaphore."))
8481
}
8582
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2024 Contributors to the Eclipse Foundation
2+
//
3+
// See the NOTICE file(s) distributed with this work for additional
4+
// information regarding copyright ownership.
5+
//
6+
// This program and the accompanying materials are made available under the
7+
// terms of the Apache Software License 2.0 which is available at
8+
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9+
// which is available at https://opensource.org/licenses/MIT.
10+
//
11+
// SPDX-License-Identifier: Apache-2.0 OR MIT
12+
13+
#[generic_tests::define]
14+
mod signal_mechanism {
15+
use std::time::Duration;
16+
17+
use iceoryx2_bb_testing::{assert_that, watchdog::Watchdog};
18+
use iceoryx2_cal::event::signal_mechanism::{semaphore::Semaphore, SignalMechanism};
19+
const TIMEOUT: Duration = Duration::from_millis(25);
20+
21+
#[test]
22+
fn try_wait_works<Sut: SignalMechanism>() {
23+
let mut sut = Sut::new();
24+
assert_that!(sut.init(), is_ok);
25+
26+
assert_that!(sut.try_wait(), eq Ok(false));
27+
//assert_that!(sut.notify(), is_ok);
28+
//assert_that!(sut.try_wait(), eq Ok(true));
29+
//assert_that!(sut.try_wait(), eq Ok(false));
30+
}
31+
32+
#[test]
33+
fn notified_signal_does_not_block<Sut: SignalMechanism>() {
34+
let _watchdog = Watchdog::new(Duration::from_secs(1));
35+
let mut sut = Sut::new();
36+
assert_that!(sut.init(), is_ok);
37+
38+
assert_that!(sut.notify(), is_ok);
39+
assert_that!(sut.try_wait(), eq Ok(true));
40+
41+
assert_that!(sut.notify(), is_ok);
42+
assert_that!(sut.timed_wait(TIMEOUT), eq Ok(true));
43+
44+
assert_that!(sut.notify(), is_ok);
45+
assert_that!(sut.blocking_wait(), is_ok);
46+
}
47+
48+
#[instantiate_tests(<Semaphore>)]
49+
mod bitset {}
50+
}

0 commit comments

Comments
 (0)