Skip to content

Commit

Permalink
test: fix advance time to account for ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
ilbertt committed Jul 21, 2024
1 parent 6e3a75f commit 9c9bc01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,9 @@ mod helpers {
websocket_message.sequence_num,
expected_websocket_message_sequence_number
);
assert_eq!(
websocket_message.timestamp,
get_test_env().get_canister_time()
);
// testing the timestamp is difficult, since pocketic advances the time
// at every round (at every update call)
assert!(websocket_message.timestamp <= get_test_env().get_canister_time());
assert_eq!(
decode_websocket_service_message_content(&websocket_message.content),
WebsocketServiceMessageContent::AckMessage(CanisterAckMessageContent {
Expand Down
15 changes: 13 additions & 2 deletions src/ic-websocket-cdk/src/tests/integration_tests/utils/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,21 @@ impl TestEnv {
self.advance_canister_time_ns(ms * NS_IN_MS);
}

/// # Panics
/// If time is advanced for less than 100ns, due to an internal logic
/// that accounts for the time advanced in `tick`s.
pub fn advance_canister_time_ns(&self, ns: u64) {
self.pic.advance_time(Duration::from_nanos(ns));
// when calling `tick`, the time on pic advances by 1ns,
// so we have to account for that difference here
let ticks = 0..100;
let advance_diff = ticks.len() as u64;
assert!(ns > advance_diff, "Cannot advance for less than 99ns");
self.pic
.advance_time(Duration::from_nanos(ns - advance_diff));
// produce and advance by some blocks to fire eventual timers
// see https://forum.dfinity.org/t/pocketic-multi-subnet-canister-testing/24901/4
self.pic.tick();
for _ in ticks {
self.pic.tick();
}
}
}

0 comments on commit 9c9bc01

Please sign in to comment.