From 9c9bc0189229c8752f8d8d521607f0b1a8d39cae Mon Sep 17 00:00:00 2001 From: ilbertt Date: Sun, 21 Jul 2024 20:45:54 +0200 Subject: [PATCH] test: fix advance time to account for ticks --- .../g_messages_acknowledgement.rs | 7 +++---- .../src/tests/integration_tests/utils/test_env.rs | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/ic-websocket-cdk/src/tests/integration_tests/g_messages_acknowledgement.rs b/src/ic-websocket-cdk/src/tests/integration_tests/g_messages_acknowledgement.rs index 8d13814..086400a 100644 --- a/src/ic-websocket-cdk/src/tests/integration_tests/g_messages_acknowledgement.rs +++ b/src/ic-websocket-cdk/src/tests/integration_tests/g_messages_acknowledgement.rs @@ -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 { diff --git a/src/ic-websocket-cdk/src/tests/integration_tests/utils/test_env.rs b/src/ic-websocket-cdk/src/tests/integration_tests/utils/test_env.rs index e26c285..54e283d 100644 --- a/src/ic-websocket-cdk/src/tests/integration_tests/utils/test_env.rs +++ b/src/ic-websocket-cdk/src/tests/integration_tests/utils/test_env.rs @@ -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(); + } } }