Skip to content

feat: add MockBinding.connect virtual loop cable#56

Open
JSap0914 wants to merge 1 commit into
serialport:mainfrom
JSap0914:feat/mockbinding-loop-cable
Open

feat: add MockBinding.connect virtual loop cable#56
JSap0914 wants to merge 1 commit into
serialport:mainfrom
JSap0914:feat/mockbinding-loop-cable

Conversation

@JSap0914

Copy link
Copy Markdown

Implements the "virtual loop cable" feature requested in serialport/node-serialport#1580 (good first issue).

What

Adds a MockBinding.connect(path1, path2) static method that wires two mock ports together with a virtual loop cable, like a physical null-modem/loopback cable between two serial ports. Data written to one connected port is delivered to the other port's read buffer (and vice versa), instead of being echoed back to the writer.

MockBinding.createPort('/dev/portA')
MockBinding.createPort('/dev/portB')
MockBinding.connect('/dev/portA', '/dev/portB')

const a = await MockBinding.open({ path: '/dev/portA', baudRate: 9600 })
const b = await MockBinding.open({ path: '/dev/portB', baudRate: 9600 })

await a.write(Buffer.from('hello'))
// `b` can now read "hello"

Behavior

  • Bidirectional: writes on either end are readable on the other.
  • Wakes a read that is already pending on the peer.
  • No self-echo: the writer does not receive its own data (distinct from the existing echo option).
  • Data written while the peer is closed is dropped (nothing is buffered for a disconnected receiver).
  • connect throws if either port has not been created first.

Tests

Adds regression tests for delivery, bidirectionality, waking a pending peer read, dropped writes when the peer is closed, and both-ports-exist validation. Full suite (npm test) passes (12 passing); npm run lint and npm run build are clean.

Docs

README updated with a "Virtual loop cable" section.

Closes serialport/node-serialport#1580

Add a `MockBinding.connect(path1, path2)` static method that wires two
mock ports together with a virtual loop cable, like a physical
null-modem/loopback cable between two serial ports. Data written to one
connected port is delivered to the other port's read buffer (and vice
versa) and wakes any pending read on the peer. Data written while the
peer is closed is dropped.

Includes regression tests covering delivery, bidirectionality, waking a
pending peer read, dropped writes when the peer is closed, and validation
that both ports exist, plus README documentation.

Closes serialport/node-serialport#1580

Signed-off-by: JSup <hanjisang0914@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] virtual loop cable for testing

1 participant