From df4af329668298d7a97d95502399611ef2e4f6a9 Mon Sep 17 00:00:00 2001 From: Paolo Scanferla Date: Sat, 13 Feb 2016 23:41:20 +0100 Subject: [PATCH] Made Queue processing synchronous. --- src/queue.js | 14 ++++++-------- test/unit/queue.js | 31 +++++++++++-------------------- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/queue.js b/src/queue.js index b15edda..4e95953 100644 --- a/src/queue.js +++ b/src/queue.js @@ -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 () { diff --git a/test/unit/queue.js b/test/unit/queue.js index e4a1da9..b987d7c 100644 --- a/test/unit/queue.js +++ b/test/unit/queue.js @@ -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); @@ -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); }); });