429 and Retry-After support for API requests
This commit is contained in:
parent
64414e49be
commit
5d6478e507
2 changed files with 160 additions and 33 deletions
|
@ -44,31 +44,6 @@ describe("Zotero.Sync.APIClient", function () {
|
|||
})
|
||||
|
||||
describe("#_checkConnection()", function () {
|
||||
var spy;
|
||||
|
||||
beforeEach(function () {
|
||||
client.failureDelayIntervals = [10];
|
||||
client.failureDelayMax = 15;
|
||||
});
|
||||
afterEach(function () {
|
||||
if (spy) {
|
||||
spy.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("should retry on 500 error", function* () {
|
||||
setResponse({
|
||||
method: "GET",
|
||||
url: "error",
|
||||
status: 500,
|
||||
text: ""
|
||||
})
|
||||
var spy = sinon.spy(Zotero.HTTP, "request");
|
||||
var e = yield getPromiseError(client.makeRequest("GET", baseURL + "error"));
|
||||
assert.instanceOf(e, Zotero.HTTP.UnexpectedStatusException);
|
||||
assert.isTrue(spy.calledTwice);
|
||||
})
|
||||
|
||||
it("should catch an interrupted connection", function* () {
|
||||
setResponse({
|
||||
method: "GET",
|
||||
|
@ -149,4 +124,119 @@ describe("Zotero.Sync.APIClient", function () {
|
|||
assert.sameMembers(results.map(o => o.id), [1, 2, 3, 4, 5]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("Retries", function () {
|
||||
var spy;
|
||||
var delayStub;
|
||||
|
||||
before(function () {
|
||||
delayStub = sinon.stub(Zotero.Promise, "delay").returns(Zotero.Promise.resolve());
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
client.failureDelayIntervals = [10, 20];
|
||||
client.failureDelayMax = 25;
|
||||
client.rateDelayIntervals = [15, 25];
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
if (spy) {
|
||||
spy.restore();
|
||||
}
|
||||
delayStub.reset();
|
||||
});
|
||||
|
||||
after(function () {
|
||||
delayStub.restore();
|
||||
});
|
||||
|
||||
|
||||
describe("#makeRequest()", function () {
|
||||
it("should retry on 500 error", function* () {
|
||||
setResponse({
|
||||
method: "GET",
|
||||
url: "error",
|
||||
status: 500,
|
||||
text: ""
|
||||
});
|
||||
spy = sinon.spy(Zotero.HTTP, "request");
|
||||
var e = yield getPromiseError(client.makeRequest("GET", baseURL + "error"));
|
||||
assert.instanceOf(e, Zotero.HTTP.UnexpectedStatusException);
|
||||
assert.isTrue(spy.calledTwice);
|
||||
});
|
||||
|
||||
it("should obey Retry-After for 503", function* () {
|
||||
var called = 0;
|
||||
server.respond(function (req) {
|
||||
if (req.method == "GET" && req.url == baseURL + "error") {
|
||||
if (called < 1) {
|
||||
req.respond(
|
||||
503,
|
||||
{
|
||||
"Retry-After": 5
|
||||
},
|
||||
""
|
||||
);
|
||||
}
|
||||
else if (called < 2) {
|
||||
req.respond(
|
||||
503,
|
||||
{
|
||||
"Retry-After": 10
|
||||
},
|
||||
""
|
||||
);
|
||||
}
|
||||
else {
|
||||
req.respond(
|
||||
200,
|
||||
{},
|
||||
""
|
||||
);
|
||||
}
|
||||
}
|
||||
called++;
|
||||
});
|
||||
spy = sinon.spy(Zotero.HTTP, "request");
|
||||
yield client.makeRequest("GET", baseURL + "error");
|
||||
assert.isTrue(spy.calledThrice);
|
||||
// DEBUG: Why are these slightly off?
|
||||
assert.approximately(delayStub.args[0][0], 5 * 1000, 5);
|
||||
assert.approximately(delayStub.args[1][0], 10 * 1000, 5);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("#_check429()", function () {
|
||||
it("should retry on 429 error", function* () {
|
||||
var called = 0;
|
||||
server.respond(function (req) {
|
||||
if (req.method == "GET" && req.url == baseURL + "error") {
|
||||
if (called < 2) {
|
||||
req.respond(
|
||||
429,
|
||||
{},
|
||||
""
|
||||
);
|
||||
}
|
||||
else {
|
||||
req.respond(
|
||||
200,
|
||||
{},
|
||||
""
|
||||
);
|
||||
}
|
||||
}
|
||||
called++;
|
||||
});
|
||||
spy = sinon.spy(Zotero.HTTP, "request");
|
||||
yield client.makeRequest("GET", baseURL + "error");
|
||||
assert.isTrue(spy.calledThrice);
|
||||
// DEBUG: Why are these slightly off?
|
||||
assert.approximately(delayStub.args[0][0], 15 * 1000, 5);
|
||||
assert.approximately(delayStub.args[1][0], 25 * 1000, 5);
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue