Skip to content

Commit

Permalink
Merge branch 'master' into feat/mosObj
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/connector/src/MosDevice.ts
  • Loading branch information
nytamin committed Jan 17, 2024
2 parents 786710a + 0368fe8 commit 5137001
Show file tree
Hide file tree
Showing 33 changed files with 1,046 additions and 398 deletions.
8 changes: 4 additions & 4 deletions packages/connector/src/MosConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
primary.on('warning', (str: string) => {
this.emit('warning', 'primary: ' + str)
})
primary.on('error', (str: string) => {
this.emit('error', 'primary: ' + str)
primary.on('error', (error: Error) => {
this.emit('error', 'primary: ' + error + ' ' + (typeof error === 'object' ? error.stack : ''))
})
primary.on('info', (str: string) => {
this.emit('info', 'primary: ' + str)
Expand Down Expand Up @@ -137,8 +137,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
secondary.on('warning', (str: string) => {
this.emit('warning', 'secondary: ' + str)
})
secondary.on('error', (str: string) => {
this.emit('error', 'secondary: ' + str)
secondary.on('error', (error: Error) => {
this.emit('error', 'secondary: ' + error + ' ' + (typeof error === 'object' ? error.stack : ''))
})
secondary.on('info', (str: string) => {
this.emit('info', 'secondary: ' + str)
Expand Down
196 changes: 114 additions & 82 deletions packages/connector/src/MosDevice.ts

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion packages/connector/src/__mocks__/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export class SocketMock extends EventEmitter implements Socket {
<ncsID>${ncsID}</ncsID>
<messageID>${messageId}</messageID>\
<heartbeat>
<time>${mosTypes.mosTime.stringify(mosTypes.mosTime.create(undefined))}</time>
<time>${mosTypes.mosTime.stringify(mosTypes.mosTime.create(Date.now()))}</time>
</heartbeat>
</mos>\r\n`
this.mockReceiveMessage(this.encode(repl))
Expand Down Expand Up @@ -285,6 +285,9 @@ export class SocketMock extends EventEmitter implements Socket {
setAutoReplyToHeartBeat(autoReplyToHeartBeat: boolean): void {
this._autoReplyToHeartBeat = autoReplyToHeartBeat
}
get autoReplyToHeartBeat(): boolean {
return this._autoReplyToHeartBeat
}
}

type SimpleTypes = string | string[] | false | Buffer | Buffer[]
Expand Down
28 changes: 26 additions & 2 deletions packages/connector/src/__mocks__/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,30 @@ const xmlApiData = {
profile1: true,
},
}),
machineInfoReply: literal<IMOSListMachInfo>({
manufacturer: mosTypes.mosString128.create('RadioVision, Ltd.'),
model: mosTypes.mosString128.create('TCS6000'),
hwRev: mosTypes.mosString128.create(''),
swRev: mosTypes.mosString128.create('2.1.0.37'),
DOM: mosTypes.mosString128.create(''),
SN: mosTypes.mosString128.create('927748927'),
ID: mosTypes.mosString128.create('airchache.newscenter.com'),
time: mosTypes.mosTime.create('2009-04-11T17:20:42'),
opTime: mosTypes.mosTime.create('2009-03-01T23:55:10'),
mosRev: mosTypes.mosString128.create('2.8.2'),

supportedProfiles: {
deviceType: 'NCS',
profile0: true,
profile1: true,
profile2: true,
profile3: true,
profile4: true,
profile5: true,
profile6: true,
profile7: true,
},
}),
mosObj: literal<IMOSObject>({
ID: mosTypes.mosString128.create('M000123'),
Slug: mosTypes.mosString128.create('My new object'),
Expand Down Expand Up @@ -1085,7 +1109,7 @@ const xmlApiData = {
literal<IMOSROStory>({
ID: mosTypes.mosString128.create('17'),
Items: [],
Slug: mosTypes.mosString128.create(undefined),
Slug: undefined,
}),
],
roElementAction_insert_item_Action: literal<IMOSItemAction>({
Expand Down Expand Up @@ -1174,7 +1198,7 @@ const xmlApiData = {
literal<IMOSROStory>({
ID: mosTypes.mosString128.create('17'),
Items: [],
Slug: mosTypes.mosString128.create(undefined),
Slug: undefined,
// MosExternalMetaData?: Array<IMOSExternalMetaData>,
}),
],
Expand Down
14 changes: 7 additions & 7 deletions packages/connector/src/__tests__/MessageChunking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ describe('message chunking', () => {
const chunks = [message.message.slice(0, 100), message.message.slice(100)]

// Send first part of the message:
await sendFakeIncomingMessage(serverSocketMockLower, chunks[0])
sendFakeIncomingMessage(serverSocketMockLower, chunks[0])
expect(onRequestMOSObject).toHaveBeenCalledTimes(0)

// Send rest of the message:
await sendFakeIncomingMessage(serverSocketMockLower, chunks[1])
sendFakeIncomingMessage(serverSocketMockLower, chunks[1])
expect(onRequestMOSObject).toHaveBeenCalledTimes(1)
})

Expand Down Expand Up @@ -281,13 +281,13 @@ describe('message chunking', () => {
]

// Send the parts of the message:
await sendFakeIncomingMessage(serverSocketMockLower, chunks[0])
sendFakeIncomingMessage(serverSocketMockLower, chunks[0])
expect(onRequestMOSObject).toHaveBeenCalledTimes(0)
await sendFakeIncomingMessage(serverSocketMockLower, chunks[1])
sendFakeIncomingMessage(serverSocketMockLower, chunks[1])
expect(onRequestMOSObject).toHaveBeenCalledTimes(0)
await sendFakeIncomingMessage(serverSocketMockLower, chunks[2])
sendFakeIncomingMessage(serverSocketMockLower, chunks[2])
expect(onRequestMOSObject).toHaveBeenCalledTimes(0)
await sendFakeIncomingMessage(serverSocketMockLower, chunks[2])
sendFakeIncomingMessage(serverSocketMockLower, chunks[2])
expect(onRequestMOSObject).toHaveBeenCalledTimes(1)
})
test('multiple messages', async () => {
Expand All @@ -308,7 +308,7 @@ describe('message chunking', () => {
)

// Send both messages right away:
await sendFakeIncomingMessage(serverSocketMockLower, message0.message + message1.message)
sendFakeIncomingMessage(serverSocketMockLower, message0.message + message1.message)
expect(onRequestMOSObject).toHaveBeenCalledTimes(2)
})
})
62 changes: 62 additions & 0 deletions packages/connector/src/__tests__/MosConnection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ describe('MosDevice: General', () => {
})
)

const onError = jest.fn((e) => console.log(e))
const onWarning = jest.fn((e) => console.log(e))
mos.on('error', onError)
mos.on('warning', onWarning)

expect(mos.profiles).toMatchObject({
'0': true,
'1': true,
Expand All @@ -83,6 +88,9 @@ describe('MosDevice: General', () => {
'7': false,
})

expect(onError).toHaveBeenCalledTimes(0)
expect(onWarning).toHaveBeenCalledTimes(0)

await mos.dispose()
})
test('Incoming connections', async () => {
Expand All @@ -94,11 +102,18 @@ describe('MosDevice: General', () => {
'1': true,
},
})
const onError = jest.fn((e) => console.log(e))
const onWarning = jest.fn((e) => console.log(e))
mos.on('error', onError)
mos.on('warning', onWarning)
expect(mos.acceptsConnections).toBe(true)
await initMosConnection(mos)
expect(mos.isListening).toBe(true)
expect(SocketMock.instances).toHaveLength(0)

expect(onError).toHaveBeenCalledTimes(0)
expect(onWarning).toHaveBeenCalledTimes(0)

// close sockets after test
await mos.dispose()
})
Expand All @@ -111,6 +126,11 @@ describe('MosDevice: General', () => {
'1': true,
},
})
const onError = jest.fn((e) => console.log(e))
const onWarning = jest.fn((e) => console.log(e))
mos.on('error', onError)
mos.on('warning', onWarning)

expect(mos.acceptsConnections).toBe(true)
await initMosConnection(mos)
expect(mos.isListening).toBe(true)
Expand Down Expand Up @@ -146,6 +166,9 @@ describe('MosDevice: General', () => {
expect(SocketMock.instances[2].destroy).toHaveBeenCalledTimes(1)
expect(SocketMock.instances[3].destroy).toHaveBeenCalledTimes(1)

expect(onError).toHaveBeenCalledTimes(0)
expect(onWarning).toHaveBeenCalledTimes(0)

expect(mosDevice.hasConnection).toEqual(false)
})
test('MosDevice secondary', async () => {
Expand All @@ -157,6 +180,11 @@ describe('MosDevice: General', () => {
'1': true,
},
})
const onError = jest.fn((e) => console.log(e))
const onWarning = jest.fn((e) => console.log(e))
mos.on('error', onError)
mos.on('warning', onWarning)

expect(mos.acceptsConnections).toBe(true)
await initMosConnection(mos)
expect(mos.isListening).toBe(true)
Expand Down Expand Up @@ -236,6 +264,9 @@ describe('MosDevice: General', () => {
returnedObj = await mosDevice.sendRequestMOSObject(xmlApiData.mosObj.ID)
expect(returnedObj).toBeTruthy()

expect(onError).toHaveBeenCalledTimes(0)
expect(onWarning).toHaveBeenCalledTimes(0)

await mos.dispose()
})
test('init and connectionStatusChanged', async () => {
Expand All @@ -252,6 +283,11 @@ describe('MosDevice: General', () => {
},
})
)
const onError = jest.fn((e) => console.log(e))
const onWarning = jest.fn((e) => console.log(e))
mosConnection.on('error', onError)
mosConnection.on('warning', onWarning)

await initMosConnection(mosConnection)

const mosDevice = await mosConnection.connect({
Expand Down Expand Up @@ -299,6 +335,10 @@ describe('MosDevice: General', () => {
// mock cause timeout

mosConnection.checkProfileValidness()

expect(onError).toHaveBeenCalledTimes(0)
expect(onWarning).toHaveBeenCalledTimes(0)

await mosConnection.dispose()
})
test('buddy failover', async () => {
Expand All @@ -310,6 +350,14 @@ describe('MosDevice: General', () => {
'1': true,
},
})
const onError = jest.fn((e) => console.log(e))
const onWarning = jest.fn((e) => console.log(e))
mos.on('error', (e) => {
// filter out heartbeat errors:
if (!(e + '').match(/heartbeat.*timed out/i)) onError(e)
})
mos.on('warning', onWarning)

await initMosConnection(mos)
const mosDevice: MosDevice = await mos.connect({
primary: {
Expand Down Expand Up @@ -382,6 +430,9 @@ describe('MosDevice: General', () => {
expect(connMocks[1].destroy).toHaveBeenCalledTimes(1)
expect(connMocks[2].destroy).toHaveBeenCalledTimes(1)

expect(onError).toHaveBeenCalledTimes(0)
expect(onWarning).toHaveBeenCalledTimes(0)

await mos.dispose()
})
test('buddy failover - primary starts offline', async () => {
Expand All @@ -393,6 +444,14 @@ describe('MosDevice: General', () => {
'1': true,
},
})
const onError = jest.fn((e) => console.log(e))
const onWarning = jest.fn((e) => console.log(e))
mos.on('error', (e) => {
// filter out heartbeat errors:
if (!(e + '').match(/heartbeat.*timed out/i)) onError(e)
})
mos.on('warning', onWarning)

await initMosConnection(mos)
const mosDevice: MosDevice = await mos.connect({
primary: {
Expand Down Expand Up @@ -471,6 +530,9 @@ describe('MosDevice: General', () => {
expect(connMocks[1].destroy).toHaveBeenCalledTimes(1)
expect(connMocks[2].destroy).toHaveBeenCalledTimes(1)

expect(onError).toHaveBeenCalledTimes(0)
expect(onWarning).toHaveBeenCalledTimes(0)

await mos.dispose()
})
})
Loading

0 comments on commit 5137001

Please sign in to comment.