Skip to content

Commit

Permalink
attempt to make route_ws_to_correct_monolith_race less flaky (#1114)
Browse files Browse the repository at this point in the history
* attempt to make `route_ws_to_correct_monolith_race` less flaky

* fix lint
  • Loading branch information
dyc3 authored Oct 22, 2023
1 parent 8d52c1e commit ebd43cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
19 changes: 15 additions & 4 deletions crates/harness-tests/src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,23 @@ async fn route_ws_to_correct_monolith_race(ctx: &mut TestRunner) {
m.load_room(room_name.clone()).await;

let mut client = Client::new(ctx).unwrap();
client.join(room_name).await;
client.join(room_name.clone()).await;

println!("waiting for monolith to receive join message");
tokio::time::timeout(Duration::from_secs(1), m.wait_recv())
.await
.expect("msg recv timeout");
// this more accurately emulates what the client would actually do
loop {
tokio::select! {
result = tokio::time::timeout(Duration::from_secs(1), m.wait_recv()) => {
result.expect("msg recv timeout");
break;
},
_ = client.wait_for_disconnect() => {
println!("client disconnected, retrying");
client.join(room_name.clone()).await;
continue;
}
};
}

let recvd = m.collect_recv();
assert_eq!(recvd.len(), 1);
Expand Down
9 changes: 9 additions & 0 deletions crates/harness/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ impl Client {
let _ = stream.close(None).await;
}

pub async fn wait_for_disconnect(&mut self) {
if !self.connected() {
return;
}

let mut stream = self.stream.take().unwrap();
while stream.next().await.is_some() {}
}

pub async fn recv(&mut self) -> anyhow::Result<Message> {
if let Some(stream) = self.stream.as_mut() {
match tokio::time::timeout(Duration::from_millis(200), stream.next()).await {
Expand Down

0 comments on commit ebd43cc

Please sign in to comment.