Skip to content

Commit

Permalink
Merge pull request #32 from mauritsl/master
Browse files Browse the repository at this point in the history
Issue #24: Handle multiple responses in a single TCP window
  • Loading branch information
ceejbot committed Apr 2, 2015
2 parents 0431ee7 + 8380aa6 commit 92eaad7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ FiveBeansClient.prototype.tryHandlingResponse = function()
callback.call.apply(callback, [null, null].concat(handler.args));
else
callback.call(null, handler.args[0]);

if (typeof handler.remainder !== 'undefined')
{
this.buffer = handler.remainder;
}
}
else
{
Expand Down Expand Up @@ -216,8 +221,12 @@ ResponseHandler.prototype.parseBody = function(how)
{
if ((this.body === undefined) || (this.body === null))
return;

var expectedLength = parseInt(this.args[this.args.length - 1], 10);
if (this.body.length > (expectedLength + 2)) {
// Body contains multiple responses. Split off the remaining bytes.
this.remainder = this.body.slice(expectedLength + 2);
this.body = this.body.slice(0, expectedLength + 2);
}
if (this.body.length === (expectedLength + 2))
{
this.args.pop();
Expand Down
18 changes: 18 additions & 0 deletions test/test-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,5 +424,23 @@ describe('FiveBeansClient', function()
});
});
});

describe('concurrent commands', function()
{
it('can be handled', function(done)
{
var concurrency = 10;
var replied = 0;
var handleResponse = function(err, response)
{
if (++replied >= concurrency) {
done();
}
};
for (var i = 0; i < 10; ++i) {
consumer.stats_tube(tube, handleResponse);
}
});
});

});

0 comments on commit 92eaad7

Please sign in to comment.