Skip to content

Commit

Permalink
more tests / no retry policy
Browse files Browse the repository at this point in the history
  • Loading branch information
rgarcia committed Nov 4, 2016
1 parent 58eee55 commit 3e5d27b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
12 changes: 11 additions & 1 deletion samples/gen-js/index.js
Expand Up @@ -35,6 +35,15 @@ const defaultRetryPolicy = {
},
};

const noRetryPolicy = {
backoffs() {
return [];
},
retry(requestOptions, err, res, body) {
return false;
},
};

module.exports = class SwaggerTest {

constructor(options) {
Expand Down Expand Up @@ -113,7 +122,7 @@ module.exports = class SwaggerTest {
}
}

const retryPolicy = this.retryPolicy || options.retryPolicy || defaultRetryPolicy;
const retryPolicy = options.retryPolicy || this.retryPolicy || defaultRetryPolicy;
const backoffs = retryPolicy.backoffs();
let retries = 0;
(function requestOnce() {
Expand Down Expand Up @@ -379,4 +388,5 @@ module.exports = class SwaggerTest {

module.exports.RetryPolicies = {
Default: defaultRetryPolicy,
No: noRetryPolicy,
};
63 changes: 60 additions & 3 deletions samples/gen-js/test/test.js
Expand Up @@ -3,13 +3,16 @@ const nock = require("nock");
const util = require("util");

const Client = require("../index");
const {RetryPolicies} = Client;

const mockAddress = "http://localhost:8000";

describe("retries", function() {
it("exponential backoff by default", function(done) {
const client = new Client({address: "http://localhost:8000"});
it("performs exponential backoff by default", function(done) {
const client = new Client({address: mockAddress});
let requestCount = 0;
let requestTimes = [];
const scope = nock("http://localhost:8000")
const scope = nock(mockAddress)
.get("/v1/books")
.times(3)
.reply(function(uri, requestBody) {
Expand All @@ -32,4 +35,58 @@ describe("retries", function() {
done();
});
});

it("lets the user configure a custom retry policy on the client", function(done) {
const client = new Client({
address: mockAddress,
retryPolicy: RetryPolicies.No
});
let requestCount = 0;
let requestTimes = [];
const scope = nock(mockAddress)
.get("/v1/books")
.times(3)
.reply(function(uri, requestBody) {
requestTimes.push(new Date());
requestCount++;
if (requestCount <= 2) {
return [500, ""];
} else {
return [200, "[]"];
}
});
client.getBooks({}, function(err, books) {
assert.equal(requestCount, 1);
assert(err);
assert(!scope.isDone());
nock.cleanAll();
done();
});
});

it("lets the user configure a custom retry policy on a single request", function(done) {
const client = new Client({address: mockAddress});
let requestCount = 0;
let requestTimes = [];
const scope = nock(mockAddress)
.get("/v1/books")
.times(3)
.reply(function(uri, requestBody) {
requestTimes.push(new Date());
requestCount++;
if (requestCount <= 2) {
return [500, ""];
} else {
return [200, "[]"];
}
});
client.getBooks({}, {retryPolicy: RetryPolicies.No}, function(err, books) {
assert.equal(requestCount, 1);
assert(err);
assert(!scope.isDone());
nock.cleanAll();
done();
});
});

});

0 comments on commit 3e5d27b

Please sign in to comment.