Skip to content

Commit

Permalink
test: added tests for duplex stream functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
KunalKumar-1 committed Oct 15, 2024
1 parent fd412e3 commit 90ac00d
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/parallel/test-stream-duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
const common = require('../common');
const assert = require('assert');
const Duplex = require('stream').Duplex;
const { Readable, Writable } = require('stream');
const { ReadableStream, WritableStream } = require('stream/web');

const stream = new Duplex({ objectMode: true });
Expand Down Expand Up @@ -131,3 +132,47 @@ process.on('exit', () => {
assert.deepStrictEqual(Buffer.from(result.value), dataToRead);
}));
}

const assertMessages = {
isDuplex: 'Expected a Duplex stream',
duplexUnchanged: 'Expected the Duplex stream to be returned unchanged',
notDuplex: 'Expected a non-Duplex stream',
isReadable: 'Expected a Readable stream'
};

// readable.compose - Duplex stream unchanged
{
const readable = new Readable({
read() {}
});

const duplex = new Duplex({
read() {},
write(chunk, encoding, callback) {
callback();
}
});

const composedStream = readable.compose(duplex);

assert(composedStream instanceof Duplex, assertMessages.isDuplex);
assert.strictEqual(composedStream, duplex, assertMessages.duplexUnchanged);

Check failure on line 159 in test/parallel/test-stream-duplex.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:assert:90 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected the Duplex stream to be returned unchanged + actual - expected ... Lines skipped Duplex { [Symbol(kCapture)]: false, [Symbol(shapeMode)]: true, + _destroy: [Function (anonymous)], + _events: { + close: undefined, + data: undefined, + drain: undefined, + end: undefined, + error: undefined, + finish: undefined, + prefinish: undefined, + readable: undefined + }, + _maxListeners: undefined, + _read: [Function (anonymous)], + _readableState: ReadableState { + [Symbol(kState)]: 1052940, + awaitDrainWriters: null, + buffer: [], + bufferIndex: 0, + highWaterMark: 65536, + length: 0, + pipes: [] + }, + _writableState: WritableState { + [Symbol(kBufferedValue)]: null, ... - _events: { - close: [ - [Function (anonymous)], - [Function: onclose], - [Function (anonymous)], - [Function: bound onceWrapper] { - listener: [Function: onclose] - }, - [Function: onclose] - ], - data: undefined, - drain: undefined, - end: [ - [Function: onend], - [Function: onend], - [Function (anonymous)] - ], - error: [ - [Function: onerror], - [Function: onerror], - [Function: onError], - [Function: onerror] - ], - finish: [ ... at Object.<anonymous> (/Users/runner/work/node/node/test/parallel/test-stream-duplex.js:159:10) at Module._compile (node:internal/modules/cjs/loader:1568:14) at Object..js (node:internal/modules/cjs/loader:1711:10) at Module.load (node:internal/modules/cjs/loader:1328:32) at Function._load (node:internal/modules/cjs/loader:1138:12) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:218:24) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5) at node:internal/main/run_main_module:36:49 { generatedMessage: false, code: 'ERR_ASSERTION', actual: Duplex { _events: { close: undefined, error: undefined, prefinish: undefined, finish: undefined, drain: undefined, data: undefined, end: undefined, readable: undefined }, _readableState: ReadableState { highWaterMark: 65536, buffer: [], bufferIndex: 0, length: 0, pipes: [], awaitDrainWriters: null, [Symbol(kState)]: 1052940 }, _writableState: WritableState { highWaterMark: 65536, length: 0, corked: 0, onwrite: [Function: bound onwrite], writelen: 0, bufferedIndex: 0, pendingcb: 0, [Symbol(kState)]: 1093432076, [Symbol(kBufferedValue)]: null }, allowHalfOpen: true, _maxListeners: undefined, _read: [Function (anonymous)], _destroy: [Function (anonymous)], [Symbol(shapeMode)]: true, [Symbol(kCapture)]: false }, expected: Duplex { _events: { close: [ [Function (anonymous)], [Function: onclose], [Function (anonymous)], [Function: bound onceWrapper] { listener: [Function: onclose] }, [Function: onclose] ], error: [ [Function: onerror], [Function: onerror], [Function: onError], [Function: onerror] ], prefinish: undefined, finish: [ [Function: onfinish], [Function: bound onceWrapper] { listener: [Function: onfinish] }, [Function: onfinish] ], drain: undefined, data: undefined, end: [ [Function: onend], [Function: onend], [Function (anonymous)] ], readable: [Function (anonymous)], unpipe: [Function: onunpipe] }, _readableState: ReadableState { highWaterMark: 65536, buffer: [], bufferIndex: 0, length: 0, pipes: [], awaitDrainWriters: null, [Symbol(kState)]: 9482508 }, _writableState: WritableState { highWaterMark: 65536,

Check failure on line 159 in test/parallel/test-stream-duplex.js

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- node:assert:90 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected the Duplex stream to be returned unchanged + actual - expected ... Lines skipped Duplex { [Symbol(kCapture)]: false, [Symbol(shapeMode)]: true, + _destroy: [Function (anonymous)], + _events: { + close: undefined, + data: undefined, + drain: undefined, + end: undefined, + error: undefined, + finish: undefined, + prefinish: undefined, + readable: undefined + }, + _maxListeners: undefined, + _read: [Function (anonymous)], + _readableState: ReadableState { + [Symbol(kState)]: 1052940, + awaitDrainWriters: null, + buffer: [], + bufferIndex: 0, + highWaterMark: 65536, + length: 0, + pipes: [] + }, + _writableState: WritableState { + [Symbol(kBufferedValue)]: null, ... - _events: { - close: [ - [Function (anonymous)], - [Function: onclose], - [Function (anonymous)], - [Function: bound onceWrapper] { - listener: [Function: onclose] - }, - [Function: onclose] - ], - data: undefined, - drain: undefined, - end: [ - [Function: onend], - [Function: onend], - [Function (anonymous)] - ], - error: [ - [Function: onerror], - [Function: onerror], - [Function: onError], - [Function: onerror] - ], - finish: [ ... at Object.<anonymous> (/home/runner/work/node/node/test/parallel/test-stream-duplex.js:159:10) at Module._compile (node:internal/modules/cjs/loader:1568:14) at Object..js (node:internal/modules/cjs/loader:1711:10) at Module.load (node:internal/modules/cjs/loader:1328:32) at Function._load (node:internal/modules/cjs/loader:1138:12) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:218:24) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5) at node:internal/main/run_main_module:36:49 { generatedMessage: false, code: 'ERR_ASSERTION', actual: Duplex { _events: { close: undefined, error: undefined, prefinish: undefined, finish: undefined, drain: undefined, data: undefined, end: undefined, readable: undefined }, _readableState: ReadableState { highWaterMark: 65536, buffer: [], bufferIndex: 0, length: 0, pipes: [], awaitDrainWriters: null, [Symbol(kState)]: 1052940 }, _writableState: WritableState { highWaterMark: 65536, length: 0, corked: 0, onwrite: [Function: bound onwrite], writelen: 0, bufferedIndex: 0, pendingcb: 0, [Symbol(kState)]: 1093432076, [Symbol(kBufferedValue)]: null }, allowHalfOpen: true, _maxListeners: undefined, _read: [Function (anonymous)], _destroy: [Function (anonymous)], [Symbol(shapeMode)]: true, [Symbol(kCapture)]: false }, expected: Duplex { _events: { close: [ [Function (anonymous)], [Function: onclose], [Function (anonymous)], [Function: bound onceWrapper] { listener: [Function: onclose] }, [Function: onclose] ], error: [ [Function: onerror], [Function: onerror], [Function: onError], [Function: onerror] ], prefinish: undefined, finish: [ [Function: onfinish], [Function: bound onceWrapper] { listener: [Function: onfinish] }, [Function: onfinish] ], drain: undefined, data: undefined, end: [ [Function: onend], [Function: onend], [Function (anonymous)] ], readable: [Function (anonymous)], unpipe: [Function: onunpipe] }, _readableState: ReadableState { highWaterMark: 65536, buffer: [], bufferIndex: 0, length: 0, pipes: [], awaitDrainWriters: null, [Symbol(kState)]: 9482508 }, _writableState: WritableState { highWaterMark: 65536, l
}

// readable.compose - wraps non-Duplex streams
{
const readable = new Readable({
read() {}
});

const writable = new Writable({
write(chunk, encoding, callback) {
callback();
}
});

const composedStream = readable.compose(writable);

assert(!(composedStream instanceof Duplex), assertMessages.notDuplex);
assert(composedStream instanceof Readable, assertMessages.isReadable);
}

0 comments on commit 90ac00d

Please sign in to comment.