Documentation
¶
Overview ¶
Package serialtest provides fakes for testing serial device handling without hardware.
FakePort carries data over a real pty pair, so the read/write path under test is the one a real tty takes. Line settings and control lines are recorded rather than applied, because a pty cannot express them: TIOCMGET on a pty returns ENOTTY, which is the whole reason moat speaks RFC2217 rather than relaying through a pty. Tests assert on the recorded values.
Index ¶
- func NewFailingEnumerator(err error) serialdev.Enumerator
- func NewFakeEnumerator(devs ...serialdev.Device) serialdev.Enumerator
- type FakePort
- func (p *FakePort) ApplySettings(s serialport.Settings) error
- func (p *FakePort) BreakCount() int
- func (p *FakePort) Close() error
- func (p *FakePort) FlushBuffers() error
- func (p *FakePort) LastModem() serialport.Modem
- func (p *FakePort) LastSettings() serialport.Settings
- func (p *FakePort) ModemSequence() []serialport.Modem
- func (p *FakePort) ModemStatus() (serialport.ModemStatus, error)
- func (p *FakePort) Name() string
- func (p *FakePort) Peer() io.ReadWriter
- func (p *FakePort) PurgeCount() int
- func (p *FakePort) Read(b []byte) (int, error)
- func (p *FakePort) SendBreak() error
- func (p *FakePort) SetApplySettingsError(err error)
- func (p *FakePort) SetFlushError(err error)
- func (p *FakePort) SetModem(m serialport.Modem) error
- func (p *FakePort) SetModemError(err error)
- func (p *FakePort) SetModemStatus(s serialport.ModemStatus)
- func (p *FakePort) SetModemStatusError(err error)
- func (p *FakePort) SetReadError(err error)
- func (p *FakePort) SettingsSequence() []serialport.Settings
- func (p *FakePort) Write(b []byte) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewFailingEnumerator ¶
func NewFailingEnumerator(err error) serialdev.Enumerator
NewFailingEnumerator returns an Enumerator that always fails, for testing how callers report an unreadable device list.
func NewFakeEnumerator ¶
func NewFakeEnumerator(devs ...serialdev.Device) serialdev.Enumerator
NewFakeEnumerator returns an Enumerator yielding a fixed device list.
Types ¶
type FakePort ¶
type FakePort struct {
// contains filtered or unexported fields
}
FakePort implements serialport.Port over a pty pair.
func NewFakePort ¶
NewFakePort returns a fake port and registers its cleanup.
func (*FakePort) ApplySettings ¶
func (p *FakePort) ApplySettings(s serialport.Settings) error
ApplySettings records the requested line settings. SetApplySettingsError, when set, is returned instead — for testing how callers handle a device that rejects a line change.
func (*FakePort) BreakCount ¶
BreakCount returns how many breaks were sent.
func (*FakePort) Close ¶
Close closes both ends. It is safe to call more than once, since both the test cleanup and the code under test may close the port.
func (*FakePort) FlushBuffers ¶
FlushBuffers records a purge. A pty has no kernel receive queue to flush that would matter to assertions, so recording is enough to prove the broker forwards PURGE_DATA to the port. FlushErr, when set, is returned instead — for testing how callers handle a device that cannot flush.
func (*FakePort) LastModem ¶
func (p *FakePort) LastModem() serialport.Modem
LastModem returns the most recently applied control lines.
func (*FakePort) LastSettings ¶
func (p *FakePort) LastSettings() serialport.Settings
LastSettings returns the most recently applied line settings.
func (*FakePort) ModemSequence ¶
func (p *FakePort) ModemSequence() []serialport.Modem
ModemSequence returns every control-line change in order.
The sequence matters, not just the final state: driving an ESP32 into its bootloader is a specific series of DTR/RTS transitions, so a relay that coalesced them would leave the final state correct and the board unflashed.
func (*FakePort) ModemStatus ¶
func (p *FakePort) ModemStatus() (serialport.ModemStatus, error)
ModemStatus reports the scripted input lines (Status field). A pty cannot express them, so the value is whatever the test set.
func (*FakePort) Peer ¶
func (p *FakePort) Peer() io.ReadWriter
Peer is the far end of the wire — what a device would read and write.
func (*FakePort) PurgeCount ¶
PurgeCount returns how many buffer flushes were requested.
func (*FakePort) SetApplySettingsError ¶
SetApplySettingsError makes subsequent ApplySettings calls fail, or clears it with nil.
func (*FakePort) SetFlushError ¶
SetFlushError makes subsequent FlushBuffers calls fail, or clears it with nil.
func (*FakePort) SetModem ¶
func (p *FakePort) SetModem(m serialport.Modem) error
SetModem records the requested control lines. SetModemError, when set, is returned instead — for testing how callers handle a device that cannot drive its control lines.
func (*FakePort) SetModemError ¶
SetModemError makes subsequent SetModem calls fail, or clears it with nil.
func (*FakePort) SetModemStatus ¶
func (p *FakePort) SetModemStatus(s serialport.ModemStatus)
SetModemStatus scripts what ModemStatus reports.
func (*FakePort) SetModemStatusError ¶
SetModemStatusError makes subsequent ModemStatus calls fail, or clears it with nil — for testing how callers handle a device that cannot report its input lines.
func (*FakePort) SetReadError ¶
SetReadError makes subsequent Read calls fail, or clears it with nil — for testing how callers handle a device that vanishes mid-session (EIO on unplug, ENXIO on disconnect).
func (*FakePort) SettingsSequence ¶
func (p *FakePort) SettingsSequence() []serialport.Settings
SettingsSequence returns every line-settings change in order.