Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for queue; allow custom backoff
- Loading branch information
Showing
2 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |