Skip to main content

serialplugin_testing/
lib.rs

1// Its snippets run as doctests of serialplugin-core, which one of them uses.
2#![cfg_attr(not(doctest), doc = include_str!("../README.md"))]
3#![warn(missing_docs)]
4//!
5//! ## Independence
6//!
7//! Written from the protocol descriptions, not with the codecs under test, so a codec bug
8//! cannot hide behind the same bug in its test peer.
9
10mod fake;
11mod modem;
12pub mod mux;
13pub mod peer;
14
15pub use fake::{Call, FakeDevice, FakePort, Lines};
16pub use modem::{Command, Modem, ModemLog, MuxOptions};
17
18use std::time::{Duration, Instant};
19
20/// Bound of every wait in the tests. It only limits how long a failure takes: waits end on
21/// the event they wait for.
22pub const WAIT: Duration = Duration::from_secs(10);
23
24/// Poll `done` (a state no event announces) until it holds; panics with `what` after
25/// [`WAIT`].
26///
27/// It looks every millisecond. That is a polling interval, not a delay anything depends on.
28pub fn eventually(what: &str, mut done: impl FnMut() -> bool) {
29    let deadline = Instant::now() + WAIT;
30    while !done() {
31        assert!(Instant::now() < deadline, "never: {what}");
32        std::thread::park_timeout(Duration::from_millis(1));
33    }
34}