From 272de7a13de64b6410b8d78b5c1ff3f48e82b9f5 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sat, 24 Oct 2026 21:41:44 +0300 Subject: [PATCH] Read the streamer's stats until they show what the viewer already saw Two voelin-core tests read Streamer::stats() once, at the moment a consumer had seen the effect, and the stats lag behind it: - stream::tests::test_pattern_through_stream_tasks asserted `video_frames >= 15` right after the viewer had decoded 15 pictures. The encoder counts a frame after the sink has taken it (`sink.send_video(..)`, then `fetch_add`), so the 15th picture can be at the viewer first: video_frames was 14 with captured_frames 15 and the picture on screen. - media::tests::an_automatic_bitrate_follows_the_stream asserted the layer's bitrate in the stats right after the sink had been told the layer's format; the stats had 0 (the layer's target is stored by the encoder thread, not by the call that tells the sink). Both now poll for the value with a deadline (5 s, and 10 s as the test's other waits), which is the same assertion without the order of two threads. Before, on a 32-core machine with 32 busy loops beside the runs: the first failed on that assertion in 7 of 150 runs of the stream tests six at a time, the second in 19 of 120 runs of the voelin-core lib tests six at a time. After, in 200 runs of the lib tests three at a time with 16 busy loops beside them, neither failed on these assertions. Co-Authored-By: Claude Sonnet 5.5 --- crates/voelin-core/src/media.rs | 7 ++++++- crates/voelin-core/src/stream.rs | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/voelin-core/src/media.rs b/crates/voelin-core/src/media.rs index 5eb0986..38db2da 100644 --- a/crates/voelin-core/src/media.rs +++ b/crates/voelin-core/src/media.rs @@ -3540,7 +3540,12 @@ mod tests { let count = lock(&sink.formats).len(); tokio::time::sleep(Duration::from_millis(200)).await; assert_eq!(lock(&sink.formats).len(), count); - assert_eq!(streamer.stats().layers[0].bitrate, auto_bitrate(640, 480, 30)); + // The stats follow the sink's being told, not the other way round. + let deadline = Instant::now() + Duration::from_secs(10); + while streamer.stats().layers[0].bitrate != auto_bitrate(640, 480, 30) { + assert!(Instant::now() < deadline, "{:?}", streamer.stats().layers); + tokio::time::sleep(Duration::from_millis(20)).await; + } let update = StreamerConfigUpdate { fps: Some(15), ..StreamerConfigUpdate::default() }; streamer.reconfigure(&codecs, update).unwrap(); told(15).await; diff --git a/crates/voelin-core/src/stream.rs b/crates/voelin-core/src/stream.rs index 585631e..478db9a 100644 --- a/crates/voelin-core/src/stream.rs +++ b/crates/voelin-core/src/stream.rs @@ -1701,7 +1701,16 @@ mod tests { let stats = pipeline.stats(); assert_eq!(stats.codec, Some(Codec::Vp8)); assert!(stats.error.is_none(), "{stats:?}"); - let sent = streamer.stats(); + // The encoder counts a frame once the sink has taken it, so the picture + // can be at the viewer before the count has moved. + let mut sent = streamer.stats(); + for _ in 0..500 { + if sent.video_frames >= 15 && sent.audio_frames > 0 { + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + sent = streamer.stats(); + } assert!(sent.video_frames >= 15 && sent.audio_frames > 0, "{sent:?}"); handles[0].send(StreamCmd::Stop);