Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix maxBytes batching sending empty messages #281

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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