test: use different contexts for listener and sender
use core::iter;
use std::time::Duration;
-use ockam_core::compat::sync::Arc;
-use ockam_core::{route, Error, Result, Routed, Worker};
+use ockam_core::{route, Address, Error, Result, Routed, Worker};
use ockam_node::Context;
use rand::Rng;
-use tokio::sync::{oneshot, RwLock};
+use tokio::sync::oneshot;
use tokio::time::timeout;
use ockam_transport_tcp::{TcpTransport, TCP};
@@ -20,23 +19,22 @@ async fn send_receive() {
assert!(rx.await.unwrap().is_ok(), "'run' method should return Ok");
}
-async fn run_with_timeout(tx: oneshot::Sender<Result<()>>, ctx: Context) {
- let ctx = Arc::new(RwLock::new(ctx));
- match timeout(Duration::from_secs(1), run_test(ctx.clone())).await {
+async fn run_with_timeout(tx: oneshot::Sender<Result<()>>, mut ctx: Context) {
+ match timeout(Duration::from_secs(1), run_test(&ctx)).await {
Ok(res) => tx.send(res).unwrap(),
Err(_) => tx.send(Err(Error::new(0, ""))).unwrap(),
};
- let mut ctx = ctx.write().await;
let _ = ctx.stop().await;
}
-async fn run_test(ctx: Arc<RwLock<Context>>) -> Result<()> {
- let mut ctx = ctx.write().await;
- let transport = TcpTransport::create(&ctx).await?;
+async fn run_test(ctx: &Context) -> Result<()> {
+ let _listener = {
+ let transport = TcpTransport::create(ctx).await?;
transport.listen("127.0.0.1:4000").await?;
-
ctx.start_worker("echoer", Echoer).await?;
-
+ };
+ let _sender = {
+ let mut ctx = ctx.new_context(Address::random(0)).await?;
let msg: String = {
let mut rng = rand::thread_rng();
iter::repeat(())
@@ -49,6 +47,8 @@ async fn run_test(ctx: Arc<RwLock<Context>>) -> Result<()> {
let reply = ctx.receive::<String>().await?;
assert_eq!(reply, msg, "Should receive the same message");
+ ctx.stop().await?;
+ };
Ok(())
}
