Skip to content

Commit aff765a

Browse files
author
Ruben Bridgewater
committed
Fix execution order
If the command_queue and the offline_queue holds commands, the offline_queue should be choosen instead of the command_queue.
1 parent 8e24380 commit aff765a

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lib/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ function callbackOrEmit (self, callback, err, res) {
9191
}
9292

9393
function replyInOrder (self, callback, err, res) {
94-
var command_obj = self.command_queue.peekBack() || self.offline_queue.peekBack();
94+
// The offline queue has to be checked first, as there might be commands in both queues at the same time
95+
var command_obj = self.offline_queue.peekBack() || self.command_queue.peekBack();
9596
if (!command_obj) {
9697
process.nextTick(function () {
9798
callbackOrEmit(self, callback, err, res);

test/utils.spec.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('utils.js', function () {
107107
emitted = false;
108108
});
109109

110-
it('no elements in either queue. Reply in the next tick', function (done) {
110+
it('no elements in either queue. Reply in the next tick with callback', function (done) {
111111
var called = false;
112112
utils.reply_in_order(clientMock, function () {
113113
called = true;
@@ -116,7 +116,7 @@ describe('utils.js', function () {
116116
assert(!called);
117117
});
118118

119-
it('no elements in either queue. Reply in the next tick', function (done) {
119+
it('no elements in either queue. Reply in the next tick without callback', function (done) {
120120
assert(!emitted);
121121
utils.reply_in_order(clientMock, null, new Error('tada'));
122122
assert(!emitted);
@@ -153,16 +153,21 @@ describe('utils.js', function () {
153153
}
154154
});
155155

156-
it('elements in the offline queue. Reply after the offline queue is empty and respect the command_obj', function (done) {
157-
clientMock.command_queue.push(create_command_obj(), {});
158-
utils.reply_in_order(clientMock, function () {
156+
it('elements in the offline queue and the command_queue. Reply all other commands got handled respect the command_obj', function (done) {
157+
clientMock.command_queue.push(create_command_obj(), create_command_obj());
158+
clientMock.offline_queue.push(create_command_obj(), {});
159+
utils.reply_in_order(clientMock, function (err, res) {
159160
assert.strictEqual(clientMock.command_queue.length, 0);
161+
assert.strictEqual(clientMock.offline_queue.length, 0);
160162
assert(!emitted);
161-
assert.strictEqual(res_count, 1);
163+
assert.strictEqual(res_count, 3);
162164
done();
163165
}, null, null);
166+
while (clientMock.offline_queue.length) {
167+
clientMock.command_queue.push(clientMock.offline_queue.shift());
168+
}
164169
while (clientMock.command_queue.length) {
165-
clientMock.command_queue.shift().callback(null, 'bar');
170+
clientMock.command_queue.shift().callback(null, 'hello world');
166171
}
167172
});
168173
});

0 commit comments

Comments
 (0)