Fix incorrect retries if multiple HTTP.request() calls get 500s

https://forums.zotero.org/discussion/comment/425814/#Comment_425814

The passed intervals array was modified, so after a request() got a 500,
subsequent calls would start with longer delays and eventually start
retrying immediately.
This commit is contained in:
Dan Stillman 2023-01-11 02:07:18 -05:00
parent 4fbb89a588
commit 21777af980
2 changed files with 24 additions and 0 deletions

View file

@ -853,6 +853,7 @@ Zotero.Utilities.Internal = {
var delay;
var totalTime = 0;
var last = false;
intervals = intervals.slice();
while (true) {
let interval = intervals.shift();
if (interval) {

View file

@ -260,6 +260,29 @@ describe("Zotero.HTTP", function () {
assert.approximately(delayStub.args[0][0], 5 * 1000, 5);
assert.approximately(delayStub.args[1][0], 10 * 1000, 5);
});
it("should start with first interval on new request() call", async function () {
var called = 0;
server.respond(function (req) {
if (req.method == "GET" && req.url.startsWith(baseURL + "error")) {
if (called < 1) {
req.respond(500, {}, "");
}
else {
req.respond(200, {}, "");
}
}
called++;
});
spy = sinon.spy(Zotero.HTTP, "_requestInternal");
var errorDelayIntervals = [20];
await Zotero.HTTP.request("GET", baseURL + "error1", { errorDelayIntervals })
called = 0;
await Zotero.HTTP.request("GET", baseURL + "error2", { errorDelayIntervals }),
assert.equal(4, spy.callCount);
assert.equal(delayStub.args[0][0], 20);
assert.equal(delayStub.args[1][0], 20);
});
});
});