Skip to content

Commit

Permalink
fix deprecation usage in async e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
lwshang committed Dec 20, 2024
1 parent 4bcb272 commit aab88ee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 39 deletions.
57 changes: 33 additions & 24 deletions e2e-tests/src/bin/async.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use candid::Principal;
use ic_cdk::call::{Call, CallError, CallResult, SendableCall};
use ic_cdk::{query, update};
use lazy_static::lazy_static;
use std::sync::RwLock;
Expand Down Expand Up @@ -31,9 +32,11 @@ async fn panic_after_async() {
let value = *lock;
// Do not drop the lock before the await point.

let _: (u64,) = ic_cdk::call(ic_cdk::api::canister_self(), "inc", (value,))
let _: u64 = Call::new(ic_cdk::api::canister_self(), "inc")
.with_arg(value)
.call()
.await
.expect("failed to call self");
.unwrap();
ic_cdk::api::trap("Goodbye, cruel world.")
}

Expand All @@ -47,9 +50,10 @@ async fn panic_twice() {
}

async fn async_then_panic() {
let _: (u64,) = ic_cdk::call(ic_cdk::api::canister_self(), "on_notify", ())
let _: u64 = Call::new(ic_cdk::api::canister_self(), "on_notify")
.call()
.await
.expect("Failed to call self");
.unwrap();
panic!();
}

Expand All @@ -65,12 +69,14 @@ fn on_notify() {

#[update]
fn notify(whom: Principal, method: String) {
ic_cdk::notify(whom, method.as_str(), ()).unwrap_or_else(|reject| {
ic_cdk::api::trap(format!(
"failed to notify (callee={}, method={}): {:?}",
whom, method, reject
))
});
Call::new(whom, method.as_str())
.call_oneway()
.unwrap_or_else(|reject| {
ic_cdk::api::trap(format!(
"failed to notify (callee={}, method={}): {:?}",
whom, method, reject
))
});
}

#[query]
Expand All @@ -80,28 +86,31 @@ fn greet(name: String) -> String {

#[query(composite = true)]
async fn greet_self(greeter: Principal) -> String {
let (greeting,) = ic_cdk::api::call::call(greeter, "greet", ("myself",))
Call::new(greeter, "greet")
.with_arg("myself")
.call()
.await
.unwrap();
greeting
.unwrap()
}

#[update]
async fn invalid_reply_payload_does_not_trap() -> String {
// We're decoding an integer instead of a string, decoding must fail.
let result: Result<(u64,), _> = ic_cdk::call(
ic_cdk::api::canister_self(),
"greet",
("World".to_string(),),
)
.await;
let result: CallResult<u64> = Call::new(ic_cdk::api::canister_self(), "greet")
.with_arg("World")
.call()
.await;

match result {
Ok((_n,)) => ic_cdk::api::trap("expected the decoding to fail"),
Err((err_code, _)) => format!(
"handled decoding error gracefully with code {}",
err_code as i32
),
Ok(_) => ic_cdk::api::trap("expected the decoding to fail"),
Err(e) => match e {
CallError::CandidDecodeFailed(candid_err) => {
format!("handled decoding error gracefully with candid error: {candid_err}")
}
other_err => ic_cdk::api::trap(format!(
"expected a CandidDecodeFailed error, got {other_err}"
)),
},
}
}

Expand Down
27 changes: 12 additions & 15 deletions e2e-tests/tests/async.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use pocket_ic::common::rest::RawEffectivePrincipal;
use pocket_ic::{call_candid, query_candid, CallError, ErrorCode, PocketIc};
use pocket_ic::{call_candid, query_candid, CallError, ErrorCode};

mod test_utilities;
use test_utilities::{cargo_build_canister, pocket_ic};

#[test]
fn panic_after_async_frees_resources() {
let pic: PocketIc = pocket_ic();
let pic = pocket_ic();
let wasm = cargo_build_canister("async");
let canister_id = pic.create_canister();
pic.add_cycles(canister_id, 2_000_000_000_000);
Expand Down Expand Up @@ -43,7 +43,7 @@ fn panic_after_async_frees_resources() {
"invocation_count",
(),
)
.expect("failed to call invocation_count");
.unwrap();

assert_eq!(i, n, "expected the invocation count to be {}, got {}", i, n);
}
Expand All @@ -55,8 +55,8 @@ fn panic_after_async_frees_resources() {
"invalid_reply_payload_does_not_trap",
(),
)
.expect("call failed");
assert_eq!(&message, "handled decoding error gracefully with code 5");
.unwrap();
assert!(message.contains("handled decoding error gracefully"));

let err = call_candid::<_, ()>(
&pic,
Expand All @@ -76,15 +76,15 @@ fn panic_after_async_frees_resources() {
"notifications_received",
(),
)
.expect("failed to call unrelated function afterwards");
.unwrap();
let _: (u64,) = call_candid(
&pic,
canister_id,
RawEffectivePrincipal::None,
"invocation_count",
(),
)
.expect("failed to recover lock");
.unwrap();
}

#[test]
Expand All @@ -98,8 +98,7 @@ fn notify_calls() {
pic.add_cycles(receiver_id, 2_000_000_000_000);
pic.install_canister(receiver_id, wasm, vec![], None);

let (n,): (u64,) = query_candid(&pic, receiver_id, "notifications_received", ())
.expect("failed to query 'notifications_received'");
let (n,): (u64,) = query_candid(&pic, receiver_id, "notifications_received", ()).unwrap();
assert_eq!(n, 0);

let () = call_candid(
Expand All @@ -109,14 +108,12 @@ fn notify_calls() {
"notify",
(receiver_id, "on_notify"),
)
.expect("failed to call 'notify'");
.unwrap();

let (n,): (u64,) = query_candid(&pic, receiver_id, "notifications_received", ())
.expect("failed to query 'notifications_received'");
let (n,): (u64,) = query_candid(&pic, receiver_id, "notifications_received", ()).unwrap();
assert_eq!(n, 1);
}

// Composite queries are not enabled yet.
#[test]
fn test_composite_query() {
let pic = pocket_ic();
Expand All @@ -128,7 +125,7 @@ fn test_composite_query() {
pic.add_cycles(receiver_id, 2_000_000_000_000);
pic.install_canister(receiver_id, wasm, vec![], None);

let (greeting,): (String,) = query_candid(&pic, sender_id, "greet_self", (receiver_id,))
.expect("failed to query 'greet_self'");
let (greeting,): (String,) =
query_candid(&pic, sender_id, "greet_self", (receiver_id,)).unwrap();
assert_eq!(greeting, "Hello, myself");
}

0 comments on commit aab88ee

Please sign in to comment.