Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding MockPortBinding.writeToPort & MockBinding.getOpenMockPort() #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion lib/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const openOptions: OpenOptions = {
}

describe('MockBinding', () => {
afterEach(() => {
beforeEach(() => {
MockBinding.reset()
})

Expand Down Expand Up @@ -88,5 +88,51 @@ describe('MockBinding', () => {
assert.strictEqual(port.port.info.serialNumber, '1')
})
})
describe('getOpenMockPort', () => {
beforeEach(async () => {
MockBinding.reset()
})

it('should return a value for an existing open port', async () => {
MockBinding.createPort('/dev/exists')
await MockBinding.open(openOptions)
const openPort = MockBinding.getOpenMockPort('/dev/exists')
assert.strictEqual(openPort.port.info.serialNumber, '1')
})

it('should return undefined for an unknown port path', async () => {
const openPort = MockBinding.getOpenMockPort('/dev/unknown')
assert.strictEqual(openPort, undefined)
})

it('should return undefined for an existing port that is not open', async () => {
MockBinding.createPort('/dev/exists')
const openPort = MockBinding.getOpenMockPort('/dev/exists')
assert.strictEqual(openPort, undefined)
})
})
})
})

describe('MockPortBinding', () => {
beforeEach(() => {
MockBinding.reset()
})

describe('instance property', () => {
describe('writeToPort', () => {
it('can send data from one port to another', async () => {
MockBinding.createPort('/dev/exists')
const port1 = await MockBinding.open(openOptions)
const port2 = await MockBinding.open(openOptions)
port1.writeToPort = port2

const message = Buffer.from('MSG')
await port1.write(message)
const receivingBuffer = Buffer.alloc(message.length)
await port2.read(receivingBuffer, 0, message.length)
assert.isTrue(receivingBuffer.equals(message))
})
})
})
})
20 changes: 19 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export interface CreatePortOptions {
let ports: {
[key: string]: MockPortInternal
} = {}
let openMockPortBindings: {
[key: string]: MockPortBinding
} = {};
let serialNumber = 0

function resolveNextTick() {
Expand All @@ -42,11 +45,13 @@ export class CanceledError extends Error {
export interface MockBindingInterface extends BindingInterface<MockPortBinding> {
reset(): void
createPort(path: string, opt?: CreatePortOptions): void
getOpenMockPort(path: string): MockPortBinding
}

export const MockBinding: MockBindingInterface = {
reset() {
ports = {}
openMockPortBindings = {}
serialNumber = 0
},

Expand Down Expand Up @@ -82,6 +87,10 @@ export const MockBinding: MockBindingInterface = {
debug(serialNumber, 'created port', JSON.stringify({ path, opt: options }))
},

getOpenMockPort(path) {
return openMockPortBindings[path];
},

async list() {
debug(null, 'list')
return Object.values(ports).map(port => port.info)
Expand Down Expand Up @@ -133,7 +142,9 @@ export const MockBinding: MockBindingInterface = {

port.openOpt = { ...openOptions }

return new MockPortBinding(port, openOptions)
const portBinding = new MockPortBinding(port, openOptions)
openMockPortBindings[path] = portBinding
return portBinding
},
}

Expand All @@ -149,6 +160,7 @@ export class MockPortBinding implements BindingPortInterface {
writeOperation: null | Promise<void>
isOpen: boolean
serialNumber?: string
writeToPort?: MockPortBinding

constructor(port: MockPortInternal, openOptions: Required<OpenOptions>) {
this.port = port
Expand Down Expand Up @@ -289,6 +301,12 @@ export class MockPortBinding implements BindingPortInterface {
this.emitData(data)
}
})
} else if (this.writeToPort) {
process.nextTick(() => {
if (this.writeToPort.isOpen) {
this.writeToPort.emitData(data);
}
});
}
this.writeOperation = null
debug(this.serialNumber, 'writing finished')
Expand Down