pub struct FakeDevice(/* private fields */);Expand description
The device side of an in-memory serial port; clones are handles to the same device.
port gives the host its side, a serialport::SerialPort. The device
records what the host does and decides what it sees: bytes, errors, held calls.
use serialplugin_testing::FakeDevice;
use std::io::{Read, Write};
let device = FakeDevice::new();
let mut port = device.port();
port.write_all(b"ping").unwrap();
assert_eq!(device.written(), b"ping");
device.send(b"pong");
let mut buf = [0; 4];
port.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"pong");Dropping the last FakeDevice ends hold_reads and
hold_writes, so a test that fails while it holds the host does not
hang on the way out.
Implementations§
Source§impl FakeDevice
impl FakeDevice
Sourcepub fn with_modem(modem: Modem) -> Self
pub fn with_modem(modem: Modem) -> Self
A device where modem answers what the host writes.
Sourcepub fn port(&self) -> FakePort ⓘ
pub fn port(&self) -> FakePort ⓘ
The host’s port (timeout 1 s). try_clone works, as on a real desktop port.
Sourcepub fn unclonable_port(&self) -> FakePort ⓘ
pub fn unclonable_port(&self) -> FakePort ⓘ
Like port, but try_clone fails: the host has one handle for reads
and writes.
Sourcepub fn wait_read(&self)
pub fn wait_read(&self)
Wait until the host read everything sent. It may still be handling it: see
wait_handled.
Sourcepub fn wait_handled(&self)
pub fn wait_handled(&self)
Wait until the host read everything sent and asks for more: a host that reads and handles the bytes on one thread has handled them by then.
Sourcepub fn wait_asked(&self, size: usize)
pub fn wait_asked(&self, size: usize)
Wait until a read of the host asks for size bytes.
Sourcepub fn wait_written(&self, what: &str, done: impl Fn(&[u8]) -> bool) -> Vec<u8> ⓘ
pub fn wait_written(&self, what: &str, done: impl Fn(&[u8]) -> bool) -> Vec<u8> ⓘ
Wait until what the host wrote satisfies done; returns it.
Sourcepub fn write_timeouts(&self) -> Vec<Duration>
pub fn write_timeouts(&self) -> Vec<Duration>
The port timeout at each write.
Sourcepub fn respond(
&self,
reply: impl FnMut(&FakeDevice, &[u8]) -> Vec<u8> + Send + 'static,
)
pub fn respond( &self, reply: impl FnMut(&FakeDevice, &[u8]) -> Vec<u8> + Send + 'static, )
Answer every write: reply gets the device and the written bytes and returns what
the device sends. Writes are answered one at a time, in order; one made while reply
runs (by another handle, or by reply itself) is answered after it.
Use the device it is given: a FakeDevice captured by reply is never dropped.
Sourcepub fn clock(&self, clock: Arc<dyn Clock>)
pub fn clock(&self, clock: Arc<dyn Clock>)
The clock of the device: Faults::delay, send_after and the
port timeout of every read that starts afterwards run on it. A read that waits for data ends when this clock
reaches its timeout (a ManualClock that is advanced, a paused
tokio clock that skips ahead). A clock that does not get there within WAIT of real
time fails the read with ErrorKind::Other: that bounds a failing test. A paused tokio
clock does not skip ahead while a spawn_blocking task runs: read on a thread of your
own. Without a clock: real time, as a port has.
Answers still waiting for their delay on the clock before are handed to the host now: their clock is gone, and the answers after them would wait for them for ever.
Sourcepub fn faults(&self, faults: Faults)
pub fn faults(&self, faults: Faults)
Faults from now on; Faults::default ends them.
Sourcepub fn send_after(&self, after: Duration, bytes: &[u8])
pub fn send_after(&self, after: Duration, bytes: &[u8])
Send bytes to the host once after has passed on the device’s clock.
Sourcepub fn subscribe_writes(&self) -> Receiver<Vec<u8>>
pub fn subscribe_writes(&self) -> Receiver<Vec<u8>>
The bytes of every write of the host that the device takes from now on, in order, one
message per write call (a failed or stalled write sends none; a held one, when it is
let go). No polling: recv wakes on the write.
Sourcepub fn subscribe_writes_async(&self) -> UnboundedReceiver<Vec<u8>>
pub fn subscribe_writes_async(&self) -> UnboundedReceiver<Vec<u8>>
subscribe_writes for async tests (feature tokio).
Sourcepub fn fail_reads(&self, kind: ErrorKind)
pub fn fail_reads(&self, kind: ErrorKind)
Every read fails with kind from now on (an unplugged device).
Sourcepub fn fail_reads_times(&self, n: usize, kind: ErrorKind)
pub fn fail_reads_times(&self, n: usize, kind: ErrorKind)
The next n reads fail with kind, then the device works again.
Sourcepub fn fail_writes(&self, kind: ErrorKind)
pub fn fail_writes(&self, kind: ErrorKind)
Every write fails with kind from now on.
Sourcepub fn stall_writes(&self, on: bool)
pub fn stall_writes(&self, on: bool)
Writes time out: the device takes no bytes.
Sourcepub fn recover(&self)
pub fn recover(&self)
The device works again: undoes fail_reads,
fail_reads_times, fail_writes,
stall_writes, eof and
eof_times.
Sourcepub fn hold_writes(&self, on: bool)
pub fn hold_writes(&self, on: bool)
While on, writes block in the device. Ends when the last FakeDevice is dropped.
Sourcepub fn hold_reads(&self, on: bool)
pub fn hold_reads(&self, on: bool)
While on, reads block in the device, also one that waits for data already (its
timeout starts again when it is let go). Ends when the last FakeDevice is dropped.
Sourcepub fn set_driver_pending(&self, n: Option<u32>)
pub fn set_driver_pending(&self, n: Option<u32>)
What bytes_to_read reports; None is the number of unread bytes.
Sourcepub fn set_out_pending(&self, n: u32)
pub fn set_out_pending(&self, n: u32)
What bytes_to_write reports.
Sourcepub fn open_ports(&self) -> usize
pub fn open_ports(&self) -> usize
Open handles of the host, clones included; 0 once it closed the port.
Sourcepub fn wait_closed(&self)
pub fn wait_closed(&self)
Wait until the host dropped every handle.