Skip to content

Commit

Permalink
Better TestClock API
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczmarczyck committed Sep 5, 2024
1 parent 269578b commit f4d3969
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
5 changes: 2 additions & 3 deletions libraries/opensk/src/ctap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3367,10 +3367,9 @@ mod test {
#[test]
fn test_check_user_presence_timeout() {
let mut env = TestEnv::default();
let now_ms = env.clock().access();
let clock = env.clock().clone();
env.user_presence().set(move || {
let mut locked_now_ms = now_ms.lock().unwrap();
*locked_now_ms += 100;
clock.advance(100);
Err(UserPresenceError::Timeout)
});
let response = check_user_presence(&mut env, DUMMY_CHANNEL);
Expand Down
18 changes: 9 additions & 9 deletions libraries/opensk/src/env/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ pub struct TestTimer {
end_ms: usize,
}

#[derive(Debug, Default)]
#[derive(Clone, Debug, Default)]
pub struct TestClock {
/// The current time, as advanced, in milliseconds.
now_ms: Arc<Mutex<usize>>,
}

impl TestClock {
pub fn advance(&mut self, milliseconds: usize) {
let mut locked_now_ms = self.now_ms.lock().unwrap();
*locked_now_ms += milliseconds;
pub fn now(&self) -> usize {
*self.now_ms.lock().unwrap()
}

pub fn access(&self) -> Arc<Mutex<usize>> {
self.now_ms.clone()
pub fn advance(&self, milliseconds: usize) {
let mut locked_now_ms = self.now_ms.lock().unwrap();
*locked_now_ms += milliseconds;
}
}

Expand All @@ -70,18 +70,18 @@ impl Clock for TestClock {

fn make_timer(&mut self, milliseconds: usize) -> Self::Timer {
TestTimer {
end_ms: *self.now_ms.lock().unwrap() + milliseconds,
end_ms: self.now() + milliseconds,
}
}

fn is_elapsed(&mut self, timer: &Self::Timer) -> bool {
*self.now_ms.lock().unwrap() >= timer.end_ms
self.now() >= timer.end_ms
}

#[cfg(feature = "debug_ctap")]
fn timestamp_us(&mut self) -> usize {
// Unused, but let's implement something because it's easy.
*self.now_ms.lock().unwrap() * 1000
self.now() * 1000
}
}

Expand Down

0 comments on commit f4d3969

Please sign in to comment.