Skip to content

Commit 20d67be

Browse files
authored
refactor(tests): await_with_timeout should take a range, not just a timeout (#3096)
The issue is that there are some cases where we know that `await_with_timeout` will have to wait a certain minimum amount of time. By taking a range, we can have the caller pre-emptively tell us the minimum amount of time they think we'll have to wait. Then we can advance that amount of time straight away
1 parent c606036 commit 20d67be

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

rs/nervous_system/integration_tests/src/pocket_ic_helpers.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ use pocket_ic::{
6767
};
6868
use prost::Message;
6969
use rust_decimal::prelude::ToPrimitive;
70+
use std::ops::Range;
7071
use std::{collections::BTreeMap, fmt::Write, time::Duration};
7172

7273
pub const STARTING_CYCLES_PER_CANISTER: u128 = 2_000_000_000_000_000;
@@ -705,13 +706,14 @@ pub async fn upgrade_nns_canister_to_tip_of_master_or_panic(
705706
);
706707
}
707708

708-
/// Gradually advances time by up to `timeout_seconds` seconds, observing the state using
709-
/// the provided `observe` function after each (evenly-timed) tick.
709+
/// First, advances time by `expected_event_interval_seconds.start` seconds.
710+
/// Then, gradually advances time by up to the length of the interval `expected_event_interval_seconds`,
711+
/// observing the state using the provided `observe` function after each (evenly-timed) tick.
710712
/// - If the observed state matches the `expected` state, it returns `Ok(())`.
711713
/// - If the timeout is reached, it returns an error with the last observation.
712714
///
713-
/// The frequency of ticks is 1 per second for small values of `timeout_seconds`, and gradually
714-
/// lower for larger `timeout_seconds` to guarantee at most 500 ticks.
715+
/// The frequency of ticks is 1 per second for small intervals of `expected_event_interval_seconds`, and gradually
716+
/// lower for larger intervals to guarantee at most 500 ticks.
715717
///
716718
/// Example:
717719
/// ```
@@ -733,7 +735,7 @@ pub async fn upgrade_nns_canister_to_tip_of_master_or_panic(
733735
/// ```
734736
pub async fn await_with_timeout<'a, T, F, Fut>(
735737
pocket_ic: &'a PocketIc,
736-
timeout_seconds: u64,
738+
expected_event_interval_seconds: Range<u64>,
737739
observe: F,
738740
expected: &T,
739741
) -> Result<(), String>
@@ -742,6 +744,13 @@ where
742744
F: Fn(&'a PocketIc) -> Fut,
743745
Fut: std::future::Future<Output = T>,
744746
{
747+
assert!(expected_event_interval_seconds.start < expected_event_interval_seconds.end, "expected_event_interval_seconds.start must be less than expected_event_interval_seconds.end");
748+
let timeout_seconds =
749+
expected_event_interval_seconds.end - expected_event_interval_seconds.start;
750+
pocket_ic
751+
.advance_time(Duration::from_secs(expected_event_interval_seconds.start))
752+
.await;
753+
745754
let mut counter = 0;
746755
let num_ticks = timeout_seconds.min(500);
747756
let seconds_per_tick = (timeout_seconds as f64 / num_ticks as f64).ceil() as u64;
@@ -1346,9 +1355,14 @@ pub mod sns {
13461355
use assert_matches::assert_matches;
13471356
use ic_crypto_sha2::Sha256;
13481357
use ic_nervous_system_agent::sns::governance::GovernanceCanister;
1358+
use ic_sns_governance::governance::UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS;
13491359
use ic_sns_governance::pb::v1::get_neuron_response;
13501360
use pocket_ic::ErrorCode;
13511361

1362+
pub const EXPECTED_UPGRADE_DURATION_MAX_SECONDS: u64 = 1000;
1363+
pub const EXPECTED_UPGRADE_STEPS_REFRESH_MAX_SECONDS: u64 =
1364+
UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS + 10;
1365+
13521366
/// Manage an SNS neuron, e.g., to make an SNS Governance proposal.
13531367
async fn manage_neuron(
13541368
pocket_ic: &PocketIc,

rs/nervous_system/integration_tests/tests/advance_sns_target_version.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use ic_nervous_system_integration_tests::pocket_ic_helpers::{await_with_timeout, sns};
1+
use ic_nervous_system_integration_tests::pocket_ic_helpers::{
2+
await_with_timeout, sns,
3+
sns::governance::{
4+
EXPECTED_UPGRADE_DURATION_MAX_SECONDS, EXPECTED_UPGRADE_STEPS_REFRESH_MAX_SECONDS,
5+
},
6+
};
27
use ic_nervous_system_integration_tests::{
38
create_service_nervous_system_builder::CreateServiceNervousSystemBuilder,
49
pocket_ic_helpers::{
@@ -82,7 +87,7 @@ async fn test_get_upgrade_journal() {
8287
// Step 1.1: wait for the upgrade steps to be refreshed.
8388
await_with_timeout(
8489
&pocket_ic,
85-
UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS,
90+
UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS..EXPECTED_UPGRADE_STEPS_REFRESH_MAX_SECONDS,
8691
|pocket_ic| async {
8792
sns::governance::get_upgrade_journal(pocket_ic, sns.governance.canister_id)
8893
.await
@@ -134,7 +139,7 @@ async fn test_get_upgrade_journal() {
134139
// Step 2.1: wait for the upgrade steps to be refreshed.
135140
await_with_timeout(
136141
&pocket_ic,
137-
UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS,
142+
UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS..EXPECTED_UPGRADE_STEPS_REFRESH_MAX_SECONDS,
138143
|pocket_ic| async {
139144
sns::governance::get_upgrade_journal(pocket_ic, sns.governance.canister_id)
140145
.await
@@ -195,7 +200,7 @@ async fn test_get_upgrade_journal() {
195200

196201
await_with_timeout(
197202
&pocket_ic,
198-
UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS,
203+
0..EXPECTED_UPGRADE_DURATION_MAX_SECONDS,
199204
|pocket_ic| async {
200205
sns::governance::get_upgrade_journal(pocket_ic, sns.governance.canister_id)
201206
.await

rs/nervous_system/integration_tests/tests/advance_target_version_upgrades_all_canisters_test.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use ic_nervous_system_integration_tests::pocket_ic_helpers::{await_with_timeout, sns};
21
use ic_nervous_system_integration_tests::{
32
create_service_nervous_system_builder::CreateServiceNervousSystemBuilder,
4-
pocket_ic_helpers::{add_wasms_to_sns_wasm, hash_sns_wasms, install_nns_canisters, nns},
3+
pocket_ic_helpers::{
4+
add_wasms_to_sns_wasm, await_with_timeout, hash_sns_wasms, install_nns_canisters, nns, sns,
5+
sns::governance::{
6+
EXPECTED_UPGRADE_DURATION_MAX_SECONDS, EXPECTED_UPGRADE_STEPS_REFRESH_MAX_SECONDS,
7+
},
8+
},
59
};
610
use ic_sns_governance::governance::UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS;
711
use ic_sns_swap::pb::v1::Lifecycle;
@@ -97,7 +101,7 @@ async fn test_advance_target_version_upgrades_all_canisters() {
97101
eprintln!("Step 3: Wait for the upgrade steps to be refreshed ...");
98102
await_with_timeout(
99103
&pocket_ic,
100-
UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS,
104+
UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS..EXPECTED_UPGRADE_STEPS_REFRESH_MAX_SECONDS,
101105
|pocket_ic| async {
102106
sns::governance::try_get_upgrade_journal(pocket_ic, sns.governance.canister_id)
103107
.await
@@ -124,7 +128,7 @@ async fn test_advance_target_version_upgrades_all_canisters() {
124128
eprintln!("Step 5: Wait for the upgrade to happen ...");
125129
await_with_timeout(
126130
&pocket_ic,
127-
UPGRADE_STEPS_INTERVAL_REFRESH_BACKOFF_SECONDS,
131+
0..EXPECTED_UPGRADE_DURATION_MAX_SECONDS,
128132
|pocket_ic| async {
129133
let journal =
130134
sns::governance::try_get_upgrade_journal(pocket_ic, sns.governance.canister_id)

0 commit comments

Comments
 (0)