Testing
Let's verify the functionality of the programs discussed in the previous section by using the gtest
library, which was discussed in the basic course.
- Create a testing environment:
let system = System::new();
- Retrieve the Proxy program from the root crate using the provided
system
and the Target program instance from the wasm file.
let proxy_program = Program::current(&system);
let target_program = Program::from_file(&system, "target/wasm32-unknown-unknown/release/target_program.opt.wasm");
- Initialize the Target program by sending an empty message. Launch the Proxy program by passing the address of the Target program to it.
let result = target_program.send_bytes(USER, []); // initialize Target program
let target_program_address: ActorId = TARGET_PROGRAM_ADDRESS.into();
let res = proxy_program.send(USER, target_program_address); // initialize Proxy program
- Send a message with
Action::MakeRandomNumber { range: 1 }
to the Proxy program and check for the responseEvent::MessageSent
, indicating that the message was successfully sent to the Target program's address.
let result = proxy_program.send(USER, Action::MakeRandomNumber {range: 1});
let log = Log::builder()
.source(1)
.dest(3)
.payload(Event::MessageSent);
assert!(result.contains(&log));
- Retrieve the user's mailbox with the specified ID. Verify that a reply message has been sent back to the user.
let mailbox = system.get_mailbox(USER);
let log = Log::builder()
.source(1)
.dest(3)
.payload(Event::Number(0));
assert!(mailbox.contains(&log));
Below is the complete test code:
use gstd::ActorId;
use gtest::{Log, Program, System};
use handle_reply_io::{Action, Event};
const USER: u64 = 3;
const TARGET_PROGRAM_ADDRESS: u64 = 2;
#[test]
fn success_test() {
// Create a new testing environment.
let system = System::new();
// Get proxy program of the root crate with provided system.
let proxy_program = Program::current(&system);
// Get target program
let target_program = Program::from_file(&system, "target/wasm32-unknown-unknown/release/target_program.opt.wasm");
// The target program is initialized with an empty payload message
let result = target_program.send_bytes(USER, []);
assert!(!result.main_failed());
let target_program_address: ActorId = TARGET_PROGRAM_ADDRESS.into();
// The proxy program is initialized using target_program in the payload message
let res = proxy_program.send(USER, target_program_address);
assert!(!res.main_failed());
// Send with the message we want to receive back
let result = proxy_program.send(USER, Action::MakeRandomNumber {range: 1});
assert!(!result.main_failed());
// check that the proxy message has arrived,
// which means that the message was successfully sent to the target program
let log = Log::builder()
.source(1)
.dest(3)
.payload(Event::MessageSent);
assert!(result.contains(&log));
// check that the target message has arrived at the mailbox,
// which means that a reply has been received.
let mailbox = system.get_mailbox(USER);
let log = Log::builder()
.source(1)
.dest(3)
.payload(Event::Number(0));
assert!(mailbox.contains(&log));
}
Hands-on experience is essential. Try implementing and testing these programs yourself.