From 4210e8c128bf38a9dc3a4f3f9b223d19f53c46fb Mon Sep 17 00:00:00 2001 From: Alex Ostrovski Date: Thu, 2 Nov 2023 16:12:58 +0200 Subject: [PATCH] fix: Fix race condition in network tests (#20) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # What ❔ Fixes a race condition in `getting_blocks_from_peers` test. ## Why ❔ Bugs are bad. --- node/actors/network/src/gossip/tests.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/node/actors/network/src/gossip/tests.rs b/node/actors/network/src/gossip/tests.rs index a5156284..77ad9f63 100644 --- a/node/actors/network/src/gossip/tests.rs +++ b/node/actors/network/src/gossip/tests.rs @@ -18,8 +18,6 @@ use test_casing::{test_casing, Product}; use tracing::Instrument as _; use utils::pipe; -const TEST_TIMEOUT: time::Duration = time::Duration::seconds(5); - #[tokio::test] async fn test_one_connection_per_node() { concurrency::testonly::abort_on_panic(); @@ -484,8 +482,7 @@ async fn run_mock_uncoordinated_dispatcher( async fn getting_blocks_from_peers(node_count: usize, gossip_peers: usize) { concurrency::testonly::abort_on_panic(); - let ctx = &ctx::test_root(&ctx::RealClock).with_timeout(TEST_TIMEOUT); - let ctx = &ctx::test_with_clock(ctx, &ctx::ManualClock::new()); + let ctx = &ctx::test_root(&ctx::ManualClock::new()); let rng = &mut ctx.rng(); let mut nodes = testonly::Instance::new(rng, node_count, gossip_peers); for node in &mut nodes { @@ -507,12 +504,11 @@ async fn getting_blocks_from_peers(node_count: usize, gossip_peers: usize) { s.spawn_bg(async { scope::run!(ctx, |ctx, s| async { s.spawn_bg(run_network(ctx, node.state.clone(), network_pipe)); - s.spawn_bg(run_get_block_dispatcher( - ctx, - dispatcher_pipe.recv, - block.clone(), - )); - node_stop_receiver.recv_or_disconnected(ctx).await?.ok(); + s.spawn_bg(async { + run_get_block_dispatcher(ctx, dispatcher_pipe.recv, block.clone()).await; + Ok(()) + }); + node_stop_receiver.recv_or_disconnected(ctx).await.ok(); Ok(()) }) .await @@ -578,7 +574,7 @@ async fn run_get_block_dispatcher( ctx: &ctx::Ctx, mut receiver: channel::UnboundedReceiver, block: FinalBlock, -) -> anyhow::Result<()> { +) { while let Ok(message) = receiver.recv(ctx).await { match message { io::OutputMessage::SyncBlocks(io::SyncBlocksRequest::GetBlock { @@ -591,7 +587,6 @@ async fn run_get_block_dispatcher( other => panic!("received unexpected message: {other:?}"), } } - Ok(()) } /// When validator node is restarted, it should immediately override