From 4d22dd329b5890387356806e45cc007b7004ee65 Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Wed, 16 Dec 2020 20:24:09 +0800 Subject: [PATCH] test: increase coverage for stream 1. test addAbortSignal with invalid signal Refs: https://coverage.nodejs.org/coverage-a1509261770cb645/lib/internal/streams/add-abort-signal.js.html#L17 2. test addAbortSignal with invalid stream Refs: https://coverage.nodejs.org/coverage-a1509261770cb645/lib/internal/streams/add-abort-signal.js.html#L28 3. test addAbortSignalNoValidate with invalid signal Refs: https://coverage.nodejs.org/coverage-a1509261770cb645/lib/internal/streams/add-abort-signal.js.html#L34 4. test addAbortSignalNoValidate with aborted signal Refs: https://coverage.nodejs.org/coverage-a1509261770cb645/lib/internal/streams/add-abort-signal.js.html#L40 5. test Readable.from with invalid args Refs: https://coverage.nodejs.org/coverage-a1509261770cb645/lib/internal/streams/from.js.html#L31 PR-URL: https://github.com/nodejs/node/pull/36538 Refs: https://coverage.nodejs.org/coverage-a1509261770cb645/lib/internal/streams/add-abort-signal.js.html#L40 Reviewed-By: Rich Trott Reviewed-By: Luigi Pinca Reviewed-By: Michael Dawson --- test/parallel/test-readable-from.js | 8 +++++- test/parallel/test-stream-add-abort-signal.js | 27 +++++++++++++++++++ test/parallel/test-stream-writable-destroy.js | 15 +++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-stream-add-abort-signal.js diff --git a/test/parallel/test-readable-from.js b/test/parallel/test-readable-from.js index 94bc2c1ae4b926..a4e0f1b566c32c 100644 --- a/test/parallel/test-readable-from.js +++ b/test/parallel/test-readable-from.js @@ -3,7 +3,13 @@ const { mustCall } = require('../common'); const { once } = require('events'); const { Readable } = require('stream'); -const { strictEqual } = require('assert'); +const { strictEqual, throws } = require('assert'); + +{ + throws(() => { + Readable.from(null); + }, /ERR_INVALID_ARG_TYPE/); +} async function toReadableBasicSupport() { async function* generate() { diff --git a/test/parallel/test-stream-add-abort-signal.js b/test/parallel/test-stream-add-abort-signal.js new file mode 100644 index 00000000000000..3077137176ff96 --- /dev/null +++ b/test/parallel/test-stream-add-abort-signal.js @@ -0,0 +1,27 @@ +// Flags: --expose-internals +'use strict'; + +require('../common'); +const assert = require('assert'); +const { addAbortSignal, Readable } = require('stream'); +const { + addAbortSignalNoValidate, +} = require('internal/streams/add-abort-signal'); + +{ + assert.throws(() => { + addAbortSignal('INVALID_SIGNAL'); + }, /ERR_INVALID_ARG_TYPE/); + + const ac = new AbortController(); + assert.throws(() => { + addAbortSignal(ac.signal, 'INVALID_STREAM'); + }, /ERR_INVALID_ARG_TYPE/); +} + +{ + const r = new Readable({ + read: () => {}, + }); + assert.deepStrictEqual(r, addAbortSignalNoValidate('INVALID_SIGNAL', r)); +} diff --git a/test/parallel/test-stream-writable-destroy.js b/test/parallel/test-stream-writable-destroy.js index 096d9468e75aee..9859c59e446c95 100644 --- a/test/parallel/test-stream-writable-destroy.js +++ b/test/parallel/test-stream-writable-destroy.js @@ -446,3 +446,18 @@ const assert = require('assert'); write.write('asd'); ac.abort(); } + +{ + const ac = new AbortController(); + ac.abort(); + + const write = new Writable({ + signal: ac.signal, + write(chunk, enc, cb) { cb(); } + }); + + write.on('error', common.mustCall((e) => { + assert.strictEqual(e.name, 'AbortError'); + assert.strictEqual(write.destroyed, true); + })); +}