Skip to content

Commit

Permalink
Merge 9e32678 into 083a9c4
Browse files Browse the repository at this point in the history
  • Loading branch information
bkendall committed Nov 1, 2018
2 parents 083a9c4 + 9e32678 commit 1165543
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/queue.js
Expand Up @@ -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;
Expand Down
74 changes: 74 additions & 0 deletions 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);
});
});
});

0 comments on commit 1165543

Please sign in to comment.