From f88938214b16584075196e13d0af7c50f671131a Mon Sep 17 00:00:00 2001 From: Andre Popovitch Date: Wed, 6 Nov 2024 20:11:25 -0600 Subject: [PATCH] test(sns): migrate SNS integration test to StateMachine (#2476) This test has been flaky, most recently reported by Bas. After switching to StateMachine it should be deterministic --- .../canister_test/src/canister.rs | 12 +++++++ rs/sns/integration_tests/src/proposals.rs | 7 ++-- rs/sns/test_utils/src/itest_helpers.rs | 36 +++++++++++-------- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/rs/rust_canisters/canister_test/src/canister.rs b/rs/rust_canisters/canister_test/src/canister.rs index fd746e5a8bd..035fa531ee2 100644 --- a/rs/rust_canisters/canister_test/src/canister.rs +++ b/rs/rust_canisters/canister_test/src/canister.rs @@ -426,6 +426,18 @@ impl<'a> Runtime { .await .map_err(|e| format!("Creation of a canister timed out. Last error was: {}", e)) } + + pub async fn tick(&'a self) { + match self { + Runtime::Remote(_) | Runtime::Local(_) => { + tokio::time::sleep(Duration::from_millis(100)).await + } + Runtime::StateMachine(state_machine) => { + state_machine.tick(); + state_machine.advance_time(Duration::from_millis(1000)); + } + } + } } /// An Internet Computer test runtime that talks to the IC using http diff --git a/rs/sns/integration_tests/src/proposals.rs b/rs/sns/integration_tests/src/proposals.rs index c0516558bf3..248d961b796 100644 --- a/rs/sns/integration_tests/src/proposals.rs +++ b/rs/sns/integration_tests/src/proposals.rs @@ -23,6 +23,7 @@ use ic_sns_governance::{ }, reward, }; +use ic_sns_test_utils::itest_helpers::state_machine_test_on_sns_subnet; use ic_sns_test_utils::{ itest_helpers::{local_test_on_sns_subnet, SnsCanisters, SnsTestsInitPayloadBuilder, UserInfo}, now_seconds, @@ -1959,7 +1960,7 @@ fn test_change_voting_rewards_round_duration() { /// ID of the most recent proposal. #[test] fn test_intermittent_proposal_submission() { - local_test_on_sns_subnet(|runtime| async move { + state_machine_test_on_sns_subnet(|runtime| async move { // Chapter 0: Prepare the world. // Initialize the ledger with an account for a user who will make proposals @@ -2262,13 +2263,13 @@ fn test_intermittent_proposal_submission() { sns_canisters.set_time_warp(delta_s).await?; // Wait for the number of proposals to decrease. - for _ in 0..25 { + for _ in 0..250 { proposals = sns_canisters.list_proposals(&proposer.sender).await; if proposals.len() < 3 { // GC occurred break; } - std::thread::sleep(std::time::Duration::from_millis(100)); + runtime.tick().await; } // Assert that proposal 1 has disappeared. diff --git a/rs/sns/test_utils/src/itest_helpers.rs b/rs/sns/test_utils/src/itest_helpers.rs index 1c4efbebf29..ff192d0adb4 100644 --- a/rs/sns/test_utils/src/itest_helpers.rs +++ b/rs/sns/test_utils/src/itest_helpers.rs @@ -1133,7 +1133,7 @@ impl SnsCanisters<'_> { if reward_event.end_timestamp_seconds.unwrap() > last_end_timestamp_seconds { return reward_event; } - tokio::time::sleep(Duration::from_millis(100)).await; + self.governance.runtime().tick().await; } panic!( @@ -1152,7 +1152,7 @@ impl SnsCanisters<'_> { if proposal.reward_event_end_timestamp_seconds.is_some() { return proposal; } - tokio::time::sleep(Duration::from_millis(100)).await; + self.governance.runtime().tick().await; } panic!("Proposal {:?} was not rewarded", proposal_id); } @@ -1179,7 +1179,7 @@ impl SnsCanisters<'_> { return status; } - tokio::time::sleep(Duration::from_millis(100)).await; + self.governance.runtime().tick().await; } panic!( "Canister {} didn't reach the running state after upgrading", @@ -1307,19 +1307,27 @@ pub async fn install_rust_canister_with_memory_allocation( .collect::>(); // Wrapping call to cargo_bin_* to avoid blocking current thread - let wasm: Wasm = tokio::runtime::Handle::current() - .spawn_blocking(move || { - println!( - "Compiling Wasm for {} in task on thread: {:?}", - binary_name_, - thread::current().id() - ); - // Second half of moving data had to be done in-thread to avoid lifetime/ownership issues + let wasm: Wasm = match canister.runtime() { + Runtime::Remote(_) | Runtime::Local(_) => { + tokio::runtime::Handle::current() + .spawn_blocking(move || { + println!( + "Compiling Wasm for {} in task on thread: {:?}", + binary_name_, + thread::current().id() + ); + // Second half of moving data had to be done in-thread to avoid lifetime/ownership issues + let features = features.iter().map(|s| s.as_str()).collect::>(); + Project::cargo_bin_maybe_from_env(&binary_name_, &features) + }) + .await + .unwrap() + } + Runtime::StateMachine(_) => { let features = features.iter().map(|s| s.as_str()).collect::>(); Project::cargo_bin_maybe_from_env(&binary_name_, &features) - }) - .await - .unwrap(); + } + }; println!("Done compiling the wasm for {}", binary_name.as_ref());