11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci// META: script=../resources/test-utils.js
31cb0ef41Sopenharmony_ci// META: script=../resources/recording-streams.js
41cb0ef41Sopenharmony_ci'use strict';
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst error1 = new Error('error1');
71cb0ef41Sopenharmony_cierror1.name = 'error1';
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst error2 = new Error('error2');
101cb0ef41Sopenharmony_cierror2.name = 'error2';
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cipromise_test(t => {
131cb0ef41Sopenharmony_ci  const ws = new WritableStream({
141cb0ef41Sopenharmony_ci    write: t.unreached_func('write() should not be called')
151cb0ef41Sopenharmony_ci  });
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
181cb0ef41Sopenharmony_ci  const writePromise = writer.write('a');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  const readyPromise = writer.ready;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  writer.abort(error1);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  assert_equals(writer.ready, readyPromise, 'the ready promise property should not change');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  return Promise.all([
271cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, readyPromise, 'the ready promise should reject with error1'),
281cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, writePromise, 'the write() promise should reject with error1')
291cb0ef41Sopenharmony_ci  ]);
301cb0ef41Sopenharmony_ci}, 'Aborting a WritableStream before it starts should cause the writer\'s unsettled ready promise to reject');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cipromise_test(t => {
331cb0ef41Sopenharmony_ci  const ws = new WritableStream();
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
361cb0ef41Sopenharmony_ci  writer.write('a');
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  const readyPromise = writer.ready;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  return readyPromise.then(() => {
411cb0ef41Sopenharmony_ci    writer.abort(error1);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    assert_not_equals(writer.ready, readyPromise, 'the ready promise property should change');
441cb0ef41Sopenharmony_ci    return promise_rejects_exactly(t, error1, writer.ready, 'the ready promise should reject with error1');
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci}, 'Aborting a WritableStream should cause the writer\'s fulfilled ready promise to reset to a rejected one');
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cipromise_test(t => {
491cb0ef41Sopenharmony_ci  const ws = new WritableStream();
501cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  writer.releaseLock();
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  return promise_rejects_js(t, TypeError, writer.abort(), 'abort() should reject with a TypeError');
551cb0ef41Sopenharmony_ci}, 'abort() on a released writer rejects');
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_cipromise_test(t => {
581cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  return delay(0)
611cb0ef41Sopenharmony_ci    .then(() => {
621cb0ef41Sopenharmony_ci      const writer = ws.getWriter();
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci      const abortPromise = writer.abort(error1);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci      return Promise.all([
671cb0ef41Sopenharmony_ci        promise_rejects_exactly(t, error1, writer.write(1), 'write(1) must reject with error1'),
681cb0ef41Sopenharmony_ci        promise_rejects_exactly(t, error1, writer.write(2), 'write(2) must reject with error1'),
691cb0ef41Sopenharmony_ci        abortPromise
701cb0ef41Sopenharmony_ci      ]);
711cb0ef41Sopenharmony_ci    })
721cb0ef41Sopenharmony_ci    .then(() => {
731cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['abort', error1]);
741cb0ef41Sopenharmony_ci    });
751cb0ef41Sopenharmony_ci}, 'Aborting a WritableStream immediately prevents future writes');
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_cipromise_test(t => {
781cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
791cb0ef41Sopenharmony_ci  const results = [];
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  return delay(0)
821cb0ef41Sopenharmony_ci    .then(() => {
831cb0ef41Sopenharmony_ci      const writer = ws.getWriter();
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci      results.push(
861cb0ef41Sopenharmony_ci        writer.write(1),
871cb0ef41Sopenharmony_ci        promise_rejects_exactly(t, error1, writer.write(2), 'write(2) must reject with error1'),
881cb0ef41Sopenharmony_ci        promise_rejects_exactly(t, error1, writer.write(3), 'write(3) must reject with error1')
891cb0ef41Sopenharmony_ci      );
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci      const abortPromise = writer.abort(error1);
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci      results.push(
941cb0ef41Sopenharmony_ci        promise_rejects_exactly(t, error1, writer.write(4), 'write(4) must reject with error1'),
951cb0ef41Sopenharmony_ci        promise_rejects_exactly(t, error1, writer.write(5), 'write(5) must reject with error1')
961cb0ef41Sopenharmony_ci      );
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci      return abortPromise;
991cb0ef41Sopenharmony_ci    }).then(() => {
1001cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['write', 1, 'abort', error1]);
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci      return Promise.all(results);
1031cb0ef41Sopenharmony_ci    });
1041cb0ef41Sopenharmony_ci}, 'Aborting a WritableStream prevents further writes after any that are in progress');
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_cipromise_test(() => {
1071cb0ef41Sopenharmony_ci  const ws = new WritableStream({
1081cb0ef41Sopenharmony_ci    abort() {
1091cb0ef41Sopenharmony_ci      return 'Hello';
1101cb0ef41Sopenharmony_ci    }
1111cb0ef41Sopenharmony_ci  });
1121cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci  return writer.abort('a').then(value => {
1151cb0ef41Sopenharmony_ci    assert_equals(value, undefined, 'fulfillment value must be undefined');
1161cb0ef41Sopenharmony_ci  });
1171cb0ef41Sopenharmony_ci}, 'Fulfillment value of writer.abort() call must be undefined even if the underlying sink returns a non-undefined ' +
1181cb0ef41Sopenharmony_ci   'value');
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_cipromise_test(t => {
1211cb0ef41Sopenharmony_ci  const ws = new WritableStream({
1221cb0ef41Sopenharmony_ci    abort() {
1231cb0ef41Sopenharmony_ci      throw error1;
1241cb0ef41Sopenharmony_ci    }
1251cb0ef41Sopenharmony_ci  });
1261cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, writer.abort(undefined),
1291cb0ef41Sopenharmony_ci    'rejection reason of abortPromise must be the error thrown by abort');
1301cb0ef41Sopenharmony_ci}, 'WritableStream if sink\'s abort throws, the promise returned by writer.abort() rejects');
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_cipromise_test(t => {
1331cb0ef41Sopenharmony_ci  const ws = new WritableStream({
1341cb0ef41Sopenharmony_ci    abort() {
1351cb0ef41Sopenharmony_ci      throw error1;
1361cb0ef41Sopenharmony_ci    }
1371cb0ef41Sopenharmony_ci  });
1381cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  const abortPromise1 = writer.abort(undefined);
1411cb0ef41Sopenharmony_ci  const abortPromise2 = writer.abort(undefined);
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci  assert_equals(abortPromise1, abortPromise2, 'the promises must be the same');
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, abortPromise1, 'promise must have matching rejection');
1461cb0ef41Sopenharmony_ci}, 'WritableStream if sink\'s abort throws, the promise returned by multiple writer.abort()s is the same and rejects');
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_cipromise_test(t => {
1491cb0ef41Sopenharmony_ci  const ws = new WritableStream({
1501cb0ef41Sopenharmony_ci    abort() {
1511cb0ef41Sopenharmony_ci      throw error1;
1521cb0ef41Sopenharmony_ci    }
1531cb0ef41Sopenharmony_ci  });
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, ws.abort(undefined),
1561cb0ef41Sopenharmony_ci    'rejection reason of abortPromise must be the error thrown by abort');
1571cb0ef41Sopenharmony_ci}, 'WritableStream if sink\'s abort throws, the promise returned by ws.abort() rejects');
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_cipromise_test(t => {
1601cb0ef41Sopenharmony_ci  let resolveWritePromise;
1611cb0ef41Sopenharmony_ci  const ws = new WritableStream({
1621cb0ef41Sopenharmony_ci    write() {
1631cb0ef41Sopenharmony_ci      return new Promise(resolve => {
1641cb0ef41Sopenharmony_ci        resolveWritePromise = resolve;
1651cb0ef41Sopenharmony_ci      });
1661cb0ef41Sopenharmony_ci    },
1671cb0ef41Sopenharmony_ci    abort() {
1681cb0ef41Sopenharmony_ci      throw error1;
1691cb0ef41Sopenharmony_ci    }
1701cb0ef41Sopenharmony_ci  });
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci  writer.write().catch(() => {});
1751cb0ef41Sopenharmony_ci  return flushAsyncEvents().then(() => {
1761cb0ef41Sopenharmony_ci    const abortPromise = writer.abort(undefined);
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci    resolveWritePromise();
1791cb0ef41Sopenharmony_ci    return promise_rejects_exactly(t, error1, abortPromise,
1801cb0ef41Sopenharmony_ci      'rejection reason of abortPromise must be the error thrown by abort');
1811cb0ef41Sopenharmony_ci  });
1821cb0ef41Sopenharmony_ci}, 'WritableStream if sink\'s abort throws, for an abort performed during a write, the promise returned by ' +
1831cb0ef41Sopenharmony_ci   'ws.abort() rejects');
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_cipromise_test(() => {
1861cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
1871cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci  return writer.abort(error1).then(() => {
1901cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['abort', error1]);
1911cb0ef41Sopenharmony_ci  });
1921cb0ef41Sopenharmony_ci}, 'Aborting a WritableStream passes through the given reason');
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_cipromise_test(t => {
1951cb0ef41Sopenharmony_ci  const ws = new WritableStream();
1961cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci  const abortPromise = writer.abort(error1);
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  const events = [];
2011cb0ef41Sopenharmony_ci  writer.ready.catch(() => {
2021cb0ef41Sopenharmony_ci    events.push('ready');
2031cb0ef41Sopenharmony_ci  });
2041cb0ef41Sopenharmony_ci  writer.closed.catch(() => {
2051cb0ef41Sopenharmony_ci    events.push('closed');
2061cb0ef41Sopenharmony_ci  });
2071cb0ef41Sopenharmony_ci
2081cb0ef41Sopenharmony_ci  return Promise.all([
2091cb0ef41Sopenharmony_ci    abortPromise,
2101cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, writer.write(), 'writing should reject with error1'),
2111cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, writer.close(), 'closing should reject with error1'),
2121cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, writer.ready, 'ready should reject with error1'),
2131cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, writer.closed, 'closed should reject with error1')
2141cb0ef41Sopenharmony_ci  ]).then(() => {
2151cb0ef41Sopenharmony_ci    assert_array_equals(['ready', 'closed'], events, 'ready should reject before closed');
2161cb0ef41Sopenharmony_ci  });
2171cb0ef41Sopenharmony_ci}, 'Aborting a WritableStream puts it in an errored state with the error passed to abort()');
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_cipromise_test(t => {
2201cb0ef41Sopenharmony_ci  const ws = new WritableStream();
2211cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci  const writePromise = promise_rejects_exactly(t, error1, writer.write('a'),
2241cb0ef41Sopenharmony_ci    'writing should reject with error1');
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci  writer.abort(error1);
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ci  return writePromise;
2291cb0ef41Sopenharmony_ci}, 'Aborting a WritableStream causes any outstanding write() promises to be rejected with the reason supplied');
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_cipromise_test(t => {
2321cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
2331cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
2341cb0ef41Sopenharmony_ci
2351cb0ef41Sopenharmony_ci  const closePromise = writer.close();
2361cb0ef41Sopenharmony_ci  const abortPromise = writer.abort(error1);
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci  return Promise.all([
2391cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, writer.closed, 'closed should reject with error1'),
2401cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, closePromise, 'close() should reject with error1'),
2411cb0ef41Sopenharmony_ci    abortPromise
2421cb0ef41Sopenharmony_ci  ]).then(() => {
2431cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['abort', error1]);
2441cb0ef41Sopenharmony_ci  });
2451cb0ef41Sopenharmony_ci}, 'Closing but then immediately aborting a WritableStream causes the stream to error');
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_cipromise_test(() => {
2481cb0ef41Sopenharmony_ci  let resolveClose;
2491cb0ef41Sopenharmony_ci  const ws = new WritableStream({
2501cb0ef41Sopenharmony_ci    close() {
2511cb0ef41Sopenharmony_ci      return new Promise(resolve => {
2521cb0ef41Sopenharmony_ci        resolveClose = resolve;
2531cb0ef41Sopenharmony_ci      });
2541cb0ef41Sopenharmony_ci    }
2551cb0ef41Sopenharmony_ci  });
2561cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci  const closePromise = writer.close();
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ci  return delay(0).then(() => {
2611cb0ef41Sopenharmony_ci    const abortPromise = writer.abort(error1);
2621cb0ef41Sopenharmony_ci    resolveClose();
2631cb0ef41Sopenharmony_ci    return Promise.all([
2641cb0ef41Sopenharmony_ci      writer.closed,
2651cb0ef41Sopenharmony_ci      abortPromise,
2661cb0ef41Sopenharmony_ci      closePromise
2671cb0ef41Sopenharmony_ci    ]);
2681cb0ef41Sopenharmony_ci  });
2691cb0ef41Sopenharmony_ci}, 'Closing a WritableStream and aborting it while it closes causes the stream to ignore the abort attempt');
2701cb0ef41Sopenharmony_ci
2711cb0ef41Sopenharmony_cipromise_test(() => {
2721cb0ef41Sopenharmony_ci  const ws = new WritableStream();
2731cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci  writer.close();
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ci  return delay(0).then(() => writer.abort());
2781cb0ef41Sopenharmony_ci}, 'Aborting a WritableStream after it is closed is a no-op');
2791cb0ef41Sopenharmony_ci
2801cb0ef41Sopenharmony_cipromise_test(t => {
2811cb0ef41Sopenharmony_ci  // Testing that per https://github.com/whatwg/streams/issues/620#issuecomment-263483953 the fallback to close was
2821cb0ef41Sopenharmony_ci  // removed.
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci  // Cannot use recordingWritableStream since it always has an abort
2851cb0ef41Sopenharmony_ci  let closeCalled = false;
2861cb0ef41Sopenharmony_ci  const ws = new WritableStream({
2871cb0ef41Sopenharmony_ci    close() {
2881cb0ef41Sopenharmony_ci      closeCalled = true;
2891cb0ef41Sopenharmony_ci    }
2901cb0ef41Sopenharmony_ci  });
2911cb0ef41Sopenharmony_ci
2921cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci  writer.abort(error1);
2951cb0ef41Sopenharmony_ci
2961cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, writer.closed, 'closed should reject with error1').then(() => {
2971cb0ef41Sopenharmony_ci    assert_false(closeCalled, 'close must not have been called');
2981cb0ef41Sopenharmony_ci  });
2991cb0ef41Sopenharmony_ci}, 'WritableStream should NOT call underlying sink\'s close if no abort is supplied (historical)');
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_cipromise_test(() => {
3021cb0ef41Sopenharmony_ci  let thenCalled = false;
3031cb0ef41Sopenharmony_ci  const ws = new WritableStream({
3041cb0ef41Sopenharmony_ci    abort() {
3051cb0ef41Sopenharmony_ci      return {
3061cb0ef41Sopenharmony_ci        then(onFulfilled) {
3071cb0ef41Sopenharmony_ci          thenCalled = true;
3081cb0ef41Sopenharmony_ci          onFulfilled();
3091cb0ef41Sopenharmony_ci        }
3101cb0ef41Sopenharmony_ci      };
3111cb0ef41Sopenharmony_ci    }
3121cb0ef41Sopenharmony_ci  });
3131cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
3141cb0ef41Sopenharmony_ci  return writer.abort().then(() => assert_true(thenCalled, 'then() should be called'));
3151cb0ef41Sopenharmony_ci}, 'returning a thenable from abort() should work');
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_cipromise_test(t => {
3181cb0ef41Sopenharmony_ci  const ws = new WritableStream({
3191cb0ef41Sopenharmony_ci    write() {
3201cb0ef41Sopenharmony_ci      return flushAsyncEvents();
3211cb0ef41Sopenharmony_ci    }
3221cb0ef41Sopenharmony_ci  });
3231cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
3241cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
3251cb0ef41Sopenharmony_ci    const writePromise = writer.write('a');
3261cb0ef41Sopenharmony_ci    writer.abort(error1);
3271cb0ef41Sopenharmony_ci    let closedRejected = false;
3281cb0ef41Sopenharmony_ci    return Promise.all([
3291cb0ef41Sopenharmony_ci      writePromise.then(() => assert_false(closedRejected, '.closed should not resolve before write()')),
3301cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.closed, '.closed should reject').then(() => {
3311cb0ef41Sopenharmony_ci        closedRejected = true;
3321cb0ef41Sopenharmony_ci      })
3331cb0ef41Sopenharmony_ci    ]);
3341cb0ef41Sopenharmony_ci  });
3351cb0ef41Sopenharmony_ci}, '.closed should not resolve before fulfilled write()');
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_cipromise_test(t => {
3381cb0ef41Sopenharmony_ci  const ws = new WritableStream({
3391cb0ef41Sopenharmony_ci    write() {
3401cb0ef41Sopenharmony_ci      return Promise.reject(error1);
3411cb0ef41Sopenharmony_ci    }
3421cb0ef41Sopenharmony_ci  });
3431cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
3441cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
3451cb0ef41Sopenharmony_ci    const writePromise = writer.write('a');
3461cb0ef41Sopenharmony_ci    const abortPromise = writer.abort(error2);
3471cb0ef41Sopenharmony_ci    let closedRejected = false;
3481cb0ef41Sopenharmony_ci    return Promise.all([
3491cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writePromise, 'write() should reject')
3501cb0ef41Sopenharmony_ci          .then(() => assert_false(closedRejected, '.closed should not resolve before write()')),
3511cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writer.closed, '.closed should reject')
3521cb0ef41Sopenharmony_ci          .then(() => {
3531cb0ef41Sopenharmony_ci            closedRejected = true;
3541cb0ef41Sopenharmony_ci          }),
3551cb0ef41Sopenharmony_ci      abortPromise
3561cb0ef41Sopenharmony_ci    ]);
3571cb0ef41Sopenharmony_ci  });
3581cb0ef41Sopenharmony_ci}, '.closed should not resolve before rejected write(); write() error should not overwrite abort() error');
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_cipromise_test(t => {
3611cb0ef41Sopenharmony_ci  const ws = new WritableStream({
3621cb0ef41Sopenharmony_ci    write() {
3631cb0ef41Sopenharmony_ci      return flushAsyncEvents();
3641cb0ef41Sopenharmony_ci    }
3651cb0ef41Sopenharmony_ci  }, new CountQueuingStrategy({ highWaterMark: 4 }));
3661cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
3671cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
3681cb0ef41Sopenharmony_ci    const settlementOrder = [];
3691cb0ef41Sopenharmony_ci    return Promise.all([
3701cb0ef41Sopenharmony_ci      writer.write('1').then(() => settlementOrder.push(1)),
3711cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.write('2'), 'first queued write should be rejected')
3721cb0ef41Sopenharmony_ci          .then(() => settlementOrder.push(2)),
3731cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.write('3'), 'second queued write should be rejected')
3741cb0ef41Sopenharmony_ci          .then(() => settlementOrder.push(3)),
3751cb0ef41Sopenharmony_ci      writer.abort(error1)
3761cb0ef41Sopenharmony_ci    ]).then(() => assert_array_equals([1, 2, 3], settlementOrder, 'writes should be satisfied in order'));
3771cb0ef41Sopenharmony_ci  });
3781cb0ef41Sopenharmony_ci}, 'writes should be satisfied in order when aborting');
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_cipromise_test(t => {
3811cb0ef41Sopenharmony_ci  const ws = new WritableStream({
3821cb0ef41Sopenharmony_ci    write() {
3831cb0ef41Sopenharmony_ci      return Promise.reject(error1);
3841cb0ef41Sopenharmony_ci    }
3851cb0ef41Sopenharmony_ci  }, new CountQueuingStrategy({ highWaterMark: 4 }));
3861cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
3871cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
3881cb0ef41Sopenharmony_ci    const settlementOrder = [];
3891cb0ef41Sopenharmony_ci    return Promise.all([
3901cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.write('1'), 'in-flight write should be rejected')
3911cb0ef41Sopenharmony_ci          .then(() => settlementOrder.push(1)),
3921cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writer.write('2'), 'first queued write should be rejected')
3931cb0ef41Sopenharmony_ci          .then(() => settlementOrder.push(2)),
3941cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writer.write('3'), 'second queued write should be rejected')
3951cb0ef41Sopenharmony_ci          .then(() => settlementOrder.push(3)),
3961cb0ef41Sopenharmony_ci      writer.abort(error2)
3971cb0ef41Sopenharmony_ci    ]).then(() => assert_array_equals([1, 2, 3], settlementOrder, 'writes should be satisfied in order'));
3981cb0ef41Sopenharmony_ci  });
3991cb0ef41Sopenharmony_ci}, 'writes should be satisfied in order after rejected write when aborting');
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_cipromise_test(t => {
4021cb0ef41Sopenharmony_ci  const ws = new WritableStream({
4031cb0ef41Sopenharmony_ci    write() {
4041cb0ef41Sopenharmony_ci      return Promise.reject(error1);
4051cb0ef41Sopenharmony_ci    }
4061cb0ef41Sopenharmony_ci  });
4071cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
4081cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
4091cb0ef41Sopenharmony_ci    return Promise.all([
4101cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.write('a'), 'writer.write() should reject with error from underlying write()'),
4111cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writer.close(),
4121cb0ef41Sopenharmony_ci                              'writer.close() should reject with error from underlying write()'),
4131cb0ef41Sopenharmony_ci      writer.abort(error2)
4141cb0ef41Sopenharmony_ci    ]);
4151cb0ef41Sopenharmony_ci  });
4161cb0ef41Sopenharmony_ci}, 'close() should reject with abort reason why abort() is first error');
4171cb0ef41Sopenharmony_ci
4181cb0ef41Sopenharmony_cipromise_test(() => {
4191cb0ef41Sopenharmony_ci  let resolveWrite;
4201cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
4211cb0ef41Sopenharmony_ci    write() {
4221cb0ef41Sopenharmony_ci      return new Promise(resolve => {
4231cb0ef41Sopenharmony_ci        resolveWrite = resolve;
4241cb0ef41Sopenharmony_ci      });
4251cb0ef41Sopenharmony_ci    }
4261cb0ef41Sopenharmony_ci  });
4271cb0ef41Sopenharmony_ci
4281cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
4291cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
4301cb0ef41Sopenharmony_ci    writer.write('a');
4311cb0ef41Sopenharmony_ci    const abortPromise = writer.abort('b');
4321cb0ef41Sopenharmony_ci    return flushAsyncEvents().then(() => {
4331cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['write', 'a'], 'abort should not be called while write is in-flight');
4341cb0ef41Sopenharmony_ci      resolveWrite();
4351cb0ef41Sopenharmony_ci      return abortPromise.then(() => {
4361cb0ef41Sopenharmony_ci        assert_array_equals(ws.events, ['write', 'a', 'abort', 'b'], 'abort should be called after the write finishes');
4371cb0ef41Sopenharmony_ci      });
4381cb0ef41Sopenharmony_ci    });
4391cb0ef41Sopenharmony_ci  });
4401cb0ef41Sopenharmony_ci}, 'underlying abort() should not be called until underlying write() completes');
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_cipromise_test(() => {
4431cb0ef41Sopenharmony_ci  let resolveClose;
4441cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
4451cb0ef41Sopenharmony_ci    close() {
4461cb0ef41Sopenharmony_ci      return new Promise(resolve => {
4471cb0ef41Sopenharmony_ci        resolveClose = resolve;
4481cb0ef41Sopenharmony_ci      });
4491cb0ef41Sopenharmony_ci    }
4501cb0ef41Sopenharmony_ci  });
4511cb0ef41Sopenharmony_ci
4521cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
4531cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
4541cb0ef41Sopenharmony_ci    writer.close();
4551cb0ef41Sopenharmony_ci    const abortPromise = writer.abort();
4561cb0ef41Sopenharmony_ci    return flushAsyncEvents().then(() => {
4571cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['close'], 'abort should not be called while close is in-flight');
4581cb0ef41Sopenharmony_ci      resolveClose();
4591cb0ef41Sopenharmony_ci      return abortPromise.then(() => {
4601cb0ef41Sopenharmony_ci        assert_array_equals(ws.events, ['close'], 'abort should not be called');
4611cb0ef41Sopenharmony_ci      });
4621cb0ef41Sopenharmony_ci    });
4631cb0ef41Sopenharmony_ci  });
4641cb0ef41Sopenharmony_ci}, 'underlying abort() should not be called if underlying close() has started');
4651cb0ef41Sopenharmony_ci
4661cb0ef41Sopenharmony_cipromise_test(t => {
4671cb0ef41Sopenharmony_ci  let rejectClose;
4681cb0ef41Sopenharmony_ci  let abortCalled = false;
4691cb0ef41Sopenharmony_ci  const ws = new WritableStream({
4701cb0ef41Sopenharmony_ci    close() {
4711cb0ef41Sopenharmony_ci      return new Promise((resolve, reject) => {
4721cb0ef41Sopenharmony_ci        rejectClose = reject;
4731cb0ef41Sopenharmony_ci      });
4741cb0ef41Sopenharmony_ci    },
4751cb0ef41Sopenharmony_ci    abort() {
4761cb0ef41Sopenharmony_ci      abortCalled = true;
4771cb0ef41Sopenharmony_ci    }
4781cb0ef41Sopenharmony_ci  });
4791cb0ef41Sopenharmony_ci
4801cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
4811cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
4821cb0ef41Sopenharmony_ci    const closePromise = writer.close();
4831cb0ef41Sopenharmony_ci    const abortPromise = writer.abort();
4841cb0ef41Sopenharmony_ci    return flushAsyncEvents().then(() => {
4851cb0ef41Sopenharmony_ci      assert_false(abortCalled, 'underlying abort should not be called while close is in-flight');
4861cb0ef41Sopenharmony_ci      rejectClose(error1);
4871cb0ef41Sopenharmony_ci      return promise_rejects_exactly(t, error1, abortPromise, 'abort should reject with the same reason').then(() => {
4881cb0ef41Sopenharmony_ci        return promise_rejects_exactly(t, error1, closePromise, 'close should reject with the same reason');
4891cb0ef41Sopenharmony_ci      }).then(() => {
4901cb0ef41Sopenharmony_ci        assert_false(abortCalled, 'underlying abort should not be called after close completes');
4911cb0ef41Sopenharmony_ci      });
4921cb0ef41Sopenharmony_ci    });
4931cb0ef41Sopenharmony_ci  });
4941cb0ef41Sopenharmony_ci}, 'if underlying close() has started and then rejects, the abort() and close() promises should reject with the ' +
4951cb0ef41Sopenharmony_ci   'underlying close rejection reason');
4961cb0ef41Sopenharmony_ci
4971cb0ef41Sopenharmony_cipromise_test(t => {
4981cb0ef41Sopenharmony_ci  let resolveWrite;
4991cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
5001cb0ef41Sopenharmony_ci    write() {
5011cb0ef41Sopenharmony_ci      return new Promise(resolve => {
5021cb0ef41Sopenharmony_ci        resolveWrite = resolve;
5031cb0ef41Sopenharmony_ci      });
5041cb0ef41Sopenharmony_ci    }
5051cb0ef41Sopenharmony_ci  });
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
5081cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
5091cb0ef41Sopenharmony_ci    writer.write('a');
5101cb0ef41Sopenharmony_ci    const closePromise = writer.close();
5111cb0ef41Sopenharmony_ci    const abortPromise = writer.abort(error1);
5121cb0ef41Sopenharmony_ci
5131cb0ef41Sopenharmony_ci    return flushAsyncEvents().then(() => {
5141cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['write', 'a'], 'abort should not be called while write is in-flight');
5151cb0ef41Sopenharmony_ci      resolveWrite();
5161cb0ef41Sopenharmony_ci      return abortPromise.then(() => {
5171cb0ef41Sopenharmony_ci        assert_array_equals(ws.events, ['write', 'a', 'abort', error1], 'abort should be called after write completes');
5181cb0ef41Sopenharmony_ci        return promise_rejects_exactly(t, error1, closePromise, 'promise returned by close() should be rejected');
5191cb0ef41Sopenharmony_ci      });
5201cb0ef41Sopenharmony_ci    });
5211cb0ef41Sopenharmony_ci  });
5221cb0ef41Sopenharmony_ci}, 'an abort() that happens during a write() should trigger the underlying abort() even with a close() queued');
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_cipromise_test(t => {
5251cb0ef41Sopenharmony_ci  const ws = new WritableStream({
5261cb0ef41Sopenharmony_ci    write() {
5271cb0ef41Sopenharmony_ci      return new Promise(() => {});
5281cb0ef41Sopenharmony_ci    }
5291cb0ef41Sopenharmony_ci  });
5301cb0ef41Sopenharmony_ci
5311cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
5321cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
5331cb0ef41Sopenharmony_ci    writer.write('a');
5341cb0ef41Sopenharmony_ci    writer.abort(error1);
5351cb0ef41Sopenharmony_ci    writer.releaseLock();
5361cb0ef41Sopenharmony_ci    const writer2 = ws.getWriter();
5371cb0ef41Sopenharmony_ci    return promise_rejects_exactly(t, error1, writer2.ready,
5381cb0ef41Sopenharmony_ci                                   'ready of the second writer should be rejected with error1');
5391cb0ef41Sopenharmony_ci  });
5401cb0ef41Sopenharmony_ci}, 'if a writer is created for a stream with a pending abort, its ready should be rejected with the abort error');
5411cb0ef41Sopenharmony_ci
5421cb0ef41Sopenharmony_cipromise_test(() => {
5431cb0ef41Sopenharmony_ci  const ws = new WritableStream();
5441cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
5451cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
5461cb0ef41Sopenharmony_ci    const closePromise = writer.close();
5471cb0ef41Sopenharmony_ci    const abortPromise = writer.abort();
5481cb0ef41Sopenharmony_ci    const events = [];
5491cb0ef41Sopenharmony_ci    return Promise.all([
5501cb0ef41Sopenharmony_ci      closePromise.then(() => { events.push('close'); }),
5511cb0ef41Sopenharmony_ci      abortPromise.then(() => { events.push('abort'); })
5521cb0ef41Sopenharmony_ci    ]).then(() => {
5531cb0ef41Sopenharmony_ci      assert_array_equals(events, ['close', 'abort']);
5541cb0ef41Sopenharmony_ci    });
5551cb0ef41Sopenharmony_ci  });
5561cb0ef41Sopenharmony_ci}, 'writer close() promise should resolve before abort() promise');
5571cb0ef41Sopenharmony_ci
5581cb0ef41Sopenharmony_cipromise_test(t => {
5591cb0ef41Sopenharmony_ci  const ws = new WritableStream({
5601cb0ef41Sopenharmony_ci    write(chunk, controller) {
5611cb0ef41Sopenharmony_ci      controller.error(error1);
5621cb0ef41Sopenharmony_ci      return new Promise(() => {});
5631cb0ef41Sopenharmony_ci    }
5641cb0ef41Sopenharmony_ci  });
5651cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
5661cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
5671cb0ef41Sopenharmony_ci    writer.write('a');
5681cb0ef41Sopenharmony_ci    return promise_rejects_exactly(t, error1, writer.ready, 'writer.ready should reject');
5691cb0ef41Sopenharmony_ci  });
5701cb0ef41Sopenharmony_ci}, 'writer.ready should reject on controller error without waiting for underlying write');
5711cb0ef41Sopenharmony_ci
5721cb0ef41Sopenharmony_cipromise_test(t => {
5731cb0ef41Sopenharmony_ci  let rejectWrite;
5741cb0ef41Sopenharmony_ci  const ws = new WritableStream({
5751cb0ef41Sopenharmony_ci    write() {
5761cb0ef41Sopenharmony_ci      return new Promise((resolve, reject) => {
5771cb0ef41Sopenharmony_ci        rejectWrite = reject;
5781cb0ef41Sopenharmony_ci      });
5791cb0ef41Sopenharmony_ci    }
5801cb0ef41Sopenharmony_ci  });
5811cb0ef41Sopenharmony_ci
5821cb0ef41Sopenharmony_ci  let writePromise;
5831cb0ef41Sopenharmony_ci  let abortPromise;
5841cb0ef41Sopenharmony_ci
5851cb0ef41Sopenharmony_ci  const events = [];
5861cb0ef41Sopenharmony_ci
5871cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
5881cb0ef41Sopenharmony_ci
5891cb0ef41Sopenharmony_ci  writer.closed.catch(() => {
5901cb0ef41Sopenharmony_ci    events.push('closed');
5911cb0ef41Sopenharmony_ci  });
5921cb0ef41Sopenharmony_ci
5931cb0ef41Sopenharmony_ci  // Wait for ws to start
5941cb0ef41Sopenharmony_ci  return flushAsyncEvents().then(() => {
5951cb0ef41Sopenharmony_ci    writePromise = writer.write('a');
5961cb0ef41Sopenharmony_ci    writePromise.catch(() => {
5971cb0ef41Sopenharmony_ci      events.push('writePromise');
5981cb0ef41Sopenharmony_ci    });
5991cb0ef41Sopenharmony_ci
6001cb0ef41Sopenharmony_ci    abortPromise = writer.abort(error1);
6011cb0ef41Sopenharmony_ci    abortPromise.then(() => {
6021cb0ef41Sopenharmony_ci      events.push('abortPromise');
6031cb0ef41Sopenharmony_ci    });
6041cb0ef41Sopenharmony_ci
6051cb0ef41Sopenharmony_ci    const writePromise2 = writer.write('a');
6061cb0ef41Sopenharmony_ci
6071cb0ef41Sopenharmony_ci    return Promise.all([
6081cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writePromise2, 'writePromise2 must reject with the error from abort'),
6091cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.ready, 'writer.ready must reject with the error from abort'),
6101cb0ef41Sopenharmony_ci      flushAsyncEvents()
6111cb0ef41Sopenharmony_ci    ]);
6121cb0ef41Sopenharmony_ci  }).then(() => {
6131cb0ef41Sopenharmony_ci    assert_array_equals(events, [], 'writePromise, abortPromise and writer.closed must not be rejected yet');
6141cb0ef41Sopenharmony_ci
6151cb0ef41Sopenharmony_ci    rejectWrite(error2);
6161cb0ef41Sopenharmony_ci
6171cb0ef41Sopenharmony_ci    return Promise.all([
6181cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writePromise,
6191cb0ef41Sopenharmony_ci                              'writePromise must reject with the error returned from the sink\'s write method'),
6201cb0ef41Sopenharmony_ci      abortPromise,
6211cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.closed,
6221cb0ef41Sopenharmony_ci                              'writer.closed must reject with the error from abort'),
6231cb0ef41Sopenharmony_ci      flushAsyncEvents()
6241cb0ef41Sopenharmony_ci    ]);
6251cb0ef41Sopenharmony_ci  }).then(() => {
6261cb0ef41Sopenharmony_ci    assert_array_equals(events, ['writePromise', 'abortPromise', 'closed'],
6271cb0ef41Sopenharmony_ci                        'writePromise, abortPromise and writer.closed must settle');
6281cb0ef41Sopenharmony_ci
6291cb0ef41Sopenharmony_ci    const writePromise3 = writer.write('a');
6301cb0ef41Sopenharmony_ci
6311cb0ef41Sopenharmony_ci    return Promise.all([
6321cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writePromise3,
6331cb0ef41Sopenharmony_ci                              'writePromise3 must reject with the error from abort'),
6341cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.ready,
6351cb0ef41Sopenharmony_ci                              'writer.ready must be still rejected with the error indicating abort')
6361cb0ef41Sopenharmony_ci    ]);
6371cb0ef41Sopenharmony_ci  }).then(() => {
6381cb0ef41Sopenharmony_ci    writer.releaseLock();
6391cb0ef41Sopenharmony_ci
6401cb0ef41Sopenharmony_ci    return Promise.all([
6411cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.ready,
6421cb0ef41Sopenharmony_ci                      'writer.ready must be rejected with an error indicating release'),
6431cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.closed,
6441cb0ef41Sopenharmony_ci                      'writer.closed must be rejected with an error indicating release')
6451cb0ef41Sopenharmony_ci    ]);
6461cb0ef41Sopenharmony_ci  });
6471cb0ef41Sopenharmony_ci}, 'writer.abort() while there is an in-flight write, and then finish the write with rejection');
6481cb0ef41Sopenharmony_ci
6491cb0ef41Sopenharmony_cipromise_test(t => {
6501cb0ef41Sopenharmony_ci  let resolveWrite;
6511cb0ef41Sopenharmony_ci  let controller;
6521cb0ef41Sopenharmony_ci  const ws = new WritableStream({
6531cb0ef41Sopenharmony_ci    write(chunk, c) {
6541cb0ef41Sopenharmony_ci      controller = c;
6551cb0ef41Sopenharmony_ci      return new Promise(resolve => {
6561cb0ef41Sopenharmony_ci        resolveWrite = resolve;
6571cb0ef41Sopenharmony_ci      });
6581cb0ef41Sopenharmony_ci    }
6591cb0ef41Sopenharmony_ci  });
6601cb0ef41Sopenharmony_ci
6611cb0ef41Sopenharmony_ci  let writePromise;
6621cb0ef41Sopenharmony_ci  let abortPromise;
6631cb0ef41Sopenharmony_ci
6641cb0ef41Sopenharmony_ci  const events = [];
6651cb0ef41Sopenharmony_ci
6661cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
6671cb0ef41Sopenharmony_ci
6681cb0ef41Sopenharmony_ci  writer.closed.catch(() => {
6691cb0ef41Sopenharmony_ci    events.push('closed');
6701cb0ef41Sopenharmony_ci  });
6711cb0ef41Sopenharmony_ci
6721cb0ef41Sopenharmony_ci  // Wait for ws to start
6731cb0ef41Sopenharmony_ci  return flushAsyncEvents().then(() => {
6741cb0ef41Sopenharmony_ci    writePromise = writer.write('a');
6751cb0ef41Sopenharmony_ci    writePromise.then(() => {
6761cb0ef41Sopenharmony_ci      events.push('writePromise');
6771cb0ef41Sopenharmony_ci    });
6781cb0ef41Sopenharmony_ci
6791cb0ef41Sopenharmony_ci    abortPromise = writer.abort(error1);
6801cb0ef41Sopenharmony_ci    abortPromise.then(() => {
6811cb0ef41Sopenharmony_ci      events.push('abortPromise');
6821cb0ef41Sopenharmony_ci    });
6831cb0ef41Sopenharmony_ci
6841cb0ef41Sopenharmony_ci    const writePromise2 = writer.write('a');
6851cb0ef41Sopenharmony_ci
6861cb0ef41Sopenharmony_ci    return Promise.all([
6871cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writePromise2, 'writePromise2 must reject with the error from abort'),
6881cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.ready, 'writer.ready must reject with the error from abort'),
6891cb0ef41Sopenharmony_ci      flushAsyncEvents()
6901cb0ef41Sopenharmony_ci    ]);
6911cb0ef41Sopenharmony_ci  }).then(() => {
6921cb0ef41Sopenharmony_ci    assert_array_equals(events, [], 'writePromise, abortPromise and writer.closed must not be fulfilled/rejected yet');
6931cb0ef41Sopenharmony_ci
6941cb0ef41Sopenharmony_ci    // This error is too late to change anything. abort() has already changed the stream state to 'erroring'.
6951cb0ef41Sopenharmony_ci    controller.error(error2);
6961cb0ef41Sopenharmony_ci
6971cb0ef41Sopenharmony_ci    const writePromise3 = writer.write('a');
6981cb0ef41Sopenharmony_ci
6991cb0ef41Sopenharmony_ci    return Promise.all([
7001cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writePromise3,
7011cb0ef41Sopenharmony_ci                              'writePromise3 must reject with the error from abort'),
7021cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.ready,
7031cb0ef41Sopenharmony_ci                              'writer.ready must be still rejected with the error indicating abort'),
7041cb0ef41Sopenharmony_ci      flushAsyncEvents()
7051cb0ef41Sopenharmony_ci    ]);
7061cb0ef41Sopenharmony_ci  }).then(() => {
7071cb0ef41Sopenharmony_ci    assert_array_equals(
7081cb0ef41Sopenharmony_ci        events, [],
7091cb0ef41Sopenharmony_ci        'writePromise, abortPromise and writer.closed must not be fulfilled/rejected yet even after ' +
7101cb0ef41Sopenharmony_ci            'controller.error() call');
7111cb0ef41Sopenharmony_ci
7121cb0ef41Sopenharmony_ci    resolveWrite();
7131cb0ef41Sopenharmony_ci
7141cb0ef41Sopenharmony_ci    return Promise.all([
7151cb0ef41Sopenharmony_ci      writePromise,
7161cb0ef41Sopenharmony_ci      abortPromise,
7171cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.closed,
7181cb0ef41Sopenharmony_ci                              'writer.closed must reject with the error from abort'),
7191cb0ef41Sopenharmony_ci      flushAsyncEvents()
7201cb0ef41Sopenharmony_ci    ]);
7211cb0ef41Sopenharmony_ci  }).then(() => {
7221cb0ef41Sopenharmony_ci    assert_array_equals(events, ['writePromise', 'abortPromise', 'closed'],
7231cb0ef41Sopenharmony_ci                        'writePromise, abortPromise and writer.closed must settle');
7241cb0ef41Sopenharmony_ci
7251cb0ef41Sopenharmony_ci    const writePromise4 = writer.write('a');
7261cb0ef41Sopenharmony_ci
7271cb0ef41Sopenharmony_ci    return Promise.all([
7281cb0ef41Sopenharmony_ci      writePromise,
7291cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writePromise4,
7301cb0ef41Sopenharmony_ci                              'writePromise4 must reject with the error from abort'),
7311cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.ready,
7321cb0ef41Sopenharmony_ci                              'writer.ready must be still rejected with the error indicating abort')
7331cb0ef41Sopenharmony_ci    ]);
7341cb0ef41Sopenharmony_ci  }).then(() => {
7351cb0ef41Sopenharmony_ci    writer.releaseLock();
7361cb0ef41Sopenharmony_ci
7371cb0ef41Sopenharmony_ci    return Promise.all([
7381cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.ready,
7391cb0ef41Sopenharmony_ci                      'writer.ready must be rejected with an error indicating release'),
7401cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.closed,
7411cb0ef41Sopenharmony_ci                      'writer.closed must be rejected with an error indicating release')
7421cb0ef41Sopenharmony_ci    ]);
7431cb0ef41Sopenharmony_ci  });
7441cb0ef41Sopenharmony_ci}, 'writer.abort(), controller.error() while there is an in-flight write, and then finish the write');
7451cb0ef41Sopenharmony_ci
7461cb0ef41Sopenharmony_cipromise_test(t => {
7471cb0ef41Sopenharmony_ci  let resolveClose;
7481cb0ef41Sopenharmony_ci  let controller;
7491cb0ef41Sopenharmony_ci  const ws = new WritableStream({
7501cb0ef41Sopenharmony_ci    start(c) {
7511cb0ef41Sopenharmony_ci      controller = c;
7521cb0ef41Sopenharmony_ci    },
7531cb0ef41Sopenharmony_ci    close() {
7541cb0ef41Sopenharmony_ci      return new Promise(resolve => {
7551cb0ef41Sopenharmony_ci        resolveClose = resolve;
7561cb0ef41Sopenharmony_ci      });
7571cb0ef41Sopenharmony_ci    }
7581cb0ef41Sopenharmony_ci  });
7591cb0ef41Sopenharmony_ci
7601cb0ef41Sopenharmony_ci  let closePromise;
7611cb0ef41Sopenharmony_ci  let abortPromise;
7621cb0ef41Sopenharmony_ci
7631cb0ef41Sopenharmony_ci  const events = [];
7641cb0ef41Sopenharmony_ci
7651cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
7661cb0ef41Sopenharmony_ci
7671cb0ef41Sopenharmony_ci  writer.closed.then(() => {
7681cb0ef41Sopenharmony_ci    events.push('closed');
7691cb0ef41Sopenharmony_ci  });
7701cb0ef41Sopenharmony_ci
7711cb0ef41Sopenharmony_ci  // Wait for ws to start
7721cb0ef41Sopenharmony_ci  return flushAsyncEvents().then(() => {
7731cb0ef41Sopenharmony_ci    closePromise = writer.close();
7741cb0ef41Sopenharmony_ci    closePromise.then(() => {
7751cb0ef41Sopenharmony_ci      events.push('closePromise');
7761cb0ef41Sopenharmony_ci    });
7771cb0ef41Sopenharmony_ci
7781cb0ef41Sopenharmony_ci    abortPromise = writer.abort(error1);
7791cb0ef41Sopenharmony_ci    abortPromise.then(() => {
7801cb0ef41Sopenharmony_ci      events.push('abortPromise');
7811cb0ef41Sopenharmony_ci    });
7821cb0ef41Sopenharmony_ci
7831cb0ef41Sopenharmony_ci    return Promise.all([
7841cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.close(),
7851cb0ef41Sopenharmony_ci        'writer.close() must reject with an error indicating already closing'),
7861cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.ready, 'writer.ready must reject with the error from abort'),
7871cb0ef41Sopenharmony_ci      flushAsyncEvents()
7881cb0ef41Sopenharmony_ci    ]);
7891cb0ef41Sopenharmony_ci  }).then(() => {
7901cb0ef41Sopenharmony_ci    assert_array_equals(events, [], 'closePromise, abortPromise and writer.closed must not be fulfilled/rejected yet');
7911cb0ef41Sopenharmony_ci
7921cb0ef41Sopenharmony_ci    controller.error(error2);
7931cb0ef41Sopenharmony_ci
7941cb0ef41Sopenharmony_ci    return Promise.all([
7951cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.close(),
7961cb0ef41Sopenharmony_ci        'writer.close() must reject with an error indicating already closing'),
7971cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.ready,
7981cb0ef41Sopenharmony_ci                              'writer.ready must be still rejected with the error indicating abort'),
7991cb0ef41Sopenharmony_ci      flushAsyncEvents()
8001cb0ef41Sopenharmony_ci    ]);
8011cb0ef41Sopenharmony_ci  }).then(() => {
8021cb0ef41Sopenharmony_ci    assert_array_equals(
8031cb0ef41Sopenharmony_ci        events, [],
8041cb0ef41Sopenharmony_ci        'closePromise, abortPromise and writer.closed must not be fulfilled/rejected yet even after ' +
8051cb0ef41Sopenharmony_ci            'controller.error() call');
8061cb0ef41Sopenharmony_ci
8071cb0ef41Sopenharmony_ci    resolveClose();
8081cb0ef41Sopenharmony_ci
8091cb0ef41Sopenharmony_ci    return Promise.all([
8101cb0ef41Sopenharmony_ci      closePromise,
8111cb0ef41Sopenharmony_ci      abortPromise,
8121cb0ef41Sopenharmony_ci      writer.closed,
8131cb0ef41Sopenharmony_ci      flushAsyncEvents()
8141cb0ef41Sopenharmony_ci    ]);
8151cb0ef41Sopenharmony_ci  }).then(() => {
8161cb0ef41Sopenharmony_ci    assert_array_equals(events, ['closePromise', 'abortPromise', 'closed'],
8171cb0ef41Sopenharmony_ci                        'closedPromise, abortPromise and writer.closed must fulfill');
8181cb0ef41Sopenharmony_ci
8191cb0ef41Sopenharmony_ci    return Promise.all([
8201cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.close(),
8211cb0ef41Sopenharmony_ci        'writer.close() must reject with an error indicating already closing'),
8221cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writer.ready,
8231cb0ef41Sopenharmony_ci                              'writer.ready must be still rejected with the error indicating abort')
8241cb0ef41Sopenharmony_ci    ]);
8251cb0ef41Sopenharmony_ci  }).then(() => {
8261cb0ef41Sopenharmony_ci    writer.releaseLock();
8271cb0ef41Sopenharmony_ci
8281cb0ef41Sopenharmony_ci    return Promise.all([
8291cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.close(),
8301cb0ef41Sopenharmony_ci        'writer.close() must reject with an error indicating release'),
8311cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.ready,
8321cb0ef41Sopenharmony_ci                      'writer.ready must be rejected with an error indicating release'),
8331cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.closed,
8341cb0ef41Sopenharmony_ci                      'writer.closed must be rejected with an error indicating release')
8351cb0ef41Sopenharmony_ci    ]);
8361cb0ef41Sopenharmony_ci  });
8371cb0ef41Sopenharmony_ci}, 'writer.abort(), controller.error() while there is an in-flight close, and then finish the close');
8381cb0ef41Sopenharmony_ci
8391cb0ef41Sopenharmony_cipromise_test(t => {
8401cb0ef41Sopenharmony_ci  let resolveWrite;
8411cb0ef41Sopenharmony_ci  let controller;
8421cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
8431cb0ef41Sopenharmony_ci    write(chunk, c) {
8441cb0ef41Sopenharmony_ci      controller = c;
8451cb0ef41Sopenharmony_ci      return new Promise(resolve => {
8461cb0ef41Sopenharmony_ci        resolveWrite = resolve;
8471cb0ef41Sopenharmony_ci      });
8481cb0ef41Sopenharmony_ci    }
8491cb0ef41Sopenharmony_ci  });
8501cb0ef41Sopenharmony_ci
8511cb0ef41Sopenharmony_ci  let writePromise;
8521cb0ef41Sopenharmony_ci  let abortPromise;
8531cb0ef41Sopenharmony_ci
8541cb0ef41Sopenharmony_ci  const events = [];
8551cb0ef41Sopenharmony_ci
8561cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
8571cb0ef41Sopenharmony_ci
8581cb0ef41Sopenharmony_ci  writer.closed.catch(() => {
8591cb0ef41Sopenharmony_ci    events.push('closed');
8601cb0ef41Sopenharmony_ci  });
8611cb0ef41Sopenharmony_ci
8621cb0ef41Sopenharmony_ci  // Wait for ws to start
8631cb0ef41Sopenharmony_ci  return flushAsyncEvents().then(() => {
8641cb0ef41Sopenharmony_ci    writePromise = writer.write('a');
8651cb0ef41Sopenharmony_ci    writePromise.then(() => {
8661cb0ef41Sopenharmony_ci      events.push('writePromise');
8671cb0ef41Sopenharmony_ci    });
8681cb0ef41Sopenharmony_ci
8691cb0ef41Sopenharmony_ci    controller.error(error2);
8701cb0ef41Sopenharmony_ci
8711cb0ef41Sopenharmony_ci    const writePromise2 = writer.write('a');
8721cb0ef41Sopenharmony_ci
8731cb0ef41Sopenharmony_ci    return Promise.all([
8741cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writePromise2,
8751cb0ef41Sopenharmony_ci                              'writePromise2 must reject with the error passed to the controller\'s error method'),
8761cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writer.ready,
8771cb0ef41Sopenharmony_ci                              'writer.ready must reject with the error passed to the controller\'s error method'),
8781cb0ef41Sopenharmony_ci      flushAsyncEvents()
8791cb0ef41Sopenharmony_ci    ]);
8801cb0ef41Sopenharmony_ci  }).then(() => {
8811cb0ef41Sopenharmony_ci    assert_array_equals(events, [], 'writePromise and writer.closed must not be fulfilled/rejected yet');
8821cb0ef41Sopenharmony_ci
8831cb0ef41Sopenharmony_ci    abortPromise = writer.abort(error1);
8841cb0ef41Sopenharmony_ci    abortPromise.catch(() => {
8851cb0ef41Sopenharmony_ci      events.push('abortPromise');
8861cb0ef41Sopenharmony_ci    });
8871cb0ef41Sopenharmony_ci
8881cb0ef41Sopenharmony_ci    const writePromise3 = writer.write('a');
8891cb0ef41Sopenharmony_ci
8901cb0ef41Sopenharmony_ci    return Promise.all([
8911cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writePromise3,
8921cb0ef41Sopenharmony_ci                              'writePromise3 must reject with the error passed to the controller\'s error method'),
8931cb0ef41Sopenharmony_ci      flushAsyncEvents()
8941cb0ef41Sopenharmony_ci    ]);
8951cb0ef41Sopenharmony_ci  }).then(() => {
8961cb0ef41Sopenharmony_ci    assert_array_equals(
8971cb0ef41Sopenharmony_ci        events, [],
8981cb0ef41Sopenharmony_ci        'writePromise and writer.closed must not be fulfilled/rejected yet even after writer.abort()');
8991cb0ef41Sopenharmony_ci
9001cb0ef41Sopenharmony_ci    resolveWrite();
9011cb0ef41Sopenharmony_ci
9021cb0ef41Sopenharmony_ci    return Promise.all([
9031cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, abortPromise,
9041cb0ef41Sopenharmony_ci                              'abort() must reject with the error passed to the controller\'s error method'),
9051cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writer.closed,
9061cb0ef41Sopenharmony_ci                              'writer.closed must reject with the error passed to the controller\'s error method'),
9071cb0ef41Sopenharmony_ci      flushAsyncEvents()
9081cb0ef41Sopenharmony_ci    ]);
9091cb0ef41Sopenharmony_ci  }).then(() => {
9101cb0ef41Sopenharmony_ci    assert_array_equals(events, ['writePromise', 'abortPromise', 'closed'],
9111cb0ef41Sopenharmony_ci                        'writePromise, abortPromise and writer.closed must fulfill/reject');
9121cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['write', 'a'], 'sink abort() should not be called');
9131cb0ef41Sopenharmony_ci
9141cb0ef41Sopenharmony_ci    const writePromise4 = writer.write('a');
9151cb0ef41Sopenharmony_ci
9161cb0ef41Sopenharmony_ci    return Promise.all([
9171cb0ef41Sopenharmony_ci      writePromise,
9181cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writePromise4,
9191cb0ef41Sopenharmony_ci                              'writePromise4 must reject with the error passed to the controller\'s error method'),
9201cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writer.ready,
9211cb0ef41Sopenharmony_ci                              'writer.ready must be still rejected with the error passed to the controller\'s error method')
9221cb0ef41Sopenharmony_ci    ]);
9231cb0ef41Sopenharmony_ci  }).then(() => {
9241cb0ef41Sopenharmony_ci    writer.releaseLock();
9251cb0ef41Sopenharmony_ci
9261cb0ef41Sopenharmony_ci    return Promise.all([
9271cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.ready,
9281cb0ef41Sopenharmony_ci                      'writer.ready must be rejected with an error indicating release'),
9291cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.closed,
9301cb0ef41Sopenharmony_ci                      'writer.closed must be rejected with an error indicating release')
9311cb0ef41Sopenharmony_ci    ]);
9321cb0ef41Sopenharmony_ci  });
9331cb0ef41Sopenharmony_ci}, 'controller.error(), writer.abort() while there is an in-flight write, and then finish the write');
9341cb0ef41Sopenharmony_ci
9351cb0ef41Sopenharmony_cipromise_test(t => {
9361cb0ef41Sopenharmony_ci  let resolveClose;
9371cb0ef41Sopenharmony_ci  let controller;
9381cb0ef41Sopenharmony_ci  const ws = new WritableStream({
9391cb0ef41Sopenharmony_ci    start(c) {
9401cb0ef41Sopenharmony_ci      controller = c;
9411cb0ef41Sopenharmony_ci    },
9421cb0ef41Sopenharmony_ci    close() {
9431cb0ef41Sopenharmony_ci      return new Promise(resolve => {
9441cb0ef41Sopenharmony_ci        resolveClose = resolve;
9451cb0ef41Sopenharmony_ci      });
9461cb0ef41Sopenharmony_ci    }
9471cb0ef41Sopenharmony_ci  });
9481cb0ef41Sopenharmony_ci
9491cb0ef41Sopenharmony_ci  let closePromise;
9501cb0ef41Sopenharmony_ci  let abortPromise;
9511cb0ef41Sopenharmony_ci
9521cb0ef41Sopenharmony_ci  const events = [];
9531cb0ef41Sopenharmony_ci
9541cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
9551cb0ef41Sopenharmony_ci
9561cb0ef41Sopenharmony_ci  writer.closed.then(() => {
9571cb0ef41Sopenharmony_ci    events.push('closed');
9581cb0ef41Sopenharmony_ci  });
9591cb0ef41Sopenharmony_ci
9601cb0ef41Sopenharmony_ci  // Wait for ws to start
9611cb0ef41Sopenharmony_ci  return flushAsyncEvents().then(() => {
9621cb0ef41Sopenharmony_ci    closePromise = writer.close();
9631cb0ef41Sopenharmony_ci    closePromise.then(() => {
9641cb0ef41Sopenharmony_ci      events.push('closePromise');
9651cb0ef41Sopenharmony_ci    });
9661cb0ef41Sopenharmony_ci
9671cb0ef41Sopenharmony_ci    controller.error(error2);
9681cb0ef41Sopenharmony_ci
9691cb0ef41Sopenharmony_ci    return flushAsyncEvents();
9701cb0ef41Sopenharmony_ci  }).then(() => {
9711cb0ef41Sopenharmony_ci    assert_array_equals(events, [], 'closePromise must not be fulfilled/rejected yet');
9721cb0ef41Sopenharmony_ci
9731cb0ef41Sopenharmony_ci    abortPromise = writer.abort(error1);
9741cb0ef41Sopenharmony_ci    abortPromise.then(() => {
9751cb0ef41Sopenharmony_ci      events.push('abortPromise');
9761cb0ef41Sopenharmony_ci    });
9771cb0ef41Sopenharmony_ci
9781cb0ef41Sopenharmony_ci    return Promise.all([
9791cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writer.ready,
9801cb0ef41Sopenharmony_ci                              'writer.ready must reject with the error passed to the controller\'s error method'),
9811cb0ef41Sopenharmony_ci      flushAsyncEvents()
9821cb0ef41Sopenharmony_ci    ]);
9831cb0ef41Sopenharmony_ci  }).then(() => {
9841cb0ef41Sopenharmony_ci    assert_array_equals(
9851cb0ef41Sopenharmony_ci        events, [],
9861cb0ef41Sopenharmony_ci        'closePromise and writer.closed must not be fulfilled/rejected yet even after writer.abort()');
9871cb0ef41Sopenharmony_ci
9881cb0ef41Sopenharmony_ci    resolveClose();
9891cb0ef41Sopenharmony_ci
9901cb0ef41Sopenharmony_ci    return Promise.all([
9911cb0ef41Sopenharmony_ci      closePromise,
9921cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writer.ready,
9931cb0ef41Sopenharmony_ci                              'writer.ready must be still rejected with the error passed to the controller\'s error method'),
9941cb0ef41Sopenharmony_ci      writer.closed,
9951cb0ef41Sopenharmony_ci      flushAsyncEvents()
9961cb0ef41Sopenharmony_ci    ]);
9971cb0ef41Sopenharmony_ci  }).then(() => {
9981cb0ef41Sopenharmony_ci    assert_array_equals(events, ['closePromise', 'abortPromise', 'closed'],
9991cb0ef41Sopenharmony_ci                        'abortPromise, closePromise and writer.closed must fulfill/reject');
10001cb0ef41Sopenharmony_ci  }).then(() => {
10011cb0ef41Sopenharmony_ci    writer.releaseLock();
10021cb0ef41Sopenharmony_ci
10031cb0ef41Sopenharmony_ci    return Promise.all([
10041cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.ready,
10051cb0ef41Sopenharmony_ci                      'writer.ready must be rejected with an error indicating release'),
10061cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, writer.closed,
10071cb0ef41Sopenharmony_ci                      'writer.closed must be rejected with an error indicating release')
10081cb0ef41Sopenharmony_ci    ]);
10091cb0ef41Sopenharmony_ci  });
10101cb0ef41Sopenharmony_ci}, 'controller.error(), writer.abort() while there is an in-flight close, and then finish the close');
10111cb0ef41Sopenharmony_ci
10121cb0ef41Sopenharmony_cipromise_test(t => {
10131cb0ef41Sopenharmony_ci  let resolveWrite;
10141cb0ef41Sopenharmony_ci  const ws = new WritableStream({
10151cb0ef41Sopenharmony_ci    write() {
10161cb0ef41Sopenharmony_ci      return new Promise(resolve => {
10171cb0ef41Sopenharmony_ci        resolveWrite = resolve;
10181cb0ef41Sopenharmony_ci      });
10191cb0ef41Sopenharmony_ci    }
10201cb0ef41Sopenharmony_ci  });
10211cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
10221cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
10231cb0ef41Sopenharmony_ci    const writePromise = writer.write('a');
10241cb0ef41Sopenharmony_ci    const closed = writer.closed;
10251cb0ef41Sopenharmony_ci    const abortPromise = writer.abort();
10261cb0ef41Sopenharmony_ci    writer.releaseLock();
10271cb0ef41Sopenharmony_ci    resolveWrite();
10281cb0ef41Sopenharmony_ci    return Promise.all([
10291cb0ef41Sopenharmony_ci      writePromise,
10301cb0ef41Sopenharmony_ci      abortPromise,
10311cb0ef41Sopenharmony_ci      promise_rejects_js(t, TypeError, closed, 'closed should reject')]);
10321cb0ef41Sopenharmony_ci  });
10331cb0ef41Sopenharmony_ci}, 'releaseLock() while aborting should reject the original closed promise');
10341cb0ef41Sopenharmony_ci
10351cb0ef41Sopenharmony_ci// TODO(ricea): Consider removing this test if it is no longer useful.
10361cb0ef41Sopenharmony_cipromise_test(t => {
10371cb0ef41Sopenharmony_ci  let resolveWrite;
10381cb0ef41Sopenharmony_ci  let resolveAbort;
10391cb0ef41Sopenharmony_ci  let resolveAbortStarted;
10401cb0ef41Sopenharmony_ci  const abortStarted = new Promise(resolve => {
10411cb0ef41Sopenharmony_ci    resolveAbortStarted = resolve;
10421cb0ef41Sopenharmony_ci  });
10431cb0ef41Sopenharmony_ci  const ws = new WritableStream({
10441cb0ef41Sopenharmony_ci    write() {
10451cb0ef41Sopenharmony_ci      return new Promise(resolve => {
10461cb0ef41Sopenharmony_ci        resolveWrite = resolve;
10471cb0ef41Sopenharmony_ci      });
10481cb0ef41Sopenharmony_ci    },
10491cb0ef41Sopenharmony_ci    abort() {
10501cb0ef41Sopenharmony_ci      resolveAbortStarted();
10511cb0ef41Sopenharmony_ci      return new Promise(resolve => {
10521cb0ef41Sopenharmony_ci        resolveAbort = resolve;
10531cb0ef41Sopenharmony_ci      });
10541cb0ef41Sopenharmony_ci    }
10551cb0ef41Sopenharmony_ci  });
10561cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
10571cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
10581cb0ef41Sopenharmony_ci    const writePromise = writer.write('a');
10591cb0ef41Sopenharmony_ci    const closed = writer.closed;
10601cb0ef41Sopenharmony_ci    const abortPromise = writer.abort();
10611cb0ef41Sopenharmony_ci    resolveWrite();
10621cb0ef41Sopenharmony_ci    return abortStarted.then(() => {
10631cb0ef41Sopenharmony_ci      writer.releaseLock();
10641cb0ef41Sopenharmony_ci      assert_equals(writer.closed, closed, 'closed promise should not have changed');
10651cb0ef41Sopenharmony_ci      resolveAbort();
10661cb0ef41Sopenharmony_ci      return Promise.all([
10671cb0ef41Sopenharmony_ci        writePromise,
10681cb0ef41Sopenharmony_ci        abortPromise,
10691cb0ef41Sopenharmony_ci        promise_rejects_js(t, TypeError, closed, 'closed should reject')]);
10701cb0ef41Sopenharmony_ci    });
10711cb0ef41Sopenharmony_ci  });
10721cb0ef41Sopenharmony_ci}, 'releaseLock() during delayed async abort() should reject the writer.closed promise');
10731cb0ef41Sopenharmony_ci
10741cb0ef41Sopenharmony_cipromise_test(() => {
10751cb0ef41Sopenharmony_ci  let resolveStart;
10761cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
10771cb0ef41Sopenharmony_ci    start() {
10781cb0ef41Sopenharmony_ci      return new Promise(resolve => {
10791cb0ef41Sopenharmony_ci        resolveStart = resolve;
10801cb0ef41Sopenharmony_ci      });
10811cb0ef41Sopenharmony_ci    }
10821cb0ef41Sopenharmony_ci  });
10831cb0ef41Sopenharmony_ci  const abortPromise = ws.abort('done');
10841cb0ef41Sopenharmony_ci  return flushAsyncEvents().then(() => {
10851cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, [], 'abort() should not be called during start()');
10861cb0ef41Sopenharmony_ci    resolveStart();
10871cb0ef41Sopenharmony_ci    return abortPromise.then(() => {
10881cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['abort', 'done'], 'abort() should be called after start() is done');
10891cb0ef41Sopenharmony_ci    });
10901cb0ef41Sopenharmony_ci  });
10911cb0ef41Sopenharmony_ci}, 'sink abort() should not be called until sink start() is done');
10921cb0ef41Sopenharmony_ci
10931cb0ef41Sopenharmony_cipromise_test(() => {
10941cb0ef41Sopenharmony_ci  let resolveStart;
10951cb0ef41Sopenharmony_ci  let controller;
10961cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
10971cb0ef41Sopenharmony_ci    start(c) {
10981cb0ef41Sopenharmony_ci      controller = c;
10991cb0ef41Sopenharmony_ci      return new Promise(resolve => {
11001cb0ef41Sopenharmony_ci        resolveStart = resolve;
11011cb0ef41Sopenharmony_ci      });
11021cb0ef41Sopenharmony_ci    }
11031cb0ef41Sopenharmony_ci  });
11041cb0ef41Sopenharmony_ci  const abortPromise = ws.abort('done');
11051cb0ef41Sopenharmony_ci  controller.error(error1);
11061cb0ef41Sopenharmony_ci  resolveStart();
11071cb0ef41Sopenharmony_ci  return abortPromise.then(() =>
11081cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['abort', 'done'],
11091cb0ef41Sopenharmony_ci                          'abort() should still be called if start() errors the controller'));
11101cb0ef41Sopenharmony_ci}, 'if start attempts to error the controller after abort() has been called, then it should lose');
11111cb0ef41Sopenharmony_ci
11121cb0ef41Sopenharmony_cipromise_test(() => {
11131cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
11141cb0ef41Sopenharmony_ci    start() {
11151cb0ef41Sopenharmony_ci      return Promise.reject(error1);
11161cb0ef41Sopenharmony_ci    }
11171cb0ef41Sopenharmony_ci  });
11181cb0ef41Sopenharmony_ci  return ws.abort('done').then(() =>
11191cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['abort', 'done'], 'abort() should still be called if start() rejects'));
11201cb0ef41Sopenharmony_ci}, 'stream abort() promise should still resolve if sink start() rejects');
11211cb0ef41Sopenharmony_ci
11221cb0ef41Sopenharmony_cipromise_test(t => {
11231cb0ef41Sopenharmony_ci  const ws = new WritableStream();
11241cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
11251cb0ef41Sopenharmony_ci  const writerReady1 = writer.ready;
11261cb0ef41Sopenharmony_ci  writer.abort(error1);
11271cb0ef41Sopenharmony_ci  const writerReady2 = writer.ready;
11281cb0ef41Sopenharmony_ci  assert_not_equals(writerReady1, writerReady2, 'abort() should replace the ready promise with a rejected one');
11291cb0ef41Sopenharmony_ci  return Promise.all([writerReady1,
11301cb0ef41Sopenharmony_ci                      promise_rejects_exactly(t, error1, writerReady2, 'writerReady2 should reject')]);
11311cb0ef41Sopenharmony_ci}, 'writer abort() during sink start() should replace the writer.ready promise synchronously');
11321cb0ef41Sopenharmony_ci
11331cb0ef41Sopenharmony_cipromise_test(t => {
11341cb0ef41Sopenharmony_ci  const events = [];
11351cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
11361cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
11371cb0ef41Sopenharmony_ci  const writePromise1 = writer.write(1);
11381cb0ef41Sopenharmony_ci  const abortPromise = writer.abort(error1);
11391cb0ef41Sopenharmony_ci  const writePromise2 = writer.write(2);
11401cb0ef41Sopenharmony_ci  const closePromise = writer.close();
11411cb0ef41Sopenharmony_ci  writePromise1.catch(() => events.push('write1'));
11421cb0ef41Sopenharmony_ci  abortPromise.then(() => events.push('abort'));
11431cb0ef41Sopenharmony_ci  writePromise2.catch(() => events.push('write2'));
11441cb0ef41Sopenharmony_ci  closePromise.catch(() => events.push('close'));
11451cb0ef41Sopenharmony_ci  return Promise.all([
11461cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, writePromise1, 'first write() should reject'),
11471cb0ef41Sopenharmony_ci    abortPromise,
11481cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, writePromise2, 'second write() should reject'),
11491cb0ef41Sopenharmony_ci    promise_rejects_exactly(t, error1, closePromise, 'close() should reject')
11501cb0ef41Sopenharmony_ci  ])
11511cb0ef41Sopenharmony_ci  .then(() => {
11521cb0ef41Sopenharmony_ci    assert_array_equals(events, ['write2', 'write1', 'abort', 'close'],
11531cb0ef41Sopenharmony_ci                        'promises should resolve in the standard order');
11541cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['abort', error1], 'underlying sink write() should not be called');
11551cb0ef41Sopenharmony_ci  });
11561cb0ef41Sopenharmony_ci}, 'promises returned from other writer methods should be rejected when writer abort() happens during sink start()');
11571cb0ef41Sopenharmony_ci
11581cb0ef41Sopenharmony_cipromise_test(t => {
11591cb0ef41Sopenharmony_ci  let writeReject;
11601cb0ef41Sopenharmony_ci  let controller;
11611cb0ef41Sopenharmony_ci  const ws = new WritableStream({
11621cb0ef41Sopenharmony_ci    write(chunk, c) {
11631cb0ef41Sopenharmony_ci      controller = c;
11641cb0ef41Sopenharmony_ci      return new Promise((resolve, reject) => {
11651cb0ef41Sopenharmony_ci        writeReject = reject;
11661cb0ef41Sopenharmony_ci      });
11671cb0ef41Sopenharmony_ci    }
11681cb0ef41Sopenharmony_ci  });
11691cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
11701cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
11711cb0ef41Sopenharmony_ci    const writePromise = writer.write('a');
11721cb0ef41Sopenharmony_ci    const abortPromise = writer.abort();
11731cb0ef41Sopenharmony_ci    controller.error(error1);
11741cb0ef41Sopenharmony_ci    writeReject(error2);
11751cb0ef41Sopenharmony_ci    return Promise.all([
11761cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writePromise, 'write() should reject with error2'),
11771cb0ef41Sopenharmony_ci      abortPromise
11781cb0ef41Sopenharmony_ci    ]);
11791cb0ef41Sopenharmony_ci  });
11801cb0ef41Sopenharmony_ci}, 'abort() should succeed despite rejection from write');
11811cb0ef41Sopenharmony_ci
11821cb0ef41Sopenharmony_cipromise_test(t => {
11831cb0ef41Sopenharmony_ci  let closeReject;
11841cb0ef41Sopenharmony_ci  let controller;
11851cb0ef41Sopenharmony_ci  const ws = new WritableStream({
11861cb0ef41Sopenharmony_ci    start(c) {
11871cb0ef41Sopenharmony_ci      controller = c;
11881cb0ef41Sopenharmony_ci    },
11891cb0ef41Sopenharmony_ci    close() {
11901cb0ef41Sopenharmony_ci      return new Promise((resolve, reject) => {
11911cb0ef41Sopenharmony_ci        closeReject = reject;
11921cb0ef41Sopenharmony_ci      });
11931cb0ef41Sopenharmony_ci    }
11941cb0ef41Sopenharmony_ci  });
11951cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
11961cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
11971cb0ef41Sopenharmony_ci    const closePromise = writer.close();
11981cb0ef41Sopenharmony_ci    const abortPromise = writer.abort();
11991cb0ef41Sopenharmony_ci    controller.error(error1);
12001cb0ef41Sopenharmony_ci    closeReject(error2);
12011cb0ef41Sopenharmony_ci    return Promise.all([
12021cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, closePromise, 'close() should reject with error2'),
12031cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, abortPromise, 'abort() should reject with error2')
12041cb0ef41Sopenharmony_ci    ]);
12051cb0ef41Sopenharmony_ci  });
12061cb0ef41Sopenharmony_ci}, 'abort() should be rejected with the rejection returned from close()');
12071cb0ef41Sopenharmony_ci
12081cb0ef41Sopenharmony_cipromise_test(t => {
12091cb0ef41Sopenharmony_ci  let rejectWrite;
12101cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
12111cb0ef41Sopenharmony_ci    write() {
12121cb0ef41Sopenharmony_ci      return new Promise((resolve, reject) => {
12131cb0ef41Sopenharmony_ci        rejectWrite = reject;
12141cb0ef41Sopenharmony_ci      });
12151cb0ef41Sopenharmony_ci    }
12161cb0ef41Sopenharmony_ci  });
12171cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
12181cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
12191cb0ef41Sopenharmony_ci    const writePromise = writer.write('1');
12201cb0ef41Sopenharmony_ci    const abortPromise = writer.abort(error2);
12211cb0ef41Sopenharmony_ci    rejectWrite(error1);
12221cb0ef41Sopenharmony_ci    return Promise.all([
12231cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, writePromise, 'write should reject'),
12241cb0ef41Sopenharmony_ci      abortPromise,
12251cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, writer.closed, 'closed should reject with error2')
12261cb0ef41Sopenharmony_ci    ]);
12271cb0ef41Sopenharmony_ci  }).then(() => {
12281cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['write', '1', 'abort', error2], 'abort sink method should be called');
12291cb0ef41Sopenharmony_ci  });
12301cb0ef41Sopenharmony_ci}, 'a rejecting sink.write() should not prevent sink.abort() from being called');
12311cb0ef41Sopenharmony_ci
12321cb0ef41Sopenharmony_cipromise_test(() => {
12331cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
12341cb0ef41Sopenharmony_ci    start() {
12351cb0ef41Sopenharmony_ci      return Promise.reject(error1);
12361cb0ef41Sopenharmony_ci    }
12371cb0ef41Sopenharmony_ci  });
12381cb0ef41Sopenharmony_ci  return ws.abort(error2)
12391cb0ef41Sopenharmony_ci      .then(() => {
12401cb0ef41Sopenharmony_ci        assert_array_equals(ws.events, ['abort', error2]);
12411cb0ef41Sopenharmony_ci      });
12421cb0ef41Sopenharmony_ci}, 'when start errors after stream abort(), underlying sink abort() should be called anyway');
12431cb0ef41Sopenharmony_ci
12441cb0ef41Sopenharmony_cipromise_test(() => {
12451cb0ef41Sopenharmony_ci  const ws = new WritableStream();
12461cb0ef41Sopenharmony_ci  const abortPromise1 = ws.abort();
12471cb0ef41Sopenharmony_ci  const abortPromise2 = ws.abort();
12481cb0ef41Sopenharmony_ci  assert_equals(abortPromise1, abortPromise2, 'the promises must be the same');
12491cb0ef41Sopenharmony_ci
12501cb0ef41Sopenharmony_ci  return abortPromise1.then(
12511cb0ef41Sopenharmony_ci      v => assert_equals(v, undefined, 'abort() should fulfill with undefined'));
12521cb0ef41Sopenharmony_ci}, 'when calling abort() twice on the same stream, both should give the same promise that fulfills with undefined');
12531cb0ef41Sopenharmony_ci
12541cb0ef41Sopenharmony_cipromise_test(() => {
12551cb0ef41Sopenharmony_ci  const ws = new WritableStream();
12561cb0ef41Sopenharmony_ci  const abortPromise1 = ws.abort();
12571cb0ef41Sopenharmony_ci
12581cb0ef41Sopenharmony_ci  return abortPromise1.then(v1 => {
12591cb0ef41Sopenharmony_ci    assert_equals(v1, undefined, 'first abort() should fulfill with undefined');
12601cb0ef41Sopenharmony_ci
12611cb0ef41Sopenharmony_ci    const abortPromise2 = ws.abort();
12621cb0ef41Sopenharmony_ci    assert_not_equals(abortPromise2, abortPromise1, 'because we waited, the second promise should be a new promise');
12631cb0ef41Sopenharmony_ci
12641cb0ef41Sopenharmony_ci    return abortPromise2.then(v2 => {
12651cb0ef41Sopenharmony_ci      assert_equals(v2, undefined, 'second abort() should fulfill with undefined');
12661cb0ef41Sopenharmony_ci    });
12671cb0ef41Sopenharmony_ci  });
12681cb0ef41Sopenharmony_ci}, 'when calling abort() twice on the same stream, but sequentially so so there\'s no pending abort the second time, ' +
12691cb0ef41Sopenharmony_ci   'both should fulfill with undefined');
12701cb0ef41Sopenharmony_ci
12711cb0ef41Sopenharmony_cipromise_test(t => {
12721cb0ef41Sopenharmony_ci  const ws = new WritableStream({
12731cb0ef41Sopenharmony_ci    start(c) {
12741cb0ef41Sopenharmony_ci      c.error(error1);
12751cb0ef41Sopenharmony_ci    }
12761cb0ef41Sopenharmony_ci  });
12771cb0ef41Sopenharmony_ci
12781cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
12791cb0ef41Sopenharmony_ci
12801cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, writer.closed, 'writer.closed should reject').then(() => {
12811cb0ef41Sopenharmony_ci    return writer.abort().then(
12821cb0ef41Sopenharmony_ci      v => assert_equals(v, undefined, 'abort() should fulfill with undefined'));
12831cb0ef41Sopenharmony_ci  });
12841cb0ef41Sopenharmony_ci}, 'calling abort() on an errored stream should fulfill with undefined');
12851cb0ef41Sopenharmony_ci
12861cb0ef41Sopenharmony_cipromise_test(t => {
12871cb0ef41Sopenharmony_ci  let controller;
12881cb0ef41Sopenharmony_ci  let resolveWrite;
12891cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
12901cb0ef41Sopenharmony_ci    start(c) {
12911cb0ef41Sopenharmony_ci      controller = c;
12921cb0ef41Sopenharmony_ci    },
12931cb0ef41Sopenharmony_ci    write() {
12941cb0ef41Sopenharmony_ci      return new Promise(resolve => {
12951cb0ef41Sopenharmony_ci        resolveWrite = resolve;
12961cb0ef41Sopenharmony_ci      });
12971cb0ef41Sopenharmony_ci    }
12981cb0ef41Sopenharmony_ci  });
12991cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
13001cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
13011cb0ef41Sopenharmony_ci    const writePromise = writer.write('chunk');
13021cb0ef41Sopenharmony_ci    controller.error(error1);
13031cb0ef41Sopenharmony_ci    const abortPromise = writer.abort(error2);
13041cb0ef41Sopenharmony_ci    resolveWrite();
13051cb0ef41Sopenharmony_ci    return Promise.all([
13061cb0ef41Sopenharmony_ci      writePromise,
13071cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, abortPromise, 'abort() should reject')
13081cb0ef41Sopenharmony_ci    ]).then(() => {
13091cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['write', 'chunk'], 'sink abort() should not be called');
13101cb0ef41Sopenharmony_ci    });
13111cb0ef41Sopenharmony_ci  });
13121cb0ef41Sopenharmony_ci}, 'sink abort() should not be called if stream was erroring due to controller.error() before abort() was called');
13131cb0ef41Sopenharmony_ci
13141cb0ef41Sopenharmony_cipromise_test(t => {
13151cb0ef41Sopenharmony_ci  let resolveWrite;
13161cb0ef41Sopenharmony_ci  let size = 1;
13171cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
13181cb0ef41Sopenharmony_ci    write() {
13191cb0ef41Sopenharmony_ci      return new Promise(resolve => {
13201cb0ef41Sopenharmony_ci        resolveWrite = resolve;
13211cb0ef41Sopenharmony_ci      });
13221cb0ef41Sopenharmony_ci    }
13231cb0ef41Sopenharmony_ci  }, {
13241cb0ef41Sopenharmony_ci    size() {
13251cb0ef41Sopenharmony_ci      return size;
13261cb0ef41Sopenharmony_ci    },
13271cb0ef41Sopenharmony_ci    highWaterMark: 1
13281cb0ef41Sopenharmony_ci  });
13291cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
13301cb0ef41Sopenharmony_ci  return writer.ready.then(() => {
13311cb0ef41Sopenharmony_ci    const writePromise1 = writer.write('chunk1');
13321cb0ef41Sopenharmony_ci    size = NaN;
13331cb0ef41Sopenharmony_ci    const writePromise2 = writer.write('chunk2');
13341cb0ef41Sopenharmony_ci    const abortPromise = writer.abort(error2);
13351cb0ef41Sopenharmony_ci    resolveWrite();
13361cb0ef41Sopenharmony_ci    return Promise.all([
13371cb0ef41Sopenharmony_ci      writePromise1,
13381cb0ef41Sopenharmony_ci      promise_rejects_js(t, RangeError, writePromise2, 'second write() should reject'),
13391cb0ef41Sopenharmony_ci      promise_rejects_js(t, RangeError, abortPromise, 'abort() should reject')
13401cb0ef41Sopenharmony_ci    ]).then(() => {
13411cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['write', 'chunk1'], 'sink abort() should not be called');
13421cb0ef41Sopenharmony_ci    });
13431cb0ef41Sopenharmony_ci  });
13441cb0ef41Sopenharmony_ci}, 'sink abort() should not be called if stream was erroring due to bad strategy before abort() was called');
13451cb0ef41Sopenharmony_ci
13461cb0ef41Sopenharmony_cipromise_test(t => {
13471cb0ef41Sopenharmony_ci  const ws = new WritableStream();
13481cb0ef41Sopenharmony_ci  return ws.abort().then(() => {
13491cb0ef41Sopenharmony_ci    const writer = ws.getWriter();
13501cb0ef41Sopenharmony_ci    return writer.closed.then(t.unreached_func('closed promise should not fulfill'),
13511cb0ef41Sopenharmony_ci                              e => assert_equals(e, undefined, 'e should be undefined'));
13521cb0ef41Sopenharmony_ci  });
13531cb0ef41Sopenharmony_ci}, 'abort with no arguments should set the stored error to undefined');
13541cb0ef41Sopenharmony_ci
13551cb0ef41Sopenharmony_cipromise_test(t => {
13561cb0ef41Sopenharmony_ci  const ws = new WritableStream();
13571cb0ef41Sopenharmony_ci  return ws.abort(undefined).then(() => {
13581cb0ef41Sopenharmony_ci    const writer = ws.getWriter();
13591cb0ef41Sopenharmony_ci    return writer.closed.then(t.unreached_func('closed promise should not fulfill'),
13601cb0ef41Sopenharmony_ci                              e => assert_equals(e, undefined, 'e should be undefined'));
13611cb0ef41Sopenharmony_ci  });
13621cb0ef41Sopenharmony_ci}, 'abort with an undefined argument should set the stored error to undefined');
13631cb0ef41Sopenharmony_ci
13641cb0ef41Sopenharmony_cipromise_test(t => {
13651cb0ef41Sopenharmony_ci  const ws = new WritableStream();
13661cb0ef41Sopenharmony_ci  return ws.abort('string argument').then(() => {
13671cb0ef41Sopenharmony_ci    const writer = ws.getWriter();
13681cb0ef41Sopenharmony_ci    return writer.closed.then(t.unreached_func('closed promise should not fulfill'),
13691cb0ef41Sopenharmony_ci                              e => assert_equals(e, 'string argument', 'e should be \'string argument\''));
13701cb0ef41Sopenharmony_ci  });
13711cb0ef41Sopenharmony_ci}, 'abort with a string argument should set the stored error to that argument');
13721cb0ef41Sopenharmony_ci
13731cb0ef41Sopenharmony_cipromise_test(t => {
13741cb0ef41Sopenharmony_ci  const ws = new WritableStream();
13751cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
13761cb0ef41Sopenharmony_ci  return promise_rejects_js(t, TypeError, ws.abort(), 'abort should reject')
13771cb0ef41Sopenharmony_ci    .then(() => writer.ready);
13781cb0ef41Sopenharmony_ci}, 'abort on a locked stream should reject');
13791cb0ef41Sopenharmony_ci
13801cb0ef41Sopenharmony_citest(t => {
13811cb0ef41Sopenharmony_ci  let ctrl;
13821cb0ef41Sopenharmony_ci  const ws = new WritableStream({start(c) { ctrl = c; }});
13831cb0ef41Sopenharmony_ci  const e = Error('hello');
13841cb0ef41Sopenharmony_ci
13851cb0ef41Sopenharmony_ci  assert_true(ctrl.signal instanceof AbortSignal);
13861cb0ef41Sopenharmony_ci  assert_false(ctrl.signal.aborted);
13871cb0ef41Sopenharmony_ci  assert_equals(ctrl.signal.reason, undefined, 'signal.reason before abort');
13881cb0ef41Sopenharmony_ci  ws.abort(e);
13891cb0ef41Sopenharmony_ci  assert_true(ctrl.signal.aborted);
13901cb0ef41Sopenharmony_ci  assert_equals(ctrl.signal.reason, e);
13911cb0ef41Sopenharmony_ci}, 'WritableStreamDefaultController.signal');
13921cb0ef41Sopenharmony_ci
13931cb0ef41Sopenharmony_cipromise_test(async t => {
13941cb0ef41Sopenharmony_ci  let ctrl;
13951cb0ef41Sopenharmony_ci  let resolve;
13961cb0ef41Sopenharmony_ci  const called = new Promise(r => resolve = r);
13971cb0ef41Sopenharmony_ci
13981cb0ef41Sopenharmony_ci  const ws = new WritableStream({
13991cb0ef41Sopenharmony_ci    start(c) { ctrl = c; },
14001cb0ef41Sopenharmony_ci    write() { resolve(); return new Promise(() => {}); }
14011cb0ef41Sopenharmony_ci  });
14021cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
14031cb0ef41Sopenharmony_ci
14041cb0ef41Sopenharmony_ci  writer.write(99);
14051cb0ef41Sopenharmony_ci  await called;
14061cb0ef41Sopenharmony_ci
14071cb0ef41Sopenharmony_ci  assert_false(ctrl.signal.aborted);
14081cb0ef41Sopenharmony_ci  assert_equals(ctrl.signal.reason, undefined, 'signal.reason before abort');
14091cb0ef41Sopenharmony_ci  writer.abort();
14101cb0ef41Sopenharmony_ci  assert_true(ctrl.signal.aborted);
14111cb0ef41Sopenharmony_ci  assert_true(ctrl.signal.reason instanceof DOMException, 'signal.reason is a DOMException');
14121cb0ef41Sopenharmony_ci  assert_equals(ctrl.signal.reason.name, 'AbortError', 'signal.reason is an AbortError');
14131cb0ef41Sopenharmony_ci}, 'the abort signal is signalled synchronously - write');
14141cb0ef41Sopenharmony_ci
14151cb0ef41Sopenharmony_cipromise_test(async t => {
14161cb0ef41Sopenharmony_ci  let ctrl;
14171cb0ef41Sopenharmony_ci  let resolve;
14181cb0ef41Sopenharmony_ci  const called = new Promise(r => resolve = r);
14191cb0ef41Sopenharmony_ci
14201cb0ef41Sopenharmony_ci  const ws = new WritableStream({
14211cb0ef41Sopenharmony_ci    start(c) { ctrl = c; },
14221cb0ef41Sopenharmony_ci    close() { resolve(); return new Promise(() => {}); }
14231cb0ef41Sopenharmony_ci  });
14241cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
14251cb0ef41Sopenharmony_ci
14261cb0ef41Sopenharmony_ci  writer.close(99);
14271cb0ef41Sopenharmony_ci  await called;
14281cb0ef41Sopenharmony_ci
14291cb0ef41Sopenharmony_ci  assert_false(ctrl.signal.aborted);
14301cb0ef41Sopenharmony_ci  writer.abort();
14311cb0ef41Sopenharmony_ci  assert_true(ctrl.signal.aborted);
14321cb0ef41Sopenharmony_ci}, 'the abort signal is signalled synchronously - close');
14331cb0ef41Sopenharmony_ci
14341cb0ef41Sopenharmony_cipromise_test(async t => {
14351cb0ef41Sopenharmony_ci  let ctrl;
14361cb0ef41Sopenharmony_ci  const ws = new WritableStream({start(c) { ctrl = c; }});
14371cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
14381cb0ef41Sopenharmony_ci
14391cb0ef41Sopenharmony_ci  const e = TypeError();
14401cb0ef41Sopenharmony_ci  ctrl.error(e);
14411cb0ef41Sopenharmony_ci  await promise_rejects_exactly(t, e, writer.closed);
14421cb0ef41Sopenharmony_ci  assert_false(ctrl.signal.aborted);
14431cb0ef41Sopenharmony_ci}, 'the abort signal is not signalled on error');
14441cb0ef41Sopenharmony_ci
14451cb0ef41Sopenharmony_cipromise_test(async t => {
14461cb0ef41Sopenharmony_ci  let ctrl;
14471cb0ef41Sopenharmony_ci  const e = TypeError();
14481cb0ef41Sopenharmony_ci  const ws = new WritableStream({
14491cb0ef41Sopenharmony_ci    start(c) { ctrl = c; },
14501cb0ef41Sopenharmony_ci    async write() { throw e; }
14511cb0ef41Sopenharmony_ci  });
14521cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
14531cb0ef41Sopenharmony_ci
14541cb0ef41Sopenharmony_ci  await promise_rejects_exactly(t, e, writer.write('hello'), 'write result');
14551cb0ef41Sopenharmony_ci  await promise_rejects_exactly(t, e, writer.closed, 'closed');
14561cb0ef41Sopenharmony_ci  assert_false(ctrl.signal.aborted);
14571cb0ef41Sopenharmony_ci}, 'the abort signal is not signalled on write failure');
14581cb0ef41Sopenharmony_ci
14591cb0ef41Sopenharmony_cipromise_test(async t => {
14601cb0ef41Sopenharmony_ci  let ctrl;
14611cb0ef41Sopenharmony_ci  const e = TypeError();
14621cb0ef41Sopenharmony_ci  const ws = new WritableStream({
14631cb0ef41Sopenharmony_ci    start(c) { ctrl = c; },
14641cb0ef41Sopenharmony_ci    async close() { throw e; }
14651cb0ef41Sopenharmony_ci  });
14661cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
14671cb0ef41Sopenharmony_ci
14681cb0ef41Sopenharmony_ci  await promise_rejects_exactly(t, e, writer.close(), 'close result');
14691cb0ef41Sopenharmony_ci  await promise_rejects_exactly(t, e, writer.closed, 'closed');
14701cb0ef41Sopenharmony_ci  assert_false(ctrl.signal.aborted);
14711cb0ef41Sopenharmony_ci}, 'the abort signal is not signalled on close failure');
14721cb0ef41Sopenharmony_ci
14731cb0ef41Sopenharmony_cipromise_test(async t => {
14741cb0ef41Sopenharmony_ci  let ctrl;
14751cb0ef41Sopenharmony_ci  const e1 = SyntaxError();
14761cb0ef41Sopenharmony_ci  const e2 = TypeError();
14771cb0ef41Sopenharmony_ci  const ws = new WritableStream({
14781cb0ef41Sopenharmony_ci    start(c) { ctrl = c; },
14791cb0ef41Sopenharmony_ci  });
14801cb0ef41Sopenharmony_ci
14811cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
14821cb0ef41Sopenharmony_ci  ctrl.signal.addEventListener('abort', () => writer.abort(e2));
14831cb0ef41Sopenharmony_ci  writer.abort(e1);
14841cb0ef41Sopenharmony_ci  assert_true(ctrl.signal.aborted);
14851cb0ef41Sopenharmony_ci
14861cb0ef41Sopenharmony_ci  await promise_rejects_exactly(t, e2, writer.closed, 'closed');
14871cb0ef41Sopenharmony_ci}, 'recursive abort() call');
1488