Skip to content

Commit 14b8297

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 <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent eadab17 commit 14b8297

1 file changed

Lines changed: 30 additions & 37 deletions

File tree

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
require('../common');
3-
varassert=require('assert');
2+
constcommon=require('../common');
3+
constassert=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-
varstream=require('stream');
13-
varhwm=10;
14-
varr=stream.Readable({highWaterMark: hwm});
15-
varchunks=10;
12+
conststream=require('stream');
13+
consthwm=10;
14+
constr=stream.Readable({highWaterMark: hwm});
15+
constchunks=10;
1616

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

23-
varpos=0;
24-
varpushedNull=false;
23+
letpos=0;
24+
letpushedNull=false;
2525
r._read=function(n){
2626
assert(!pushedNull,'_read after null push');
2727

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

3131
functionpush(fast){
3232
assert(!pushedNull,'push() after null push');
33-
varc=pos>=data.length ? null : data.slice(pos,pos+n);
33+
constc=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
functionpushError(){
5050
assert.throws(function(){
5151
r.push(Buffer.allocUnsafe(1));
52-
});
52+
},/^Error:stream.push\(\)afterEOF$/);
5353
}
5454

5555

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

63-
varended=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\(\)afterendevent$/);
7067
w.end();
71-
});
68+
}));
7269

7370
r.on('readable',function(){
74-
varchunk;
71+
letchunk;
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-
varfinished=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-
varasdf='d';
83+
assert.strictEqual(written[0],'asdfasdfas');
84+
letasdf='d';
9085
console.error('0: %s',written[0]);
91-
for(vari=1;i<written.length;i++){
86+
for(leti=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(varj=4;j<written[i].length;j++){
95-
varc=written[i].charAt(j);
96-
assert.equal(c,asdf);
88+
assert.strictEqual(written[i].slice(0,4),'1234');
89+
for(letj=4;j<written[i].length;j++){
90+
constc=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)