From cd80cdc8118197507e19d023ddda1eecdcbd2215 Mon Sep 17 00:00:00 2001 From: naortedgi Date: Sun, 2 Oct 2022 22:53:13 +0300 Subject: [PATCH] test: adding unit tests to cover the autoCleanup parameter logic doc: update autoCleanup default value to false --- doc/api/stream.md | 3 +- lib/internal/streams/end-of-stream.js | 4 +-- test/parallel/test-stream-promises.js | 50 +++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/doc/api/stream.md b/doc/api/stream.md index 52fc787f287cc2..fc4c208be9157e 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -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. diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index 50569bca1c655a..730856721810c9 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -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; } diff --git a/test/parallel/test-stream-promises.js b/test/parallel/test-stream-promises.js index 33bfa292720da1..cea303e7dc3796 100644 --- a/test/parallel/test-stream-promises.js +++ b/test/parallel/test-stream-promises.js @@ -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); + }) +}