Skip to content

Commit

Permalink
test: adding unit tests to cover the autoCleanup parameter logic
Browse files Browse the repository at this point in the history
doc: update autoCleanup default value to false
  • Loading branch information
ntedgi committed Oct 2, 2022
1 parent b48145b commit 1ba59b1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
3 changes: 2 additions & 1 deletion doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -2377,10 +2377,11 @@ changes:
callback will get called with an `AbortError`. All registered
listeners added by this function will also be removed.
* `autoCleanup` {boolean} remove all registered stream listeners.
**Default:** `true`.
**Default:** `false`.

* `callback` {Function} A callback function that takes an optional error
argument.

* Returns: {Function} A cleanup function which removes all registered
listeners.

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ function eos(stream, options, callback) {
}

function finished(stream, opts) {
let autoCleanup = true;
let autoCleanup = false;
if (opts === null) {
opts = kEmptyObject;
}
if (opts.autoCleanup) {
if (opts?.autoCleanup) {
validateBoolean(opts.autoCleanup, 'autoCleanup');
autoCleanup = opts.autoCleanup;
}
Expand Down
50 changes: 50 additions & 0 deletions test/parallel/test-stream-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,53 @@ assert.strictEqual(finished, promisify(stream.finished));
code: 'ENOENT'
}).then(common.mustCall());
}

{
const streamObj = new Readable();
assert.throws(
() => {
// Passing autoCleanup option not as boolean
// should throw error
finished(streamObj, {autoCleanup: 2});
},
{code: 'ERR_INVALID_ARG_TYPE'}
);
}

// Below code should not throw any errors as the
// streamObj is `Stream` and autoCleanup is boolean
{
const streamObj = new Readable();
finished(streamObj, {autoCleanup: true})
}


// cleanup function should not be called when autoCleanup is set to false
// listenerCount should be 1 after calling finish
{
const streamObj = new Writable();
assert(streamObj.listenerCount('end') === 0);
finished(streamObj, {autoCleanup: false}).then(() => {
assert(streamObj.listenerCount('end') === 1);
})
}

// cleanup function should be called when autoCleanup is set to true
// listenerCount should be 0 after calling finish
{
const streamObj = new Writable();
assert(streamObj.listenerCount('end') === 0);
finished(streamObj, {autoCleanup: true}).then(() => {
assert(streamObj.listenerCount('end') === 0);
})
}

// cleanup function should not be called when autoCleanup has not been set
// listenerCount should be 1 after calling finish
{
const streamObj = new Writable();
assert(streamObj.listenerCount('end') === 0);
finished(streamObj).then(() => {
assert(streamObj.listenerCount('end') === 1);
})
}

0 comments on commit 1ba59b1

Please sign in to comment.