Skip to content

Commit

Permalink
test: add acceptance tests for enable and disable retries
Browse files Browse the repository at this point in the history
  • Loading branch information
Barrett Schonefeld committed Aug 25, 2021
1 parent 9d16bcb commit 57e2456
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/request-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,17 @@ export class RequestWrapper {
}

public enableRetries(retryOptions?: RetryOptions): void {
// avoid attaching the same interceptor multiple times
// to protect against user error and ensure disableRetries() always disables retries
if (typeof this.retryInterceptorId === 'number') {
this.disableRetries();
}
this.raxConfig = RequestWrapper.getRaxConfig(this.axiosInstance, retryOptions);
this.retryInterceptorId = rax.attach(this.axiosInstance);
}

public disableRetries(): void {
if (this.retryInterceptorId) {
if (typeof this.retryInterceptorId === 'number') {
rax.detach(this.retryInterceptorId, this.axiosInstance);
delete this.retryInterceptorId;
delete this.raxConfig;
Expand Down
24 changes: 24 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"eslint-plugin-node": "^9.0.0",
"eslint-plugin-prettier": "^3.0.1",
"jest": "^26.6.3",
"nock": "^13.1.2",
"object.assign": "~4.1.0",
"prettier": "^2.3.0",
"semantic-release": "17.4.2",
Expand Down Expand Up @@ -97,7 +98,7 @@
"lint": "npm run eslint:check",
"lint:fix": "npm run eslint:fix",
"jest": "jest",
"test": "jest test/unit/",
"test": "jest test/unit/ test/mock-server/",
"test-travis": "jest --runInBand test/unit/",
"report-coverage": "codecov",
"build": "tsc",
Expand Down
77 changes: 77 additions & 0 deletions test/mock-server/retry.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const nock = require('nock');
const rax = require('retry-axios');
const retry = require('retry');

const url = 'http://example.test-url.com';
nock.disableNetConnect();

const { NoAuthAuthenticator } = require('../../dist/auth/authenticators/no-auth-authenticator');
const AUTHENTICATOR = new NoAuthAuthenticator();

const { BaseService } = require('../../dist/lib/base-service');
const service = new BaseService({
authenticator: AUTHENTICATOR,
serviceUrl: url,
});

const parameters = {
options: {
method: 'GET',
url: '/',
headers: {}
},
defaultOptions: {
serviceUrl: url,
},
};

describe('Node Core retries', () => {
beforeEach(() => {
service.enableRetries();
});

afterEach(() => {
nock.cleanAll();
service.disableRetries();
});

it('should retry after we call enableRetries', async function () {
const scopes = [
nock(url).get('/').reply(429, undefined),
nock(url).get('/').reply(200, 'retry success!'),
];

const result = await service.createRequest(parameters);
expect(result.result).toBe('retry success!');
// ensure all mocks satisfied
scopes.forEach(s => s.done());
});

it('should not retry more than `maxRetries`', async function () {
const scopes = [
nock(url).get('/').reply(500, undefined),
nock(url).get('/').reply(500, undefined), // should stop after this response
nock(url).get('/').reply(200, 'should not get this!'),
];

service.enableRetries({ maxRetries: 1});

// ensure 1 assertion executed in this test (i.e. promise not resolved.)
expect.assertions(1);
await service.createRequest(parameters).catch((err) => expect(err).toBeDefined());
});

it('should not retry after we call disableRetries', async function () {
const scopes = [
nock(url).get('/').reply(500, 'success!'),
nock(url).get('/').reply(200, 'should not get this!'),
];

// disable retries
service.disableRetries();

// ensure 1 assertion executed in this test (i.e. promise not resolved.)
expect.assertions(1);
await service.createRequest(parameters).catch((err) => expect(err).toBeDefined());
});
});

0 comments on commit 57e2456

Please sign in to comment.