Skip to content

Commit

Permalink
fix: maxBytes batching sending empty messages (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
Legogris authored and JustinBeckwith committed Oct 10, 2018
1 parent 3a8667a commit 81b9a4d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 26 additions & 0 deletions test/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 81b9a4d

Please sign in to comment.