Skip to content

Commit

Permalink
feat(tests): For atemsocket class (child process wrapper)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Dec 7, 2019
1 parent c0a2471 commit caa9ac5
Show file tree
Hide file tree
Showing 11 changed files with 742 additions and 227 deletions.
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ module.exports = {
coverageDirectory: "./coverage/",
collectCoverage: true,
collectCoverageFrom: [
"**/src/**/**",
"!**/src/@types/**",
"src/**/*.{js,ts}",
"!**/@types/**",
"!**/__tests__/**",
"!**/__mocks__/**",
'!**/node_modules/**',
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@
"video"
],
"dependencies": {
"emittery": "^0.5.1",
"exit-hook": "^2.0.0",
"nanotimer": "^0.3.15",
"threadedclass": "^0.6.8",
"tslib": "^1.9.0",
"tslib": "^1.10.0",
"wavefile": "^8.4.4"
},
"standard-version": {
Expand Down
70 changes: 12 additions & 58 deletions src/__tests__/atem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ describe('Atem', () => {
const socket = (conn as any).socket as AtemSocket
expect(socket).toBeTruthy()

let nextTrackId = 123
let nextId = 123
Object.defineProperty(socket, 'nextCommandTrackingId', {
get: jest.fn(() => nextTrackId++),
get: jest.fn(() => nextId++),
set: jest.fn()
})
expect(socket.nextCommandTrackingId).toEqual(123)

socket._sendCommand = jest.fn(() => Promise.resolve(35) as any)
socket.sendCommand = jest.fn(() => Promise.resolve(35) as any)

const sentQueue = (conn as any)._sentQueue as object
expect(Object.keys(sentQueue)).toHaveLength(0)
Expand All @@ -119,61 +119,15 @@ describe('Atem', () => {
await setImmediatePromise()
expect(Object.keys(sentQueue)).toHaveLength(1)

expect(socket._sendCommand).toHaveBeenCalledTimes(1)
expect(socket._sendCommand).toHaveBeenCalledWith(cmd, 124)
expect(socket.sendCommand).toHaveBeenCalledTimes(1)
expect(socket.sendCommand).toHaveBeenCalledWith(cmd, 124)

// Trigger the ack, and it should switfy resolve
socket.emit(IPCMessageType.CommandAcknowledged, { trackingId: 124 })
socket.emit(IPCMessageType.CommandAcknowledged, 124)
expect(Object.keys(sentQueue)).toHaveLength(0)

// Finally, it should now resolve without a timeout
expect(await res).toEqual(cmd)
} finally {
cleanupAtem(conn)
}
}, 500)

test('sendCommand - timeout', async () => {
(AtemSocket as any).mockImplementation(() => new EventEmitter())
const conn = new Atem({ debug: true, address: 'test1', port: 23 })

try {
const socket = (conn as any).socket as AtemSocket
expect(socket).toBeTruthy()

let nextTrackId = 123
Object.defineProperty(socket, 'nextCommandTrackingId', {
get: jest.fn(() => nextTrackId++),
set: jest.fn()
})
expect(socket.nextCommandTrackingId).toEqual(123)

socket._sendCommand = jest.fn(() => Promise.resolve(35) as any)

const sentQueue = (conn as any)._sentQueue as object
expect(Object.keys(sentQueue)).toHaveLength(0)

const cmd = new CutCommand(0)
const res = conn.sendCommand(cmd)
await setImmediatePromise()
expect(Object.keys(sentQueue)).toHaveLength(1)

expect(socket._sendCommand).toHaveBeenCalledTimes(1)
expect(socket._sendCommand).toHaveBeenCalledWith(cmd, 124)

// Trigger the timeout, and it should switfy resolve
socket.emit(IPCMessageType.CommandTimeout, { trackingId: 124 })
expect(Object.keys(sentQueue)).toHaveLength(0)

// Finally, it should now resolve without a timeout
try {
await res
// Should not get here
expect(false).toBeTruthy()
} catch (e) {
expect(e).toEqual(cmd)
}
// expect(await res).toEqual(cmd)
expect(await res).toBeUndefined()
} finally {
cleanupAtem(conn)
}
Expand All @@ -187,14 +141,14 @@ describe('Atem', () => {
const socket = (conn as any).socket as AtemSocket
expect(socket).toBeTruthy()

let nextTrackId = 123
let nextId = 123
Object.defineProperty(socket, 'nextCommandTrackingId', {
get: jest.fn(() => nextTrackId++),
get: jest.fn(() => nextId++),
set: jest.fn()
})
expect(socket.nextCommandTrackingId).toEqual(123)

socket._sendCommand = jest.fn(() => Promise.reject(35) as any)
socket.sendCommand = jest.fn(() => Promise.reject(35) as any)

const sentQueue = (conn as any)._sentQueue as object
expect(Object.keys(sentQueue)).toHaveLength(0)
Expand All @@ -203,8 +157,8 @@ describe('Atem', () => {
const res = conn.sendCommand(cmd)

// Send command should be called
expect(socket._sendCommand).toHaveBeenCalledTimes(1)
expect(socket._sendCommand).toHaveBeenCalledWith(cmd, 124)
expect(socket.sendCommand).toHaveBeenCalledTimes(1)
expect(socket.sendCommand).toHaveBeenCalledWith(cmd, 124)

expect(Object.keys(sentQueue)).toHaveLength(0)

Expand Down
2 changes: 1 addition & 1 deletion src/atem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class Atem extends EventEmitter {
public sendCommand (command: ISerializableCommand): Promise<ISerializableCommand> {
const commandTrackingId = this.socket.nextCommandTrackingId
return new Promise((resolve, reject) => {
this.socket._sendCommand(command, commandTrackingId).then(() => {
this.socket.sendCommand(command, commandTrackingId).then(() => {
this._sentQueue[commandTrackingId] = {
command,
resolve,
Expand Down
5 changes: 4 additions & 1 deletion src/lib/__mocks__/dgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EventEmitter } from 'events'
import { SocketType, RemoteInfo } from 'dgram'
import 'jest-extended'
import { DEFAULT_PORT } from '../../atem'
import { InstalledClock } from 'lolex'

export class Socket extends EventEmitter {
public isOpen: boolean = false
Expand All @@ -15,7 +16,7 @@ export class Socket extends EventEmitter {

public sendImpl?: (msg: Buffer) => void

public emitMessage (msg: Buffer) {
public async emitMessage (clock: InstalledClock, msg: Buffer) {
expect(Buffer.isBuffer(msg)).toBeTruthy()

const rinfo: RemoteInfo = {
Expand All @@ -25,6 +26,8 @@ export class Socket extends EventEmitter {
size: msg.length
}
this.emit('message', msg, rinfo)

await clock.tickAsync(0)
}

public bind (port?: number, address?: string, callback?: () => void): void {
Expand Down
Loading

0 comments on commit caa9ac5

Please sign in to comment.