oscyl_conformance/
pump.rs1use std::time::Instant;
6
7use oscyl_driver::{DriverSession, SessionEvent, Simulator};
8
9pub fn run_until_idle(
12 session: &mut dyn DriverSession,
13 sim: &mut dyn Simulator,
14 who: &str,
15) -> Vec<SessionEvent> {
16 let mut now = Instant::now();
17 let mut events = vec![];
18 loop {
19 if let Some(msg) = session.poll_transmit(now) {
20 for reply in sim.handle(&msg) {
21 session.handle_input(&reply);
22 }
23 } else if let Some(deadline) = session.next_deadline() {
24 now = deadline;
25 session.handle_timeout(now);
26 } else if session.is_idle() {
27 events.extend(std::iter::from_fn(|| session.poll_event()));
28 return events;
29 } else {
30 panic!("{who}: session is busy but has nothing to send and no deadline");
31 }
32 events.extend(std::iter::from_fn(|| session.poll_event()));
33 }
34}
35
36pub fn settle(sim: &mut dyn Simulator, who: &str) {
38 for _ in 0..1000 {
39 if sim.is_settled() {
40 return;
41 }
42 sim.idle_tick();
43 }
44 panic!("{who}: simulator never settled");
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50 use oscyl_driver::{DriverError, SessionOp};
51
52 struct Stuck;
54 impl DriverSession for Stuck {
55 fn submit(&mut self, _: SessionOp) -> Result<(), DriverError> {
56 Ok(())
57 }
58 fn is_idle(&self) -> bool {
59 false
60 }
61 fn poll_transmit(&mut self, _: Instant) -> Option<Vec<u8>> {
62 None
63 }
64 fn next_deadline(&self) -> Option<Instant> {
65 None
66 }
67 fn handle_timeout(&mut self, _: Instant) {}
68 fn handle_input(&mut self, _: &[u8]) {}
69 fn poll_event(&mut self) -> Option<SessionEvent> {
70 None
71 }
72 }
73
74 struct Silent;
75 impl Simulator for Silent {
76 fn handle(&mut self, _: &[u8]) -> Vec<Vec<u8>> {
77 vec![]
78 }
79 fn idle_tick(&mut self) {}
80 fn program(&self, _: u16) -> Option<Vec<u8>> {
81 None
82 }
83 fn edit_buffer(&self) -> Vec<u8> {
84 vec![]
85 }
86 fn is_settled(&self) -> bool {
87 true
88 }
89 fn faults(&self) -> &'static [&'static str] {
90 &[]
91 }
92 fn inject(&mut self, _: &str) {}
93 }
94
95 #[test]
96 #[should_panic(
97 expected = "stuck-driver: session is busy but has nothing to send and no deadline"
98 )]
99 fn stuck_session_fails_instead_of_spinning() {
100 run_until_idle(&mut Stuck, &mut Silent, "stuck-driver");
101 }
102}