Skip to content

Commit

Permalink
refactor(gtest): Change block execution model in gtest to be simila…
Browse files Browse the repository at this point in the history
…r to the vara-runtime (#4119)
  • Loading branch information
techraed authored Aug 14, 2024
1 parent 67410ab commit 8e4f55f
Show file tree
Hide file tree
Showing 23 changed files with 679 additions and 518 deletions.
14 changes: 8 additions & 6 deletions examples/autoreply/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ mod tests {
let from = 42;

// Init Program-1
let res = prog1.send(from, ActorId::zero());
assert!(!res.main_failed());
let init_msg1 = prog1.send(from, ActorId::zero());

// Init Program-2 with Program-1 as destination
let prog2 = Program::current(&system);
let res = prog2.send(from, prog1_id);
assert!(!res.main_failed());
let init_msg2 = prog2.send(from, prog1_id);

// Send a message from Program-2 to Program-1
let res = prog2.send_bytes(from, b"Let's go!");
assert!(!res.main_failed());
let msg3 = prog2.send_bytes(from, b"Let's go!");

let res = system.run_next_block();
for msg in [init_msg1, init_msg2, msg3] {
assert!(res.succeed.contains(&msg));
}

// Check whether the auto-reply was received
let reply_received: bool = prog2
Expand Down
11 changes: 7 additions & 4 deletions examples/custom/src/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ mod tests {

let from = 42;

let res = program.send(from, InitMessage::BTree);
program.send(from, InitMessage::BTree);
let res = system.run_next_block();
let log = Log::builder().source(program.id()).dest(from);
assert!(res.contains(&log));
}
Expand All @@ -119,8 +120,7 @@ mod tests {

let from = 42;

let _res = program.send(from, InitMessage::BTree);

program.send(from, InitMessage::BTree);
IntoIterator::into_iter([
Request::Insert(0, 1),
Request::Insert(0, 2),
Expand All @@ -131,7 +131,10 @@ mod tests {
Request::Clear,
Request::List,
])
.map(|r| program.send(from, r))
.map(|r| {
program.send(from, r);
system.run_next_block()
})
.zip(IntoIterator::into_iter([
Reply::Value(None),
Reply::Value(Some(1)),
Expand Down
40 changes: 26 additions & 14 deletions examples/distributor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ mod tests {

let from = 42;

let res = program.send_bytes(from, b"init");
program.send_bytes(from, b"init");
let res = system.run_next_block();
let log = Log::builder().source(program.id()).dest(from);
assert!(res.contains(&log));
}
Expand All @@ -83,16 +84,19 @@ mod tests {

let from = 42;

let _res = program.send_bytes(from, b"init");
// Init
program.send_bytes(from, b"init");

let res = program.send(from, Request::Receive(10));
program.send(from, Request::Receive(10));
let res = system.run_next_block();
let log = Log::builder()
.source(program.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program.send(from, Request::Report);
program.send(from, Request::Report);
let res = system.run_next_block();
let log = Log::builder()
.source(program.id())
.dest(from)
Expand All @@ -111,22 +115,24 @@ mod tests {
let from = 42;

let program_1 = Program::current_with_id(system, program_1_id);
let _res = program_1.send_bytes(from, b"init");
program_1.send_bytes(from, b"init");

let program_2 = Program::current_with_id(system, program_2_id);
let _res = program_2.send_bytes(from, b"init");
program_2.send_bytes(from, b"init");

let program_3 = Program::current_with_id(system, program_3_id);
let _res = program_3.send_bytes(from, b"init");
program_3.send_bytes(from, b"init");

let res = program_1.send(from, Request::Join(program_2_id.into()));
program_1.send(from, Request::Join(program_2_id.into()));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program_1.send(from, Request::Join(program_3_id.into()));
program_1.send(from, Request::Join(program_3_id.into()));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
Expand All @@ -143,21 +149,24 @@ mod tests {

let from = 42;

let res = program_1.send(from, Request::Receive(11));
program_1.send(from, Request::Receive(11));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program_2.send(from, Request::Report);
program_2.send(from, Request::Report);
let res = system.run_next_block();
let log = Log::builder()
.source(program_2.id())
.dest(from)
.payload(Reply::Amount(5));
assert!(res.contains(&log));

let res = program_1.send(from, Request::Report);
program_1.send(from, Request::Report);
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
Expand All @@ -175,10 +184,13 @@ mod tests {
let from = 42;

let program_4 = Program::current_with_id(&system, program_4_id);
let _res = program_4.send_bytes(from, b"init");
program_4.send_bytes(from, b"init");

IntoIterator::into_iter([Request::Receive(11), Request::Join(program_4_id.into())])
.map(|request| program_1.send(from, request))
.map(|request| {
program_1.send(from, request);
system.run_next_block()
})
.zip(IntoIterator::into_iter([Reply::Success, Reply::Success]))
.for_each(|(result, reply)| {
let log = Log::builder()
Expand Down
18 changes: 14 additions & 4 deletions examples/gas-burned/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ mod tests {
let from = 42;

let program = Program::current(&system);
let res = program.send_bytes(from, "init");
let init_gas_burned = res.main_gas_burned();
let init_msg_id = program.send_bytes(from, "init");
let res = system.run_next_block();
let init_gas_burned = res
.gas_burned
.get(&init_msg_id)
.copied()
.expect("internal error: init message isn't sent");
log::debug!("Init gas burned: {init_gas_burned}");
assert!(init_gas_burned > Gas::zero());

let res = program.send_bytes(from, "handle");
let handle_gas_burned = res.main_gas_burned();
let handle_msg_id = program.send_bytes(from, "handle");
let res = system.run_next_block();
let handle_gas_burned = res
.gas_burned
.get(&handle_msg_id)
.copied()
.expect("internal error: init message isn't sent");
log::debug!("Handle gas burned: {handle_gas_burned}");
assert!(handle_gas_burned > init_gas_burned);
}
Expand Down
1 change: 1 addition & 0 deletions examples/new-meta/tests/read_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,6 @@ fn initialize_current_program(system: &System) -> Program {
currency: "USD".into(),
},
);
system.run_next_block();
program
}
39 changes: 24 additions & 15 deletions examples/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ mod tests {
let from = 42;

let program = Program::current(&system);
let res = program.send(from, Request::IsReady);
assert!(res.main_failed());
let msg_id = program.send(from, Request::IsReady);
let res = system.run_next_block();
assert!(res.failed.contains(&msg_id));
}

#[test]
Expand All @@ -83,9 +84,10 @@ mod tests {
let from = 42;

let program = Program::current(&system);
let res = program.send(from, Initialization { status: 5 });
let msg_id = program.send(from, Initialization { status: 5 });
let log = Log::builder().source(program.id()).dest(from);
assert!(!res.main_failed());
let res = system.run_next_block();
assert!(res.succeed.contains(&msg_id));
assert!(res.contains(&log));
}

Expand All @@ -97,23 +99,26 @@ mod tests {
let from = 42;

let program = Program::current(&system);
let _res = program.send(from, Initialization { status: 5 });
program.send(from, Initialization { status: 5 });

let res = program.send(from, Request::IsReady);
program.send(from, Request::IsReady);
let log = Log::builder()
.source(program.id())
.dest(from)
.payload(Reply::Yes);
let res = system.run_next_block();
assert!(res.contains(&log));

let res = program.send(from, Request::Begin(Operation { to_status: 7 }));
program.send(from, Request::Begin(Operation { to_status: 7 }));
let res = system.run_next_block();
let log = Log::builder()
.source(program.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program.send(from, Request::Commit);
program.send(from, Request::Commit);
let res = system.run_next_block();
let log = Log::builder()
.source(program.id())
.dest(from)
Expand All @@ -133,36 +138,40 @@ mod tests {
let program_3_id = 3;

let program_1 = Program::current_with_id(&system, program_1_id);
let _res = program_1.send(from, Initialization { status: 5 });
program_1.send(from, Initialization { status: 5 });

let program_2 = Program::current_with_id(&system, program_2_id);
let _res = program_2.send(from, Initialization { status: 5 });
program_2.send(from, Initialization { status: 5 });

let program_3 = Program::current_with_id(&system, program_3_id);
let _res = program_3.send(from, Initialization { status: 9 });
program_3.send(from, Initialization { status: 9 });

let res = program_1.send(from, Request::Add(program_2_id.into()));
program_1.send(from, Request::Add(program_2_id.into()));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program_1.send(from, Request::Add(program_3_id.into()));
program_1.send(from, Request::Add(program_3_id.into()));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program_1.send(from, Request::Begin(Operation { to_status: 7 }));
program_1.send(from, Request::Begin(Operation { to_status: 7 }));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program_1.send(from, Request::Commit);
program_1.send(from, Request::Commit);
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
Expand Down
Loading

0 comments on commit 8e4f55f

Please sign in to comment.