Skip to content

Commit b098916

Browse files
authored
chore: update canister_logs examples with replicated query cases (#928)
1 parent 9b9f3d5 commit b098916

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

motoko/canister_logs/Makefile

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,37 @@ upgrade: build
2020
.PHONY: test
2121
.SILENT: test
2222
test: install
23-
dfx canister call CanisterLogs print 'hi'
23+
# Test print via update call.
24+
dfx canister call CanisterLogs print 'print via update'
2425
dfx canister logs CanisterLogs \
25-
| grep 'hi' && echo 'PASS'
26+
| grep 'print via update' && echo 'PASS'
27+
28+
# Test print via replicated query call.
29+
dfx canister call --update CanisterLogs print_query 'print via replicated query'
30+
dfx canister logs CanisterLogs \
31+
| grep 'print via replicated query' && echo 'PASS'
32+
33+
# Test print via non-replicated query call should NOT record the message.
34+
dfx canister call --query CanisterLogs print_query 'print via non-replicated query'
35+
! dfx canister logs CanisterLogs \
36+
| grep 'print via non-replicated query' && echo 'PASS'
37+
38+
# Test trapped call is recorded in logs.
39+
# Ignore failed call output (so the test continues) and check the logs to contain the message.
40+
-dfx canister call CanisterLogs trap 'trap via update'
41+
dfx canister logs CanisterLogs \
42+
| grep 'trap via update' && echo 'PASS'
43+
44+
# Test call to fail with memory out of bounds.
45+
# Ignore failed call output (so the test continues) and check the logs to contain the message.
46+
-dfx canister call CanisterLogs memory_oob
47+
dfx canister logs CanisterLogs \
48+
| grep 'StableMemory range out of bounds' && echo 'PASS'
49+
50+
# Test timer trap, assume it's been 5 seconds since the start.
51+
sleep 2
52+
dfx canister logs CanisterLogs \
53+
| grep 'timer trap' && echo 'PASS'
2654

2755
.PHONY: clean
2856
.SILENT: clean

motoko/canister_logs/src/Main.mo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ actor CanisterLogs {
2424
Debug.print(text);
2525
};
2626

27+
public query func print_query(text : Text) : async () {
28+
Debug.print(text);
29+
};
30+
2731
public func trap(text : Text) : async () {
2832
Debug.print("right before trap");
2933
Debug.trap(text);

rust/canister_logs/Makefile

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,48 @@ upgrade: build
2020
.PHONY: test
2121
.SILENT: test
2222
test: install
23-
dfx canister call canister_logs print 'hi'
23+
# Test print via update call.
24+
dfx canister call canister_logs print 'print via update'
2425
dfx canister logs canister_logs \
25-
| grep 'hi' && echo 'PASS'
26+
| grep 'print via update' && echo 'PASS'
27+
28+
# Test print via replicated query call.
29+
dfx canister call --update canister_logs print_query 'print via replicated query'
30+
dfx canister logs canister_logs \
31+
| grep 'print via replicated query' && echo 'PASS'
32+
33+
# Test print via non-replicated query call should NOT record the message.
34+
dfx canister call --query canister_logs print_query 'print via non-replicated query'
35+
! dfx canister logs canister_logs \
36+
| grep 'print via non-replicated query' && echo 'PASS'
37+
38+
# Test trapped call is recorded in logs.
39+
# Ignore failed call output (so the test continues) and check the logs to contain the message.
40+
-dfx canister call canister_logs trap 'trap via update'
41+
dfx canister logs canister_logs \
42+
| grep 'trap via update' && echo 'PASS'
43+
44+
# Test the call with panic is recorded in logs.
45+
# Ignore failed call output (so the test continues) and check the logs to contain the message.
46+
-dfx canister call canister_logs panic 'panic via update'
47+
dfx canister logs canister_logs \
48+
| grep 'panic via update' && echo 'PASS'
49+
50+
# Test call to fail with memory out of bounds.
51+
# Ignore failed call output (so the test continues) and check the logs to contain the message.
52+
-dfx canister call canister_logs memory_oob
53+
dfx canister logs canister_logs \
54+
| grep 'stable memory out of bounds' && echo 'PASS'
55+
56+
# Test call to fail with failed unwrap.
57+
# Ignore failed call output (so the test continues) and check the logs to contain the message.
58+
-dfx canister call canister_logs failed_unwrap
59+
dfx canister logs canister_logs \
60+
| grep 'Result::unwrap()' && echo 'PASS'
61+
62+
# Test timer trap, assume it's been 5 seconds since the start.
63+
dfx canister logs canister_logs \
64+
| grep 'timer trap' && echo 'PASS'
2665

2766
.PHONY: clean
2867
.SILENT: clean

rust/canister_logs/canister_logs.did

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
service : {
22
"print" : (text) -> ();
3+
"print_query" : (text) -> () query;
34
"trap" : (text) -> ();
45
"panic" : (text) -> ();
56
"memory_oob" : () -> ();

rust/canister_logs/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ic_cdk::{init, post_upgrade, update};
1+
use ic_cdk::{init, post_upgrade, query, update};
22
use std::time::Duration;
33

44
const TIMER_INTERVAL_SEC: u64 = 5;
@@ -25,6 +25,11 @@ fn print(text: String) {
2525
ic_cdk::print(text);
2626
}
2727

28+
#[query]
29+
fn print_query(text: String) {
30+
ic_cdk::print(text);
31+
}
32+
2833
#[update]
2934
fn trap(message: String) {
3035
ic_cdk::print("right before trap");

0 commit comments

Comments
 (0)