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

refactor: Move statesync_test canister to Rust CDK #2959

Merged
merged 8 commits into from
Dec 10, 2024
Merged
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
11 changes: 7 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions rs/rust_canisters/statesync_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package(default_visibility = ["//visibility:public"])

DEPENDENCIES = [
# Keep sorted.
"//rs/rust_canisters/dfn_core",
"//rs/rust_canisters/dfn_json",
"@crate_index//:candid",
"@crate_index//:ic-cdk",
"@crate_index//:lazy_static",
"@crate_index//:rand",
"@crate_index//:rand_pcg",
Expand All @@ -18,13 +18,15 @@ DEPENDENCIES = [
DEV_DEPENDENCIES = [
# Keep sorted.
"//rs/rust_canisters/canister_test",
"//rs/state_machine_tests",
"//rs/test_utilities",
"//rs/types/management_canister_types",
"//rs/types/types",
]

MACRO_DEPENDENCIES = [
# Keep sorted.
"//rs/rust_canisters/dfn_macro",
"@crate_index//:ic-cdk-macros",
]

rust_binary(
Expand Down
8 changes: 5 additions & 3 deletions rs/rust_canisters/statesync_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ name = "statesync-test-canister"
path = "src/main.rs"

[dependencies]
dfn_core = { path = "../dfn_core" }
dfn_json = { path = "../dfn_json" }
dfn_macro = { path = "../dfn_macro" }
candid = { workspace = true }
ic-cdk = { workspace = true }
ic-cdk-macros = { workspace = true }
lazy_static = { workspace = true }
rand = { workspace = true }
rand_pcg = "0.3.1"
Expand All @@ -22,6 +22,8 @@ serde_json = { workspace = true }

[dev-dependencies]
canister-test = { path = "../../rust_canisters/canister_test" }
ic-management-canister-types = { path = "../../types/management_canister_types" }
ic-state-machine-tests = { path = "../../state_machine_tests" }
ic-test-utilities = { path = "../../test_utilities" }
ic-types = { path = "../../types/types" }

Expand Down
2 changes: 1 addition & 1 deletion rs/rust_canisters/statesync_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// This canister is used in the testcase 5_2. The canister stores a vector of
/// variable length, and the number of times the canister update method has
/// been called.
use dfn_macro::{query, update};
use ic_cdk_macros::{query, update};
use lazy_static::lazy_static;
use rand::{Rng, SeedableRng};
use rand_pcg::Pcg64Mcg;
Expand Down
101 changes: 59 additions & 42 deletions rs/rust_canisters/statesync_test/test/test.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,68 @@
use candid::{Decode, Encode};
use canister_test::*;
use ic_management_canister_types::CanisterSettingsArgsBuilder;
use ic_state_machine_tests::StateMachine;

#[test]
fn test_statesync_test_canisters() {
local_test_e(|r| async move {
let proj = Project::new();
let env = StateMachine::new();

println!("Start installing statesync test canister");
let canister = proj
.cargo_bin("statesync-test-canister", &[])
.install(&r)
.with_memory_allocation(8 * 1024 * 1024 * 1024) // 8GiB
.bytes(Vec::new())
.await?;
println!("Installed statesync test canister");
let features = [];
let wasm = Project::cargo_bin_maybe_from_env("statesync-test-canister", &features);
let canister_id = env
.install_canister(
wasm.bytes(),
vec![],
Some(
CanisterSettingsArgsBuilder::new()
.with_memory_allocation(8 * 1024 * 1024 * 1024)
.build(),
),
)
.expect("Failed to install canister");

let mut res: Result<u8, String> = canister
.query_("read_state", dfn_json::json, 0_usize)
.await
.unwrap();
assert_eq!(
res,
Ok(0),
"Queried first element of state vector, should have been 0, was {:?}",
res
);
let result = env
.query(canister_id, "read_state", Encode!(&0usize).unwrap())
.unwrap();
let res = assert_reply(result);
let val = Decode!(&res, Result<u8, String>).unwrap();
assert_eq!(
val,
Ok(0),
"Queried first element of state vector, should have been 0, was {:?}",
res
);

res = canister
.update_("change_state", dfn_json::json, 33_u32)
.await
.unwrap();
assert_eq!(
res,
Ok(1),
"Changed state for the first time, result should have been 1, was {:?}",
res
);
let result = env
.execute_ingress(canister_id, "change_state", Encode!(&33u32).unwrap())
.unwrap();
let res = assert_reply(result);
let val = Decode!(&res, Result<u64, String>).unwrap();
assert_eq!(
val,
Ok(1),
"Changed state for the first time, result should have been 1, was {:?}",
res
);

res = canister
.query_("read_state", dfn_json::json, 0_usize)
.await
.unwrap();
assert_eq!(
res,
Ok(119),
"Queried 0th element of state vector, should be 20 for seed 33, was {:?}",
res
);
Ok(())
})
let result = env
.query(canister_id, "read_state", Encode!(&0usize).unwrap())
.unwrap();
let res = assert_reply(result);
let val = Decode!(&res, Result<u8, String>).unwrap();
assert_eq!(
val,
Ok(119),
"Queried 0th element of state vector, should be 20 for seed 33, was {:?}",
res
);
}

fn assert_reply(res: WasmResult) -> Vec<u8> {
match res {
WasmResult::Reply(res) => res,
WasmResult::Reject(_) => {
unreachable!("Unexpected reject, should have been a reply");
}
}
}
3 changes: 2 additions & 1 deletion rs/tests/message_routing/rejoin_test_lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ rust_library(
crate_name = "rejoin_test_lib",
deps = [
"//rs/rust_canisters/canister_test",
"//rs/rust_canisters/dfn_json",
"//rs/rust_canisters/dfn_candid",
"//rs/tests/driver:ic-system-test-driver",
"@crate_index//:candid",
"@crate_index//:chrono",
"@crate_index//:futures",
"@crate_index//:slog",
Expand Down
3 changes: 2 additions & 1 deletion rs/tests/message_routing/rejoin_test_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ description.workspace = true
documentation.workspace = true

[dependencies]
candid = { workspace = true }
canister-test = { path = "../../../rust_canisters/canister_test" }
chrono = { workspace = true }
futures = { workspace = true }
ic-system-test-driver = { path = "../../driver" }
slog = { workspace = true }
tokio = { workspace = true }
dfn_json = { path = "../../../rust_canisters/dfn_json" }
dfn_candid = { path = "../../../rust_canisters/dfn_candid" }

[lib]
name = "rejoin_test_lib"
Expand Down
9 changes: 3 additions & 6 deletions rs/tests/message_routing/rejoin_test_lib/rejoin_test_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,11 @@ async fn modify_canister_heap(
continue;
}
let seed_for_canister = i + (x - 1) * num_canisters + seed;
let payload = (x as u32, seed_for_canister as u32);
// Each call will expand the memory by writing a chunk of 128 MiB.
// There are 8 chunks in the canister, so the memory will grow by 1 GiB after 8 calls.
let _res: Result<u8, String> = canister
.update_(
"expand_state",
dfn_json::json,
(x as u32, seed_for_canister as u32),
)
let _res: Result<u64, String> = canister
.update_("expand_state", dfn_candid::candid, payload)
.await
.unwrap_or_else(|e| {
panic!(
Expand Down
Loading