Wait for a preview frame of the size and scene asked for in the studio test

it_composes_into_the_preview_and_follows_commands took the first preview frame
after SetPreview and asserted its size. The studio composes from the moment it
starts, at the preview size and rate it has then (480x270, 15 fps), so the
first frame there is can be one of that size, composed before the command was
applied; on a busy machine it is. The 2 s waits for the new scene's colour
and the test's overall 5 s and 10 s limits measured the machine too.

preview_until now reads the preview until a frame that the test takes comes,
and looks at 20 frames at most: the preview keeps only the latest frame, so
few older ones are waiting or on their way, and the number of frames looked at
is the bound by the work. The wait for each frame, 30 s, only ends a hang.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-25 03:26:33 +03:00
1 parent 77304604fe
commit 9d04e0158a
1 file changed
+26 -17
+26 -17
View File
@@ -1180,19 +1180,32 @@ mod tests {
scenes
}
/// The first preview frame that `wanted` takes. The preview keeps the latest
/// frame only, so few older ones are waiting or on their way: the number of
/// frames looked at is the bound (the work), and the wait for each, which a
/// busy machine can stretch, only ends a hang.
fn preview_until(
preview: &Handoff<VideoFrame>,
wanted: impl Fn(&VideoFrame) -> bool,
) -> Arc<VideoFrame> {
for _ in 0..20 {
let frame = preview.wait_timeout(Duration::from_secs(30)).expect("a preview frame");
if wanted(&frame) {
return frame;
}
}
panic!("the preview never showed what was asked for");
}
#[tokio::test]
async fn it_composes_into_the_preview_and_follows_commands() {
let studio = Studio::start(one_colour_scene()).await.unwrap();
let preview = studio.preview();
studio.apply(Command::SetPreview { width: 32, height: 24, fps: 60 }).await.unwrap();
let started = Instant::now();
let frame = loop {
if let Some(frame) = preview.wait_timeout(Duration::from_millis(200)) {
break frame;
}
assert!(started.elapsed() < Duration::from_secs(5), "no preview frame");
};
assert_eq!((frame.width, frame.height), (32, 24));
// The studio composes from the moment it starts, at the size and rate the
// preview has then (480x270, 15 fps): a frame of that size can be the
// first one there is.
let frame = preview_until(&preview, |f| (f.width, f.height) == (32, 24));
let crate::frame::FrameData::Rgba(p) = &frame.data else { panic!("not RGBA") };
assert_eq!(&p.data[..4], &[9, 8, 7, 255]);
@@ -1211,15 +1224,11 @@ mod tests {
.unwrap();
studio.apply(Command::SetActiveScene { scene: break_id }).await.unwrap();
assert_eq!(studio.status().active_scene, break_id);
// The new scene's source draws.
let frame = loop {
let frame = preview.wait_timeout(Duration::from_secs(2)).expect("a preview frame");
let crate::frame::FrameData::Rgba(p) = &frame.data else { panic!() };
if p.data[..3] == [1, 2, 3] {
break frame;
}
assert!(started.elapsed() < Duration::from_secs(10), "the scene never switched");
};
// The new scene's source draws, from the first frame composed after the
// command.
let frame = preview_until(&preview, |f| {
matches!(&f.data, crate::frame::FrameData::Rgba(p) if p.data[..3] == [1, 2, 3])
});
drop(frame);
studio.apply(Command::RemoveScene { scene: break_id }).await.unwrap();