-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle unhandled errors in
socket.reconnectStrategry
(#2226)
* handle errors in reconnect strategy * add test * fix retries typo * fix #2237 - flush queues on reconnect strategy error * Update socket.ts * Update socket.ts
- Loading branch information
Showing
2 changed files
with
59 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,63 @@ | ||
import { strict as assert } from 'assert'; | ||
import { SinonFakeTimers, useFakeTimers, spy } from 'sinon'; | ||
import RedisSocket from './socket'; | ||
import RedisSocket, { RedisSocketOptions } from './socket'; | ||
|
||
describe('Socket', () => { | ||
function createSocket(options: RedisSocketOptions): RedisSocket { | ||
const socket = new RedisSocket( | ||
() => Promise.resolve(), | ||
options | ||
); | ||
|
||
socket.on('error', (err) => { | ||
// ignore errors | ||
console.log(err); | ||
}); | ||
|
||
return socket; | ||
} | ||
|
||
describe('reconnectStrategy', () => { | ||
let clock: SinonFakeTimers; | ||
beforeEach(() => clock = useFakeTimers()); | ||
afterEach(() => clock.restore()); | ||
|
||
it('custom strategy', () => { | ||
const reconnectStrategy = spy((retries: number): number | Error => { | ||
it('custom strategy', async () => { | ||
const reconnectStrategy = spy((retries: number) => { | ||
assert.equal(retries + 1, reconnectStrategy.callCount); | ||
|
||
if (retries === 50) { | ||
return Error('50'); | ||
} | ||
if (retries === 50) return new Error('50'); | ||
|
||
const time = retries * 2; | ||
queueMicrotask(() => clock.tick(time)); | ||
return time; | ||
}); | ||
|
||
const socket = new RedisSocket( | ||
() => Promise.resolve(), | ||
{ | ||
host: 'error', | ||
reconnectStrategy | ||
} | ||
); | ||
|
||
socket.on('error', () => { | ||
// ignore errors | ||
const socket = createSocket({ | ||
host: 'error', | ||
reconnectStrategy | ||
}); | ||
|
||
return assert.rejects(socket.connect(), { | ||
await assert.rejects(socket.connect(), { | ||
message: '50' | ||
}); | ||
|
||
assert.equal(socket.isOpen, false); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const socket = createSocket({ | ||
host: 'error', | ||
reconnectStrategy(retries: number) { | ||
if (retries === 1) return new Error('done'); | ||
queueMicrotask(() => clock.tick(500)); | ||
throw new Error(); | ||
} | ||
}); | ||
|
||
await assert.rejects(socket.connect()); | ||
|
||
assert.equal(socket.isOpen, false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters