Skip to content

Commit e1fedfb

Browse files
DavidGoussevTrott
authored andcommitted
test: refactor test-stream2-readable-wrap.js
Use common.mustCall() where appropriate, var to const/let, assert.equal() -> assert.strictEqual(), explicit time provided to setTimeout() PR-URL: nodejs#10551 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
1 parent ff1efa6 commit e1fedfb

File tree

1 file changed

+18
-37
lines changed

1 file changed

+18
-37
lines changed
+18-37
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,63 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
4-
54
const Readable = require('_stream_readable');
65
const Writable = require('_stream_writable');
76
const EE = require('events').EventEmitter;
87

9-
var testRuns = 0, completedRuns = 0;
108
function runTest(highWaterMark, objectMode, produce) {
11-
testRuns++;
129

13-
var old = new EE();
14-
var r = new Readable({ highWaterMark: highWaterMark,
15-
objectMode: objectMode });
16-
assert.equal(r, r.wrap(old));
10+
const old = new EE();
11+
const r = new Readable({ highWaterMark: highWaterMark,
12+
objectMode: objectMode });
13+
assert.strictEqual(r, r.wrap(old));
1714

18-
var ended = false;
19-
r.on('end', function() {
20-
ended = true;
21-
});
15+
r.on('end', common.mustCall(function() {}));
2216

2317
old.pause = function() {
24-
console.error('old.pause()');
2518
old.emit('pause');
2619
flowing = false;
2720
};
2821

2922
old.resume = function() {
30-
console.error('old.resume()');
3123
old.emit('resume');
3224
flow();
3325
};
3426

35-
var flowing;
36-
var chunks = 10;
37-
var oldEnded = false;
38-
var expected = [];
27+
let flowing;
28+
let chunks = 10;
29+
let oldEnded = false;
30+
const expected = [];
3931
function flow() {
4032
flowing = true;
4133
while (flowing && chunks-- > 0) {
42-
var item = produce();
34+
const item = produce();
4335
expected.push(item);
44-
console.log('old.emit', chunks, flowing);
4536
old.emit('data', item);
46-
console.log('after emit', chunks, flowing);
4737
}
4838
if (chunks <= 0) {
4939
oldEnded = true;
50-
console.log('old end', chunks, flowing);
5140
old.emit('end');
5241
}
5342
}
5443

55-
var w = new Writable({ highWaterMark: highWaterMark * 2,
56-
objectMode: objectMode });
57-
var written = [];
44+
const w = new Writable({ highWaterMark: highWaterMark * 2,
45+
objectMode: objectMode });
46+
const written = [];
5847
w._write = function(chunk, encoding, cb) {
59-
console.log('_write', chunk);
6048
written.push(chunk);
61-
setTimeout(cb);
49+
setTimeout(cb, 1);
6250
};
6351

64-
w.on('finish', function() {
65-
completedRuns++;
52+
w.on('finish', common.mustCall(function() {
6653
performAsserts();
67-
});
54+
}));
6855

6956
r.pipe(w);
7057

7158
flow();
7259

7360
function performAsserts() {
74-
assert(ended);
7561
assert(oldEnded);
7662
assert.deepStrictEqual(written, expected);
7763
}
@@ -81,10 +67,5 @@ runTest(100, false, function() { return Buffer.allocUnsafe(100); });
8167
runTest(10, false, function() { return Buffer.from('xxxxxxxxxx'); });
8268
runTest(1, true, function() { return { foo: 'bar' }; });
8369

84-
var objectChunks = [ 5, 'a', false, 0, '', 'xyz', { x: 4 }, 7, [], 555 ];
70+
const objectChunks = [ 5, 'a', false, 0, '', 'xyz', { x: 4 }, 7, [], 555 ];
8571
runTest(1, true, function() { return objectChunks.shift(); });
86-
87-
process.on('exit', function() {
88-
assert.equal(testRuns, completedRuns);
89-
console.log('ok');
90-
});

0 commit comments

Comments
 (0)