From d992bc7498f82bf793511884bb0d807f57401c5f Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Fri, 4 Feb 2022 19:26:58 -0300 Subject: [PATCH 1/4] test: improve code coverage for streams/duplexify --- test/parallel/test-stream-duplexify.js | 148 +++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 test/parallel/test-stream-duplexify.js diff --git a/test/parallel/test-stream-duplexify.js b/test/parallel/test-stream-duplexify.js new file mode 100644 index 00000000000000..8ed2116b695641 --- /dev/null +++ b/test/parallel/test-stream-duplexify.js @@ -0,0 +1,148 @@ +'use strict'; +// Flags: --expose-internals + +const common = require('../common'); +const assert = require('assert'); +const Duplexify = require('internal/streams/duplexify'); +const { + Blob +} = require('internal/blob'); +const { + Duplex, + Readable, + Writable +} = require('stream'); + +// Ensure that isDuplexNodeStream was called +{ + const duplex = new Duplex(); + assert.deepStrictEqual(Duplexify(duplex), duplex); +} + +// Ensure that Duplexify works for blobs +{ + const blob = new Blob(['blob']); + const expecteByteLength = blob.size; + const duplex = Duplexify(blob); + duplex.on('data', common.mustCall((arrayBuffer) => { + assert.deepStrictEqual(arrayBuffer.byteLength, expecteByteLength); + })); +} + +// Ensure that given a promise rejection it emits an error +{ + const myErrorMessage = 'myCustomError'; + Duplexify(Promise.reject(myErrorMessage)) + .on('error', common.mustCall((error) => { + assert.deepStrictEqual(error, myErrorMessage); + })); +} + +// Ensure that given a promise rejection on an async function it emits an error +{ + const myErrorMessage = 'myCustomError'; + async function asyncFn() { + return Promise.reject(myErrorMessage); + } + + Duplexify(asyncFn) + .on('error', common.mustCall((error) => { + assert.deepStrictEqual(error, myErrorMessage); + })); +} + +// Ensure that Duplexify throws an Invalid return value when function is void +{ + assert.throws(() => Duplexify(() => {}), { + code: 'ERR_INVALID_RETURN_VALUE', + }); +} + +// Ensure data if a sub object has a readable stream it's duplexified +{ + const msg = Buffer.from('hello'); + const duplex = Duplexify({ + readable: Readable({ + read() { + this.push(msg); + this.push(null); + } + }) + }).on('data', common.mustCall((data) => { + assert.deepStrictEqual(data, msg); + })); + + assert.strictEqual(duplex.writable, false); +} + +// Ensure data if a sub object has a writable stream it's duplexified +{ + const msg = Buffer.from('hello'); + const duplex = Duplexify({ + writable: Writable({ + write: common.mustCall((data) => { + assert.deepStrictEqual(data, msg); + }) + }) + }); + + duplex.write(msg); + assert.strictEqual(duplex.readable, false); +} + +// Ensure data if a sub object has a writable and readable stream it's duplexified +{ + const msg = Buffer.from('hello'); + + const duplex = Duplexify({ + readable: Readable({ + read() { + this.push(msg); + this.push(null); + } + }), + writable: Writable({ + write: common.mustCall((data) => { + assert.deepStrictEqual(data, msg); + }) + }) + }); + + duplex.pipe(duplex) + .on('data', common.mustCall((data) => { + assert.deepStrictEqual(data, msg); + assert.strictEqual(duplex.readable, true); + assert.strictEqual(duplex.writable, true); + })) + .on('end', common.mustCall()); + +} + +// Ensure that given readable stream that throws an error it calls destroy +{ + const myErrorMessage = 'error!'; + const duplex = Duplexify(Readable({ + read() { + throw new Error(myErrorMessage); + } + })); + duplex.on('error', common.mustCall((msg) => { + assert.deepStrictEqual(msg.message, myErrorMessage); + })); +} + +// Ensure that given writable stream that throws an error it calls destroy +{ + const myErrorMessage = 'error!'; + const duplex = Duplexify(Writable({ + write(chunk, enc, cb) { + cb(myErrorMessage); + } + })); + + duplex.on('error', common.mustCall((msg) => { + assert.deepStrictEqual(msg, myErrorMessage); + })); + + duplex.write('test'); +} From 38c8b4e1a9cb01ad06a6f4ed5a9fc2fc1ae1e6fb Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Mon, 7 Feb 2022 11:49:52 -0300 Subject: [PATCH 2/4] test: make assertions on duplex.from instead of internal.duplexify Signed-off-by: Erick Wendel --- test/parallel/test-stream-duplex-from.js | 135 +++++++++++++++++++++ test/parallel/test-stream-duplexify.js | 148 ----------------------- 2 files changed, 135 insertions(+), 148 deletions(-) delete mode 100644 test/parallel/test-stream-duplexify.js diff --git a/test/parallel/test-stream-duplex-from.js b/test/parallel/test-stream-duplex-from.js index 6c9c59a5c82d52..bdef7e2af3ef40 100644 --- a/test/parallel/test-stream-duplex-from.js +++ b/test/parallel/test-stream-duplex-from.js @@ -3,6 +3,7 @@ const common = require('../common'); const assert = require('assert'); const { Duplex, Readable, Writable, pipeline } = require('stream'); +const { Blob } = require('buffer'); { const d = Duplex.from({ @@ -144,3 +145,137 @@ const { Duplex, Readable, Writable, pipeline } = require('stream'); common.mustCall(() => {}), ); } + +// Ensure that isDuplexNodeStream was called +{ + const duplex = new Duplex(); + assert.deepStrictEqual(Duplex.from(duplex), duplex); +} + +// Ensure that Duplex.from works for blobs +{ + const blob = new Blob(['blob']); + const expecteByteLength = blob.size; + const duplex = Duplex.from(blob); + duplex.on('data', common.mustCall((arrayBuffer) => { + assert.deepStrictEqual(arrayBuffer.byteLength, expecteByteLength); + })); +} + +// Ensure that given a promise rejection it emits an error +{ + const myErrorMessage = 'myCustomError'; + Duplex.from(Promise.reject(myErrorMessage)) + .on('error', common.mustCall((error) => { + assert.deepStrictEqual(error, myErrorMessage); + })); +} + +// Ensure that given a promise rejection on an async function it emits an error +{ + const myErrorMessage = 'myCustomError'; + async function asyncFn() { + return Promise.reject(myErrorMessage); + } + + Duplex.from(asyncFn) + .on('error', common.mustCall((error) => { + assert.deepStrictEqual(error, myErrorMessage); + })); +} + +// Ensure that Duplex.from throws an Invalid return value when function is void +{ + assert.throws(() => Duplex.from(() => {}), { + code: 'ERR_INVALID_RETURN_VALUE', + }); +} + +// Ensure data if a sub object has a readable stream it's duplexified +{ + const msg = Buffer.from('hello'); + const duplex = Duplex.from({ + readable: Readable({ + read() { + this.push(msg); + this.push(null); + } + }) + }).on('data', common.mustCall((data) => { + assert.deepStrictEqual(data, msg); + })); + + assert.strictEqual(duplex.writable, false); +} + +// Ensure data if a sub object has a writable stream it's duplexified +{ + const msg = Buffer.from('hello'); + const duplex = Duplex.from({ + writable: Writable({ + write: common.mustCall((data) => { + assert.deepStrictEqual(data, msg); + }) + }) + }); + + duplex.write(msg); + assert.strictEqual(duplex.readable, false); +} + +// Ensure data if a sub object has a writable and readable stream it's duplexified +{ + const msg = Buffer.from('hello'); + + const duplex = Duplex.from({ + readable: Readable({ + read() { + this.push(msg); + this.push(null); + } + }), + writable: Writable({ + write: common.mustCall((data) => { + assert.deepStrictEqual(data, msg); + }) + }) + }); + + duplex.pipe(duplex) + .on('data', common.mustCall((data) => { + assert.deepStrictEqual(data, msg); + assert.strictEqual(duplex.readable, true); + assert.strictEqual(duplex.writable, true); + })) + .on('end', common.mustCall()); + +} + +// Ensure that given readable stream that throws an error it calls destroy +{ + const myErrorMessage = 'error!'; + const duplex = Duplex.from(Readable({ + read() { + throw new Error(myErrorMessage); + } + })); + duplex.on('error', common.mustCall((msg) => { + assert.deepStrictEqual(msg.message, myErrorMessage); + })); +} + +// Ensure that given writable stream that throws an error it calls destroy +{ + const myErrorMessage = 'error!'; + const duplex = Duplex.from(Writable({ + write(chunk, enc, cb) { + cb(myErrorMessage); + } + })); + + duplex.on('error', common.mustCall((msg) => { + assert.deepStrictEqual(msg, myErrorMessage); + })); + + duplex.write('test'); +} diff --git a/test/parallel/test-stream-duplexify.js b/test/parallel/test-stream-duplexify.js deleted file mode 100644 index 8ed2116b695641..00000000000000 --- a/test/parallel/test-stream-duplexify.js +++ /dev/null @@ -1,148 +0,0 @@ -'use strict'; -// Flags: --expose-internals - -const common = require('../common'); -const assert = require('assert'); -const Duplexify = require('internal/streams/duplexify'); -const { - Blob -} = require('internal/blob'); -const { - Duplex, - Readable, - Writable -} = require('stream'); - -// Ensure that isDuplexNodeStream was called -{ - const duplex = new Duplex(); - assert.deepStrictEqual(Duplexify(duplex), duplex); -} - -// Ensure that Duplexify works for blobs -{ - const blob = new Blob(['blob']); - const expecteByteLength = blob.size; - const duplex = Duplexify(blob); - duplex.on('data', common.mustCall((arrayBuffer) => { - assert.deepStrictEqual(arrayBuffer.byteLength, expecteByteLength); - })); -} - -// Ensure that given a promise rejection it emits an error -{ - const myErrorMessage = 'myCustomError'; - Duplexify(Promise.reject(myErrorMessage)) - .on('error', common.mustCall((error) => { - assert.deepStrictEqual(error, myErrorMessage); - })); -} - -// Ensure that given a promise rejection on an async function it emits an error -{ - const myErrorMessage = 'myCustomError'; - async function asyncFn() { - return Promise.reject(myErrorMessage); - } - - Duplexify(asyncFn) - .on('error', common.mustCall((error) => { - assert.deepStrictEqual(error, myErrorMessage); - })); -} - -// Ensure that Duplexify throws an Invalid return value when function is void -{ - assert.throws(() => Duplexify(() => {}), { - code: 'ERR_INVALID_RETURN_VALUE', - }); -} - -// Ensure data if a sub object has a readable stream it's duplexified -{ - const msg = Buffer.from('hello'); - const duplex = Duplexify({ - readable: Readable({ - read() { - this.push(msg); - this.push(null); - } - }) - }).on('data', common.mustCall((data) => { - assert.deepStrictEqual(data, msg); - })); - - assert.strictEqual(duplex.writable, false); -} - -// Ensure data if a sub object has a writable stream it's duplexified -{ - const msg = Buffer.from('hello'); - const duplex = Duplexify({ - writable: Writable({ - write: common.mustCall((data) => { - assert.deepStrictEqual(data, msg); - }) - }) - }); - - duplex.write(msg); - assert.strictEqual(duplex.readable, false); -} - -// Ensure data if a sub object has a writable and readable stream it's duplexified -{ - const msg = Buffer.from('hello'); - - const duplex = Duplexify({ - readable: Readable({ - read() { - this.push(msg); - this.push(null); - } - }), - writable: Writable({ - write: common.mustCall((data) => { - assert.deepStrictEqual(data, msg); - }) - }) - }); - - duplex.pipe(duplex) - .on('data', common.mustCall((data) => { - assert.deepStrictEqual(data, msg); - assert.strictEqual(duplex.readable, true); - assert.strictEqual(duplex.writable, true); - })) - .on('end', common.mustCall()); - -} - -// Ensure that given readable stream that throws an error it calls destroy -{ - const myErrorMessage = 'error!'; - const duplex = Duplexify(Readable({ - read() { - throw new Error(myErrorMessage); - } - })); - duplex.on('error', common.mustCall((msg) => { - assert.deepStrictEqual(msg.message, myErrorMessage); - })); -} - -// Ensure that given writable stream that throws an error it calls destroy -{ - const myErrorMessage = 'error!'; - const duplex = Duplexify(Writable({ - write(chunk, enc, cb) { - cb(myErrorMessage); - } - })); - - duplex.on('error', common.mustCall((msg) => { - assert.deepStrictEqual(msg, myErrorMessage); - })); - - duplex.write('test'); -} From 1530f06bb699f368af38e1d193d6cd81d5206d73 Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Tue, 8 Feb 2022 11:17:59 -0300 Subject: [PATCH 3/4] test: add strictEqual for primitive values Signed-off-by: Erick Wendel --- test/parallel/test-stream-duplex-from.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-stream-duplex-from.js b/test/parallel/test-stream-duplex-from.js index bdef7e2af3ef40..b43a0e4992fb05 100644 --- a/test/parallel/test-stream-duplex-from.js +++ b/test/parallel/test-stream-duplex-from.js @@ -158,7 +158,7 @@ const { Blob } = require('buffer'); const expecteByteLength = blob.size; const duplex = Duplex.from(blob); duplex.on('data', common.mustCall((arrayBuffer) => { - assert.deepStrictEqual(arrayBuffer.byteLength, expecteByteLength); + assert.strictEqual(arrayBuffer.byteLength, expecteByteLength); })); } @@ -248,7 +248,6 @@ const { Blob } = require('buffer'); assert.strictEqual(duplex.writable, true); })) .on('end', common.mustCall()); - } // Ensure that given readable stream that throws an error it calls destroy @@ -260,7 +259,7 @@ const { Blob } = require('buffer'); } })); duplex.on('error', common.mustCall((msg) => { - assert.deepStrictEqual(msg.message, myErrorMessage); + assert.strictEqual(msg.message, myErrorMessage); })); } @@ -274,7 +273,7 @@ const { Blob } = require('buffer'); })); duplex.on('error', common.mustCall((msg) => { - assert.deepStrictEqual(msg, myErrorMessage); + assert.strictEqual(msg, myErrorMessage); })); duplex.write('test'); From 3e3a3ed8d55658e6139cc7331c404b59635bcd5e Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Tue, 8 Feb 2022 12:10:38 -0300 Subject: [PATCH 4/4] test: add strictEqual to ensure objects references Signed-off-by: Erick Wendel --- test/parallel/test-stream-duplex-from.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/parallel/test-stream-duplex-from.js b/test/parallel/test-stream-duplex-from.js index b43a0e4992fb05..07ef2f9ccf7c3c 100644 --- a/test/parallel/test-stream-duplex-from.js +++ b/test/parallel/test-stream-duplex-from.js @@ -149,7 +149,7 @@ const { Blob } = require('buffer'); // Ensure that isDuplexNodeStream was called { const duplex = new Duplex(); - assert.deepStrictEqual(Duplex.from(duplex), duplex); + assert.strictEqual(Duplex.from(duplex), duplex); } // Ensure that Duplex.from works for blobs @@ -167,7 +167,7 @@ const { Blob } = require('buffer'); const myErrorMessage = 'myCustomError'; Duplex.from(Promise.reject(myErrorMessage)) .on('error', common.mustCall((error) => { - assert.deepStrictEqual(error, myErrorMessage); + assert.strictEqual(error, myErrorMessage); })); } @@ -180,7 +180,7 @@ const { Blob } = require('buffer'); Duplex.from(asyncFn) .on('error', common.mustCall((error) => { - assert.deepStrictEqual(error, myErrorMessage); + assert.strictEqual(error, myErrorMessage); })); } @@ -202,7 +202,7 @@ const { Blob } = require('buffer'); } }) }).on('data', common.mustCall((data) => { - assert.deepStrictEqual(data, msg); + assert.strictEqual(data, msg); })); assert.strictEqual(duplex.writable, false); @@ -214,7 +214,7 @@ const { Blob } = require('buffer'); const duplex = Duplex.from({ writable: Writable({ write: common.mustCall((data) => { - assert.deepStrictEqual(data, msg); + assert.strictEqual(data, msg); }) }) }); @@ -236,14 +236,14 @@ const { Blob } = require('buffer'); }), writable: Writable({ write: common.mustCall((data) => { - assert.deepStrictEqual(data, msg); + assert.strictEqual(data, msg); }) }) }); duplex.pipe(duplex) .on('data', common.mustCall((data) => { - assert.deepStrictEqual(data, msg); + assert.strictEqual(data, msg); assert.strictEqual(duplex.readable, true); assert.strictEqual(duplex.writable, true); }))