Browse Source

test: cleanup stream tests

const and let instead var
assert.strictEqual instead assert.equal

PR-URL: https://github.com/nodejs/node/pull/8668
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
v7.x
Italo A. Casas 8 years ago
committed by James M Snell
parent
commit
3f88692fe7
  1. 16
      test/parallel/test-stream-big-packet.js
  2. 26
      test/parallel/test-stream-big-push.js
  3. 11
      test/parallel/test-stream-duplex.js
  4. 8
      test/parallel/test-stream-end-paused.js
  5. 7
      test/parallel/test-stream-ispaused.js
  6. 21
      test/parallel/test-stream-pipe-after-end.js
  7. 72
      test/parallel/test-stream-pipe-cleanup.js

16
test/parallel/test-stream-big-packet.js

@ -1,10 +1,10 @@
'use strict'; 'use strict';
require('../common'); require('../common');
var assert = require('assert'); const assert = require('assert');
var util = require('util'); const util = require('util');
var stream = require('stream'); const stream = require('stream');
var passed = false; let passed = false;
function PassThrough() { function PassThrough() {
stream.Transform.call(this); stream.Transform.call(this);
@ -27,15 +27,15 @@ TestStream.prototype._transform = function(chunk, encoding, done) {
done(); done();
}; };
var s1 = new PassThrough(); const s1 = new PassThrough();
var s2 = new PassThrough(); const s2 = new PassThrough();
var s3 = new TestStream(); const s3 = new TestStream();
s1.pipe(s3); s1.pipe(s3);
// Don't let s2 auto close which may close s3 // Don't let s2 auto close which may close s3
s2.pipe(s3, {end: false}); s2.pipe(s3, {end: false});
// We must write a buffer larger than highWaterMark // We must write a buffer larger than highWaterMark
var big = Buffer.alloc(s1._writableState.highWaterMark + 1, 'x'); const big = Buffer.alloc(s1._writableState.highWaterMark + 1, 'x');
// Since big is larger than highWaterMark, it will be buffered internally. // Since big is larger than highWaterMark, it will be buffered internally.
assert(!s1.write(big)); assert(!s1.write(big));

26
test/parallel/test-stream-big-push.js

@ -1,17 +1,17 @@
'use strict'; 'use strict';
require('../common'); require('../common');
var assert = require('assert'); const assert = require('assert');
var stream = require('stream'); const stream = require('stream');
var str = 'asdfasdfasdfasdfasdf'; const str = 'asdfasdfasdfasdfasdf';
var r = new stream.Readable({ const r = new stream.Readable({
highWaterMark: 5, highWaterMark: 5,
encoding: 'utf8' encoding: 'utf8'
}); });
var reads = 0; let reads = 0;
var eofed = false; let eofed = false;
var ended = false; let ended = false;
r._read = function(n) { r._read = function(n) {
if (reads === 0) { if (reads === 0) {
@ -21,7 +21,7 @@ r._read = function(n) {
reads++; reads++;
} else if (reads === 1) { } else if (reads === 1) {
var ret = r.push(str); var ret = r.push(str);
assert.equal(ret, false); assert.strictEqual(ret, false);
reads++; reads++;
} else { } else {
assert(!eofed); assert(!eofed);
@ -40,9 +40,9 @@ var ret = r.push(str);
// should be false. > hwm // should be false. > hwm
assert(!ret); assert(!ret);
var chunk = r.read(); var chunk = r.read();
assert.equal(chunk, str); assert.strictEqual(chunk, str);
chunk = r.read(); chunk = r.read();
assert.equal(chunk, null); assert.strictEqual(chunk, null);
r.once('readable', function() { r.once('readable', function() {
// this time, we'll get *all* the remaining data, because // this time, we'll get *all* the remaining data, because
@ -50,15 +50,15 @@ r.once('readable', function() {
// us below the hwm, and so it triggered a _read() again, // us below the hwm, and so it triggered a _read() again,
// which synchronously added more, which we then return. // which synchronously added more, which we then return.
chunk = r.read(); chunk = r.read();
assert.equal(chunk, str + str); assert.strictEqual(chunk, str + str);
chunk = r.read(); chunk = r.read();
assert.equal(chunk, null); assert.strictEqual(chunk, null);
}); });
process.on('exit', function() { process.on('exit', function() {
assert(eofed); assert(eofed);
assert(ended); assert(ended);
assert.equal(reads, 2); assert.strictEqual(reads, 2);
console.log('ok'); console.log('ok');
}); });

11
test/parallel/test-stream-duplex.js

@ -1,16 +1,15 @@
'use strict'; 'use strict';
require('../common'); require('../common');
var assert = require('assert'); const assert = require('assert');
const Duplex = require('stream').Transform;
var Duplex = require('stream').Transform; const stream = new Duplex({ objectMode: true });
var stream = new Duplex({ objectMode: true });
assert(stream._readableState.objectMode); assert(stream._readableState.objectMode);
assert(stream._writableState.objectMode); assert(stream._writableState.objectMode);
var written; let written;
var read; let read;
stream._write = function(obj, _, cb) { stream._write = function(obj, _, cb) {
written = obj; written = obj;

8
test/parallel/test-stream-end-paused.js

@ -1,12 +1,12 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
// Make sure we don't miss the end event for paused 0-length streams // Make sure we don't miss the end event for paused 0-length streams
var Readable = require('stream').Readable; const Readable = require('stream').Readable;
var stream = new Readable(); const stream = new Readable();
var calledRead = false; let calledRead = false;
stream._read = function() { stream._read = function() {
assert(!calledRead); assert(!calledRead);
calledRead = true; calledRead = true;

7
test/parallel/test-stream-ispaused.js

@ -1,10 +1,9 @@
'use strict'; 'use strict';
require('../common'); require('../common');
var assert = require('assert'); const assert = require('assert');
const stream = require('stream');
var stream = require('stream'); const readable = new stream.Readable();
var readable = new stream.Readable();
// _read is a noop, here. // _read is a noop, here.
readable._read = Function(); readable._read = Function();

21
test/parallel/test-stream-pipe-after-end.js

@ -1,10 +1,9 @@
'use strict'; 'use strict';
require('../common'); require('../common');
var assert = require('assert'); const assert = require('assert');
const Readable = require('_stream_readable');
var Readable = require('_stream_readable'); const Writable = require('_stream_writable');
var Writable = require('_stream_writable'); const util = require('util');
var util = require('util');
util.inherits(TestReadable, Readable); util.inherits(TestReadable, Readable);
function TestReadable(opt) { function TestReadable(opt) {
@ -35,11 +34,11 @@ TestWritable.prototype._write = function(chunk, encoding, cb) {
}; };
// this one should not emit 'end' until we read() from it later. // this one should not emit 'end' until we read() from it later.
var ender = new TestReadable(); const ender = new TestReadable();
var enderEnded = false; let enderEnded = false;
// what happens when you pipe() a Readable that's already ended? // what happens when you pipe() a Readable that's already ended?
var piper = new TestReadable(); const piper = new TestReadable();
// pushes EOF null, and length=0, so this will trigger 'end' // pushes EOF null, and length=0, so this will trigger 'end'
piper.read(); piper.read();
@ -48,11 +47,11 @@ setTimeout(function() {
enderEnded = true; enderEnded = true;
}); });
assert(!enderEnded); assert(!enderEnded);
var c = ender.read(); const c = ender.read();
assert.equal(c, null); assert.equal(c, null);
var w = new TestWritable(); const w = new TestWritable();
var writableFinished = false; let writableFinished = false;
w.on('finish', function() { w.on('finish', function() {
writableFinished = true; writableFinished = true;
}); });

72
test/parallel/test-stream-pipe-cleanup.js

@ -1,11 +1,10 @@
'use strict'; 'use strict';
// This test asserts that Stream.prototype.pipe does not leave listeners // This test asserts that Stream.prototype.pipe does not leave listeners
// hanging on the source or dest. // hanging on the source or dest.
require('../common'); require('../common');
var stream = require('stream'); const stream = require('stream');
var assert = require('assert'); const assert = require('assert');
var util = require('util'); const util = require('util');
function Writable() { function Writable() {
this.writable = true; this.writable = true;
@ -33,20 +32,20 @@ function Duplex() {
} }
util.inherits(Duplex, Writable); util.inherits(Duplex, Writable);
var i = 0; let i = 0;
var limit = 100; const limit = 100;
var w = new Writable(); let w = new Writable();
var r; let r;
for (i = 0; i < limit; i++) { for (i = 0; i < limit; i++) {
r = new Readable(); r = new Readable();
r.pipe(w); r.pipe(w);
r.emit('end'); r.emit('end');
} }
assert.equal(0, r.listeners('end').length); assert.strictEqual(0, r.listeners('end').length);
assert.equal(limit, w.endCalls); assert.strictEqual(limit, w.endCalls);
w.endCalls = 0; w.endCalls = 0;
@ -55,8 +54,8 @@ for (i = 0; i < limit; i++) {
r.pipe(w); r.pipe(w);
r.emit('close'); r.emit('close');
} }
assert.equal(0, r.listeners('close').length); assert.strictEqual(0, r.listeners('close').length);
assert.equal(limit, w.endCalls); assert.strictEqual(limit, w.endCalls);
w.endCalls = 0; w.endCalls = 0;
@ -67,36 +66,37 @@ for (i = 0; i < limit; i++) {
r.pipe(w); r.pipe(w);
w.emit('close'); w.emit('close');
} }
assert.equal(0, w.listeners('close').length); assert.strictEqual(0, w.listeners('close').length);
r = new Readable(); r = new Readable();
w = new Writable(); w = new Writable();
var d = new Duplex(); const d = new Duplex();
r.pipe(d); // pipeline A r.pipe(d); // pipeline A
d.pipe(w); // pipeline B d.pipe(w); // pipeline B
assert.equal(r.listeners('end').length, 2); // A.onend, A.cleanup assert.strictEqual(r.listeners('end').length, 2); // A.onend, A.cleanup
assert.equal(r.listeners('close').length, 2); // A.onclose, A.cleanup assert.strictEqual(r.listeners('close').length, 2); // A.onclose, A.cleanup
assert.equal(d.listeners('end').length, 2); // B.onend, B.cleanup assert.strictEqual(d.listeners('end').length, 2); // B.onend, B.cleanup
assert.equal(d.listeners('close').length, 3); // A.cleanup, B.onclose, B.cleanup // A.cleanup, B.onclose, B.cleanup
assert.equal(w.listeners('end').length, 0); assert.strictEqual(d.listeners('close').length, 3);
assert.equal(w.listeners('close').length, 1); // B.cleanup assert.strictEqual(w.listeners('end').length, 0);
assert.strictEqual(w.listeners('close').length, 1); // B.cleanup
r.emit('end'); r.emit('end');
assert.equal(d.endCalls, 1); assert.strictEqual(d.endCalls, 1);
assert.equal(w.endCalls, 0); assert.strictEqual(w.endCalls, 0);
assert.equal(r.listeners('end').length, 0); assert.strictEqual(r.listeners('end').length, 0);
assert.equal(r.listeners('close').length, 0); assert.strictEqual(r.listeners('close').length, 0);
assert.equal(d.listeners('end').length, 2); // B.onend, B.cleanup assert.strictEqual(d.listeners('end').length, 2); // B.onend, B.cleanup
assert.equal(d.listeners('close').length, 2); // B.onclose, B.cleanup assert.strictEqual(d.listeners('close').length, 2); // B.onclose, B.cleanup
assert.equal(w.listeners('end').length, 0); assert.strictEqual(w.listeners('end').length, 0);
assert.equal(w.listeners('close').length, 1); // B.cleanup assert.strictEqual(w.listeners('close').length, 1); // B.cleanup
d.emit('end'); d.emit('end');
assert.equal(d.endCalls, 1); assert.strictEqual(d.endCalls, 1);
assert.equal(w.endCalls, 1); assert.strictEqual(w.endCalls, 1);
assert.equal(r.listeners('end').length, 0); assert.strictEqual(r.listeners('end').length, 0);
assert.equal(r.listeners('close').length, 0); assert.strictEqual(r.listeners('close').length, 0);
assert.equal(d.listeners('end').length, 0); assert.strictEqual(d.listeners('end').length, 0);
assert.equal(d.listeners('close').length, 0); assert.strictEqual(d.listeners('close').length, 0);
assert.equal(w.listeners('end').length, 0); assert.strictEqual(w.listeners('end').length, 0);
assert.equal(w.listeners('close').length, 0); assert.strictEqual(w.listeners('close').length, 0);

Loading…
Cancel
Save