Skip to main content

FakeDevice

Struct FakeDevice 

Source
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

Source

pub fn new() -> Self

A silent device at 115200 8N1.

Source

pub fn with_modem(modem: Modem) -> Self

A device where modem answers what the host writes.

Source

pub fn port(&self) -> FakePort ⓘ

The host’s port (timeout 1 s). try_clone works, as on a real desktop port.

Source

pub fn unclonable_port(&self) -> FakePort ⓘ

Like port, but try_clone fails: the host has one handle for reads and writes.

Source

pub fn send(&self, bytes: &[u8])

Send bytes to the host.

Source

pub fn unread(&self) -> usize

Bytes sent that the host has not read yet.

Source

pub fn wait_read(&self)

Wait until the host read everything sent. It may still be handling it: see wait_handled.

Source

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.

Source

pub fn wait_asked(&self, size: usize)

Wait until a read of the host asks for size bytes.

Source

pub fn written(&self) -> Vec<u8> ⓘ

Everything the host wrote so far.

Source

pub fn wait_written(&self, what: &str, done: impl Fn(&[u8]) -> bool) -> Vec<u8> ⓘ

Wait until what the host wrote satisfies done; returns it.

Source

pub fn write_timeouts(&self) -> Vec<Duration>

The port timeout at each write.

Source

pub fn calls(&self) -> Vec<Call>

Setting and control calls so far.

Source

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.

Source

pub fn modem(&self, modem: Modem)

Let modem answer what the host writes.

Source

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.

Source

pub fn faults(&self, faults: Faults)

Faults from now on; Faults::default ends them.

Source

pub fn send_after(&self, after: Duration, bytes: &[u8])

Send bytes to the host once after has passed on the device’s clock.

Source

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.

Source

pub fn subscribe_writes_async(&self) -> UnboundedReceiver<Vec<u8>>

subscribe_writes for async tests (feature tokio).

Source

pub fn fail_reads(&self, kind: ErrorKind)

Every read fails with kind from now on (an unplugged device).

Source

pub fn fail_reads_times(&self, n: usize, kind: ErrorKind)

The next n reads fail with kind, then the device works again.

Source

pub fn fail_writes(&self, kind: ErrorKind)

Every write fails with kind from now on.

Source

pub fn stall_writes(&self, on: bool)

Writes time out: the device takes no bytes.

Source

pub fn eof(&self)

Every read returns Ok(0) from now on (a tty after hang-up).

Source

pub fn recover(&self)

The device works again: undoes fail_reads, fail_reads_times, fail_writes, stall_writes, eof and eof_times.

Source

pub fn eof_times(&self, n: usize)

The next n reads return Ok(0).

Source

pub fn eof_reads(&self) -> usize

Reads that returned Ok(0) so far.

Source

pub fn hold_writes(&self, on: bool)

While on, writes block in the device. Ends when the last FakeDevice is dropped.

Source

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.

Source

pub fn held(&self) -> usize

Calls blocked in the device right now.

Source

pub fn wait_held(&self, n: usize)

Wait until n calls are blocked in the device.

Source

pub fn set_lines(&self, lines: Lines)

Set the status lines.

Source

pub fn set_driver_pending(&self, n: Option<u32>)

What bytes_to_read reports; None is the number of unread bytes.

Source

pub fn set_out_pending(&self, n: u32)

What bytes_to_write reports.

Source

pub fn open_ports(&self) -> usize

Open handles of the host, clones included; 0 once it closed the port.

Source

pub fn wait_closed(&self)

Wait until the host dropped every handle.

Trait Implementations§

Source§

impl Clone for FakeDevice

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FakeDevice

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FakeDevice

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for FakeDevice

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.