From f9770963a48f426fdc224fb9bbf23379f2eec4cc 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 ++-- src/transporters/WebsocketTransport.js | 4 ++-- test/units/InteractorClient.mocha.js | 6 +++--- test/units/transporters/AxonTransport.mocha.js | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+), 7 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/src/transporters/WebsocketTransport.js b/src/transporters/WebsocketTransport.js index f620f3d..8d9d251 100644 --- a/src/transporters/WebsocketTransport.js +++ b/src/transporters/WebsocketTransport.js @@ -118,8 +118,8 @@ module.exports = class WebsocketTransport 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/InteractorClient.mocha.js b/test/units/InteractorClient.mocha.js index b16c903..52ee9dc 100644 --- a/test/units/InteractorClient.mocha.js +++ b/test/units/InteractorClient.mocha.js @@ -369,7 +369,7 @@ describe('InteractorClient', () => { assert(config.secret_key === 'private') assert(config.machine_name === 'machine') assert(config.reverse_interact === true) - assert(config.info_node === 'info') + assert(config.info_node === 'https://info') fs.writeFileSync = tmpWrite fs.readFileSync = tmpRead module.exports = fs @@ -387,7 +387,7 @@ describe('InteractorClient', () => { public_key: 'public', secret_key: 'private', machine_name: 'machine', - info_node: 'info', + info_node: 'https://info', reverse_interact: 'lol', version_management: { active: false @@ -403,7 +403,7 @@ describe('InteractorClient', () => { assert(config.secret_key === 'private') assert(config.machine_name === 'machine') assert(config.reverse_interact === 'lol') - assert(config.info_node === 'info') + assert(config.info_node === 'https://info') fs.writeFileSync = tmpWrite fs.readFileSync = tmpRead module.exports = fs 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)