Skip to content

Commit 2c1d0ad

Browse files
TrottMylesBorins
authored andcommitted
test: refactor test-stream-unshift-read-race
* add RegExp as second argument to assert.throws() * replace process.on('exit', ...) boolean checks with common.mustCall() * assert.equal() -> assert.strictEqual() * add 1 ms duration as second argument to setTimeout() * var -> const PR-URL: #10532 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d6266fb commit 2c1d0ad

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed
+30-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
require('../common');
3-
var assert = require('assert');
2+
const common = require('../common');
3+
const assert = require('assert');
44

55
// This test verifies that:
66
// 1. unshift() does not cause colliding _read() calls.
@@ -9,19 +9,19 @@ var assert = require('assert');
99
// 3. push() after the EOF signaling null is an error.
1010
// 4. _read() is not called after pushing the EOF null chunk.
1111

12-
var stream = require('stream');
13-
var hwm = 10;
14-
var r = stream.Readable({ highWaterMark: hwm });
15-
var chunks = 10;
12+
const stream = require('stream');
13+
const hwm = 10;
14+
const r = stream.Readable({ highWaterMark: hwm });
15+
const chunks = 10;
1616

17-
var data = Buffer.allocUnsafe(chunks * hwm + Math.ceil(hwm / 2));
18-
for (var i = 0; i < data.length; i++) {
19-
var c = 'asdf'.charCodeAt(i % 4);
17+
const data = Buffer.allocUnsafe(chunks * hwm + Math.ceil(hwm / 2));
18+
for (let i = 0; i < data.length; i++) {
19+
const c = 'asdf'.charCodeAt(i % 4);
2020
data[i] = c;
2121
}
2222

23-
var pos = 0;
24-
var pushedNull = false;
23+
let pos = 0;
24+
let pushedNull = false;
2525
r._read = function(n) {
2626
assert(!pushedNull, '_read after null push');
2727

@@ -30,7 +30,7 @@ r._read = function(n) {
3030

3131
function push(fast) {
3232
assert(!pushedNull, 'push() after null push');
33-
var c = pos >= data.length ? null : data.slice(pos, pos + n);
33+
const c = pos >= data.length ? null : data.slice(pos, pos + n);
3434
pushedNull = c === null;
3535
if (fast) {
3636
pos += n;
@@ -41,59 +41,54 @@ r._read = function(n) {
4141
pos += n;
4242
r.push(c);
4343
if (c === null) pushError();
44-
});
44+
}, 1);
4545
}
4646
}
4747
};
4848

4949
function pushError() {
5050
assert.throws(function() {
5151
r.push(Buffer.allocUnsafe(1));
52-
});
52+
}, /^Error: stream.push\(\) after EOF$/);
5353
}
5454

5555

56-
var w = stream.Writable();
57-
var written = [];
56+
const w = stream.Writable();
57+
const written = [];
5858
w._write = function(chunk, encoding, cb) {
5959
written.push(chunk.toString());
6060
cb();
6161
};
6262

63-
var ended = false;
64-
r.on('end', function() {
65-
assert(!ended, 'end emitted more than once');
63+
r.on('end', common.mustCall(function() {
6664
assert.throws(function() {
6765
r.unshift(Buffer.allocUnsafe(1));
68-
});
69-
ended = true;
66+
}, /^Error: stream.unshift\(\) after end event$/);
7067
w.end();
71-
});
68+
}));
7269

7370
r.on('readable', function() {
74-
var chunk;
71+
let chunk;
7572
while (null !== (chunk = r.read(10))) {
7673
w.write(chunk);
7774
if (chunk.length > 4)
7875
r.unshift(Buffer.from('1234'));
7976
}
8077
});
8178

82-
var finished = false;
83-
w.on('finish', function() {
84-
finished = true;
79+
w.on('finish', common.mustCall(function() {
8580
// each chunk should start with 1234, and then be asfdasdfasdf...
8681
// The first got pulled out before the first unshift('1234'), so it's
8782
// lacking that piece.
88-
assert.equal(written[0], 'asdfasdfas');
89-
var asdf = 'd';
83+
assert.strictEqual(written[0], 'asdfasdfas');
84+
let asdf = 'd';
9085
console.error('0: %s', written[0]);
91-
for (var i = 1; i < written.length; i++) {
86+
for (let i = 1; i < written.length; i++) {
9287
console.error('%s: %s', i.toString(32), written[i]);
93-
assert.equal(written[i].slice(0, 4), '1234');
94-
for (var j = 4; j < written[i].length; j++) {
95-
var c = written[i].charAt(j);
96-
assert.equal(c, asdf);
88+
assert.strictEqual(written[i].slice(0, 4), '1234');
89+
for (let j = 4; j < written[i].length; j++) {
90+
const c = written[i].charAt(j);
91+
assert.strictEqual(c, asdf);
9792
switch (asdf) {
9893
case 'a': asdf = 's'; break;
9994
case 's': asdf = 'd'; break;
@@ -102,11 +97,9 @@ w.on('finish', function() {
10297
}
10398
}
10499
}
105-
});
100+
}));
106101

107102
process.on('exit', function() {
108-
assert.equal(written.length, 18);
109-
assert(ended, 'stream ended');
110-
assert(finished, 'stream finished');
103+
assert.strictEqual(written.length, 18);
111104
console.log('ok');
112105
});

0 commit comments

Comments
 (0)