Skip to content

Commit

Permalink
test(sns): migrate SNS integration test to StateMachine (#2476)
Browse files Browse the repository at this point in the history
This test has been flaky, most recently reported by Bas. After switching
to StateMachine it should be deterministic
  • Loading branch information
anchpop authored Nov 7, 2024
1 parent 4a7913a commit f889382
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
12 changes: 12 additions & 0 deletions rs/rust_canisters/canister_test/src/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions rs/sns/integration_tests/src/proposals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
36 changes: 22 additions & 14 deletions rs/sns/test_utils/src/itest_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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);
}
Expand All @@ -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",
Expand Down Expand Up @@ -1307,19 +1307,27 @@ pub async fn install_rust_canister_with_memory_allocation(
.collect::<Box<[String]>>();

// 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::<Box<[&str]>>();
Project::cargo_bin_maybe_from_env(&binary_name_, &features)
})
.await
.unwrap()
}
Runtime::StateMachine(_) => {
let features = features.iter().map(|s| s.as_str()).collect::<Box<[&str]>>();
Project::cargo_bin_maybe_from_env(&binary_name_, &features)
})
.await
.unwrap();
}
};

println!("Done compiling the wasm for {}", binary_name.as_ref());

Expand Down

0 comments on commit f889382

Please sign in to comment.