11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_citest(() => {
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci  const theError = new Error('a unique string');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci  assert_throws_exactly(theError, () => {
91cb0ef41Sopenharmony_ci    new ReadableStream({}, {
101cb0ef41Sopenharmony_ci      get size() {
111cb0ef41Sopenharmony_ci        throw theError;
121cb0ef41Sopenharmony_ci      },
131cb0ef41Sopenharmony_ci      highWaterMark: 5
141cb0ef41Sopenharmony_ci    });
151cb0ef41Sopenharmony_ci  }, 'construction should re-throw the error');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci}, 'Readable stream: throwing strategy.size getter');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cipromise_test(t => {
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  const controllerError = { name: 'controller error' };
221cb0ef41Sopenharmony_ci  const thrownError = { name: 'thrown error' };
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  let controller;
251cb0ef41Sopenharmony_ci  const rs = new ReadableStream(
261cb0ef41Sopenharmony_ci    {
271cb0ef41Sopenharmony_ci      start(c) {
281cb0ef41Sopenharmony_ci        controller = c;
291cb0ef41Sopenharmony_ci      }
301cb0ef41Sopenharmony_ci    },
311cb0ef41Sopenharmony_ci    {
321cb0ef41Sopenharmony_ci      size() {
331cb0ef41Sopenharmony_ci        controller.error(controllerError);
341cb0ef41Sopenharmony_ci        throw thrownError;
351cb0ef41Sopenharmony_ci      },
361cb0ef41Sopenharmony_ci      highWaterMark: 5
371cb0ef41Sopenharmony_ci    }
381cb0ef41Sopenharmony_ci  );
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  assert_throws_exactly(thrownError, () => controller.enqueue('a'), 'enqueue should re-throw the error');
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, controllerError, rs.getReader().closed);
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci}, 'Readable stream: strategy.size errors the stream and then throws');
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cipromise_test(t => {
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  const theError = { name: 'my error' };
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  let controller;
511cb0ef41Sopenharmony_ci  const rs = new ReadableStream(
521cb0ef41Sopenharmony_ci    {
531cb0ef41Sopenharmony_ci      start(c) {
541cb0ef41Sopenharmony_ci        controller = c;
551cb0ef41Sopenharmony_ci      }
561cb0ef41Sopenharmony_ci    },
571cb0ef41Sopenharmony_ci    {
581cb0ef41Sopenharmony_ci      size() {
591cb0ef41Sopenharmony_ci        controller.error(theError);
601cb0ef41Sopenharmony_ci        return Infinity;
611cb0ef41Sopenharmony_ci      },
621cb0ef41Sopenharmony_ci      highWaterMark: 5
631cb0ef41Sopenharmony_ci    }
641cb0ef41Sopenharmony_ci  );
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  assert_throws_js(RangeError, () => controller.enqueue('a'), 'enqueue should throw a RangeError');
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, theError, rs.getReader().closed, 'closed should reject with the error');
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci}, 'Readable stream: strategy.size errors the stream and then returns Infinity');
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cipromise_test(() => {
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  const theError = new Error('a unique string');
751cb0ef41Sopenharmony_ci  const rs = new ReadableStream(
761cb0ef41Sopenharmony_ci    {
771cb0ef41Sopenharmony_ci      start(c) {
781cb0ef41Sopenharmony_ci        assert_throws_exactly(theError, () => c.enqueue('a'), 'enqueue should throw the error');
791cb0ef41Sopenharmony_ci      }
801cb0ef41Sopenharmony_ci    },
811cb0ef41Sopenharmony_ci    {
821cb0ef41Sopenharmony_ci      size() {
831cb0ef41Sopenharmony_ci        throw theError;
841cb0ef41Sopenharmony_ci      },
851cb0ef41Sopenharmony_ci      highWaterMark: 5
861cb0ef41Sopenharmony_ci    }
871cb0ef41Sopenharmony_ci  );
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  return rs.getReader().closed.catch(e => {
901cb0ef41Sopenharmony_ci    assert_equals(e, theError, 'closed should reject with the error');
911cb0ef41Sopenharmony_ci  });
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci}, 'Readable stream: throwing strategy.size method');
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_citest(() => {
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  const theError = new Error('a unique string');
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  assert_throws_exactly(theError, () => {
1001cb0ef41Sopenharmony_ci    new ReadableStream({}, {
1011cb0ef41Sopenharmony_ci      size() {
1021cb0ef41Sopenharmony_ci        return 1;
1031cb0ef41Sopenharmony_ci      },
1041cb0ef41Sopenharmony_ci      get highWaterMark() {
1051cb0ef41Sopenharmony_ci        throw theError;
1061cb0ef41Sopenharmony_ci      }
1071cb0ef41Sopenharmony_ci    });
1081cb0ef41Sopenharmony_ci  }, 'construction should re-throw the error');
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci}, 'Readable stream: throwing strategy.highWaterMark getter');
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_citest(() => {
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci  for (const highWaterMark of [-1, -Infinity, NaN, 'foo', {}]) {
1151cb0ef41Sopenharmony_ci    assert_throws_js(RangeError, () => {
1161cb0ef41Sopenharmony_ci      new ReadableStream({}, {
1171cb0ef41Sopenharmony_ci        size() {
1181cb0ef41Sopenharmony_ci          return 1;
1191cb0ef41Sopenharmony_ci        },
1201cb0ef41Sopenharmony_ci        highWaterMark
1211cb0ef41Sopenharmony_ci      });
1221cb0ef41Sopenharmony_ci    }, 'construction should throw a RangeError for ' + highWaterMark);
1231cb0ef41Sopenharmony_ci  }
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci}, 'Readable stream: invalid strategy.highWaterMark');
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_cipromise_test(() => {
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci  const promises = [];
1301cb0ef41Sopenharmony_ci  for (const size of [NaN, -Infinity, Infinity, -1]) {
1311cb0ef41Sopenharmony_ci    let theError;
1321cb0ef41Sopenharmony_ci    const rs = new ReadableStream(
1331cb0ef41Sopenharmony_ci      {
1341cb0ef41Sopenharmony_ci        start(c) {
1351cb0ef41Sopenharmony_ci          try {
1361cb0ef41Sopenharmony_ci            c.enqueue('hi');
1371cb0ef41Sopenharmony_ci            assert_unreached('enqueue didn\'t throw');
1381cb0ef41Sopenharmony_ci          } catch (error) {
1391cb0ef41Sopenharmony_ci            assert_equals(error.name, 'RangeError', 'enqueue should throw a RangeError for ' + size);
1401cb0ef41Sopenharmony_ci            theError = error;
1411cb0ef41Sopenharmony_ci          }
1421cb0ef41Sopenharmony_ci        }
1431cb0ef41Sopenharmony_ci      },
1441cb0ef41Sopenharmony_ci      {
1451cb0ef41Sopenharmony_ci        size() {
1461cb0ef41Sopenharmony_ci          return size;
1471cb0ef41Sopenharmony_ci        },
1481cb0ef41Sopenharmony_ci        highWaterMark: 5
1491cb0ef41Sopenharmony_ci      }
1501cb0ef41Sopenharmony_ci    );
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci    promises.push(rs.getReader().closed.catch(e => {
1531cb0ef41Sopenharmony_ci      assert_equals(e, theError, 'closed should reject with the error for ' + size);
1541cb0ef41Sopenharmony_ci    }));
1551cb0ef41Sopenharmony_ci  }
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci  return Promise.all(promises);
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci}, 'Readable stream: invalid strategy.size return value');
160