Skip to content

Commit 23da442

Browse files
committed
Account for node 10 change to defer 'readable' events to nextTick
As of node 10, all 'readable' events are deferred to nextTick. See nodejs/node@1e0f3315c7 for more info.
1 parent 88e2f77 commit 23da442

File tree

2 files changed

+109
-70
lines changed

2 files changed

+109
-70
lines changed

test/unit/parser-tests.js

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,22 @@ describe('Parser', function () {
9595
header: getFrameHeader(4, types.opcodes.result, 2, true),
9696
chunk: body
9797
}, null, doneIfError(done));
98-
assert.strictEqual(responseCounter, 1);
99-
parser.setOptions(88, { byRow: true });
100-
for (let i = 0; i < body.length; i++) {
101-
parser._transform({
102-
header: getFrameHeader(4, types.opcodes.result, 2, true, 88),
103-
chunk: body.slice(i, i + 1),
104-
offset: 0
105-
}, null, doneIfError(done));
106-
}
107-
assert.strictEqual(responseCounter, 2);
108-
done();
98+
99+
process.nextTick(() => {
100+
assert.strictEqual(responseCounter, 1);
101+
parser.setOptions(88, { byRow: true });
102+
for (let i = 0; i < body.length; i++) {
103+
parser._transform({
104+
header: getFrameHeader(4, types.opcodes.result, 2, true, 88),
105+
chunk: body.slice(i, i + 1),
106+
offset: 0
107+
}, null, doneIfError(done));
108+
}
109+
process.nextTick(() => {
110+
assert.strictEqual(responseCounter, 2);
111+
done();
112+
});
113+
});
109114
});
110115
it('should read a RESULT result with trace id chunked', function (done) {
111116
const parser = newInstance();
@@ -125,17 +130,21 @@ describe('Parser', function () {
125130
chunk: body,
126131
offset: 0
127132
}, null, doneIfError(done));
128-
assert.strictEqual(responseCounter, 1);
129-
parser.setOptions(88, { byRow: true });
130-
for (let i = 0; i < body.length; i++) {
131-
parser._transform({
132-
header: getFrameHeader(4, types.opcodes.result, 2, true, 88),
133-
chunk: body.slice(i, i + 1),
134-
offset: 0
135-
}, null, doneIfError(done));
136-
}
137-
assert.strictEqual(responseCounter, 2);
138-
done();
133+
process.nextTick(() => {
134+
assert.strictEqual(responseCounter, 1);
135+
parser.setOptions(88, { byRow: true });
136+
for (let i = 0; i < body.length; i++) {
137+
parser._transform({
138+
header: getFrameHeader(4, types.opcodes.result, 2, true, 88),
139+
chunk: body.slice(i, i + 1),
140+
offset: 0
141+
}, null, doneIfError(done));
142+
}
143+
process.nextTick(() => {
144+
assert.strictEqual(responseCounter, 2);
145+
done();
146+
});
147+
});
139148
});
140149
it('should read a VOID result with warnings and custom payload', function (done) {
141150
const parser = newInstance();
@@ -743,11 +752,16 @@ describe('Parser', function () {
743752
const rowLength = 2;
744753
let rowCounter = 0;
745754
parser.on('readable', function () {
746-
const item = parser.read();
747-
assert.strictEqual(item.header.opcode, types.opcodes.result);
748-
assert.ok(item.row);
749-
if ((++rowCounter) === rowLength) {
750-
done();
755+
let item;
756+
while ((item = parser.read())) {
757+
if (!item.row && item.frameEnded) {
758+
continue;
759+
}
760+
assert.strictEqual(item.header.opcode, types.opcodes.result);
761+
assert.ok(item.row);
762+
if ((++rowCounter) === rowLength) {
763+
done();
764+
}
751765
}
752766
});
753767
parser.setOptions(33, { byRow: true });
@@ -773,7 +787,7 @@ describe('Parser', function () {
773787
}
774788
});
775789
[1, 3, 5, 13].forEach(function (chunkLength) {
776-
it('should emit rows chunked with chunk length of ' + chunkLength, function () {
790+
it('should emit rows chunked with chunk length of ' + chunkLength, function (done) {
777791
result = {};
778792
const expected = [
779793
{ columnLength: 3, rowLength: 10 },
@@ -814,10 +828,13 @@ describe('Parser', function () {
814828
for (let i = 0; i < items.length; i++) {
815829
transformChunkedItem(i);
816830
}
817-
//assert result
818-
expected.forEach(function (expectedItem, index) {
819-
assert.ok(result[index], 'Result not found for index ' + index);
820-
assert.strictEqual(result[index].length, expectedItem.rowLength);
831+
process.nextTick(() => {
832+
//assert result
833+
expected.forEach(function (expectedItem, index) {
834+
assert.ok(result[index], 'Result not found for index ' + index);
835+
assert.strictEqual(result[index].length, expectedItem.rowLength);
836+
});
837+
done();
821838
});
822839
});
823840
});
@@ -847,7 +864,7 @@ describe('Parser', function () {
847864
{ columnLength: 1, rowLength: 20 }
848865
];
849866
[1, 2, 7, 11].forEach(function (chunkLength) {
850-
it('should emit rows chunked with chunk length of ' + chunkLength, function () {
867+
it('should emit rows chunked with chunk length of ' + chunkLength, function (done) {
851868
result = {};
852869
const buffer = Buffer.concat(expected.map(function (expectedItem, index) {
853870
parser.setOptions(index, { byRow: true });
@@ -862,11 +879,14 @@ describe('Parser', function () {
862879
}
863880
protocol._transform(buffer.slice(j, end), null, helper.throwop);
864881
}
865-
//assert result
866-
expected.forEach(function (expectedItem, index) {
867-
assert.ok(result[index], 'Result not found for index ' + index);
868-
assert.strictEqual(result[index].length, expectedItem.rowLength);
869-
assert.strictEqual(result[index][0].keys().length, expectedItem.columnLength);
882+
process.nextTick(() => {
883+
//assert result
884+
expected.forEach(function (expectedItem, index) {
885+
assert.ok(result[index], 'Result not found for index ' + index);
886+
assert.strictEqual(result[index].length, expectedItem.rowLength);
887+
assert.strictEqual(result[index][0].keys().length, expectedItem.columnLength);
888+
});
889+
done();
870890
});
871891
});
872892
});
@@ -1021,10 +1041,14 @@ describe('Parser', function () {
10211041
parser._transform(getBodyChunks(3, rowLength, 0, 10), null, doneIfError(done));
10221042
parser._transform(getBodyChunks(3, rowLength, 10, 32), null, doneIfError(done));
10231043
parser._transform(getBodyChunks(3, rowLength, 32, 55), null, doneIfError(done));
1024-
assert.strictEqual(rowCounter, 1);
1025-
parser._transform(getBodyChunks(3, rowLength, 55, null), null, doneIfError(done));
1026-
assert.strictEqual(rowCounter, 2);
1027-
done();
1044+
process.nextTick(() => {
1045+
assert.strictEqual(rowCounter, 1);
1046+
parser._transform(getBodyChunks(3, rowLength, 55, null), null, doneIfError(done));
1047+
process.nextTick(() => {
1048+
assert.strictEqual(rowCounter, 2);
1049+
done();
1050+
});
1051+
});
10281052
});
10291053
});
10301054
});

test/unit/protocol-stream-tests.js

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const types = require('../../lib/types');
66
const utils = require('../../lib/utils');
77

88
describe('Protocol', function () {
9-
it('should emit a single frame with 0-length body', function () {
9+
it('should emit a single frame with 0-length body', function (done) {
1010
const p = newInstance();
1111
const items = [];
1212
p.on('readable', function () {
@@ -17,10 +17,13 @@ describe('Protocol', function () {
1717
});
1818
const buffer = generateBuffer(4, [ 0 ]);
1919
p.readItems(buffer);
20-
assert.strictEqual(items.length, 1);
21-
assert.strictEqual(items[0].header.bodyLength, 0);
20+
process.nextTick(() => {
21+
assert.strictEqual(items.length, 1);
22+
assert.strictEqual(items[0].header.bodyLength, 0);
23+
done();
24+
});
2225
});
23-
it('should emit a single frame with 0-length body chunked', function () {
26+
it('should emit a single frame with 0-length body chunked', function (done) {
2427
const p = newInstance();
2528
const items = [];
2629
p.on('readable', function () {
@@ -32,10 +35,13 @@ describe('Protocol', function () {
3235
const buffer = generateBuffer(4, [ 0 ]);
3336
p.readItems(buffer.slice(0, 2));
3437
p.readItems(buffer.slice(2));
35-
assert.strictEqual(items.length, 1);
36-
assert.strictEqual(items[0].header.bodyLength, 0);
38+
process.nextTick(() => {
39+
assert.strictEqual(items.length, 1);
40+
assert.strictEqual(items[0].header.bodyLength, 0);
41+
done();
42+
});
3743
});
38-
it('should emit multiple frames from a single chunk', function () {
44+
it('should emit multiple frames from a single chunk', function (done) {
3945
const p = newInstance();
4046
const items = [];
4147
p.on('readable', function () {
@@ -47,12 +53,15 @@ describe('Protocol', function () {
4753
const bodyLengths = [ 0, 10, 0, 20, 30, 0];
4854
const buffer = generateBuffer(4, bodyLengths);
4955
p.readItems(buffer);
50-
assert.strictEqual(items.length, bodyLengths.length);
51-
bodyLengths.forEach(function (length, index) {
52-
assert.strictEqual(items[index].header.bodyLength, length);
56+
process.nextTick(() => {
57+
assert.strictEqual(items.length, bodyLengths.length);
58+
bodyLengths.forEach(function (length, index) {
59+
assert.strictEqual(items[index].header.bodyLength, length);
60+
});
61+
done();
5362
});
5463
});
55-
it('should emit multiple frames from multiples chunks', function () {
64+
it('should emit multiple frames from multiples chunks', function (done) {
5665
const p = newInstance();
5766
const items = {};
5867
p.on('readable', function () {
@@ -70,17 +79,20 @@ describe('Protocol', function () {
7079
p.readItems(buffer.slice(33, 45));
7180
p.readItems(buffer.slice(45, 65));
7281
p.readItems(buffer.slice(65));
73-
bodyLengths.forEach(function (length, index) {
74-
const item = items[index];
75-
assert.ok(item);
76-
assert.ok(item.length);
77-
const sumLength = item.reduce(function (previousValue, subItem) {
78-
return previousValue + subItem.chunk.length - subItem.offset;
79-
}, 0);
80-
assert.ok(sumLength >= length, sumLength + ' >= ' + length + ' failed');
82+
process.nextTick(() => {
83+
bodyLengths.forEach(function (length, index) {
84+
const item = items[index];
85+
assert.ok(item);
86+
assert.ok(item.length);
87+
const sumLength = item.reduce(function (previousValue, subItem) {
88+
return previousValue + subItem.chunk.length - subItem.offset;
89+
}, 0);
90+
assert.ok(sumLength >= length, sumLength + ' >= ' + length + ' failed');
91+
});
92+
done();
8193
});
8294
});
83-
it('should emit multiple frames from multiples small chunks', function () {
95+
it('should emit multiple frames from multiples small chunks', function (done) {
8496
const p = newInstance();
8597
const items = {};
8698
p.on('readable', function () {
@@ -99,14 +111,17 @@ describe('Protocol', function () {
99111
}
100112
p.readItems(buffer.slice(i, i + 2));
101113
}
102-
bodyLengths.forEach(function (length, index) {
103-
const item = items[index];
104-
assert.ok(item);
105-
assert.ok(item.length);
106-
const sumLength = item.reduce(function (previousValue, subItem) {
107-
return previousValue + subItem.chunk.length - subItem.offset;
108-
}, 0);
109-
assert.ok(sumLength >= length, sumLength + ' >= ' + length + ' failed');
114+
process.nextTick(() => {
115+
bodyLengths.forEach(function (length, index) {
116+
const item = items[index];
117+
assert.ok(item);
118+
assert.ok(item.length);
119+
const sumLength = item.reduce(function (previousValue, subItem) {
120+
return previousValue + subItem.chunk.length - subItem.offset;
121+
}, 0);
122+
assert.ok(sumLength >= length, sumLength + ' >= ' + length + ' failed');
123+
});
124+
done();
110125
});
111126
});
112127
});

0 commit comments

Comments
 (0)