Skip to content

Commit

Permalink
feat(gtest): add self-sent delayed message test (#4240)
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteNacked authored Sep 18, 2024
1 parent a5fa004 commit 5536e04
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions examples/delayed-sender/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,34 @@ use gstd::{exec, msg, MessageId};
static mut MID: Option<MessageId> = None;
static mut DONE: bool = false;

fn send_delayed_to_self() -> bool {
let to_self = msg::load_bytes().unwrap().as_slice() == b"self";
if to_self {
gstd::debug!("sending delayed message to self");
msg::send_bytes_delayed(exec::program_id(), b"self", 0, DELAY).unwrap();
}

to_self
}

#[no_mangle]
extern "C" fn init() {
// Send message to self
if send_delayed_to_self() {
return;
}

let delay: u32 = msg::load().unwrap();

msg::send_bytes_delayed(msg::source(), "Delayed hello!", 0, delay).unwrap();
}

#[no_mangle]
extern "C" fn handle() {
if send_delayed_to_self() {
return;
}

let size = msg::size();

if size == 0 {
Expand Down
1 change: 1 addition & 0 deletions gtest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ demo-piggy-bank.workspace = true
demo-ping.workspace = true
demo-futures-unordered.workspace = true
demo-constructor = { workspace = true, features = ["std"] }
demo-delayed-sender.workspace = true
28 changes: 27 additions & 1 deletion gtest/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,7 @@ mod tests {
#[test]
fn tests_unused_gas_value_not_transferred() {
let sys = System::new();
sys.init_verbose_logger();
sys.init_logger();

let user = 42;
sys.mint_to(user, 2 * EXISTENTIAL_DEPOSIT);
Expand All @@ -1497,4 +1497,30 @@ mod tests {
// lower than ED
assert_eq!(sys.balance_of(user), 0)
}

#[test]
fn tests_self_sent_delayed_message() {
use demo_delayed_sender::DELAY;

let sys = System::new();
sys.init_logger();

let user = DEFAULT_USER_ALICE;
let program_id = 69;

let prog = Program::from_binary_with_id(&sys, program_id, demo_delayed_sender::WASM_BINARY);

// Init message starts sequence of self-sent messages
prog.send_bytes(user, "self".as_bytes());
let res = sys.run_next_block();
assert_eq!(res.succeed.len(), 1);

let mut target_block_nb = sys.block_height() + DELAY;
let res = sys.run_to_block(target_block_nb);
assert_eq!(res.iter().last().unwrap().succeed.len(), 1);

target_block_nb += DELAY;
let res = sys.run_to_block(target_block_nb);
assert_eq!(res.iter().last().unwrap().succeed.len(), 1);
}
}

0 comments on commit 5536e04

Please sign in to comment.