Skip to content

Commit

Permalink
chore: refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Oct 3, 2023
1 parent d33c81e commit 82a398d
Showing 1 changed file with 37 additions and 80 deletions.
117 changes: 37 additions & 80 deletions src/__tests__/connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ describe('connection', () => {
expect(sockets).toHaveLength(0)
})

it('receive whole response', async () => {
async function runWithConnection(
fn: (
connection: Connection,
socket: MockSocket,
onConnError: jest.Mock,
onConnData: jest.Mock
) => Promise<void>
) {
const conn = new Connection('127.0.0.1', 5250, true)
try {
expect(conn).toBeTruthy()
Expand All @@ -98,6 +105,15 @@ describe('connection', () => {
const sockets = SocketMock.openSockets()
expect(sockets).toHaveLength(1)

await fn(conn, sockets[0], onConnError, onConnData)
} finally {
// Ensure cleaned up
conn.disconnect()
}
}

it('receive whole response', async () => {
await runWithConnection(async (conn, socket, onConnError, onConnData) => {
// Dispatch a command
const sendError = await conn.sendCommand({
command: Commands.Info,
Expand All @@ -112,7 +128,7 @@ describe('connection', () => {
expect(onSocketWrite).toHaveBeenLastCalledWith('INFO\r\n', 'utf-8')

// Reply with a single blob
sockets[0].mockData(
socket.mockData(
Buffer.from(
`201 INFO OK\r\n<?xml version="1.0" encoding="utf-8"?>\n<channel><test/></channel>\r\n\r\n`
)
Expand Down Expand Up @@ -142,25 +158,11 @@ describe('connection', () => {
},
undefined
)
} finally {
// Ensure cleaned up
conn.disconnect()
}
})
})

it('receive fragmented response', async () => {
const conn = new Connection('127.0.0.1', 5250, true)
try {
expect(conn).toBeTruthy()

const onConnError = jest.fn()
const onConnData = jest.fn()
conn.on('error', onConnError)
conn.on('data', onConnData)

const sockets = SocketMock.openSockets()
expect(sockets).toHaveLength(1)

await runWithConnection(async (conn, socket, onConnError, onConnData) => {
// Dispatch a command
const sendError = await conn.sendCommand({
command: Commands.Info,
Expand All @@ -175,8 +177,8 @@ describe('connection', () => {
expect(onSocketWrite).toHaveBeenLastCalledWith('INFO\r\n', 'utf-8')

// Reply with a fragmented message
sockets[0].mockData(Buffer.from(`201 INFO OK\r\n<?xml version="1.0" encoding="utf-8"?>\n<channel>`))
sockets[0].mockData(Buffer.from(`<test/></channel>\r\n\r\n`))
socket.mockData(Buffer.from(`201 INFO OK\r\n<?xml version="1.0" encoding="utf-8"?>\n<channel>`))
socket.mockData(Buffer.from(`<test/></channel>\r\n\r\n`))

// Wait for deserializer to run
await new Promise(setImmediate)
Expand All @@ -202,25 +204,11 @@ describe('connection', () => {
},
undefined
)
} finally {
// Ensure cleaned up
conn.disconnect()
}
})
})

it('receive fast responses', async () => {
const conn = new Connection('127.0.0.1', 5250, true)
try {
expect(conn).toBeTruthy()

const onConnError = jest.fn()
const onConnData = jest.fn()
conn.on('error', onConnError)
conn.on('data', onConnData)

const sockets = SocketMock.openSockets()
expect(sockets).toHaveLength(1)

await runWithConnection(async (conn, socket, onConnError, onConnData) => {
// Dispatch a command
const sendError = await conn.sendCommand(
{
Expand Down Expand Up @@ -250,12 +238,12 @@ describe('connection', () => {
expect(onSocketWrite).toHaveBeenNthCalledWith(2, 'REQ cmd2 PLAY 1-10\r\n', 'utf-8')

// Send replies
sockets[0].mockData(
socket.mockData(
Buffer.from(
`RES cmd1 201 INFO OK\r\n<?xml version="1.0" encoding="utf-8"?>\n<channel><test/></channel>\r\n\r\n`
)
)
sockets[0].mockData(Buffer.from(`RES cmd2 202 PLAY OK\r\n`))
socket.mockData(Buffer.from(`RES cmd2 202 PLAY OK\r\n`))

// Wait for deserializer to run
await new Promise(setImmediate)
Expand Down Expand Up @@ -294,25 +282,11 @@ describe('connection', () => {
},
undefined
)
} finally {
// Ensure cleaned up
conn.disconnect()
}
})
})

it('receive broken response', async () => {
const conn = new Connection('127.0.0.1', 5250, true)
try {
expect(conn).toBeTruthy()

const onConnError = jest.fn()
const onConnData = jest.fn()
conn.on('error', onConnError)
conn.on('data', onConnData)

const sockets = SocketMock.openSockets()
expect(sockets).toHaveLength(1)

await runWithConnection(async (conn, socket, onConnError, onConnData) => {
// Dispatch a command
const sendError = await conn.sendCommand(
{
Expand Down Expand Up @@ -342,7 +316,7 @@ describe('connection', () => {
expect(onSocketWrite).toHaveBeenNthCalledWith(2, 'REQ cmd2 PLAY 1-10\r\n', 'utf-8')

// Reply with a blob designed to crash the xml parser
sockets[0].mockData(Buffer.from(`RES cmd1 201 INFO OK\r\n<?xml\r\n\r\n`))
socket.mockData(Buffer.from(`RES cmd1 201 INFO OK\r\n<?xml\r\n\r\n`))
await new Promise(setImmediate)

expect(onConnError).toHaveBeenCalledTimes(0)
Expand All @@ -365,7 +339,7 @@ describe('connection', () => {
onConnData.mockClear()

// Reply with successful PLAY
sockets[0].mockData(Buffer.from(`RES cmd2 202 PLAY OK\r\n`))
socket.mockData(Buffer.from(`RES cmd2 202 PLAY OK\r\n`))
await new Promise(setImmediate)

expect(onConnError).toHaveBeenCalledTimes(0)
Expand All @@ -384,10 +358,7 @@ describe('connection', () => {
},
undefined
)
} finally {
// Ensure cleaned up
conn.disconnect()
}
})
})

it('test with full client', async () => {
Expand Down Expand Up @@ -486,18 +457,7 @@ describe('connection', () => {
})

it('connection loss midway through response', async () => {
const conn = new Connection('127.0.0.1', 5250, true)
try {
expect(conn).toBeTruthy()

const onConnError = jest.fn()
const onConnData = jest.fn()
conn.on('error', onConnError)
conn.on('data', onConnData)

const sockets = SocketMock.openSockets()
expect(sockets).toHaveLength(1)

await runWithConnection(async (conn, socket, onConnError, onConnData) => {
// Dispatch a command
const sendError = await conn.sendCommand(
{
Expand All @@ -517,23 +477,23 @@ describe('connection', () => {
onSocketWrite.mockClear()

// Reply with a part of a fragmented message
sockets[0].mockData(Buffer.from(`RES cmd1 201 INFO OK\r\n<?xml`))
socket.mockData(Buffer.from(`RES cmd1 201 INFO OK\r\n<?xml`))
await new Promise(setImmediate)

expect(conn.connected).toBeTruthy()
expect(onConnError).toHaveBeenCalledTimes(0)
expect(onConnData).toHaveBeenCalledTimes(0)

// Simulate connection failure
sockets[0].emit('close', new Error('Connection lost'))
socket.emit('close', new Error('Connection lost'))
await new Promise(setImmediate)

expect(conn.connected).toBeFalsy()
expect(onConnError).toHaveBeenCalledTimes(0)
expect(onConnData).toHaveBeenCalledTimes(0)

// Reconnect
sockets[0].emit('connect')
socket.emit('connect')
await new Promise(setImmediate)

expect(conn.connected).toBeTruthy()
Expand Down Expand Up @@ -562,7 +522,7 @@ describe('connection', () => {
expect(onConnData).toHaveBeenCalledTimes(0)

// Reply with successful PLAY
sockets[0].mockData(Buffer.from(`RES cmd2 202 PLAY OK\r\n`))
socket.mockData(Buffer.from(`RES cmd2 202 PLAY OK\r\n`))
await new Promise(setImmediate)

expect(onConnError).toHaveBeenCalledTimes(0)
Expand All @@ -581,10 +541,7 @@ describe('connection', () => {
},
undefined
)
} finally {
// Ensure cleaned up
conn.disconnect()
}
})
})
})
})

0 comments on commit 82a398d

Please sign in to comment.