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_cifunction createErroredWritableStream(t) {
131cb0ef41Sopenharmony_ci  return Promise.resolve().then(() => {
141cb0ef41Sopenharmony_ci    const ws = recordingWritableStream({
151cb0ef41Sopenharmony_ci      start(c) {
161cb0ef41Sopenharmony_ci        c.error(error2);
171cb0ef41Sopenharmony_ci      }
181cb0ef41Sopenharmony_ci    });
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    const writer = ws.getWriter();
211cb0ef41Sopenharmony_ci    return promise_rejects_exactly(t, error2, writer.closed, 'the writable stream must be errored with error2')
221cb0ef41Sopenharmony_ci        .then(() => {
231cb0ef41Sopenharmony_ci          writer.releaseLock();
241cb0ef41Sopenharmony_ci          assert_array_equals(ws.events, []);
251cb0ef41Sopenharmony_ci          return ws;
261cb0ef41Sopenharmony_ci        });
271cb0ef41Sopenharmony_ci  });
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cipromise_test(t => {
311cb0ef41Sopenharmony_ci  const rs = recordingReadableStream({
321cb0ef41Sopenharmony_ci    start(c) {
331cb0ef41Sopenharmony_ci      c.error(error1);
341cb0ef41Sopenharmony_ci    }
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
371cb0ef41Sopenharmony_ci    start(c) {
381cb0ef41Sopenharmony_ci      c.error(error2);
391cb0ef41Sopenharmony_ci    }
401cb0ef41Sopenharmony_ci  });
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  // Trying to abort a stream that is erroring will give the writable's error
431cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error2, rs.pipeTo(ws), 'pipeTo must reject with the writable stream\'s error').then(() => {
441cb0ef41Sopenharmony_ci    assert_array_equals(rs.events, []);
451cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, []);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    return Promise.all([
481cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1'),
491cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, ws.getWriter().closed, 'the writable stream must be errored with error2')
501cb0ef41Sopenharmony_ci    ]);
511cb0ef41Sopenharmony_ci  });
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci}, 'Piping from an errored readable stream to an erroring writable stream');
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cipromise_test(t => {
561cb0ef41Sopenharmony_ci  const rs = recordingReadableStream({
571cb0ef41Sopenharmony_ci    start(c) {
581cb0ef41Sopenharmony_ci      c.error(error1);
591cb0ef41Sopenharmony_ci    }
601cb0ef41Sopenharmony_ci  });
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  return createErroredWritableStream(t)
631cb0ef41Sopenharmony_ci      .then(ws => promise_rejects_exactly(t, error1, rs.pipeTo(ws), 'pipeTo must reject with the readable stream\'s error'))
641cb0ef41Sopenharmony_ci      .then(() => {
651cb0ef41Sopenharmony_ci        assert_array_equals(rs.events, []);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci        return promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1');
681cb0ef41Sopenharmony_ci      });
691cb0ef41Sopenharmony_ci}, 'Piping from an errored readable stream to an errored writable stream');
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_cipromise_test(t => {
721cb0ef41Sopenharmony_ci  const rs = recordingReadableStream({
731cb0ef41Sopenharmony_ci    start(c) {
741cb0ef41Sopenharmony_ci      c.error(error1);
751cb0ef41Sopenharmony_ci    }
761cb0ef41Sopenharmony_ci  });
771cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
781cb0ef41Sopenharmony_ci    start(c) {
791cb0ef41Sopenharmony_ci      c.error(error2);
801cb0ef41Sopenharmony_ci    }
811cb0ef41Sopenharmony_ci  });
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, rs.pipeTo(ws, { preventAbort: true }),
841cb0ef41Sopenharmony_ci    'pipeTo must reject with the readable stream\'s error')
851cb0ef41Sopenharmony_ci  .then(() => {
861cb0ef41Sopenharmony_ci    assert_array_equals(rs.events, []);
871cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, []);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci    return Promise.all([
901cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1'),
911cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error2, ws.getWriter().closed, 'the writable stream must be errored with error2')
921cb0ef41Sopenharmony_ci    ]);
931cb0ef41Sopenharmony_ci  });
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci}, 'Piping from an errored readable stream to an erroring writable stream; preventAbort = true');
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_cipromise_test(t => {
981cb0ef41Sopenharmony_ci  const rs = recordingReadableStream({
991cb0ef41Sopenharmony_ci    start(c) {
1001cb0ef41Sopenharmony_ci      c.error(error1);
1011cb0ef41Sopenharmony_ci    }
1021cb0ef41Sopenharmony_ci  });
1031cb0ef41Sopenharmony_ci  return createErroredWritableStream(t)
1041cb0ef41Sopenharmony_ci  .then(ws => promise_rejects_exactly(t, error1, rs.pipeTo(ws, { preventAbort: true }),
1051cb0ef41Sopenharmony_ci                                      'pipeTo must reject with the readable stream\'s error'))
1061cb0ef41Sopenharmony_ci  .then(() => {
1071cb0ef41Sopenharmony_ci    assert_array_equals(rs.events, []);
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci    return promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1');
1101cb0ef41Sopenharmony_ci  });
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci}, 'Piping from an errored readable stream to an errored writable stream; preventAbort = true');
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_cipromise_test(t => {
1151cb0ef41Sopenharmony_ci  const rs = recordingReadableStream({
1161cb0ef41Sopenharmony_ci    start(c) {
1171cb0ef41Sopenharmony_ci      c.error(error1);
1181cb0ef41Sopenharmony_ci    }
1191cb0ef41Sopenharmony_ci  });
1201cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
1211cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
1221cb0ef41Sopenharmony_ci  const closePromise = writer.close();
1231cb0ef41Sopenharmony_ci  writer.releaseLock();
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, rs.pipeTo(ws), 'pipeTo must reject with the readable stream\'s error').then(() => {
1261cb0ef41Sopenharmony_ci    assert_array_equals(rs.events, []);
1271cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['abort', error1]);
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci    return Promise.all([
1301cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1'),
1311cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, ws.getWriter().closed,
1321cb0ef41Sopenharmony_ci        'closed must reject with error1'),
1331cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, closePromise,
1341cb0ef41Sopenharmony_ci        'close() must reject with error1')
1351cb0ef41Sopenharmony_ci    ]);
1361cb0ef41Sopenharmony_ci  });
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci}, 'Piping from an errored readable stream to a closing writable stream');
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_cipromise_test(t => {
1411cb0ef41Sopenharmony_ci  const rs = recordingReadableStream({
1421cb0ef41Sopenharmony_ci    start(c) {
1431cb0ef41Sopenharmony_ci      c.error(error1);
1441cb0ef41Sopenharmony_ci    }
1451cb0ef41Sopenharmony_ci  });
1461cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
1471cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
1481cb0ef41Sopenharmony_ci  const closePromise = writer.close();
1491cb0ef41Sopenharmony_ci  writer.releaseLock();
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci  return flushAsyncEvents().then(() => {
1521cb0ef41Sopenharmony_ci    return promise_rejects_exactly(t, error1, rs.pipeTo(ws), 'pipeTo must reject with the readable stream\'s error').then(() => {
1531cb0ef41Sopenharmony_ci      assert_array_equals(rs.events, []);
1541cb0ef41Sopenharmony_ci      assert_array_equals(ws.events, ['close']);
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci      return Promise.all([
1571cb0ef41Sopenharmony_ci        promise_rejects_exactly(t, error1, rs.getReader().closed, 'the readable stream must be errored with error1'),
1581cb0ef41Sopenharmony_ci        ws.getWriter().closed,
1591cb0ef41Sopenharmony_ci        closePromise
1601cb0ef41Sopenharmony_ci      ]);
1611cb0ef41Sopenharmony_ci    });
1621cb0ef41Sopenharmony_ci  });
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci}, 'Piping from an errored readable stream to a closed writable stream');
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_cipromise_test(t => {
1671cb0ef41Sopenharmony_ci  const rs = recordingReadableStream({
1681cb0ef41Sopenharmony_ci    start(c) {
1691cb0ef41Sopenharmony_ci      c.close();
1701cb0ef41Sopenharmony_ci    }
1711cb0ef41Sopenharmony_ci  });
1721cb0ef41Sopenharmony_ci  const ws = recordingWritableStream({
1731cb0ef41Sopenharmony_ci    start(c) {
1741cb0ef41Sopenharmony_ci      c.error(error1);
1751cb0ef41Sopenharmony_ci    }
1761cb0ef41Sopenharmony_ci  });
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, rs.pipeTo(ws), 'pipeTo must reject with the writable stream\'s error').then(() => {
1791cb0ef41Sopenharmony_ci    assert_array_equals(rs.events, []);
1801cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, []);
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci    return Promise.all([
1831cb0ef41Sopenharmony_ci      rs.getReader().closed,
1841cb0ef41Sopenharmony_ci      promise_rejects_exactly(t, error1, ws.getWriter().closed, 'the writable stream must be errored with error1')
1851cb0ef41Sopenharmony_ci    ]);
1861cb0ef41Sopenharmony_ci  });
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci}, 'Piping from a closed readable stream to an erroring writable stream');
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_cipromise_test(t => {
1911cb0ef41Sopenharmony_ci  const rs = recordingReadableStream({
1921cb0ef41Sopenharmony_ci    start(c) {
1931cb0ef41Sopenharmony_ci      c.close();
1941cb0ef41Sopenharmony_ci    }
1951cb0ef41Sopenharmony_ci  });
1961cb0ef41Sopenharmony_ci  return createErroredWritableStream(t)
1971cb0ef41Sopenharmony_ci  .then(ws => promise_rejects_exactly(t, error2, rs.pipeTo(ws), 'pipeTo must reject with the writable stream\'s error'))
1981cb0ef41Sopenharmony_ci  .then(() => {
1991cb0ef41Sopenharmony_ci    assert_array_equals(rs.events, []);
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci    return rs.getReader().closed;
2021cb0ef41Sopenharmony_ci  });
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ci}, 'Piping from a closed readable stream to an errored writable stream');
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_cipromise_test(() => {
2071cb0ef41Sopenharmony_ci  const rs = recordingReadableStream({
2081cb0ef41Sopenharmony_ci    start(c) {
2091cb0ef41Sopenharmony_ci      c.close();
2101cb0ef41Sopenharmony_ci    }
2111cb0ef41Sopenharmony_ci  });
2121cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
2131cb0ef41Sopenharmony_ci  const writer = ws.getWriter();
2141cb0ef41Sopenharmony_ci  writer.close();
2151cb0ef41Sopenharmony_ci  writer.releaseLock();
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci  return rs.pipeTo(ws).then(() => {
2181cb0ef41Sopenharmony_ci    assert_array_equals(rs.events, []);
2191cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['close']);
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ci    return Promise.all([
2221cb0ef41Sopenharmony_ci      rs.getReader().closed,
2231cb0ef41Sopenharmony_ci      ws.getWriter().closed
2241cb0ef41Sopenharmony_ci    ]);
2251cb0ef41Sopenharmony_ci  });
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci}, 'Piping from a closed readable stream to a closed writable stream');
228