mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
Merge pull request #3964 from dotCooCoo/fix/platform-test-handshakes
platform: the portal and dialog lifecycle tests wait on the worker's messages instead of on time
This commit is contained in:
4 files changed
+65
-61
No files matched your search
Generated
+1
@@ -3803,6 +3803,7 @@ dependencies = [
|
||||
"craft-docpath",
|
||||
"craft-host",
|
||||
"craft-i18n",
|
||||
"craft-testkit",
|
||||
"craft-theme",
|
||||
"fmv-macos-events",
|
||||
"futures-lite",
|
||||
|
||||
@@ -86,5 +86,8 @@ web-sys = { version = "0.3", features = ["Window", "Document", "HtmlElement", "H
|
||||
rfd = { version = "0.17.2", default-features = false, optional = true }
|
||||
wasm-bindgen-futures = { version = "0.4", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
craft-testkit = { workspace = true }
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
||||
wasm-bindgen-test = "0.3"
|
||||
@@ -494,7 +494,11 @@ mod tests {
|
||||
use super::{ANIMATIONS_KEY, Answers, GNOME, KDE, KDE_MOTION_KEY, MOTION_KEY, STANDARD, decode, decode_motion};
|
||||
use crate::appearance::is_gnome_session;
|
||||
use crate::appearance::{Theme, Watcher};
|
||||
use craft_testkit::deadline::with_deadline;
|
||||
use std::time::Duration;
|
||||
// A hang guard only: each wait under it ends at a message the worker sends, so a run that works
|
||||
// never reaches the limit.
|
||||
const HANG_GUARD: Duration = Duration::from_secs(60);
|
||||
use zbus::zvariant::Value;
|
||||
// The portal's, GNOME's and KDE's keys each decode to reduced or not; malformed values say
|
||||
// nothing. Combined, any one asking for reduced motion wins, all answering otherwise is not
|
||||
@@ -524,7 +528,7 @@ mod tests {
|
||||
},
|
||||
)
|
||||
.ok_or("worker creation failed")?;
|
||||
let shared = sent.recv_timeout(Duration::from_secs(2))?;
|
||||
let shared = with_deadline(HANG_GUARD, "the source's shared state", move || sent.recv())?;
|
||||
let mut answers = Answers { standard: None, gnome: None, motion: None, gnome_motion: None, kde_motion: None };
|
||||
answers.apply(&shared, KDE, "SingleClick", &Value::F64(0.0));
|
||||
answers.apply(&shared, KDE, KDE_MOTION_KEY, &Value::from("fast"));
|
||||
@@ -618,7 +622,7 @@ mod tests {
|
||||
},
|
||||
)
|
||||
.ok_or("worker creation failed")?;
|
||||
finished.recv_timeout(Duration::from_secs(2))?;
|
||||
with_deadline(HANG_GUARD, "the source's publishing", move || finished.recv())?;
|
||||
assert_eq!(watcher.generation(), 4, "theme, contrast, motion and text scale each changed once");
|
||||
assert_eq!((watcher.current(), watcher.contrast()), (Some(Theme::Dark), Some(Contrast::High)));
|
||||
assert_eq!(watcher.text_scale(), Some(1.25));
|
||||
@@ -656,18 +660,19 @@ mod tests {
|
||||
},
|
||||
)
|
||||
.ok_or("worker creation failed")?;
|
||||
ready.recv_timeout(Duration::from_secs(2))?;
|
||||
with_deadline(HANG_GUARD, "the source's start", move || ready.recv())?;
|
||||
let (dropped, drop_result) = std::sync::mpsc::channel();
|
||||
let dropper = std::thread::spawn(move || {
|
||||
drop(watcher);
|
||||
let _ = dropped.send(());
|
||||
});
|
||||
let bounded = drop_result.recv_timeout(Duration::from_secs(1)).is_ok();
|
||||
// Release before asserting, including when an unbounded implementation regresses.
|
||||
// The source stays blocked until `release` drops, so a drop that waits for it never returns
|
||||
// and the hang guard fails the test; unwinding from that failure drops `release`.
|
||||
let returned = with_deadline(HANG_GUARD, "the watcher drop while its source is blocked", move || drop_result.recv());
|
||||
drop(release);
|
||||
exit.recv_timeout(Duration::from_secs(2))?;
|
||||
with_deadline(HANG_GUARD, "the released source's exit", move || exit.recv())?;
|
||||
assert!(dropper.join().is_ok(), "watcher drop panicked");
|
||||
assert!(bounded, "watcher drop waited for a blocked synchronous poll");
|
||||
assert!(returned.is_ok(), "watcher drop waited for a blocked synchronous poll");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -2,71 +2,66 @@
|
||||
|
||||
#![cfg(not(target_arch = "wasm32"))]
|
||||
use craft_platform::{dialog_outcome::Outcome, dialogs::NativeDialogs};
|
||||
use craft_testkit::deadline::with_deadline;
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
mpsc,
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
fn pump_until<R, T: Send + 'static>(
|
||||
service: &mut NativeDialogs<R, T>,
|
||||
mut create: impl FnMut(R) -> craft_platform::dialogs::DialogFuture<T>,
|
||||
until: impl Fn() -> bool,
|
||||
) {
|
||||
let deadline = Instant::now() + Duration::from_secs(3);
|
||||
while !until() {
|
||||
assert!(Instant::now() < deadline, "worker did not settle");
|
||||
service.pump(&mut create);
|
||||
std::thread::yield_now();
|
||||
}
|
||||
}
|
||||
use std::time::Duration;
|
||||
// The service wakes the host once per request, once when its worker is ready for the picker and once
|
||||
// when the picker has answered. The test pumps after each wake instead of polling against a clock.
|
||||
#[test]
|
||||
fn completion_between_pumps_cannot_reject_a_chained_request() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let wakes = Arc::new(AtomicUsize::new(0));
|
||||
let count = wakes.clone();
|
||||
let (client, mut service) = NativeDialogs::<u8, u8>::new(move || {
|
||||
count.fetch_add(1, Ordering::SeqCst);
|
||||
});
|
||||
let first = client.request(1);
|
||||
let (release, wait) = mpsc::channel();
|
||||
let mut wait = Some(wait);
|
||||
let created = Arc::new(AtomicUsize::new(0));
|
||||
let count = created.clone();
|
||||
pump_until(
|
||||
&mut service,
|
||||
move |value| {
|
||||
fn completion_between_pumps_cannot_reject_a_chained_request() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
// A hang guard only: every wait below ends at a wake, so a run that works never reaches the limit.
|
||||
with_deadline(Duration::from_secs(60), "the dialog lifecycle", || {
|
||||
let wakes = Arc::new(AtomicUsize::new(0));
|
||||
let count = wakes.clone();
|
||||
let (wake, woken) = mpsc::channel();
|
||||
let (client, mut service) = NativeDialogs::<u8, u8>::new(move || {
|
||||
count.fetch_add(1, Ordering::SeqCst);
|
||||
let _ = wake.send(());
|
||||
});
|
||||
let first = client.request(1);
|
||||
woken.recv()?;
|
||||
let (release, wait) = mpsc::channel();
|
||||
let mut wait = Some(wait);
|
||||
let created = Arc::new(AtomicUsize::new(0));
|
||||
let count = created.clone();
|
||||
let mut create = move |value| -> craft_platform::dialogs::DialogFuture<u8> {
|
||||
count.fetch_add(1, Ordering::SeqCst);
|
||||
let wait = wait.take();
|
||||
Box::pin(async move {
|
||||
let _ = wait.map(|wait| wait.recv());
|
||||
Outcome::Selected(value)
|
||||
})
|
||||
},
|
||||
|| created.load(Ordering::SeqCst) == 1,
|
||||
);
|
||||
// Finish after the host's pump, before its app continuation polls the reply.
|
||||
release.send(())?;
|
||||
let deadline = Instant::now() + Duration::from_secs(3);
|
||||
while wakes.load(Ordering::SeqCst) < 3 {
|
||||
assert!(Instant::now() < deadline, "worker did not publish completion");
|
||||
std::thread::yield_now();
|
||||
}
|
||||
assert!(matches!(first.try_recv(), Err(mpsc::TryRecvError::Empty)));
|
||||
assert!(service.pending(), "an unpublished completion still retains the parent");
|
||||
service.pump(|value| Box::pin(async move { Outcome::Selected(value) }));
|
||||
assert_eq!(first.try_recv()?, Outcome::Selected(1));
|
||||
assert!(!service.pending());
|
||||
// The app can chain the next dialog immediately upon observing the first answer.
|
||||
let second = client.request(2);
|
||||
assert!(matches!(second.try_recv(), Err(mpsc::TryRecvError::Empty)));
|
||||
let mut result = None;
|
||||
while result.is_none() {
|
||||
assert!(Instant::now() < deadline, "chained picker did not settle");
|
||||
};
|
||||
// The first pump starts the worker; the second, after its wake, opens the picker.
|
||||
service.pump(&mut create);
|
||||
woken.recv()?;
|
||||
service.pump(&mut create);
|
||||
assert_eq!(created.load(Ordering::SeqCst), 1, "worker did not settle");
|
||||
// Finish after the host's pump, before its app continuation polls the reply.
|
||||
release.send(())?;
|
||||
woken.recv()?;
|
||||
assert_eq!(wakes.load(Ordering::SeqCst), 3, "worker did not publish completion");
|
||||
assert!(matches!(first.try_recv(), Err(mpsc::TryRecvError::Empty)));
|
||||
assert!(service.pending(), "an unpublished completion still retains the parent");
|
||||
service.pump(|value| Box::pin(async move { Outcome::Selected(value) }));
|
||||
result = second.try_recv().ok();
|
||||
std::thread::yield_now();
|
||||
}
|
||||
assert_eq!(result, Some(Outcome::Selected(2)));
|
||||
assert!(!service.pending());
|
||||
Ok(())
|
||||
assert_eq!(first.try_recv()?, Outcome::Selected(1));
|
||||
assert!(!service.pending());
|
||||
// The app can chain the next dialog immediately upon observing the first answer.
|
||||
let second = client.request(2);
|
||||
assert!(matches!(second.try_recv(), Err(mpsc::TryRecvError::Empty)));
|
||||
// The request's wake, the worker's and the picker's answer each lead to one pump.
|
||||
for _ in 0..3 {
|
||||
woken.recv()?;
|
||||
service.pump(|value| Box::pin(async move { Outcome::Selected(value) }));
|
||||
}
|
||||
let result = second.try_recv().ok();
|
||||
assert!(result.is_some(), "chained picker did not settle");
|
||||
assert_eq!(result, Some(Outcome::Selected(2)));
|
||||
assert!(!service.pending());
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user