use anyhow::Context;
use demo_ping::WASM_BINARY;
use futures::StreamExt;
use gclient::{GearApi, UserMessageSentFilter};
use gear_core::ids::ActorId;
use std::convert::TryFrom;
use tokio::time::{Duration, timeout};
const GEAR_PATH: &str = "../target/release/gear";
const SUBSCRIPTION_TIMEOUT: Duration = Duration::from_secs(5);
#[tokio::test]
async fn subscribe_user_messages_receives_reply() -> anyhow::Result<()> {
let api = GearApi::dev_from_path(GEAR_PATH).await?;
let destination =
ActorId::try_from(api.account_id().as_ref()).expect("account id must be a valid ActorId");
let mut subscription = api
.subscribe_user_message_sent(
UserMessageSentFilter::new()
.with_destination(destination)
.with_payload_prefix(b"PONG"),
)
.await?;
let gas_limit = api.block_gas_limit()?;
let salt = gclient::now_micros().to_le_bytes();
api.upload_program_bytes(WASM_BINARY, salt, b"PING".to_vec(), gas_limit, 0)
.await?;
let mut received = None;
for _ in 0..10 {
let next_event = timeout(SUBSCRIPTION_TIMEOUT, subscription.next())
.await
.context("timed out waiting for user message event")?;
match next_event {
Some(Ok(event)) if event.destination == destination => {
if event.payload == b"PONG" {
received = Some(event);
break;
}
}
Some(Ok(_)) => continue,
Some(Err(err)) => return Err(err.into()),
None => break,
}
}
let event = received.expect("expected user message reply");
assert_eq!(event.payload, b"PONG");
let reply = event.reply.expect("expected reply details");
assert!(
reply.code.is_success(),
"expected successful reply code, got {:?}",
reply.code
);
Ok(())
}