From b09891600491426b1585b31aed9c7f2fd779c9bb Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan <103510076+maksymar@users.noreply.github.com> Date: Wed, 3 Jul 2024 13:14:03 +0200 Subject: [PATCH] chore: update canister_logs examples with replicated query cases (#928) --- motoko/canister_logs/Makefile | 32 +++++++++++++++++++-- motoko/canister_logs/src/Main.mo | 4 +++ rust/canister_logs/Makefile | 43 ++++++++++++++++++++++++++-- rust/canister_logs/canister_logs.did | 1 + rust/canister_logs/src/lib.rs | 7 ++++- 5 files changed, 82 insertions(+), 5 deletions(-) diff --git a/motoko/canister_logs/Makefile b/motoko/canister_logs/Makefile index cab31b86f..280d0bdea 100644 --- a/motoko/canister_logs/Makefile +++ b/motoko/canister_logs/Makefile @@ -20,9 +20,37 @@ upgrade: build .PHONY: test .SILENT: test test: install - dfx canister call CanisterLogs print 'hi' + # Test print via update call. + dfx canister call CanisterLogs print 'print via update' dfx canister logs CanisterLogs \ - | grep 'hi' && echo 'PASS' + | grep 'print via update' && echo 'PASS' + + # Test print via replicated query call. + dfx canister call --update CanisterLogs print_query 'print via replicated query' + dfx canister logs CanisterLogs \ + | grep 'print via replicated query' && echo 'PASS' + + # Test print via non-replicated query call should NOT record the message. + dfx canister call --query CanisterLogs print_query 'print via non-replicated query' + ! dfx canister logs CanisterLogs \ + | grep 'print via non-replicated query' && echo 'PASS' + + # Test trapped call is recorded in logs. + # Ignore failed call output (so the test continues) and check the logs to contain the message. + -dfx canister call CanisterLogs trap 'trap via update' + dfx canister logs CanisterLogs \ + | grep 'trap via update' && echo 'PASS' + + # Test call to fail with memory out of bounds. + # Ignore failed call output (so the test continues) and check the logs to contain the message. + -dfx canister call CanisterLogs memory_oob + dfx canister logs CanisterLogs \ + | grep 'StableMemory range out of bounds' && echo 'PASS' + + # Test timer trap, assume it's been 5 seconds since the start. + sleep 2 + dfx canister logs CanisterLogs \ + | grep 'timer trap' && echo 'PASS' .PHONY: clean .SILENT: clean diff --git a/motoko/canister_logs/src/Main.mo b/motoko/canister_logs/src/Main.mo index 3fa87bc76..0f13dd387 100644 --- a/motoko/canister_logs/src/Main.mo +++ b/motoko/canister_logs/src/Main.mo @@ -24,6 +24,10 @@ actor CanisterLogs { Debug.print(text); }; + public query func print_query(text : Text) : async () { + Debug.print(text); + }; + public func trap(text : Text) : async () { Debug.print("right before trap"); Debug.trap(text); diff --git a/rust/canister_logs/Makefile b/rust/canister_logs/Makefile index 140a60012..9141d17a6 100644 --- a/rust/canister_logs/Makefile +++ b/rust/canister_logs/Makefile @@ -20,9 +20,48 @@ upgrade: build .PHONY: test .SILENT: test test: install - dfx canister call canister_logs print 'hi' + # Test print via update call. + dfx canister call canister_logs print 'print via update' dfx canister logs canister_logs \ - | grep 'hi' && echo 'PASS' + | grep 'print via update' && echo 'PASS' + + # Test print via replicated query call. + dfx canister call --update canister_logs print_query 'print via replicated query' + dfx canister logs canister_logs \ + | grep 'print via replicated query' && echo 'PASS' + + # Test print via non-replicated query call should NOT record the message. + dfx canister call --query canister_logs print_query 'print via non-replicated query' + ! dfx canister logs canister_logs \ + | grep 'print via non-replicated query' && echo 'PASS' + + # Test trapped call is recorded in logs. + # Ignore failed call output (so the test continues) and check the logs to contain the message. + -dfx canister call canister_logs trap 'trap via update' + dfx canister logs canister_logs \ + | grep 'trap via update' && echo 'PASS' + + # Test the call with panic is recorded in logs. + # Ignore failed call output (so the test continues) and check the logs to contain the message. + -dfx canister call canister_logs panic 'panic via update' + dfx canister logs canister_logs \ + | grep 'panic via update' && echo 'PASS' + + # Test call to fail with memory out of bounds. + # Ignore failed call output (so the test continues) and check the logs to contain the message. + -dfx canister call canister_logs memory_oob + dfx canister logs canister_logs \ + | grep 'stable memory out of bounds' && echo 'PASS' + + # Test call to fail with failed unwrap. + # Ignore failed call output (so the test continues) and check the logs to contain the message. + -dfx canister call canister_logs failed_unwrap + dfx canister logs canister_logs \ + | grep 'Result::unwrap()' && echo 'PASS' + + # Test timer trap, assume it's been 5 seconds since the start. + dfx canister logs canister_logs \ + | grep 'timer trap' && echo 'PASS' .PHONY: clean .SILENT: clean diff --git a/rust/canister_logs/canister_logs.did b/rust/canister_logs/canister_logs.did index ac8bc93fe..b042ad52c 100644 --- a/rust/canister_logs/canister_logs.did +++ b/rust/canister_logs/canister_logs.did @@ -1,5 +1,6 @@ service : { "print" : (text) -> (); + "print_query" : (text) -> () query; "trap" : (text) -> (); "panic" : (text) -> (); "memory_oob" : () -> (); diff --git a/rust/canister_logs/src/lib.rs b/rust/canister_logs/src/lib.rs index 25b1232ec..e7dae58f6 100644 --- a/rust/canister_logs/src/lib.rs +++ b/rust/canister_logs/src/lib.rs @@ -1,4 +1,4 @@ -use ic_cdk::{init, post_upgrade, update}; +use ic_cdk::{init, post_upgrade, query, update}; use std::time::Duration; const TIMER_INTERVAL_SEC: u64 = 5; @@ -25,6 +25,11 @@ fn print(text: String) { ic_cdk::print(text); } +#[query] +fn print_query(text: String) { + ic_cdk::print(text); +} + #[update] fn trap(message: String) { ic_cdk::print("right before trap");