serialplugin-testing
Fake serial devices for tests: no hardware, no timing. Works with anything that takes a
serialport::SerialPort, with
serialplugin-core and with
tauri-plugin-serialplugin.
FakeDevice: an in-memory serial port. Script what the host reads, see what it wrote and set, fail or hold its calls, unplug it.Modem: an AT modem. Your replies, echo, numeric result codes, the SMS prompt, a 27.010 multiplexer with the faults real modems have.mux: 27.010 frames for hand-written multiplexer tests.Faults,Clock: answers dropped, late, followed by a burst or cut into small reads; time that moves when the test says (ManualClock) or withtokio::time(TokioClock, featuretokio); an event per write (subscribe_writes).peer: the device side of a TCP serial bridge or (unix) a PTY, for tests that need a real file descriptor.
[]
= "0.2" # features = ["tokio"] for TokioClock
A port without hardware
use ;
use ;
// The code under test: anything that uses a serial port.
let device = new;
device.respond;
let mut port = device.port;
assert_eq!;
assert_eq!;
// The device is unplugged.
device.fail_reads;
assert_eq!;
// And plugged in again.
device.recover;
assert_eq!;
An AT modem
use ;
use ;
let modem = new
.reply
.fails
.silent;
let log = modem.log;
let device = with_modem;
let mut port = device.port;
port.write_all.unwrap;
let mut reply = ;
let n = port.read.unwrap;
assert_eq!;
assert_eq!;
// An unsolicited line.
device.send;
With serialplugin-core
use SerialManager;
use ;
let device = with_modem;
let serial = new;
serial.insert_port.unwrap;
let reply = serial.at.unwrap;
assert_eq!;
serial.close.unwrap;
device.wait_closed;
With the Tauri plugin (through IPC, as a webview calls it) and with the multiplexer: the testing guide.
Faults and time
Faults act on what the device answers and on how the host reads it; a Clock runs the
delays and the port timeout. ManualClock moves only on advance, TokioClock (feature
tokio) follows tokio::time, so a test with a paused clock waits for nothing.
use ;
use Write;
use Arc;
use Duration;
let device = with_modem;
let clock = new;
device.clock;
device.faults;
let writes = device.subscribe_writes; // one message per write, no polling
let mut port = device.port;
port.write_all.unwrap;
assert_eq!;
assert_eq!;
clock.advance;
assert_eq!;
Faults::default().silent(true) drops the answers (the modem still sees the commands);
device.send_after(d, bytes) sends on the clock. With a clock set, a read that waits for data
times out when the clock reaches the port timeout. A paused tokio clock does not skip ahead
while a spawn_blocking task runs: read on a thread of your own, as a port reader does.
No timing
No test depends on a delay: delays run on a test clock. Waits end on what they wait for (wait_written, wait_read,
wait_handled, wait_held, wait_closed, ModemLog::wait_command); WAIT (10 s) only
limits how long a failure takes. eventually and the peers, whose state no event announces,
poll (every millisecond, every 20 ms). To test code that must not block, hold the device
(hold_reads, hold_writes) instead of measuring time; a hold lasts until the test lets go
or drops its last FakeDevice, so a failing test does not hang.
wait_read means that the host took the bytes, not that it handled them. wait_handled does,
for a host with one reading thread: it asked for more.
Desktop only (Linux, macOS, Windows).
License
Apache-2.0 OR MIT.