|
31 | 31 | use std::time::Duration; |
32 | 32 |
|
33 | 33 | use livekit_protocol as proto; |
34 | | -use tokio::time::timeout; |
| 34 | +use tokio::time::{timeout, timeout_at, Instant}; |
35 | 35 |
|
36 | 36 | use super::{SignalClient, SignalError, SignalEvent, SignalEvents, SignalOptions}; |
37 | 37 | use crate::access_token::{AccessToken, VideoGrants}; |
@@ -166,15 +166,26 @@ async fn happy_stays_connected() { |
166 | 166 | let (client, join, mut events) = |
167 | 167 | connect(&base, &token("happy"), false).await.expect("happy connect should succeed"); |
168 | 168 |
|
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. |
171 | 177 | 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))) => { |
175 | 185 | panic!("connection closed while it should stay alive: {reason}") |
176 | 186 | } |
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"), |
178 | 189 | } |
179 | 190 | } |
180 | 191 |
|
|
0 commit comments