Skip to content

Commit

Permalink
chore: update canister_logs examples with replicated query cases (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymar authored Jul 3, 2024
1 parent 9b9f3d5 commit b098916
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
32 changes: 30 additions & 2 deletions motoko/canister_logs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions motoko/canister_logs/src/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
43 changes: 41 additions & 2 deletions rust/canister_logs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions rust/canister_logs/canister_logs.did
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
service : {
"print" : (text) -> ();
"print_query" : (text) -> () query;
"trap" : (text) -> ();
"panic" : (text) -> ();
"memory_oob" : () -> ();
Expand Down
7 changes: 6 additions & 1 deletion rust/canister_logs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
Expand Down

0 comments on commit b098916

Please sign in to comment.