|
| 1 | +import { expect, AssertionError } from 'chai'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { Request, Response, FetchError } from 'node-fetch'; |
| 4 | +import OpenSessionRetryPolicy from '../../../../lib/connection/connections/OpenSessionRetryPolicy'; |
| 5 | +import RetryError, { RetryErrorCode } from '../../../../lib/errors/RetryError'; |
| 6 | + |
| 7 | +import ClientContextStub from '../../.stubs/ClientContextStub'; |
| 8 | + |
| 9 | +function systemError(code: string, reason = code): FetchError { |
| 10 | + return new FetchError(`request to https://example.com failed, reason: ${reason}`, 'system', { |
| 11 | + code, |
| 12 | + } as unknown as NodeJS.ErrnoException); |
| 13 | +} |
| 14 | + |
| 15 | +function statusResponse(status: number) { |
| 16 | + return { request: new Request('http://localhost'), response: new Response(undefined, { status }) }; |
| 17 | +} |
| 18 | + |
| 19 | +function createPolicy(retryMaxAttempts = 5) { |
| 20 | + return new OpenSessionRetryPolicy(new ClientContextStub({ retryDelayMin: 1, retryDelayMax: 2, retryMaxAttempts })); |
| 21 | +} |
| 22 | + |
| 23 | +async function expectRejection(promise: Promise<unknown>): Promise<unknown> { |
| 24 | + try { |
| 25 | + await promise; |
| 26 | + } catch (error) { |
| 27 | + if (error instanceof AssertionError) { |
| 28 | + throw error; |
| 29 | + } |
| 30 | + return error; |
| 31 | + } |
| 32 | + return expect.fail('It should throw an error'); |
| 33 | +} |
| 34 | + |
| 35 | +describe('OpenSessionRetryPolicy', () => { |
| 36 | + for (const status of [408, 429, 500, 502, 503, 504]) { |
| 37 | + it(`retries an HTTP ${status} response`, async () => { |
| 38 | + const operation = sinon.stub(); |
| 39 | + operation.onCall(0).resolves(statusResponse(status)); |
| 40 | + operation.onCall(1).resolves(statusResponse(200)); |
| 41 | + |
| 42 | + const result = await createPolicy().invokeWithRetry(operation); |
| 43 | + |
| 44 | + expect(result.response.status).to.equal(200); |
| 45 | + expect(operation.callCount).to.equal(2); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + for (const status of [400, 401, 403, 501]) { |
| 50 | + it(`does not retry an HTTP ${status} response`, async () => { |
| 51 | + const details = statusResponse(status); |
| 52 | + const operation = sinon.stub().resolves(details); |
| 53 | + |
| 54 | + expect(await createPolicy().invokeWithRetry(operation)).to.equal(details); |
| 55 | + expect(operation.callCount).to.equal(1); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + it('throws a RetryError once a persistent 503 runs out of attempts', async () => { |
| 60 | + const operation = sinon.stub().resolves(statusResponse(503)); |
| 61 | + |
| 62 | + const error = await expectRejection(createPolicy(3).invokeWithRetry(operation)); |
| 63 | + |
| 64 | + expect(error).to.be.instanceOf(RetryError); |
| 65 | + expect((error as RetryError).errorCode).to.equal(RetryErrorCode.AttemptsExceeded); |
| 66 | + expect(operation.callCount).to.equal(3); |
| 67 | + }); |
| 68 | + |
| 69 | + const networkErrors: Array<[string, Error]> = [ |
| 70 | + ['an untrusted certificate', systemError('DEPTH_ZERO_SELF_SIGNED_CERT', 'self-signed certificate')], |
| 71 | + ['a hostname mismatch', systemError('ERR_TLS_CERT_ALTNAME_INVALID')], |
| 72 | + ['an unknown host', systemError('ENOTFOUND')], |
| 73 | + ['a refused connection', systemError('ECONNREFUSED')], |
| 74 | + ['a connection reset', systemError('ECONNRESET', 'socket hang up')], |
| 75 | + ['a premature close', new Error('Premature close')], |
| 76 | + ]; |
| 77 | + |
| 78 | + for (const [label, error] of networkErrors) { |
| 79 | + it(`throws ${label} at once, without retrying`, async () => { |
| 80 | + const operation = sinon.stub().rejects(error); |
| 81 | + |
| 82 | + expect(await expectRejection(createPolicy().invokeWithRetry(operation))).to.equal(error); |
| 83 | + expect(operation.callCount).to.equal(1); |
| 84 | + }); |
| 85 | + } |
| 86 | +}); |
0 commit comments