Skip to content

Commit

Permalink
test: adding limbo state tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Harley committed Nov 24, 2017
1 parent f75d726 commit 3532fbc
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 8 deletions.
15 changes: 14 additions & 1 deletion dist/test/connection/connectionSpec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/test/connection/connectionSpec.js.map

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions dist/test/event/event-handlerSpec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/test/event/event-handlerSpec.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/test/mocks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export declare const getServicesMock: () => {
getHandle: () => Function | null;
simulateConnectionLost: () => void;
simulateConnectionReestablished: () => void;
simulateExitLimbo: () => void;
storage: {
get: () => void;
set: () => void;
Expand Down
1 change: 1 addition & 0 deletions dist/test/mocks.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/test/mocks.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions dist/test/presence/presence-handlerSpec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/test/presence/presence-handlerSpec.js.map

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions dist/test/rpc/rpc-handlerSpec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/test/rpc/rpc-handlerSpec.js.map

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion test/connection/connectionSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ describe('connection', () => {
const reconnectIntervalIncrement = 10
const maxReconnectAttempts = 3
const maxReconnectInterval = 30
const offlineBufferTimeout = 10

beforeEach(() => {
services = getServicesMock()
options = Object.assign(DefaultOptions, {
heartbeatInterval,
reconnectIntervalIncrement,
maxReconnectAttempts,
maxReconnectInterval
maxReconnectInterval,
offlineBufferTimeout
})
emitter = new Emitter()
emitterMock = mock(emitter)
Expand Down Expand Up @@ -313,6 +315,20 @@ describe('connection', () => {
assert.calledOnce(authCallback)
})

it('goes into limbo on connection lost', async () => {
await openConnection()
const limboSpy = spy()

connection.onExitLimbo(limboSpy)

await loseConnection()
expect(connection.isInLimbo).to.equal(true)

await BBPromise.delay(1000)
assert.calledOnce(limboSpy)
expect(connection.isInLimbo).to.equal(false)
})

async function openConnection () {
socket.simulateOpen()
await BBPromise.delay(0)
Expand Down Expand Up @@ -577,6 +593,7 @@ describe('connection', () => {

socket.close()
await BBPromise.delay(0)
expect(connection.isConnected).to.equal(false)
}

async function reconnectToInitialServer () {
Expand Down
23 changes: 22 additions & 1 deletion test/event/event-handlerSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getServicesMock, getListenerMock } from '../mocks'
import { EVENT } from '../../src/constants'
import { TOPIC, EVENT_ACTIONS as EVENT_ACTION } from '../../binary-protocol/src/message-constants'
import * as Emitter from 'component-emitter2'

import { Promise as BBPromise } from 'bluebird'
import { DefaultOptions } from '../../src/client-options'
import { EventHandler } from '../../src/event/event-handler'

Expand Down Expand Up @@ -364,4 +364,25 @@ describe('event handler', () => {
})
})

describe('limbo', () => {

beforeEach(() => {
services.connection.isConnected = false
services.connection.isInLimbo = true
})

it('sends messages once re-established if in limbo', async () => {
eventHandler.emit(name, 6)

services.connectionMock
.expects('sendMessage')
.once()
.withExactArgs({ topic: TOPIC.EVENT, action: EVENT_ACTION.EMIT, parsedData: 6, name })

services.simulateConnectionReestablished()
await BBPromise.delay(1)
})

})

})
1 change: 1 addition & 0 deletions test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export const getServicesMock = () => {
getHandle: (): Function | null => handle,
simulateConnectionLost: (): void => onLost(),
simulateConnectionReestablished: (): void => onReestablished(),
simulateExitLimbo: (): void => onExitLimbo(),
storage,
storageMock,
verify: () => {
Expand Down
32 changes: 32 additions & 0 deletions test/presence/presence-handlerSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,38 @@ describe('Presence handler', () => {
})
})

describe('limbo', () => {

beforeEach(() => {
services.connection.isConnected = false
services.connection.isInLimbo = true
})

it('returns client offline error once limbo state over', async () => {
presenceHandler.getAll(callbackSpy)
services.simulateExitLimbo()

await BBPromise.delay(1)

assert.calledOnce(callbackSpy)
assert.calledWithExactly(callbackSpy, EVENT.CLIENT_OFFLINE)
})

it('sends messages once re-established if in limbo', async () => {
presenceHandler.getAll(callbackSpy)

services.connectionMock
.expects('sendMessage')
.once()
services.timeoutRegistryMock
.expects('add')
.once()

services.simulateConnectionReestablished()
await BBPromise.delay(1)
})
})

})

function message (action: PRESENCE_ACTIONS, user?: string): Message {
Expand Down
34 changes: 34 additions & 0 deletions test/rpc/rpc-handlerSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('RPC handler', () => {
let rpcHandler: RPCHandler
let handle: Function
let rpcProviderSpy: sinon.SinonSpy
let rpcMakeSpy: sinon.SinonSpy
let data: any
const name = 'myRpc'
const rpcAcceptTimeout = 3
Expand All @@ -27,6 +28,7 @@ describe('RPC handler', () => {
rpcHandler = new RPCHandler(services, options)
handle = services.getHandle()
rpcProviderSpy = sinon.spy()
rpcMakeSpy = sinon.spy()
data = { foo: 'bar' }
})

Expand Down Expand Up @@ -551,6 +553,38 @@ describe('RPC handler', () => {
sinon.assert.calledOnce(rpcPromiseResponseFail)
sinon.assert.calledWithExactly(rpcPromiseResponseFail, EVENT.CLIENT_OFFLINE)
})
})

describe('limbo', () => {

beforeEach(() => {
services.connection.isConnected = false
services.connection.isInLimbo = true
})

it('returns client offline error once limbo state over', async () => {
rpcHandler.make(name, data, rpcMakeSpy)
services.simulateExitLimbo()

await BBPromise.delay(1)

sinon.assert.calledOnce(rpcMakeSpy)
sinon.assert.calledWithExactly(rpcMakeSpy, EVENT.CLIENT_OFFLINE)
})

it('sends messages once re-established if in limbo', async () => {
rpcHandler.make(name, data, rpcMakeSpy)

services.connectionMock
.expects('sendMessage')
.once()
services.timeoutRegistryMock
.expects('add')
.twice()

services.simulateConnectionReestablished()
await BBPromise.delay(1)
})

})

Expand Down

0 comments on commit 3532fbc

Please sign in to comment.