Skip to content

Commit

Permalink
feat: Retry images create requests
Browse files Browse the repository at this point in the history
  • Loading branch information
avaly committed May 12, 2020
1 parent cc9dabd commit 119e99f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
36 changes: 31 additions & 5 deletions __tests__/images.test.js
Expand Up @@ -59,8 +59,6 @@ describe('images methods', () => {
const filePath = path.resolve(__dirname, '../package.json');

test('resolves on successfull request', () => {
const body = image;

const scope = nock()
.post(`/builds/${buildID}/images`, reqBody => {
return (
Expand All @@ -73,17 +71,32 @@ describe('images methods', () => {
);
})
.matchHeader('Authorization', 'Bearer foobar')
.reply(200, body);
.reply(200, image);

return instance
.createImage(buildID, 'test-file-name', filePath)
.then(response => {
expect(response).toEqual(body);
expect(response).toEqual(image);
expect(scope.isDone()).toBeTruthy();
});
});

test('rejects on error request', () => {
test.skip('retries and resolves after a 502 error and a 200 request', () => {
const scope = nock()
.post(`/builds/${buildID}/images`)
.matchHeader('Authorization', 'Bearer foobar')
.reply(502)
.post(`/builds/${buildID}/images`)
.matchHeader('Authorization', 'Bearer foobar')
.reply(200, image);

return instance.createImage(buildID, 'foo', filePath).then(response => {
expect(response).toEqual(image);
expect(scope.isDone()).toBeTruthy();
});
}, 5000);

test('rejects on 400 error request', () => {
const scope = nock()
.post(`/builds/${buildID}/images`)
.matchHeader('Authorization', 'Bearer foobar')
Expand All @@ -95,6 +108,19 @@ describe('images methods', () => {
});
});

test.skip('retries and rejects on 502 error request', () => {
const scope = nock()
.post(`/builds/${buildID}/images`)
.matchHeader('Authorization', 'Bearer foobar')
.times(3)
.reply(502);

return instance.createImage(buildID, 'foo', filePath).catch(err => {
expect(err.response.statusCode).toBe(502);
expect(scope.isDone()).toBeTruthy();
});
}, 10000);

test('rejects on missing buildID', () => {
return instance.createImage(null, 'foo', filePath).catch(err => {
expect(err).toBeTruthy();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -44,7 +44,7 @@
"dependencies": {
"form-data": "3.0.0",
"glob": "7.1.6",
"got": "10.6.0",
"got": "10.7.0",
"p-map": "4.0.0"
},
"devDependencies": {
Expand Down
26 changes: 18 additions & 8 deletions src/sdk.js
Expand Up @@ -41,15 +41,19 @@ class VisWiz {
* @param {string} method - http method
* @param {string} path - path for the request
* @param {object} body - body parameters / object
* @param {object} [headers] - header parameters
* @param {object} [headers] - HTTP headers for the request
* @param {object} [options] - `got` options
*/
_request(method, path, body, headers) {
_request(method, path, body, headers, options = {}) {
const url = this.server + path;
const options = {
headers,
method,
retry: 0,
};

options.headers = headers;
options.method = method;
if (!options.retry) {
options.retry = {
limit: 0,
};
}

if (body) {
if (body instanceof FormData) {
Expand Down Expand Up @@ -391,7 +395,13 @@ class VisWiz {
'POST',
path,
form,
this._getHeaders(form.getHeaders())
this._getHeaders(form.getHeaders()),
{
retry: {
limit: 3,
methods: ['POST'],
},
}
);
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -2970,10 +2970,10 @@ globals@^9.18.0:
version "9.18.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-9.18.0.tgz#aa3896b3e69b487f17e31ed2143d69a8e30c2d8a"

got@10.6.0:
version "10.6.0"
resolved "https://registry.yarnpkg.com/got/-/got-10.6.0.tgz#ac3876261a4d8e5fc4f81186f79955ce7b0501dc"
integrity sha512-3LIdJNTdCFbbJc+h/EH0V5lpNpbJ6Bfwykk21lcQvQsEcrzdi/ltCyQehFHLzJ/ka0UMH4Slg0hkYvAZN9qUDg==
got@10.7.0:
version "10.7.0"
resolved "https://registry.yarnpkg.com/got/-/got-10.7.0.tgz#62889dbcd6cca32cd6a154cc2d0c6895121d091f"
integrity sha512-aWTDeNw9g+XqEZNcTjMMZSy7B7yE9toWOFYip7ofFTLleJhvZwUxxTxkTpKvF+p1SAA4VHmuEy7PiHTHyq8tJg==
dependencies:
"@sindresorhus/is" "^2.0.0"
"@szmarczak/http-timer" "^4.0.0"
Expand Down

0 comments on commit 119e99f

Please sign in to comment.