Browse Source

test: remove node-tap lookalike

This commit removes the small node-tap lookalike from several
of the streams2 tests. It's only used by six tests, and is
inconsistent with all other tests.

PR-URL: https://github.com/nodejs/node/pull/13707
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
v6
cjihrig 8 years ago
parent
commit
d3c668cead
  1. 150
      test/parallel/test-stream2-basic.js
  2. 176
      test/parallel/test-stream2-objects.js
  3. 69
      test/parallel/test-stream2-readable-from-list.js
  4. 146
      test/parallel/test-stream2-set-encoding.js
  5. 334
      test/parallel/test-stream2-transform.js
  6. 260
      test/parallel/test-stream2-writable.js

150
test/parallel/test-stream2-basic.js

@ -87,42 +87,8 @@ TestWriter.prototype.end = function(c) {
this.emit('end', this.received); this.emit('end', this.received);
}; };
//////// {
// Test basic functionality
// tiny node-tap lookalike.
const tests = [];
let count = 0;
function test(name, fn) {
count++;
tests.push([name, fn]);
}
function run() {
const next = tests.shift();
if (!next)
return console.error('ok');
const name = next[0];
const fn = next[1];
console.log('# %s', name);
fn({
end: function() {
count--;
run();
}
});
}
// ensure all tests have run
process.on('exit', function() {
assert.strictEqual(count, 0);
});
process.nextTick(run);
test('a most basic test', function(t) {
const r = new TestReader(20); const r = new TestReader(20);
const reads = []; const reads = [];
@ -143,10 +109,9 @@ test('a most basic test', function(t) {
'xxxxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxx',
'xxxxxxxxxxxxxxxxxxxxx' ]; 'xxxxxxxxxxxxxxxxxxxxx' ];
r.on('end', function() { r.on('end', common.mustCall(function() {
assert.deepStrictEqual(reads, expect); assert.deepStrictEqual(reads, expect);
t.end(); }));
});
let readSize = 1; let readSize = 1;
function flow() { function flow() {
@ -158,9 +123,10 @@ test('a most basic test', function(t) {
} }
flow(); flow();
}); }
test('pipe', function(t) { {
// Verify pipe
const r = new TestReader(5); const r = new TestReader(5);
const expect = [ 'xxxxx', const expect = [ 'xxxxx',
@ -176,17 +142,16 @@ test('pipe', function(t) {
const w = new TestWriter(); const w = new TestWriter();
w.on('end', function(received) { w.on('end', common.mustCall(function(received) {
assert.deepStrictEqual(received, expect); assert.deepStrictEqual(received, expect);
t.end(); }));
});
r.pipe(w); r.pipe(w);
}); }
[1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(function(SPLIT) { [1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(function(SPLIT) {
test('unpipe', function(t) { // Verify unpipe
const r = new TestReader(5); const r = new TestReader(5);
// unpipe after 3 writes, then write to another stream instead. // unpipe after 3 writes, then write to another stream instead.
@ -217,31 +182,24 @@ test('pipe', function(t) {
let ended = 0; let ended = 0;
let ended0 = false; w[0].on('end', common.mustCall(function(results) {
let ended1 = false;
w[0].on('end', function(results) {
assert.strictEqual(ended0, false);
ended0 = true;
ended++; ended++;
assert.strictEqual(ended, 1);
assert.deepStrictEqual(results, expect[0]); assert.deepStrictEqual(results, expect[0]);
}); }));
w[1].on('end', function(results) { w[1].on('end', common.mustCall(function(results) {
assert.strictEqual(ended1, false);
ended1 = true;
ended++; ended++;
assert.strictEqual(ended, 2); assert.strictEqual(ended, 2);
assert.deepStrictEqual(results, expect[1]); assert.deepStrictEqual(results, expect[1]);
t.end(); }));
});
r.pipe(w[0]); r.pipe(w[0]);
}); });
});
// both writers should get the same exact data. {
test('multipipe', function(t) { // Verify both writers get the same data when piping to destinations
const r = new TestReader(5); const r = new TestReader(5);
const w = [ new TestWriter(), new TestWriter() ]; const w = [ new TestWriter(), new TestWriter() ];
@ -256,23 +214,20 @@ test('multipipe', function(t) {
'xxxxx', 'xxxxx',
'xxxxx' ]; 'xxxxx' ];
let c = 2; w[0].on('end', common.mustCall(function(received) {
w[0].on('end', function(received) {
assert.deepStrictEqual(received, expect, 'first'); assert.deepStrictEqual(received, expect, 'first');
if (--c === 0) t.end(); }));
}); w[1].on('end', common.mustCall(function(received) {
w[1].on('end', function(received) {
assert.deepStrictEqual(received, expect, 'second'); assert.deepStrictEqual(received, expect, 'second');
if (--c === 0) t.end(); }));
});
r.pipe(w[0]); r.pipe(w[0]);
r.pipe(w[1]); r.pipe(w[1]);
}); }
[1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(function(SPLIT) { [1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(function(SPLIT) {
test('multi-unpipe', function(t) { // Verify multi-unpipe
const r = new TestReader(5); const r = new TestReader(5);
// unpipe after 3 writes, then write to another stream instead. // unpipe after 3 writes, then write to another stream instead.
@ -301,24 +256,24 @@ test('multipipe', function(t) {
let ended = 0; let ended = 0;
w[0].on('end', function(results) { w[0].on('end', common.mustCall(function(results) {
ended++; ended++;
assert.strictEqual(ended, 1);
assert.deepStrictEqual(results, expect[0]); assert.deepStrictEqual(results, expect[0]);
}); }));
w[1].on('end', function(results) { w[1].on('end', common.mustCall(function(results) {
ended++; ended++;
assert.strictEqual(ended, 2); assert.strictEqual(ended, 2);
assert.deepStrictEqual(results, expect[1]); assert.deepStrictEqual(results, expect[1]);
t.end(); }));
});
r.pipe(w[0]); r.pipe(w[0]);
r.pipe(w[2]); r.pipe(w[2]);
}); });
});
test('back pressure respected', function(t) { {
// Verify that back pressure is respected
const r = new R({ objectMode: true }); const r = new R({ objectMode: true });
r._read = common.mustNotCall(); r._read = common.mustNotCall();
let counter = 0; let counter = 0;
@ -330,7 +285,6 @@ test('back pressure respected', function(t) {
const w1 = new R(); const w1 = new R();
w1.write = function(chunk) { w1.write = function(chunk) {
console.error('w1.emit("close")');
assert.strictEqual(chunk[0], 'one'); assert.strictEqual(chunk[0], 'one');
w1.emit('close'); w1.emit('close');
process.nextTick(function() { process.nextTick(function() {
@ -346,7 +300,6 @@ test('back pressure respected', function(t) {
const w2 = new R(); const w2 = new R();
w2.write = function(chunk) { w2.write = function(chunk) {
console.error('w2 write', chunk, counter);
assert.strictEqual(chunk[0], expected.shift()); assert.strictEqual(chunk[0], expected.shift());
assert.strictEqual(counter, 0); assert.strictEqual(counter, 0);
@ -358,7 +311,6 @@ test('back pressure respected', function(t) {
setTimeout(function() { setTimeout(function() {
counter--; counter--;
console.error('w2 drain');
w2.emit('drain'); w2.emit('drain');
}, 10); }, 10);
@ -368,7 +320,6 @@ test('back pressure respected', function(t) {
const w3 = new R(); const w3 = new R();
w3.write = function(chunk) { w3.write = function(chunk) {
console.error('w3 write', chunk, counter);
assert.strictEqual(chunk[0], expected.shift()); assert.strictEqual(chunk[0], expected.shift());
assert.strictEqual(counter, 1); assert.strictEqual(counter, 1);
@ -380,20 +331,19 @@ test('back pressure respected', function(t) {
setTimeout(function() { setTimeout(function() {
counter--; counter--;
console.error('w3 drain');
w3.emit('drain'); w3.emit('drain');
}, 50); }, 50);
return false; return false;
}; };
w3.end = function() { w3.end = common.mustCall(function() {
assert.strictEqual(counter, 2); assert.strictEqual(counter, 2);
assert.strictEqual(expected.length, 0); assert.strictEqual(expected.length, 0);
t.end();
};
}); });
}
test('read(0) for ended streams', function(t) { {
// Verify read(0) behavior for ended streams
const r = new R(); const r = new R();
let written = false; let written = false;
let ended = false; let ended = false;
@ -414,16 +364,16 @@ test('read(0) for ended streams', function(t) {
assert.strictEqual(buffer.toString(), 'foo'); assert.strictEqual(buffer.toString(), 'foo');
}; };
w.end = function() { w.end = common.mustCall(function() {
ended = true; ended = true;
assert.strictEqual(written, true); assert.strictEqual(written, true);
t.end(); });
};
r.pipe(w); r.pipe(w);
}); }
test('sync _read ending', function(t) { {
// Verify synchronous _read ending
const r = new R(); const r = new R();
let called = false; let called = false;
r._read = function(n) { r._read = function(n) {
@ -431,6 +381,7 @@ test('sync _read ending', function(t) {
}; };
r.once('end', function() { r.once('end', function() {
// Verify that this is called before the next tick
called = true; called = true;
}); });
@ -438,11 +389,11 @@ test('sync _read ending', function(t) {
process.nextTick(function() { process.nextTick(function() {
assert.strictEqual(called, true); assert.strictEqual(called, true);
t.end();
});
}); });
}
test('adding readable triggers data flow', function(t) { {
// Verify that adding readable listeners trigger data flow
const r = new R({ highWaterMark: 5 }); const r = new R({ highWaterMark: 5 });
let onReadable = false; let onReadable = false;
let readCalled = 0; let readCalled = 0;
@ -459,17 +410,16 @@ test('adding readable triggers data flow', function(t) {
r.read(); r.read();
}); });
r.on('end', function() { r.on('end', common.mustCall(function() {
assert.strictEqual(readCalled, 3); assert.strictEqual(readCalled, 3);
assert.ok(onReadable); assert.ok(onReadable);
t.end(); }));
}); }
});
test('chainable', function(t) { {
// Verify that streams are chainable
const r = new R(); const r = new R();
r._read = common.mustCall(); r._read = common.mustCall();
const r2 = r.setEncoding('utf8').pause().resume().pause(); const r2 = r.setEncoding('utf8').pause().resume().pause();
assert.strictEqual(r, r2); assert.strictEqual(r, r2);
t.end(); }
});

176
test/parallel/test-stream2-objects.js

@ -26,40 +26,6 @@ const Readable = require('_stream_readable');
const Writable = require('_stream_writable'); const Writable = require('_stream_writable');
const assert = require('assert'); const assert = require('assert');
// tiny node-tap lookalike.
const tests = [];
let count = 0;
function test(name, fn) {
count++;
tests.push([name, fn]);
}
function run() {
const next = tests.shift();
if (!next)
return console.error('ok');
const name = next[0];
const fn = next[1];
console.log('# %s', name);
fn({
same: assert.deepStrictEqual,
equal: assert.strictEqual,
end: function() {
count--;
run();
}
});
}
// ensure all tests have run
process.on('exit', function() {
assert.strictEqual(count, 0);
});
process.nextTick(run);
function toArray(callback) { function toArray(callback) {
const stream = new Writable({ objectMode: true }); const stream = new Writable({ objectMode: true });
const list = []; const list = [];
@ -67,9 +33,9 @@ function toArray(callback) {
list.push(chunk); list.push(chunk);
}; };
stream.end = function() { stream.end = common.mustCall(function() {
callback(list); callback(list);
}; });
return stream; return stream;
} }
@ -85,7 +51,8 @@ function fromArray(list) {
return r; return r;
} }
test('can read objects from stream', function(t) { {
// Verify that objects can be read from the stream
const r = fromArray([{ one: '1'}, { two: '2' }]); const r = fromArray([{ one: '1'}, { two: '2' }]);
const v1 = r.read(); const v1 = r.read();
@ -95,34 +62,30 @@ test('can read objects from stream', function(t) {
assert.deepStrictEqual(v1, { one: '1' }); assert.deepStrictEqual(v1, { one: '1' });
assert.deepStrictEqual(v2, { two: '2' }); assert.deepStrictEqual(v2, { two: '2' });
assert.deepStrictEqual(v3, null); assert.deepStrictEqual(v3, null);
}
t.end(); {
}); // Verify that objects can be piped into the stream
test('can pipe objects into stream', function(t) {
const r = fromArray([{ one: '1'}, { two: '2' }]); const r = fromArray([{ one: '1'}, { two: '2' }]);
r.pipe(toArray(function(list) { r.pipe(toArray(common.mustCall(function(list) {
assert.deepStrictEqual(list, [ assert.deepStrictEqual(list, [
{ one: '1' }, { one: '1' },
{ two: '2' } { two: '2' }
]); ]);
})));
}
t.end(); {
})); // Verify that read(n) is ignored
});
test('read(n) is ignored', function(t) {
const r = fromArray([{ one: '1'}, { two: '2' }]); const r = fromArray([{ one: '1'}, { two: '2' }]);
const value = r.read(2); const value = r.read(2);
assert.deepStrictEqual(value, { one: '1' }); assert.deepStrictEqual(value, { one: '1' });
}
t.end(); {
}); // Verify that objects can be synchronously read
test('can read objects from _read (sync)', function(t) {
const r = new Readable({ objectMode: true }); const r = new Readable({ objectMode: true });
const list = [{ one: '1'}, { two: '2' }]; const list = [{ one: '1'}, { two: '2' }];
r._read = function(n) { r._read = function(n) {
@ -130,17 +93,16 @@ test('can read objects from _read (sync)', function(t) {
r.push(item || null); r.push(item || null);
}; };
r.pipe(toArray(function(list) { r.pipe(toArray(common.mustCall(function(list) {
assert.deepStrictEqual(list, [ assert.deepStrictEqual(list, [
{ one: '1' }, { one: '1' },
{ two: '2' } { two: '2' }
]); ]);
})));
}
t.end(); {
})); // Verify that objects can be asynchronously read
});
test('can read objects from _read (async)', function(t) {
const r = new Readable({ objectMode: true }); const r = new Readable({ objectMode: true });
const list = [{ one: '1'}, { two: '2' }]; const list = [{ one: '1'}, { two: '2' }];
r._read = function(n) { r._read = function(n) {
@ -150,17 +112,16 @@ test('can read objects from _read (async)', function(t) {
}); });
}; };
r.pipe(toArray(function(list) { r.pipe(toArray(common.mustCall(function(list) {
assert.deepStrictEqual(list, [ assert.deepStrictEqual(list, [
{ one: '1' }, { one: '1' },
{ two: '2' } { two: '2' }
]); ]);
})));
}
t.end(); {
})); // Verify that strings can be read as objects
});
test('can read strings as objects', function(t) {
const r = new Readable({ const r = new Readable({
objectMode: true objectMode: true
}); });
@ -171,14 +132,13 @@ test('can read strings as objects', function(t) {
}); });
r.push(null); r.push(null);
r.pipe(toArray(function(array) { r.pipe(toArray(common.mustCall(function(array) {
assert.deepStrictEqual(array, list); assert.deepStrictEqual(array, list);
})));
}
t.end(); {
})); // Verify read(0) behavior for object streams
});
test('read(0) for object streams', function(t) {
const r = new Readable({ const r = new Readable({
objectMode: true objectMode: true
}); });
@ -187,14 +147,13 @@ test('read(0) for object streams', function(t) {
r.push('foobar'); r.push('foobar');
r.push(null); r.push(null);
r.pipe(toArray(function(array) { r.pipe(toArray(common.mustCall(function(array) {
assert.deepStrictEqual(array, ['foobar']); assert.deepStrictEqual(array, ['foobar']);
})));
}
t.end(); {
})); // Verify the behavior of pushing falsey values
});
test('falsey values', function(t) {
const r = new Readable({ const r = new Readable({
objectMode: true objectMode: true
}); });
@ -205,14 +164,13 @@ test('falsey values', function(t) {
r.push(''); r.push('');
r.push(null); r.push(null);
r.pipe(toArray(function(array) { r.pipe(toArray(common.mustCall(function(array) {
assert.deepStrictEqual(array, [false, 0, '']); assert.deepStrictEqual(array, [false, 0, '']);
})));
}
t.end(); {
})); // Verify high watermark _read() behavior
});
test('high watermark _read', function(t) {
const r = new Readable({ const r = new Readable({
highWaterMark: 6, highWaterMark: 6,
objectMode: true objectMode: true
@ -240,11 +198,10 @@ test('high watermark _read', function(t) {
assert.strictEqual(v3, '3'); assert.strictEqual(v3, '3');
assert.strictEqual(calls, 1); assert.strictEqual(calls, 1);
}
t.end(); {
}); // Verify high watermark push behavior
test('high watermark push', function(t) {
const r = new Readable({ const r = new Readable({
highWaterMark: 6, highWaterMark: 6,
objectMode: true objectMode: true
@ -254,11 +211,10 @@ test('high watermark push', function(t) {
const bool = r.push(i); const bool = r.push(i);
assert.strictEqual(bool, i !== 5); assert.strictEqual(bool, i !== 5);
} }
}
t.end(); {
}); // Verify that objects can be written to stream
test('can write objects to stream', function(t) {
const w = new Writable({ objectMode: true }); const w = new Writable({ objectMode: true });
w._write = function(chunk, encoding, cb) { w._write = function(chunk, encoding, cb) {
@ -266,15 +222,13 @@ test('can write objects to stream', function(t) {
cb(); cb();
}; };
w.on('finish', function() { w.on('finish', common.mustCall());
t.end();
});
w.write({ foo: 'bar' }); w.write({ foo: 'bar' });
w.end(); w.end();
}); }
test('can write multiple objects to stream', function(t) { {
// Verify that multiple objects can be written to stream
const w = new Writable({ objectMode: true }); const w = new Writable({ objectMode: true });
const list = []; const list = [];
@ -283,11 +237,9 @@ test('can write multiple objects to stream', function(t) {
cb(); cb();
}; };
w.on('finish', function() { w.on('finish', common.mustCall(function() {
assert.deepStrictEqual(list, [0, 1, 2, 3, 4]); assert.deepStrictEqual(list, [0, 1, 2, 3, 4]);
}));
t.end();
});
w.write(0); w.write(0);
w.write(1); w.write(1);
@ -295,9 +247,10 @@ test('can write multiple objects to stream', function(t) {
w.write(3); w.write(3);
w.write(4); w.write(4);
w.end(); w.end();
}); }
test('can write strings as objects', function(t) { {
// Verify that strings can be written as objects
const w = new Writable({ const w = new Writable({
objectMode: true objectMode: true
}); });
@ -308,11 +261,9 @@ test('can write strings as objects', function(t) {
process.nextTick(cb); process.nextTick(cb);
}; };
w.on('finish', function() { w.on('finish', common.mustCall(function() {
assert.deepStrictEqual(list, ['0', '1', '2', '3', '4']); assert.deepStrictEqual(list, ['0', '1', '2', '3', '4']);
}));
t.end();
});
w.write('0'); w.write('0');
w.write('1'); w.write('1');
@ -320,9 +271,10 @@ test('can write strings as objects', function(t) {
w.write('3'); w.write('3');
w.write('4'); w.write('4');
w.end(); w.end();
}); }
test('buffers finish until cb is called', function(t) { {
// Verify that stream buffers finish until callback is called
const w = new Writable({ const w = new Writable({
objectMode: true objectMode: true
}); });
@ -337,12 +289,10 @@ test('buffers finish until cb is called', function(t) {
}); });
}; };
w.on('finish', function() { w.on('finish', common.mustCall(function() {
assert.strictEqual(called, true); assert.strictEqual(called, true);
}));
t.end();
});
w.write('foo'); w.write('foo');
w.end(); w.end();
}); }

69
test/parallel/test-stream2-readable-from-list.js

@ -26,33 +26,6 @@ const assert = require('assert');
const fromList = require('_stream_readable')._fromList; const fromList = require('_stream_readable')._fromList;
const BufferList = require('internal/streams/BufferList'); const BufferList = require('internal/streams/BufferList');
// tiny node-tap lookalike.
const tests = [];
let count = 0;
function test(name, fn) {
count++;
tests.push([name, fn]);
}
function run() {
const next = tests.shift();
if (!next)
return console.error('ok');
const name = next[0];
const fn = next[1];
console.log('# %s', name);
fn({
same: assert.deepStrictEqual,
equal: assert.strictEqual,
end: function() {
count--;
run();
}
});
}
function bufferListFromArray(arr) { function bufferListFromArray(arr) {
const bl = new BufferList(); const bl = new BufferList();
for (let i = 0; i < arr.length; ++i) for (let i = 0; i < arr.length; ++i)
@ -60,15 +33,8 @@ function bufferListFromArray(arr) {
return bl; return bl;
} }
// ensure all tests have run {
process.on('exit', function() { // Verify behavior with buffers
assert.strictEqual(count, 0);
});
process.nextTick(run);
test('buffers', function(t) {
let list = [ Buffer.from('foog'), let list = [ Buffer.from('foog'),
Buffer.from('bark'), Buffer.from('bark'),
Buffer.from('bazy'), Buffer.from('bazy'),
@ -77,27 +43,26 @@ test('buffers', function(t) {
// read more than the first element. // read more than the first element.
let ret = fromList(6, { buffer: list, length: 16 }); let ret = fromList(6, { buffer: list, length: 16 });
t.equal(ret.toString(), 'foogba'); assert.strictEqual(ret.toString(), 'foogba');
// read exactly the first element. // read exactly the first element.
ret = fromList(2, { buffer: list, length: 10 }); ret = fromList(2, { buffer: list, length: 10 });
t.equal(ret.toString(), 'rk'); assert.strictEqual(ret.toString(), 'rk');
// read less than the first element. // read less than the first element.
ret = fromList(2, { buffer: list, length: 8 }); ret = fromList(2, { buffer: list, length: 8 });
t.equal(ret.toString(), 'ba'); assert.strictEqual(ret.toString(), 'ba');
// read more than we have. // read more than we have.
ret = fromList(100, { buffer: list, length: 6 }); ret = fromList(100, { buffer: list, length: 6 });
t.equal(ret.toString(), 'zykuel'); assert.strictEqual(ret.toString(), 'zykuel');
// all consumed. // all consumed.
t.same(list, new BufferList()); assert.deepStrictEqual(list, new BufferList());
}
t.end();
});
test('strings', function(t) { {
// Verify behavior with strings
let list = [ 'foog', let list = [ 'foog',
'bark', 'bark',
'bazy', 'bazy',
@ -106,22 +71,20 @@ test('strings', function(t) {
// read more than the first element. // read more than the first element.
let ret = fromList(6, { buffer: list, length: 16, decoder: true }); let ret = fromList(6, { buffer: list, length: 16, decoder: true });
t.equal(ret, 'foogba'); assert.strictEqual(ret, 'foogba');
// read exactly the first element. // read exactly the first element.
ret = fromList(2, { buffer: list, length: 10, decoder: true }); ret = fromList(2, { buffer: list, length: 10, decoder: true });
t.equal(ret, 'rk'); assert.strictEqual(ret, 'rk');
// read less than the first element. // read less than the first element.
ret = fromList(2, { buffer: list, length: 8, decoder: true }); ret = fromList(2, { buffer: list, length: 8, decoder: true });
t.equal(ret, 'ba'); assert.strictEqual(ret, 'ba');
// read more than we have. // read more than we have.
ret = fromList(100, { buffer: list, length: 6, decoder: true }); ret = fromList(100, { buffer: list, length: 6, decoder: true });
t.equal(ret, 'zykuel'); assert.strictEqual(ret, 'zykuel');
// all consumed. // all consumed.
t.same(list, new BufferList()); assert.deepStrictEqual(list, new BufferList());
}
t.end();
});

146
test/parallel/test-stream2-set-encoding.js

@ -20,47 +20,11 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const R = require('_stream_readable'); const R = require('_stream_readable');
const util = require('util'); const util = require('util');
// tiny node-tap lookalike.
const tests = [];
let count = 0;
function test(name, fn) {
count++;
tests.push([name, fn]);
}
function run() {
const next = tests.shift();
if (!next)
return console.error('ok');
const name = next[0];
const fn = next[1];
console.log('# %s', name);
fn({
same: assert.deepStrictEqual,
equal: assert.strictEqual,
end: function() {
count--;
run();
}
});
}
// ensure all tests have run
process.on('exit', function() {
assert.strictEqual(count, 0);
});
process.nextTick(run);
/////
util.inherits(TestReader, R); util.inherits(TestReader, R);
function TestReader(n, opts) { function TestReader(n, opts) {
@ -89,13 +53,12 @@ TestReader.prototype._read = function(n) {
this.pos += n; this.pos += n;
const ret = Buffer.alloc(n, 'a'); const ret = Buffer.alloc(n, 'a');
console.log('this.push(ret)', ret);
return this.push(ret); return this.push(ret);
}.bind(this), 1); }.bind(this), 1);
}; };
test('setEncoding utf8', function(t) { {
// Verify utf8 encoding
const tr = new TestReader(100); const tr = new TestReader(100);
tr.setEncoding('utf8'); tr.setEncoding('utf8');
const out = []; const out = [];
@ -117,14 +80,14 @@ test('setEncoding utf8', function(t) {
out.push(chunk); out.push(chunk);
}); });
tr.on('end', function() { tr.on('end', common.mustCall(function() {
t.same(out, expect); assert.deepStrictEqual(out, expect);
t.end(); }));
}); }
});
test('setEncoding hex', function(t) { {
// Verify hex encoding
const tr = new TestReader(100); const tr = new TestReader(100);
tr.setEncoding('hex'); tr.setEncoding('hex');
const out = []; const out = [];
@ -156,13 +119,13 @@ test('setEncoding hex', function(t) {
out.push(chunk); out.push(chunk);
}); });
tr.on('end', function() { tr.on('end', common.mustCall(function() {
t.same(out, expect); assert.deepStrictEqual(out, expect);
t.end(); }));
}); }
});
test('setEncoding hex with read(13)', function(t) { {
// Verify hex encoding with read(13)
const tr = new TestReader(100); const tr = new TestReader(100);
tr.setEncoding('hex'); tr.setEncoding('hex');
const out = []; const out = [];
@ -185,20 +148,18 @@ test('setEncoding hex with read(13)', function(t) {
'16161' ]; '16161' ];
tr.on('readable', function flow() { tr.on('readable', function flow() {
console.log('readable once');
let chunk; let chunk;
while (null !== (chunk = tr.read(13))) while (null !== (chunk = tr.read(13)))
out.push(chunk); out.push(chunk);
}); });
tr.on('end', function() { tr.on('end', common.mustCall(function() {
console.log('END'); assert.deepStrictEqual(out, expect);
t.same(out, expect); }));
t.end(); }
});
});
test('setEncoding base64', function(t) { {
// Verify base64 encoding
const tr = new TestReader(100); const tr = new TestReader(100);
tr.setEncoding('base64'); tr.setEncoding('base64');
const out = []; const out = [];
@ -224,13 +185,13 @@ test('setEncoding base64', function(t) {
out.push(chunk); out.push(chunk);
}); });
tr.on('end', function() { tr.on('end', common.mustCall(function() {
t.same(out, expect); assert.deepStrictEqual(out, expect);
t.end(); }));
}); }
});
test('encoding: utf8', function(t) { {
// Verify utf8 encoding
const tr = new TestReader(100, { encoding: 'utf8' }); const tr = new TestReader(100, { encoding: 'utf8' });
const out = []; const out = [];
const expect = const expect =
@ -251,14 +212,14 @@ test('encoding: utf8', function(t) {
out.push(chunk); out.push(chunk);
}); });
tr.on('end', function() { tr.on('end', common.mustCall(function() {
t.same(out, expect); assert.deepStrictEqual(out, expect);
t.end(); }));
}); }
});
test('encoding: hex', function(t) { {
// Verify hex encoding
const tr = new TestReader(100, { encoding: 'hex' }); const tr = new TestReader(100, { encoding: 'hex' });
const out = []; const out = [];
const expect = const expect =
@ -289,13 +250,13 @@ test('encoding: hex', function(t) {
out.push(chunk); out.push(chunk);
}); });
tr.on('end', function() { tr.on('end', common.mustCall(function() {
t.same(out, expect); assert.deepStrictEqual(out, expect);
t.end(); }));
}); }
});
test('encoding: hex with read(13)', function(t) { {
// Verify hex encoding with read(13)
const tr = new TestReader(100, { encoding: 'hex' }); const tr = new TestReader(100, { encoding: 'hex' });
const out = []; const out = [];
const expect = const expect =
@ -322,13 +283,13 @@ test('encoding: hex with read(13)', function(t) {
out.push(chunk); out.push(chunk);
}); });
tr.on('end', function() { tr.on('end', common.mustCall(function() {
t.same(out, expect); assert.deepStrictEqual(out, expect);
t.end(); }));
}); }
});
test('encoding: base64', function(t) { {
// Verify base64 encoding
const tr = new TestReader(100, { encoding: 'base64' }); const tr = new TestReader(100, { encoding: 'base64' });
const out = []; const out = [];
const expect = const expect =
@ -353,14 +314,13 @@ test('encoding: base64', function(t) {
out.push(chunk); out.push(chunk);
}); });
tr.on('end', function() { tr.on('end', common.mustCall(function() {
t.same(out, expect); assert.deepStrictEqual(out, expect);
t.end(); }));
}); }
});
test('chainable', function(t) { {
// Verify chaining behavior
const tr = new TestReader(100); const tr = new TestReader(100);
t.equal(tr.setEncoding('utf8'), tr); assert.deepStrictEqual(tr.setEncoding('utf8'), tr);
t.end(); }
});

334
test/parallel/test-stream2-transform.js

@ -20,49 +20,13 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const PassThrough = require('_stream_passthrough'); const PassThrough = require('_stream_passthrough');
const Transform = require('_stream_transform'); const Transform = require('_stream_transform');
// tiny node-tap lookalike. {
const tests = []; // Verify writable side consumption
let count = 0;
function test(name, fn) {
count++;
tests.push([name, fn]);
}
function run() {
const next = tests.shift();
if (!next)
return console.error('ok');
const name = next[0];
const fn = next[1];
console.log('# %s', name);
fn({
same: assert.deepStrictEqual,
equal: assert.strictEqual,
ok: assert,
end: function() {
count--;
run();
}
});
}
// ensure all tests have run
process.on('exit', function() {
assert.strictEqual(count, 0);
});
process.nextTick(run);
/////
test('writable side consumption', function(t) {
const tx = new Transform({ const tx = new Transform({
highWaterMark: 10 highWaterMark: 10
}); });
@ -79,17 +43,16 @@ test('writable side consumption', function(t) {
} }
tx.end(); tx.end();
t.equal(tx._readableState.length, 10); assert.strictEqual(tx._readableState.length, 10);
t.equal(transformed, 10); assert.strictEqual(transformed, 10);
t.equal(tx._transformState.writechunk.length, 5); assert.strictEqual(tx._transformState.writechunk.length, 5);
t.same(tx._writableState.getBuffer().map(function(c) { assert.deepStrictEqual(tx._writableState.getBuffer().map(function(c) {
return c.chunk.length; return c.chunk.length;
}), [6, 7, 8, 9, 10]); }), [6, 7, 8, 9, 10]);
}
t.end(); {
}); // Verify passthrough behavior
test('passthrough', function(t) {
const pt = new PassThrough(); const pt = new PassThrough();
pt.write(Buffer.from('foog')); pt.write(Buffer.from('foog'));
@ -98,14 +61,14 @@ test('passthrough', function(t) {
pt.write(Buffer.from('kuel')); pt.write(Buffer.from('kuel'));
pt.end(); pt.end();
t.equal(pt.read(5).toString(), 'foogb'); assert.strictEqual(pt.read(5).toString(), 'foogb');
t.equal(pt.read(5).toString(), 'arkba'); assert.strictEqual(pt.read(5).toString(), 'arkba');
t.equal(pt.read(5).toString(), 'zykue'); assert.strictEqual(pt.read(5).toString(), 'zykue');
t.equal(pt.read(5).toString(), 'l'); assert.strictEqual(pt.read(5).toString(), 'l');
t.end(); }
});
test('object passthrough', function(t) { {
// Verify object passthrough behavior
const pt = new PassThrough({ objectMode: true }); const pt = new PassThrough({ objectMode: true });
pt.write(1); pt.write(1);
@ -117,25 +80,24 @@ test('object passthrough', function(t) {
pt.write({ a: 'b'}); pt.write({ a: 'b'});
pt.end(); pt.end();
t.equal(pt.read(), 1); assert.strictEqual(pt.read(), 1);
t.equal(pt.read(), true); assert.strictEqual(pt.read(), true);
t.equal(pt.read(), false); assert.strictEqual(pt.read(), false);
t.equal(pt.read(), 0); assert.strictEqual(pt.read(), 0);
t.equal(pt.read(), 'foo'); assert.strictEqual(pt.read(), 'foo');
t.equal(pt.read(), ''); assert.strictEqual(pt.read(), '');
t.same(pt.read(), { a: 'b'}); assert.deepStrictEqual(pt.read(), { a: 'b'});
t.end(); }
});
test('passthrough constructor', function(t) { {
// Verify passthrough constructor behavior
const pt = PassThrough(); const pt = PassThrough();
assert(pt instanceof PassThrough); assert(pt instanceof PassThrough);
}
t.end(); {
}); // Perform a simple transform
test('simple transform', function(t) {
const pt = new Transform(); const pt = new Transform();
pt._transform = function(c, e, cb) { pt._transform = function(c, e, cb) {
const ret = Buffer.alloc(c.length, 'x'); const ret = Buffer.alloc(c.length, 'x');
@ -149,14 +111,14 @@ test('simple transform', function(t) {
pt.write(Buffer.from('kuel')); pt.write(Buffer.from('kuel'));
pt.end(); pt.end();
t.equal(pt.read(5).toString(), 'xxxxx'); assert.strictEqual(pt.read(5).toString(), 'xxxxx');
t.equal(pt.read(5).toString(), 'xxxxx'); assert.strictEqual(pt.read(5).toString(), 'xxxxx');
t.equal(pt.read(5).toString(), 'xxxxx'); assert.strictEqual(pt.read(5).toString(), 'xxxxx');
t.equal(pt.read(5).toString(), 'x'); assert.strictEqual(pt.read(5).toString(), 'x');
t.end(); }
});
test('simple object transform', function(t) { {
// Verify simple object transform
const pt = new Transform({ objectMode: true }); const pt = new Transform({ objectMode: true });
pt._transform = function(c, e, cb) { pt._transform = function(c, e, cb) {
pt.push(JSON.stringify(c)); pt.push(JSON.stringify(c));
@ -172,17 +134,17 @@ test('simple object transform', function(t) {
pt.write({ a: 'b'}); pt.write({ a: 'b'});
pt.end(); pt.end();
t.equal(pt.read(), '1'); assert.strictEqual(pt.read(), '1');
t.equal(pt.read(), 'true'); assert.strictEqual(pt.read(), 'true');
t.equal(pt.read(), 'false'); assert.strictEqual(pt.read(), 'false');
t.equal(pt.read(), '0'); assert.strictEqual(pt.read(), '0');
t.equal(pt.read(), '"foo"'); assert.strictEqual(pt.read(), '"foo"');
t.equal(pt.read(), '""'); assert.strictEqual(pt.read(), '""');
t.equal(pt.read(), '{"a":"b"}'); assert.strictEqual(pt.read(), '{"a":"b"}');
t.end(); }
});
test('async passthrough', function(t) { {
// Verify async passthrough
const pt = new Transform(); const pt = new Transform();
pt._transform = function(chunk, encoding, cb) { pt._transform = function(chunk, encoding, cb) {
setTimeout(function() { setTimeout(function() {
@ -197,16 +159,16 @@ test('async passthrough', function(t) {
pt.write(Buffer.from('kuel')); pt.write(Buffer.from('kuel'));
pt.end(); pt.end();
pt.on('finish', function() { pt.on('finish', common.mustCall(function() {
t.equal(pt.read(5).toString(), 'foogb'); assert.strictEqual(pt.read(5).toString(), 'foogb');
t.equal(pt.read(5).toString(), 'arkba'); assert.strictEqual(pt.read(5).toString(), 'arkba');
t.equal(pt.read(5).toString(), 'zykue'); assert.strictEqual(pt.read(5).toString(), 'zykue');
t.equal(pt.read(5).toString(), 'l'); assert.strictEqual(pt.read(5).toString(), 'l');
t.end(); }));
}); }
});
test('assymetric transform (expand)', function(t) { {
// Verify assymetric transform (expand)
const pt = new Transform(); const pt = new Transform();
// emit each chunk 2 times. // emit each chunk 2 times.
@ -226,19 +188,19 @@ test('assymetric transform (expand)', function(t) {
pt.write(Buffer.from('kuel')); pt.write(Buffer.from('kuel'));
pt.end(); pt.end();
pt.on('finish', function() { pt.on('finish', common.mustCall(function() {
t.equal(pt.read(5).toString(), 'foogf'); assert.strictEqual(pt.read(5).toString(), 'foogf');
t.equal(pt.read(5).toString(), 'oogba'); assert.strictEqual(pt.read(5).toString(), 'oogba');
t.equal(pt.read(5).toString(), 'rkbar'); assert.strictEqual(pt.read(5).toString(), 'rkbar');
t.equal(pt.read(5).toString(), 'kbazy'); assert.strictEqual(pt.read(5).toString(), 'kbazy');
t.equal(pt.read(5).toString(), 'bazyk'); assert.strictEqual(pt.read(5).toString(), 'bazyk');
t.equal(pt.read(5).toString(), 'uelku'); assert.strictEqual(pt.read(5).toString(), 'uelku');
t.equal(pt.read(5).toString(), 'el'); assert.strictEqual(pt.read(5).toString(), 'el');
t.end(); }));
}); }
});
test('assymetric transform (compress)', function(t) { {
// Verify assymetric trasform (compress)
const pt = new Transform(); const pt = new Transform();
// each output is the first char of 3 consecutive chunks, // each output is the first char of 3 consecutive chunks,
@ -283,17 +245,17 @@ test('assymetric transform (compress)', function(t) {
pt.end(); pt.end();
// 'abcdeabcdeabcd' // 'abcdeabcdeabcd'
pt.on('finish', function() { pt.on('finish', common.mustCall(function() {
t.equal(pt.read(5).toString(), 'abcde'); assert.strictEqual(pt.read(5).toString(), 'abcde');
t.equal(pt.read(5).toString(), 'abcde'); assert.strictEqual(pt.read(5).toString(), 'abcde');
t.equal(pt.read(5).toString(), 'abcd'); assert.strictEqual(pt.read(5).toString(), 'abcd');
t.end(); }));
}); }
});
// this tests for a stall when data is written to a full stream // this tests for a stall when data is written to a full stream
// that has empty transforms. // that has empty transforms.
test('complex transform', function(t) { {
// Verify compex transform behavior
let count = 0; let count = 0;
let saved = null; let saved = null;
const pt = new Transform({highWaterMark: 3}); const pt = new Transform({highWaterMark: 3});
@ -314,118 +276,96 @@ test('complex transform', function(t) {
pt.once('readable', function() { pt.once('readable', function() {
process.nextTick(function() { process.nextTick(function() {
pt.write(Buffer.from('d')); pt.write(Buffer.from('d'));
pt.write(Buffer.from('ef'), function() { pt.write(Buffer.from('ef'), common.mustCall(function() {
pt.end(); pt.end();
t.end(); }));
}); assert.strictEqual(pt.read().toString(), 'abcdef');
t.equal(pt.read().toString(), 'abcdef'); assert.strictEqual(pt.read(), null);
t.equal(pt.read(), null);
}); });
}); });
pt.write(Buffer.from('abc')); pt.write(Buffer.from('abc'));
}); }
test('passthrough event emission', function(t) { {
// Verify passthrough event emission
const pt = new PassThrough(); const pt = new PassThrough();
let emits = 0; let emits = 0;
pt.on('readable', function() { pt.on('readable', function() {
console.error('>>> emit readable %d', emits);
emits++; emits++;
}); });
pt.write(Buffer.from('foog')); pt.write(Buffer.from('foog'));
console.error('need emit 0');
pt.write(Buffer.from('bark')); pt.write(Buffer.from('bark'));
console.error('should have emitted readable now 1 === %d', emits); assert.strictEqual(emits, 1);
t.equal(emits, 1); assert.strictEqual(pt.read(5).toString(), 'foogb');
assert.strictEqual(String(pt.read(5)), 'null');
t.equal(pt.read(5).toString(), 'foogb');
t.equal(String(pt.read(5)), 'null');
console.error('need emit 1');
pt.write(Buffer.from('bazy')); pt.write(Buffer.from('bazy'));
console.error('should have emitted, but not again');
pt.write(Buffer.from('kuel')); pt.write(Buffer.from('kuel'));
console.error('should have emitted readable now 2 === %d', emits); assert.strictEqual(emits, 2);
t.equal(emits, 2); assert.strictEqual(pt.read(5).toString(), 'arkba');
assert.strictEqual(pt.read(5).toString(), 'zykue');
t.equal(pt.read(5).toString(), 'arkba'); assert.strictEqual(pt.read(5), null);
t.equal(pt.read(5).toString(), 'zykue');
t.equal(pt.read(5), null);
console.error('need emit 2');
pt.end(); pt.end();
t.equal(emits, 3); assert.strictEqual(emits, 3);
assert.strictEqual(pt.read(5).toString(), 'l');
assert.strictEqual(pt.read(5), null);
t.equal(pt.read(5).toString(), 'l'); assert.strictEqual(emits, 3);
t.equal(pt.read(5), null); }
console.error('should not have emitted again');
t.equal(emits, 3);
t.end();
});
test('passthrough event emission reordered', function(t) { {
// Verify passthrough event emission reordering
const pt = new PassThrough(); const pt = new PassThrough();
let emits = 0; let emits = 0;
pt.on('readable', function() { pt.on('readable', function() {
console.error('emit readable', emits);
emits++; emits++;
}); });
pt.write(Buffer.from('foog')); pt.write(Buffer.from('foog'));
console.error('need emit 0');
pt.write(Buffer.from('bark')); pt.write(Buffer.from('bark'));
console.error('should have emitted readable now 1 === %d', emits);
t.equal(emits, 1);
t.equal(pt.read(5).toString(), 'foogb');
t.equal(pt.read(5), null);
console.error('need emit 1'); assert.strictEqual(emits, 1);
pt.once('readable', function() { assert.strictEqual(pt.read(5).toString(), 'foogb');
t.equal(pt.read(5).toString(), 'arkba'); assert.strictEqual(pt.read(5), null);
t.equal(pt.read(5), null); pt.once('readable', common.mustCall(function() {
assert.strictEqual(pt.read(5).toString(), 'arkba');
console.error('need emit 2'); assert.strictEqual(pt.read(5), null);
pt.once('readable', function() {
t.equal(pt.read(5).toString(), 'zykue'); pt.once('readable', common.mustCall(function() {
t.equal(pt.read(5), null); assert.strictEqual(pt.read(5).toString(), 'zykue');
pt.once('readable', function() { assert.strictEqual(pt.read(5), null);
t.equal(pt.read(5).toString(), 'l'); pt.once('readable', common.mustCall(function() {
t.equal(pt.read(5), null); assert.strictEqual(pt.read(5).toString(), 'l');
t.equal(emits, 4); assert.strictEqual(pt.read(5), null);
t.end(); assert.strictEqual(emits, 4);
}); }));
pt.end(); pt.end();
}); }));
pt.write(Buffer.from('kuel')); pt.write(Buffer.from('kuel'));
}); }));
pt.write(Buffer.from('bazy')); pt.write(Buffer.from('bazy'));
}); }
test('passthrough facaded', function(t) { {
console.error('passthrough facaded'); // Verify passthrough facade
const pt = new PassThrough(); const pt = new PassThrough();
const datas = []; const datas = [];
pt.on('data', function(chunk) { pt.on('data', function(chunk) {
datas.push(chunk.toString()); datas.push(chunk.toString());
}); });
pt.on('end', function() { pt.on('end', common.mustCall(function() {
t.same(datas, ['foog', 'bark', 'bazy', 'kuel']); assert.deepStrictEqual(datas, ['foog', 'bark', 'bazy', 'kuel']);
t.end(); }));
});
pt.write(Buffer.from('foog')); pt.write(Buffer.from('foog'));
setTimeout(function() { setTimeout(function() {
@ -440,10 +380,10 @@ test('passthrough facaded', function(t) {
}, 10); }, 10);
}, 10); }, 10);
}, 10); }, 10);
}); }
test('object transform (json parse)', function(t) { {
console.error('json parse stream'); // Verify object transform (JSON parse)
const jp = new Transform({ objectMode: true }); const jp = new Transform({ objectMode: true });
jp._transform = function(data, encoding, cb) { jp._transform = function(data, encoding, cb) {
try { try {
@ -471,21 +411,20 @@ test('object transform (json parse)', function(t) {
objects.forEach(function(obj) { objects.forEach(function(obj) {
jp.write(JSON.stringify(obj)); jp.write(JSON.stringify(obj));
const res = jp.read(); const res = jp.read();
t.same(res, obj); assert.deepStrictEqual(res, obj);
}); });
jp.end(); jp.end();
// read one more time to get the 'end' event // read one more time to get the 'end' event
jp.read(); jp.read();
process.nextTick(function() { process.nextTick(common.mustCall(function() {
t.ok(ended); assert.strictEqual(ended, true);
t.end(); }));
}); }
});
test('object transform (json stringify)', function(t) { {
console.error('json parse stream'); // Verify object transform (JSON stringify)
const js = new Transform({ objectMode: true }); const js = new Transform({ objectMode: true });
js._transform = function(data, encoding, cb) { js._transform = function(data, encoding, cb) {
try { try {
@ -513,15 +452,14 @@ test('object transform (json stringify)', function(t) {
objects.forEach(function(obj) { objects.forEach(function(obj) {
js.write(obj); js.write(obj);
const res = js.read(); const res = js.read();
t.equal(res, JSON.stringify(obj)); assert.strictEqual(res, JSON.stringify(obj));
}); });
js.end(); js.end();
// read one more time to get the 'end' event // read one more time to get the 'end' event
js.read(); js.read();
process.nextTick(function() { process.nextTick(common.mustCall(function() {
t.ok(ended); assert.strictEqual(ended, true);
t.end(); }));
}); }
});

260
test/parallel/test-stream2-writable.js

@ -49,66 +49,32 @@ for (let i = 0; i < chunks.length; i++) {
chunks[i] = 'x'.repeat(i); chunks[i] = 'x'.repeat(i);
} }
// tiny node-tap lookalike. {
const tests = []; // Verify fast writing
let count = 0;
function test(name, fn) {
count++;
tests.push([name, fn]);
}
function run() {
const next = tests.shift();
if (!next)
return console.error('ok');
const name = next[0];
const fn = next[1];
console.log('# %s', name);
fn({
same: assert.deepStrictEqual,
equal: assert.strictEqual,
end: function() {
count--;
run();
}
});
}
// ensure all tests have run
process.on('exit', function() {
assert.strictEqual(count, 0);
});
process.nextTick(run);
test('write fast', function(t) {
const tw = new TestWriter({ const tw = new TestWriter({
highWaterMark: 100 highWaterMark: 100
}); });
tw.on('finish', function() { tw.on('finish', common.mustCall(function() {
t.same(tw.buffer, chunks, 'got chunks in the right order'); assert.deepStrictEqual(tw.buffer, chunks, 'got chunks in the right order');
t.end(); }));
});
chunks.forEach(function(chunk) { chunks.forEach(function(chunk) {
// screw backpressure. Just buffer it all up. // Ignore backpressure. Just buffer it all up.
tw.write(chunk); tw.write(chunk);
}); });
tw.end(); tw.end();
}); }
test('write slow', function(t) { {
// Verify slow writing
const tw = new TestWriter({ const tw = new TestWriter({
highWaterMark: 100 highWaterMark: 100
}); });
tw.on('finish', function() { tw.on('finish', common.mustCall(function() {
t.same(tw.buffer, chunks, 'got chunks in the right order'); assert.deepStrictEqual(tw.buffer, chunks, 'got chunks in the right order');
t.end(); }));
});
let i = 0; let i = 0;
(function W() { (function W() {
@ -118,20 +84,20 @@ test('write slow', function(t) {
else else
tw.end(); tw.end();
})(); })();
}); }
test('write backpressure', function(t) { {
// Verify write backpressure
const tw = new TestWriter({ const tw = new TestWriter({
highWaterMark: 50 highWaterMark: 50
}); });
let drains = 0; let drains = 0;
tw.on('finish', function() { tw.on('finish', common.mustCall(function() {
t.same(tw.buffer, chunks, 'got chunks in the right order'); assert.deepStrictEqual(tw.buffer, chunks, 'got chunks in the right order');
t.equal(drains, 17); assert.strictEqual(drains, 17);
t.end(); }));
});
tw.on('drain', function() { tw.on('drain', function() {
drains++; drains++;
@ -151,9 +117,10 @@ test('write backpressure', function(t) {
tw.end(); tw.end();
} }
})(); })();
}); }
test('write bufferize', function(t) { {
// Verify write buffersize
const tw = new TestWriter({ const tw = new TestWriter({
highWaterMark: 100 highWaterMark: 100
}); });
@ -173,7 +140,7 @@ test('write bufferize', function(t) {
undefined ]; undefined ];
tw.on('finish', function() { tw.on('finish', function() {
t.same(tw.buffer, chunks, 'got the expected chunks'); assert.deepStrictEqual(tw.buffer, chunks, 'got the expected chunks');
}); });
chunks.forEach(function(chunk, i) { chunks.forEach(function(chunk, i) {
@ -181,10 +148,10 @@ test('write bufferize', function(t) {
chunk = Buffer.from(chunk); chunk = Buffer.from(chunk);
tw.write(chunk.toString(enc), enc); tw.write(chunk.toString(enc), enc);
}); });
t.end(); }
});
test('write no bufferize', function(t) { {
// Verify write with no buffersize
const tw = new TestWriter({ const tw = new TestWriter({
highWaterMark: 100, highWaterMark: 100,
decodeStrings: false decodeStrings: false
@ -211,7 +178,7 @@ test('write no bufferize', function(t) {
undefined ]; undefined ];
tw.on('finish', function() { tw.on('finish', function() {
t.same(tw.buffer, chunks, 'got the expected chunks'); assert.deepStrictEqual(tw.buffer, chunks, 'got the expected chunks');
}); });
chunks.forEach(function(chunk, i) { chunks.forEach(function(chunk, i) {
@ -219,10 +186,10 @@ test('write no bufferize', function(t) {
chunk = Buffer.from(chunk); chunk = Buffer.from(chunk);
tw.write(chunk.toString(enc), enc); tw.write(chunk.toString(enc), enc);
}); });
t.end(); }
});
test('write callbacks', function(t) { {
// Verify write callbacks
const callbacks = chunks.map(function(chunk, i) { const callbacks = chunks.map(function(chunk, i) {
return [i, function() { return [i, function() {
callbacks._called[i] = chunk; callbacks._called[i] = chunk;
@ -237,118 +204,115 @@ test('write callbacks', function(t) {
highWaterMark: 100 highWaterMark: 100
}); });
tw.on('finish', function() { tw.on('finish', common.mustCall(function() {
process.nextTick(function() { process.nextTick(common.mustCall(function() {
t.same(tw.buffer, chunks, 'got chunks in the right order'); assert.deepStrictEqual(tw.buffer, chunks,
t.same(callbacks._called, chunks, 'called all callbacks'); 'got chunks in the right order');
t.end(); assert.deepStrictEqual(callbacks._called, chunks, 'called all callbacks');
}); }));
}); }));
chunks.forEach(function(chunk, i) { chunks.forEach(function(chunk, i) {
tw.write(chunk, callbacks[`callback-${i}`]); tw.write(chunk, callbacks[`callback-${i}`]);
}); });
tw.end(); tw.end();
}); }
test('end callback', function(t) { {
// Verify end() callback
const tw = new TestWriter(); const tw = new TestWriter();
tw.end(function() { tw.end(common.mustCall());
t.end(); }
});
});
test('end callback with chunk', function(t) { {
// Verify end() callback with chunk
const tw = new TestWriter(); const tw = new TestWriter();
tw.end(Buffer.from('hello world'), function() { tw.end(Buffer.from('hello world'), common.mustCall());
t.end(); }
});
});
test('end callback with chunk and encoding', function(t) { {
// Verify end() callback with chunk and encoding
const tw = new TestWriter(); const tw = new TestWriter();
tw.end('hello world', 'ascii', function() { tw.end('hello world', 'ascii', common.mustCall());
t.end(); }
});
});
test('end callback after .write() call', function(t) { {
// Verify end() callback after write() call
const tw = new TestWriter(); const tw = new TestWriter();
tw.write(Buffer.from('hello world')); tw.write(Buffer.from('hello world'));
tw.end(function() { tw.end(common.mustCall());
t.end(); }
});
});
test('end callback called after write callback', function(t) { {
// Verify end() callback after write() callback
const tw = new TestWriter(); const tw = new TestWriter();
let writeCalledback = false; let writeCalledback = false;
tw.write(Buffer.from('hello world'), function() { tw.write(Buffer.from('hello world'), function() {
writeCalledback = true; writeCalledback = true;
}); });
tw.end(function() { tw.end(common.mustCall(function() {
t.equal(writeCalledback, true); assert.strictEqual(writeCalledback, true);
t.end(); }));
}); }
});
test('encoding should be ignored for buffers', function(t) { {
// Verify encoding is ignored for buffers
const tw = new W(); const tw = new W();
const hex = '018b5e9a8f6236ffe30e31baf80d2cf6eb'; const hex = '018b5e9a8f6236ffe30e31baf80d2cf6eb';
tw._write = function(chunk) { tw._write = common.mustCall(function(chunk) {
t.equal(chunk.toString('hex'), hex); assert.strictEqual(chunk.toString('hex'), hex);
t.end(); });
};
const buf = Buffer.from(hex, 'hex'); const buf = Buffer.from(hex, 'hex');
tw.write(buf, 'latin1'); tw.write(buf, 'latin1');
}); }
test('writables are not pipable', function(t) { {
// Verify writables cannot be piped
const w = new W(); const w = new W();
w._write = common.noop; w._write = common.mustNotCall();
let gotError = false; let gotError = false;
w.on('error', function() { w.on('error', function() {
gotError = true; gotError = true;
}); });
w.pipe(process.stdout); w.pipe(process.stdout);
assert(gotError); assert.strictEqual(gotError, true);
t.end(); }
});
test('duplexes are pipable', function(t) { {
// Verify that duplex streams cannot be piped
const d = new D(); const d = new D();
d._read = common.noop; d._read = common.mustCall();
d._write = common.noop; d._write = common.mustNotCall();
let gotError = false; let gotError = false;
d.on('error', function() { d.on('error', function() {
gotError = true; gotError = true;
}); });
d.pipe(process.stdout); d.pipe(process.stdout);
assert(!gotError); assert.strictEqual(gotError, false);
t.end(); }
});
test('end(chunk) two times is an error', function(t) { {
// Verify that end(chunk) twice is an error
const w = new W(); const w = new W();
w._write = common.noop; w._write = common.noop;
let gotError = false; let gotError = false;
w.on('error', function(er) { w.on('error', function(er) {
gotError = true; gotError = true;
t.equal(er.message, 'write after end'); assert.strictEqual(er.message, 'write after end');
}); });
w.end('this is the end'); w.end('this is the end');
w.end('and so is this'); w.end('and so is this');
process.nextTick(function() { process.nextTick(common.mustCall(function() {
assert(gotError); assert.strictEqual(gotError, true);
t.end(); }));
}); }
});
test('dont end while writing', function(t) { {
// Verify stream doesn't end while writing
const w = new W(); const w = new W();
let wrote = false; let wrote = false;
w._write = function(chunk, e, cb) { w._write = function(chunk, e, cb) {
assert(!this.writing); assert.strictEqual(this.writing, undefined);
wrote = true; wrote = true;
this.writing = true; this.writing = true;
setTimeout(function() { setTimeout(function() {
@ -356,15 +320,15 @@ test('dont end while writing', function(t) {
cb(); cb();
}, 1); }, 1);
}; };
w.on('finish', function() { w.on('finish', common.mustCall(function() {
assert(wrote); assert.strictEqual(wrote, true);
t.end(); }));
});
w.write(Buffer.alloc(0)); w.write(Buffer.alloc(0));
w.end(); w.end();
}); }
test('finish does not come before write cb', function(t) { {
// Verify finish does not come before write() callback
const w = new W(); const w = new W();
let writeCb = false; let writeCb = false;
w._write = function(chunk, e, cb) { w._write = function(chunk, e, cb) {
@ -373,43 +337,42 @@ test('finish does not come before write cb', function(t) {
cb(); cb();
}, 10); }, 10);
}; };
w.on('finish', function() { w.on('finish', common.mustCall(function() {
assert(writeCb); assert.strictEqual(writeCb, true);
t.end(); }));
});
w.write(Buffer.alloc(0)); w.write(Buffer.alloc(0));
w.end(); w.end();
}); }
test('finish does not come before sync _write cb', function(t) { {
// Verify finish does not come before synchronous _write() callback
const w = new W(); const w = new W();
let writeCb = false; let writeCb = false;
w._write = function(chunk, e, cb) { w._write = function(chunk, e, cb) {
cb(); cb();
}; };
w.on('finish', function() { w.on('finish', common.mustCall(function() {
assert(writeCb); assert.strictEqual(writeCb, true);
t.end(); }));
});
w.write(Buffer.alloc(0), function() { w.write(Buffer.alloc(0), function() {
writeCb = true; writeCb = true;
}); });
w.end(); w.end();
}); }
test('finish is emitted if last chunk is empty', function(t) { {
// Verify finish is emitted if the last chunk is empty
const w = new W(); const w = new W();
w._write = function(chunk, e, cb) { w._write = function(chunk, e, cb) {
process.nextTick(cb); process.nextTick(cb);
}; };
w.on('finish', function() { w.on('finish', common.mustCall());
t.end();
});
w.write(Buffer.allocUnsafe(1)); w.write(Buffer.allocUnsafe(1));
w.end(Buffer.alloc(0)); w.end(Buffer.alloc(0));
}); }
test('finish is emitted after shutdown', function(t) { {
// Verify that finish is emitted after shutdown
const w = new W(); const w = new W();
let shutdown = false; let shutdown = false;
@ -424,9 +387,8 @@ test('finish is emitted after shutdown', function(t) {
process.nextTick(cb); process.nextTick(cb);
}; };
w.on('finish', common.mustCall(function() { w.on('finish', common.mustCall(function() {
assert(shutdown); assert.strictEqual(shutdown, true);
t.end();
})); }));
w.write(Buffer.allocUnsafe(1)); w.write(Buffer.allocUnsafe(1));
w.end(Buffer.allocUnsafe(0)); w.end(Buffer.allocUnsafe(0));
}); }

Loading…
Cancel
Save