-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add acceptance tests for enable and disable retries
- Loading branch information
Barrett Schonefeld
committed
Aug 25, 2021
1 parent
9d16bcb
commit 57e2456
Showing
4 changed files
with
109 additions
and
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -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()); | ||
}); | ||
}); |