From 221be806bb3792a2ebd9eaefb6b9fe2407ca4988 Mon Sep 17 00:00:00 2001 From: Yutaka Hirano Date: Fri, 25 Jun 2021 22:17:20 +0000 Subject: [PATCH] Bug 1715566 [wpt PR 29310] - Add tests for WritableStreamDefaultController.signal, a=testonly Automatic update from web-platform-tests Add tests for WritableStreamDefaultController's signal Follows whatwg/streams#1132. -- wpt-commits: 861f0cb7a2d84019544d3622aab60b93056fc203 wpt-pr: 29310 --- .../streams/writable-streams/aborting.any.js | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/testing/web-platform/tests/streams/writable-streams/aborting.any.js b/testing/web-platform/tests/streams/writable-streams/aborting.any.js index 5c053bab91570..ab154a705ed0e 100644 --- a/testing/web-platform/tests/streams/writable-streams/aborting.any.js +++ b/testing/web-platform/tests/streams/writable-streams/aborting.any.js @@ -1376,3 +1376,111 @@ promise_test(t => { return promise_rejects_js(t, TypeError, ws.abort(), 'abort should reject') .then(() => writer.ready); }, 'abort on a locked stream should reject'); + +test(t => { + let ctrl; + const ws = new WritableStream({start(c) { ctrl = c; }}); + const e = Error('hello'); + + assert_true(ctrl.signal instanceof AbortSignal); + assert_false(ctrl.signal.aborted); + assert_equals(ctrl.abortReason, undefined); + ws.abort(e); + assert_true(ctrl.signal.aborted); + assert_equals(ctrl.abortReason, e); +}, 'WritableStreamDefaultController.signal'); + +promise_test(async t => { + let ctrl; + let resolve; + const called = new Promise(r => resolve = r); + + const ws = new WritableStream({ + start(c) { ctrl = c; }, + write() { resolve(); return new Promise(() => {}); } + }); + const writer = ws.getWriter(); + + writer.write(99); + await called; + + assert_false(ctrl.signal.aborted); + assert_equals(ctrl.abortReason, undefined); + writer.abort(); + assert_true(ctrl.signal.aborted); + assert_equals(ctrl.abortReason, undefined); +}, 'the abort signal is signalled synchronously - write'); + +promise_test(async t => { + let ctrl; + let resolve; + const called = new Promise(r => resolve = r); + + const ws = new WritableStream({ + start(c) { ctrl = c; }, + close() { resolve(); return new Promise(() => {}); } + }); + const writer = ws.getWriter(); + + writer.close(99); + await called; + + assert_false(ctrl.signal.aborted); + writer.abort(); + assert_true(ctrl.signal.aborted); +}, 'the abort signal is signalled synchronously - close'); + +promise_test(async t => { + let ctrl; + const ws = new WritableStream({start(c) { ctrl = c; }}); + const writer = ws.getWriter(); + + const e = TypeError(); + ctrl.error(e); + await promise_rejects_exactly(t, e, writer.closed); + assert_false(ctrl.signal.aborted); +}, 'the abort signal is not signalled on error'); + +promise_test(async t => { + let ctrl; + const e = TypeError(); + const ws = new WritableStream({ + start(c) { ctrl = c; }, + async write() { throw e; } + }); + const writer = ws.getWriter(); + + await promise_rejects_exactly(t, e, writer.write('hello'), 'write result'); + await promise_rejects_exactly(t, e, writer.closed, 'closed'); + assert_false(ctrl.signal.aborted); +}, 'the abort signal is not signalled on write failure'); + +promise_test(async t => { + let ctrl; + const e = TypeError(); + const ws = new WritableStream({ + start(c) { ctrl = c; }, + async close() { throw e; } + }); + const writer = ws.getWriter(); + + await promise_rejects_exactly(t, e, writer.close(), 'close result'); + await promise_rejects_exactly(t, e, writer.closed, 'closed'); + assert_false(ctrl.signal.aborted); +}, 'the abort signal is not signalled on close failure'); + +promise_test(async t => { + let ctrl; + const e1 = SyntaxError(); + const e2 = TypeError(); + const ws = new WritableStream({ + start(c) { ctrl = c; }, + }); + + const writer = ws.getWriter(); + ctrl.signal.addEventListener('abort', () => writer.abort(e2)); + writer.abort(e1); + assert_true(ctrl.signal.aborted); + + await promise_rejects_exactly(t, e2, writer.closed, 'closed'); +}, 'recursive abort() call');