Skip to content

Commit

Permalink
chore: update canister logging examples with trapped query calls (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymar authored Jul 4, 2024
1 parent b098916 commit 24c76d1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
26 changes: 20 additions & 6 deletions motoko/canister_logs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,33 @@ test: install
| 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'
# Ignore failed dfx command 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 trap via replicated query call.
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
- dfx canister call --update CanisterLogs trap_query 'trap via replicated query'
dfx canister logs CanisterLogs \
| grep 'trap via replicated query' && echo 'PASS'

# Test trap via non-replicated query call should NOT record the message.
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
- dfx canister call --query CanisterLogs trap_query 'trap via non-replicated query'
! dfx canister logs CanisterLogs \
| grep 'trap via non-replicated query' && 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
# Ignore failed dfx command 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
# Test timer trap.
# The timer is setup to trap every 5 seconds, so this test has to be called
# at least 5 seconds after the start to record the trap log message.
sleep 5
dfx canister logs CanisterLogs \
| grep 'timer trap' && echo 'PASS'

Expand Down
5 changes: 5 additions & 0 deletions motoko/canister_logs/src/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ actor CanisterLogs {
Debug.trap(text);
};

public query func trap_query(text : Text) : async () {
Debug.print("right before trap_query");
Debug.trap(text);
};

public func memory_oob() : async () {
Debug.print("right before memory out of bounds");
let offset : Nat64 = 10;
Expand Down
33 changes: 24 additions & 9 deletions rust/canister_logs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,45 @@ test: install
| 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'
# Ignore failed dfx command 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 trap via replicated query call.
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
- dfx canister call --update canister_logs trap_query 'trap via replicated query'
dfx canister logs canister_logs \
| grep 'trap via replicated query' && echo 'PASS'

# Test trap via non-replicated query call should NOT record the message.
# Ignore failed dfx command output (so the test continues) and check the logs to contain the message.
- dfx canister call --query canister_logs trap_query 'trap via non-replicated query'
! dfx canister logs canister_logs \
| grep 'trap via non-replicated query' && 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'
# Ignore failed dfx command 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
# Ignore failed dfx command 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
# Ignore failed dfx command 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.
# Test timer trap.
# The timer is setup to trap every 5 seconds, so this test has to be called
# at least 5 seconds after the start to record the trap log message.
sleep 5
dfx canister logs canister_logs \
| grep 'timer trap' && echo 'PASS'

Expand Down
1 change: 1 addition & 0 deletions rust/canister_logs/canister_logs.did
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ service : {
"print" : (text) -> ();
"print_query" : (text) -> () query;
"trap" : (text) -> ();
"trap_query" : (text) -> () query;
"panic" : (text) -> ();
"memory_oob" : () -> ();
"failed_unwrap" : () -> ();
Expand Down
6 changes: 6 additions & 0 deletions rust/canister_logs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ fn trap(message: String) {
ic_cdk::trap(&message);
}

#[query]
fn trap_query(message: String) {
ic_cdk::print("right before trap_query");
ic_cdk::trap(&message);
}

#[update]
fn panic(message: String) {
ic_cdk::print("right before panic");
Expand Down

0 comments on commit 24c76d1

Please sign in to comment.