Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gtest): Improve value and ED managment #4111

Merged
merged 35 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a60dc13
State todos
techraed Aug 1, 2024
5b040b1
Change block execution model
techraed Aug 7, 2024
32ced45
State proper todos
techraed Aug 7, 2024
b23c60a
Fix most of workspace tests
techraed Aug 8, 2024
7d8ac30
Adjust signals test
techraed Aug 9, 2024
389bd45
Merge branch 'master' of github.com:gear-tech/gear into st-block-exec…
techraed Aug 9, 2024
17bcfa2
Fix workspace tests
techraed Aug 9, 2024
aeabdb2
Fix test
techraed Aug 9, 2024
977913e
feat(gtest): Decrease value after message sent
ByteNacked Aug 4, 2024
d42557b
WIP
ByteNacked Aug 11, 2024
aad49be
Update tests
ByteNacked Aug 11, 2024
2b52970
Update test, fix ED charging
ByteNacked Aug 12, 2024
322bd57
Clean up, return send_value on child program creation
ByteNacked Aug 12, 2024
6d8115d
Fix mailbox test
ByteNacked Aug 12, 2024
cd44374
Update cargo-gbuild tests
ByteNacked Aug 12, 2024
1867a54
Fix CI
techraed Aug 12, 2024
9183214
Review fixes
ByteNacked Aug 12, 2024
6eda532
Update gtest doc-comments
ByteNacked Aug 12, 2024
032c74e
Merge branch 'master' of github.com:gear-tech/gear into st-block-exec…
techraed Aug 13, 2024
e684bb4
Merge remote-tracking branch 'origin/st-block-exec-gtest' into rmasl-…
ByteNacked Aug 13, 2024
7ce2a48
Fix tests
ByteNacked Aug 13, 2024
e66c7e7
Fix tests II
ByteNacked Aug 13, 2024
8edf691
Add test and additional check
ByteNacked Aug 13, 2024
3fb0d65
Merge remote-tracking branch 'origin/master' into rmasl-gtest-improve…
ByteNacked Aug 14, 2024
e2594b2
Fix doc-comment
ByteNacked Aug 14, 2024
09e7882
refactor Actors storages
ByteNacked Aug 15, 2024
e32e4ee
Merge remote-tracking branch 'origin/master' into rmasl-gtest-improve…
ByteNacked Aug 15, 2024
fffbe78
Fix fn withdraw_gas params
ByteNacked Aug 15, 2024
26548fc
Update gtest/src/lib.rs
ByteNacked Aug 19, 2024
ccd3104
Review fixes
ByteNacked Aug 19, 2024
6ddf71a
Review fixes II
ByteNacked Aug 19, 2024
9421b16
Merge remote-tracking branch 'origin/master' into rmasl-gtest-improve…
ByteNacked Aug 19, 2024
820cbd1
Fix docs
ByteNacked Aug 19, 2024
8c17db4
Fix docs II
ByteNacked Aug 19, 2024
1317078
Review fixes III
ByteNacked Aug 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions examples/autoreply/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod wasm;
mod tests {
use alloc::vec::Vec;
use gstd::ActorId;
use gtest::{Program, System};
use gtest::{constants::DEFAULT_USER_ALICE, Program, System};

#[test]
fn auto_reply_received() {
Expand All @@ -45,20 +45,22 @@ mod tests {
let prog1 = Program::current(&system);
let prog1_id = ActorId::try_from(prog1.id().as_ref()).unwrap();

let from = 42;
let from = DEFAULT_USER_ALICE;

// 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
16 changes: 10 additions & 6 deletions examples/custom/src/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mod tests {
use super::{Reply, Request};
use crate::InitMessage;
use alloc::vec;
use gtest::{Log, Program, System};
use gtest::{constants::DEFAULT_USER_ALICE, Log, Program, System};

#[test]
fn program_can_be_initialized() {
Expand All @@ -103,9 +103,10 @@ mod tests {

let program = Program::current(&system);

let from = 42;
let from = DEFAULT_USER_ALICE;

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 @@ -117,9 +118,9 @@ mod tests {

let program = Program::current_opt(&system);

let from = 42;
let from = DEFAULT_USER_ALICE;

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

IntoIterator::into_iter([
Request::Insert(0, 1),
Expand All @@ -131,7 +132,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
52 changes: 32 additions & 20 deletions examples/distributor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod wasm;
#[cfg(test)]
mod tests {
use super::{Reply, Request};
use gtest::{Log, Program, System};
use gtest::{constants::DEFAULT_USER_ALICE, Log, Program, System};

#[test]
fn program_can_be_initialized() {
Expand All @@ -67,9 +67,10 @@ mod tests {

let program = Program::current(&system);

let from = 42;
let from = DEFAULT_USER_ALICE;

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 @@ -81,18 +82,21 @@ mod tests {

let program = Program::current(&system);

let from = 42;
let from = DEFAULT_USER_ALICE;

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 @@ -108,25 +112,27 @@ mod tests {
) -> (Program, Program, Program) {
system.init_logger();

let from = 42;
let from = DEFAULT_USER_ALICE;

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 @@ -141,23 +147,26 @@ mod tests {
let system = System::new();
let (program_1, program_2, _program_3) = multi_program_setup(&system, 1, 2, 3);

let from = 42;
let from = DEFAULT_USER_ALICE;

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 @@ -172,13 +181,16 @@ mod tests {
let (program_1, _program_2, _program_3) = multi_program_setup(&system, 1, 2, 3);

let program_4_id = 4;
let from = 42;
let from = DEFAULT_USER_ALICE;

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
22 changes: 16 additions & 6 deletions examples/gas-burned/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,33 @@ mod wasm;

#[cfg(test)]
mod tests {
use gtest::{Gas, Program, System};
use gtest::{constants::DEFAULT_USER_ALICE, Gas, Program, System};

#[test]
fn gas_burned() {
let system = System::new();
system.init_logger();

let from = 42;
let from = DEFAULT_USER_ALICE;

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
6 changes: 3 additions & 3 deletions examples/new-meta/tests/read_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use demo_new_meta::{
MessageInitIn, Person, Wallet, META_EXPORTS_V1, META_EXPORTS_V2, META_WASM_V1, META_WASM_V2,
};
use gstd::Encode;
use gtest::{state_args, state_args_encoded, Program, System};
use gtest::{constants::DEFAULT_USER_ALICE, state_args, state_args_encoded, Program, System};

#[test]
fn read_state_bytes_returns_full_state() {
Expand Down Expand Up @@ -183,14 +183,14 @@ fn read_state_with_two_args_wasm_func_returns_transformed_state() {
}

fn initialize_current_program(system: &System) -> Program {
const SOME_USER_ID: u64 = 3;
let program = Program::current(system);
program.send(
SOME_USER_ID,
DEFAULT_USER_ALICE,
MessageInitIn {
amount: 123,
currency: "USD".into(),
},
);
system.run_next_block();
program
}
Loading