Skip to content

Commit

Permalink
Made Queue processing synchronous.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Scanferla committed Feb 13, 2016
1 parent a304618 commit df4af32
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
14 changes: 6 additions & 8 deletions src/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ export default class Queue {
}

process () {
setTimeout(() => {
if (this.queue.length !== 0) {
const ack = this.consumer(this.queue[0]);
if (ack) {
this.queue.shift();
this.process();
}
if (this.queue.length !== 0) {
const ack = this.consumer(this.queue[0]);
if (ack) {
this.queue.shift();
this.process();
}
}, 0);
}
}

empty () {
Expand Down
31 changes: 11 additions & 20 deletions test/unit/queue.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import chai, {expect} from "chai";
import sinon from "sinon";
import sinonChai from "sinon-chai";
import takeTen from "./take-ten";

chai.use(sinonChai);

Expand Down Expand Up @@ -31,41 +30,33 @@ describe("`Queue` class", () => {

describe("`process` method", () => {

it("calls the consumer (asynchronously) on each element of the queue", done => {
it("calls the consumer on each element of the queue", () => {
const consumer = sinon.spy(() => true);
const q = new Queue(consumer);
q.queue = [0, 1, 2];
q.process();
// Test the asynchronicity
expect(consumer).to.have.callCount(0);
takeTen(() => {
expect(consumer).to.have.been.calledWith(0);
expect(consumer).to.have.been.calledWith(1);
expect(consumer).to.have.been.calledWith(2);
expect(consumer).to.have.callCount(3);
}, done);
expect(consumer).to.have.been.calledWith(0);
expect(consumer).to.have.been.calledWith(1);
expect(consumer).to.have.been.calledWith(2);
expect(consumer).to.have.callCount(3);
});

it("removes elements from the queue", done => {
it("removes elements from the queue", () => {
const consumer = sinon.spy(() => true);
const q = new Queue(consumer);
q.queue = [0, 1, 2];
q.process();
takeTen(() => {
expect(q.queue.length).to.equal(0);
}, done);
expect(q.queue.length).to.equal(0);
});

it("doesn't remove elements from the queue if the consumer doesn't ack", done => {
it("doesn't remove elements from the queue if the consumer doesn't ack", () => {
const consumer = sinon.spy(() => false);
const q = new Queue(consumer);
q.queue = [0, 1, 2];
q.process();
takeTen(() => {
expect(consumer).to.have.been.calledWith(0);
expect(consumer).to.have.callCount(1);
expect(q.queue.length).to.equal(3);
}, done);
expect(consumer).to.have.been.calledWith(0);
expect(consumer).to.have.callCount(1);
expect(q.queue.length).to.equal(3);
});

});
Expand Down

0 comments on commit df4af32

Please sign in to comment.