From a9dc870e4025f193ccaed40a42af5d9da8df328e Mon Sep 17 00:00:00 2001 From: Bryan Kendall Date: Thu, 1 Nov 2018 11:41:44 -0700 Subject: [PATCH 1/2] Add tests for queue; allow custom backoff --- src/queue.js | 2 +- src/test/queue.spec.ts | 74 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/test/queue.spec.ts diff --git a/src/queue.js b/src/queue.js index 80209e4c856..0f54272ccb6 100644 --- a/src/queue.js +++ b/src/queue.js @@ -29,7 +29,7 @@ class Queue { this.max = 0; this.avg = 0; this.retries = options.retries || 0; - this.backoff = 200; + this.backoff = typeof options.backoff == "number" ? options.backoff : 200; this.retryCounts = {}; this.closed = false; this.finished = false; diff --git a/src/test/queue.spec.ts b/src/test/queue.spec.ts new file mode 100644 index 00000000000..114d714780e --- /dev/null +++ b/src/test/queue.spec.ts @@ -0,0 +1,74 @@ +import * as sinon from "sinon"; +import * as chai from "chai"; + +chai.use(require("chai-as-promised")); +const { expect } = chai; + +const Queue = require("../queue"); + +const TEST_ERROR = new Error("foobar"); + +describe("Queue", () => { + it("should handle tasks", () => { + const handler = sinon.stub().resolves(); + const q = new Queue({ + handler, + }); + + q.add(4); + q.close(); + q.process(); + + return q.wait() + .then(() => { + expect(handler.callCount).to.equal(1); + }); + }); + + it("should not retry", () => { + const handler = sinon.stub().rejects(TEST_ERROR); + const q = new Queue({ + handler, + retries: 0, + }); + + q.add(4); + q.close(); + q.process(); + + return q.wait() + .then(() => { + throw new Error("handler should have rejected"); + }) + .catch((err: Error) => { + expect(err).to.equal(TEST_ERROR); + }) + .then(() => { + expect(handler.callCount).to.equal(1); + }); + }); + + it("should retry the number of retries, plus one", () => { + const handler = sinon.stub().rejects(TEST_ERROR); + const q = new Queue({ + backoff: 0, + handler, + retries: 3, + }); + + q.add(4); + q.close(); + q.process(); + + return q.wait() + .then(() => { + throw new Error("handler should have rejected"); + }) + .catch((err: Error) => { + expect(err).to.equal(TEST_ERROR); + }) + .then(() => { + expect(handler.callCount).to.equal(4); + }); + }); +}); From 9e3267851ad4e66f7840906c006926d26ad61842 Mon Sep 17 00:00:00 2001 From: Bryan Kendall Date: Thu, 1 Nov 2018 12:36:33 -0700 Subject: [PATCH 2/2] add sinon types --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 1f67ddc209c..6ef18ec48e0 100644 --- a/package.json +++ b/package.json @@ -106,6 +106,7 @@ "@types/glob": "^7.1.1", "@types/mocha": "^5.2.5", "@types/node": "^10.12.0", + "@types/sinon": "^5.0.5", "chai": "^4.2.0", "chai-as-promised": "^7.1.1", "coveralls": "^3.0.1",