Skip to content

Commit 95dcb3d

Browse files
committed
address devin comment
1 parent d6020b2 commit 95dcb3d

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

livekit-api/src/signal_client/signal_test.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use std::time::Duration;
3232

3333
use livekit_protocol as proto;
34-
use tokio::time::timeout;
34+
use tokio::time::{timeout, timeout_at, Instant};
3535

3636
use super::{SignalClient, SignalError, SignalEvent, SignalEvents, SignalOptions};
3737
use crate::access_token::{AccessToken, VideoGrants};
@@ -166,15 +166,26 @@ async fn happy_stays_connected() {
166166
let (client, join, mut events) =
167167
connect(&base, &token("happy"), false).await.expect("happy connect should succeed");
168168

169-
// Wait comfortably past the ping timeout: if pongs weren't flowing, the
170-
// signal task would emit Close("ping timeout") by then.
169+
// Wait comfortably past the ping timeout: if pongs weren't flowing, the signal task
170+
// would emit Close("ping timeout") by then.
171+
//
172+
// Drained in a loop against a fixed deadline rather than a single `recv`. In happy mode
173+
// the server's pong is forwarded as a `Message`, and it arrives within the first ping
174+
// interval — so a one-shot recv returns on that pong, long before the window is up, and
175+
// the test passes no matter what happens afterwards. A connection that dies right after
176+
// its first pong is exactly the failure this test exists to catch.
171177
let window = Duration::from_secs(join.ping_timeout as u64) + Duration::from_secs(2);
172-
if let Ok(Some(event)) = timeout(window, events.recv()).await {
173-
match event {
174-
SignalEvent::Close(reason) => {
178+
let deadline = Instant::now() + window;
179+
loop {
180+
match timeout_at(deadline, events.recv()).await {
181+
// the whole window elapsed with no Close — the connection stayed up, which is
182+
// the only outcome that passes
183+
Err(_) => break,
184+
Ok(Some(SignalEvent::Close(reason))) => {
175185
panic!("connection closed while it should stay alive: {reason}")
176186
}
177-
SignalEvent::Message(_) => { /* server-initiated messages are fine */ }
187+
Ok(Some(SignalEvent::Message(_))) => { /* server-initiated traffic is fine */ }
188+
Ok(None) => panic!("event stream closed unexpectedly"),
178189
}
179190
}
180191

0 commit comments

Comments
 (0)