Skip to content

Commit

Permalink
Add tests for queue; allow custom backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
bkendall committed Nov 1, 2018
1 parent 083a9c4 commit a9dc870
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
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 a9dc870

Please sign in to comment.