Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: reproduce that global timer is not reset after loading snapshot #3504

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/pocket-ic/test_canister/src/canister.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use candid::{define_function, CandidType, Principal};
use ic_cdk::api::call::{accept_message, arg_data_raw, reject, RejectionCode};
use ic_cdk::api::instruction_counter;
use ic_cdk::api::management_canister::ecdsa::{
ecdsa_public_key as ic_cdk_ecdsa_public_key, sign_with_ecdsa as ic_cdk_sign_with_ecdsa,
EcdsaCurve, EcdsaKeyId, EcdsaPublicKeyArgument, EcdsaPublicKeyResponse, SignWithEcdsaArgument,
Expand All @@ -9,6 +8,7 @@ use ic_cdk::api::management_canister::http_request::{
http_request as canister_http_outcall, CanisterHttpRequestArgument, HttpMethod, HttpResponse,
TransformArgs, TransformContext, TransformFunc,
};
use ic_cdk::api::{instruction_counter, set_global_timer};
use ic_cdk::{inspect_message, query, trap, update};
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
Expand Down Expand Up @@ -347,4 +347,17 @@ fn trap_update() {
trap("trap in update method");
}

// global timer

#[export_name = "canister_global_timer"]
fn timer() {
ic_cdk::print("I'm running in a global timer!");
}

#[update]
fn set_timer() {
let time = ic_cdk::api::time() + 10_000_000_000;
set_global_timer(time);
}

fn main() {}
40 changes: 40 additions & 0 deletions packages/pocket-ic/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2220,3 +2220,43 @@ fn test_reject_response_type() {
assert!(!err.certified);
}
}

#[test]
fn loading_snapshot_resets_global_timer() {
let pic = PocketIc::new();

// We create a test canister.
let canister = pic.create_canister();
pic.add_cycles(canister, INIT_CYCLES);
pic.install_canister(canister, test_canister_wasm(), vec![], None);

// We take a snapshot of the test canister.
pic.stop_canister(canister, None).unwrap();
let snapshot = pic.take_canister_snapshot(canister, None, None).unwrap();
pic.start_canister(canister, None).unwrap();

// We set the global timer to 10s from now.
update_candid::<_, ((),)>(&pic, canister, "set_timer", ((),)).unwrap();

// We load the snapshot of the test canister taken before setting the global timer.
pic.stop_canister(canister, None).unwrap();
pic.load_canister_snapshot(canister, None, snapshot.id)
.unwrap();
pic.start_canister(canister, None).unwrap();

// We advance time by 10s and execute a couple of rounds to see if the global timer triggers.
pic.advance_time(std::time::Duration::from_secs(10));
for _ in 0..4 {
pic.tick();
}

// there shouldn't be any logs since the timer should be reset after loading the snapshot
let logs: Vec<_> = pic
.fetch_canister_logs(canister, Principal::anonymous())
.unwrap()
.into_iter()
.map(|log| String::from_utf8(log.content).unwrap())
.collect();
println!("logs: {:?}", logs);
assert!(logs.is_empty());
}
Loading