Skip to content

Commit

Permalink
feat(gtest): gtest run blocks auto and next mode (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich authored Jan 9, 2025
1 parent bf06a8a commit d68e332
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
39 changes: 39 additions & 0 deletions examples/demo/app/tests/gtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,45 @@ async fn references_guess_num() {
assert_eq!(Ok("demo".to_owned()), res5);
}

#[tokio::test]
async fn counter_add_works_via_next_mode() {
// Arrange
const DEMO_WASM_PATH: &str = "../../../target/wasm32-unknown-unknown/debug/demo.opt.wasm";
let system = System::new();
system.init_logger();
system.mint_to(fixture::ADMIN_ID, 100_000_000_000_000);
let demo_code_id = system.submit_code_file(DEMO_WASM_PATH);

let remoting = GTestRemoting::new(system, fixture::ADMIN_ID.into())
.with_block_run_mode(BlockRunMode::Next);

let demo_factory = demo_client::DemoFactory::new(remoting.clone());

let demo_program_id = demo_factory
.new(Some(42), None)
.send_recv(demo_code_id, "123")
.await
.unwrap();

let mut counter_client = demo_client::Counter::new(remoting.clone());
// Listen to Counter events
let mut counter_listener = demo_client::counter::events::listener(remoting.clone());
let mut counter_events = counter_listener.listen().await.unwrap();

// Act
let result = counter_client
.add(10)
.send_recv(demo_program_id)
.await
.unwrap();

// Asert
let event = counter_events.next().await.unwrap();

assert_eq!(result, 52);
assert_eq!((demo_program_id, CounterEvents::Added(10)), event);
}

#[tokio::test]
async fn counter_add_works_via_manual_mode() {
// Arrange
Expand Down
47 changes: 35 additions & 12 deletions rs/src/gtest/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ impl GTestArgs {

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BlockRunMode {
Manual,
/// Run blocks automatically until all pending replies are received.
Auto,
/// Run only next block and exract events and replies from it.
/// If there is no reply in this block then `RtlError::ReplyIsMissing` error will be returned.
Next,
/// Sending messages does not cause blocks to run.
/// Use `GTestRemoting::run_next_block` method to run the next block and extract events and replies.
Manual,
}

#[derive(Clone)]
Expand Down Expand Up @@ -129,10 +135,6 @@ impl GTestRemoting {
}
}
}
// drain reply senders that not founded in block
for (_message_id, sender) in reply_senders.drain() {
_ = sender.send(Err(RtlError::ReplyIsMissing.into()));
}
}

fn send_message(
Expand Down Expand Up @@ -161,16 +163,23 @@ impl GTestRemoting {
Ok(message_id)
}

fn message_reply_from_next_block(
fn message_reply_from_next_blocks(
&self,
message_id: MessageId,
) -> impl Future<Output = Result<Vec<u8>>> {
let (tx, rx) = oneshot::channel::<Result<Vec<u8>>>();
self.block_reply_senders.borrow_mut().insert(message_id, tx);

if self.block_run_mode == BlockRunMode::Auto {
_ = self.run_next_block_and_extract();
}
match self.block_run_mode {
BlockRunMode::Auto => {
self.run_until_extract_replies();
}
BlockRunMode::Next => {
self.run_next_block_and_extract();
self.drain_reply_senders();
}
BlockRunMode::Manual => (),
};

rx.unwrap_or_else(|_| Err(RtlError::ReplyIsMissing.into()))
}
Expand All @@ -180,6 +189,20 @@ impl GTestRemoting {
self.extract_events_and_replies(&run_result);
run_result
}

fn run_until_extract_replies(&self) {
while !self.block_reply_senders.borrow().is_empty() {
self.run_next_block_and_extract();
}
}

fn drain_reply_senders(&self) {
let mut reply_senders = self.block_reply_senders.borrow_mut();
// drain reply senders that not founded in block
for (_message_id, sender) in reply_senders.drain() {
_ = sender.send(Err(RtlError::ReplyIsMissing.into()));
}
}
}

impl Remoting for GTestRemoting {
Expand Down Expand Up @@ -212,7 +235,7 @@ impl Remoting for GTestRemoting {
value,
);
Ok(self
.message_reply_from_next_block(message_id)
.message_reply_from_next_blocks(message_id)
.map(move |result| result.map(|reply| (program_id, reply))))
}

Expand All @@ -232,7 +255,7 @@ impl Remoting for GTestRemoting {
value,
args,
)?;
Ok(self.message_reply_from_next_block(message_id))
Ok(self.message_reply_from_next_blocks(message_id))
}

async fn query(
Expand All @@ -251,7 +274,7 @@ impl Remoting for GTestRemoting {
value,
args,
)?;
self.message_reply_from_next_block(message_id).await
self.message_reply_from_next_blocks(message_id).await
}
}

Expand Down

0 comments on commit d68e332

Please sign in to comment.