From 65518856123408a4514349389685c04a4daddc09 Mon Sep 17 00:00:00 2001 From: Eywek Date: Thu, 24 May 2018 15:26:41 +0200 Subject: [PATCH] improv: fix #58 agent queue --- src/transporters/AxonTransport.js | 4 ++-- test/units/transporters/AxonTransport.mocha.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/transporters/AxonTransport.js b/src/transporters/AxonTransport.js index f9c1887..0c024a6 100644 --- a/src/transporters/AxonTransport.js +++ b/src/transporters/AxonTransport.js @@ -140,8 +140,8 @@ module.exports = class AxonTransport extends Transporter { log('Trying to send data while not connected, buffering ...') // remove last element if the queue is full - if (this.queue.size >= cst.PACKET_QUEUE_SIZE) { - this.queue.pop() + if (this.queue.length >= cst.PACKET_QUEUE_SIZE) { + this.queue.shift() } return this.queue.push({ channel: channel, data: data }) } diff --git a/test/units/transporters/AxonTransport.mocha.js b/test/units/transporters/AxonTransport.mocha.js index 137e605..1448fe9 100644 --- a/test/units/transporters/AxonTransport.mocha.js +++ b/test/units/transporters/AxonTransport.mocha.js @@ -200,6 +200,24 @@ describe('AxonTransport', () => { assert(transport.queue[0].data === 'data') done() }) + it('should add to queue and remove old queue', (done) => { + let transport = new AxonTransport(opts, daemon) + let _connectCalls = 0 + clearInterval(transport._worker) + transport.isConnected = _ => { + _connectCalls++ + return false + } + transport.queue = [] + for (let i = 0; i < 200; i++) transport.queue.push(['channel', 'data']) + assert(transport.queue.length === 200) + assert(transport.send('channel-custom', 'data-custom') === 200) + assert(_connectCalls === 1) + assert(transport.queue.length === 200) + assert(transport.queue[199].channel === 'channel-custom') + assert(transport.queue[199].data === 'data-custom') + done() + }) }) it('should store status if channel is status', (done) => { let transport = new AxonTransport(opts, daemon)