From 81b9a4d7c8fdf6105456ee62cf6fa1e56618277b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Edstr=C3=B6m?= Date: Wed, 10 Oct 2018 17:48:31 +0200 Subject: [PATCH] fix: maxBytes batching sending empty messages (#281) --- src/publisher.js | 9 ++++++--- test/publisher.js | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/publisher.js b/src/publisher.js index baeecfe02..1ba9e9fde 100644 --- a/src/publisher.js +++ b/src/publisher.js @@ -163,15 +163,18 @@ class Publisher { const opts = this.settings.batching; // if this message puts us over the maxBytes option, then let's ship // what we have and add it to the next batch - if (this.inventory_.bytes + data.length > opts.maxBytes) { + if ( + this.inventory_.bytes > 0 && + this.inventory_.bytes + data.length > opts.maxBytes + ) { this.publish_(); } // add it to the queue! this.queue_(data, attributes, callback); // next lets check if this message brings us to the message cap or if we - // magically hit the max byte limit + // hit the max byte limit const hasMaxMessages = this.inventory_.queued.length === opts.maxMessages; - if (this.inventory_.bytes === opts.maxBytes || hasMaxMessages) { + if (this.inventory_.bytes >= opts.maxBytes || hasMaxMessages) { this.publish_(); return; } diff --git a/test/publisher.js b/test/publisher.js index 4cf2cc7d8..a63e4e50c 100644 --- a/test/publisher.js +++ b/test/publisher.js @@ -210,6 +210,32 @@ All attributes must be in the form of a string. publisher.publish(DATA, done); }); + it('should not attempt to publish empty payload if data puts payload above size cap', function(done) { + const pushRequests = []; + publisher.settings.batching.maxBytes = 2; + publisher.inventory_.bytes = 0; + + publisher.publish_ = function() { + assert.notStrictEqual(publisher.inventory_.queued.length, 0); + pushRequests.push(publisher.inventory_.queued); + publisher.inventory_.callbacks.forEach(function(callback) { + callback(); + }); + }; + + publisher.publish(DATA, function() { + assert.deepStrictEqual(pushRequests, [ + [ + { + data: DATA, + attributes: {}, + }, + ], + ]); + done(); + }); + }); + it('should publish if data puts payload at size cap', function(done) { publisher.queue_ = function() { publisher.inventory_.bytes += DATA.length;