11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci// META: script=../resources/recording-streams.js
31cb0ef41Sopenharmony_ci// META: script=../resources/rs-utils.js
41cb0ef41Sopenharmony_ci// META: script=../resources/test-utils.js
51cb0ef41Sopenharmony_ci'use strict';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// The size() function of the readable strategy can re-entrantly call back into the ReadableStream implementation. This
81cb0ef41Sopenharmony_ci// makes it risky to cache state across the call to ReadableStreamDefaultControllerEnqueue. These tests attempt to catch
91cb0ef41Sopenharmony_ci// such errors. They are separated from the other strategy tests because no real user code should ever do anything like
101cb0ef41Sopenharmony_ci// this.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst error1 = new Error('error1');
131cb0ef41Sopenharmony_cierror1.name = 'error1';
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cipromise_test(() => {
161cb0ef41Sopenharmony_ci  let controller;
171cb0ef41Sopenharmony_ci  let calls = 0;
181cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
191cb0ef41Sopenharmony_ci    start(c) {
201cb0ef41Sopenharmony_ci      controller = c;
211cb0ef41Sopenharmony_ci    }
221cb0ef41Sopenharmony_ci  }, {
231cb0ef41Sopenharmony_ci    size() {
241cb0ef41Sopenharmony_ci      ++calls;
251cb0ef41Sopenharmony_ci      if (calls < 2) {
261cb0ef41Sopenharmony_ci        controller.enqueue('b');
271cb0ef41Sopenharmony_ci      }
281cb0ef41Sopenharmony_ci      return 1;
291cb0ef41Sopenharmony_ci    }
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci  controller.enqueue('a');
321cb0ef41Sopenharmony_ci  controller.close();
331cb0ef41Sopenharmony_ci  return readableStreamToArray(rs)
341cb0ef41Sopenharmony_ci      .then(array => assert_array_equals(array, ['b', 'a'], 'array should contain two chunks'));
351cb0ef41Sopenharmony_ci}, 'enqueue() inside size() should work');
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_cipromise_test(() => {
381cb0ef41Sopenharmony_ci  let controller;
391cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
401cb0ef41Sopenharmony_ci    start(c) {
411cb0ef41Sopenharmony_ci      controller = c;
421cb0ef41Sopenharmony_ci    }
431cb0ef41Sopenharmony_ci  }, {
441cb0ef41Sopenharmony_ci    size() {
451cb0ef41Sopenharmony_ci      // The queue is empty.
461cb0ef41Sopenharmony_ci      controller.close();
471cb0ef41Sopenharmony_ci      // The state has gone from "readable" to "closed".
481cb0ef41Sopenharmony_ci      return 1;
491cb0ef41Sopenharmony_ci      // This chunk will be enqueued, but will be impossible to read because the state is already "closed".
501cb0ef41Sopenharmony_ci    }
511cb0ef41Sopenharmony_ci  });
521cb0ef41Sopenharmony_ci  controller.enqueue('a');
531cb0ef41Sopenharmony_ci  return readableStreamToArray(rs)
541cb0ef41Sopenharmony_ci      .then(array => assert_array_equals(array, [], 'array should contain no chunks'));
551cb0ef41Sopenharmony_ci  // The chunk 'a' is still in rs's queue. It is closed so 'a' cannot be read.
561cb0ef41Sopenharmony_ci}, 'close() inside size() should not crash');
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_cipromise_test(() => {
591cb0ef41Sopenharmony_ci  let controller;
601cb0ef41Sopenharmony_ci  let calls = 0;
611cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
621cb0ef41Sopenharmony_ci    start(c) {
631cb0ef41Sopenharmony_ci      controller = c;
641cb0ef41Sopenharmony_ci    }
651cb0ef41Sopenharmony_ci  }, {
661cb0ef41Sopenharmony_ci    size() {
671cb0ef41Sopenharmony_ci      ++calls;
681cb0ef41Sopenharmony_ci      if (calls === 2) {
691cb0ef41Sopenharmony_ci        // The queue contains one chunk.
701cb0ef41Sopenharmony_ci        controller.close();
711cb0ef41Sopenharmony_ci        // The state is still "readable", but closeRequest is now true.
721cb0ef41Sopenharmony_ci      }
731cb0ef41Sopenharmony_ci      return 1;
741cb0ef41Sopenharmony_ci    }
751cb0ef41Sopenharmony_ci  });
761cb0ef41Sopenharmony_ci  controller.enqueue('a');
771cb0ef41Sopenharmony_ci  controller.enqueue('b');
781cb0ef41Sopenharmony_ci  return readableStreamToArray(rs)
791cb0ef41Sopenharmony_ci      .then(array => assert_array_equals(array, ['a', 'b'], 'array should contain two chunks'));
801cb0ef41Sopenharmony_ci}, 'close request inside size() should work');
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_cipromise_test(t => {
831cb0ef41Sopenharmony_ci  let controller;
841cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
851cb0ef41Sopenharmony_ci    start(c) {
861cb0ef41Sopenharmony_ci      controller = c;
871cb0ef41Sopenharmony_ci    }
881cb0ef41Sopenharmony_ci  }, {
891cb0ef41Sopenharmony_ci    size() {
901cb0ef41Sopenharmony_ci      controller.error(error1);
911cb0ef41Sopenharmony_ci      return 1;
921cb0ef41Sopenharmony_ci    }
931cb0ef41Sopenharmony_ci  });
941cb0ef41Sopenharmony_ci  controller.enqueue('a');
951cb0ef41Sopenharmony_ci  return promise_rejects_exactly(t, error1, rs.getReader().read(), 'read() should reject');
961cb0ef41Sopenharmony_ci}, 'error() inside size() should work');
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_cipromise_test(() => {
991cb0ef41Sopenharmony_ci  let controller;
1001cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
1011cb0ef41Sopenharmony_ci    start(c) {
1021cb0ef41Sopenharmony_ci      controller = c;
1031cb0ef41Sopenharmony_ci    }
1041cb0ef41Sopenharmony_ci  }, {
1051cb0ef41Sopenharmony_ci    size() {
1061cb0ef41Sopenharmony_ci      assert_equals(controller.desiredSize, 1, 'desiredSize should be 1');
1071cb0ef41Sopenharmony_ci      return 1;
1081cb0ef41Sopenharmony_ci    },
1091cb0ef41Sopenharmony_ci    highWaterMark: 1
1101cb0ef41Sopenharmony_ci  });
1111cb0ef41Sopenharmony_ci  controller.enqueue('a');
1121cb0ef41Sopenharmony_ci  controller.close();
1131cb0ef41Sopenharmony_ci  return readableStreamToArray(rs)
1141cb0ef41Sopenharmony_ci      .then(array => assert_array_equals(array, ['a'], 'array should contain one chunk'));
1151cb0ef41Sopenharmony_ci}, 'desiredSize inside size() should work');
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_cipromise_test(t => {
1181cb0ef41Sopenharmony_ci  let cancelPromise;
1191cb0ef41Sopenharmony_ci  let controller;
1201cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
1211cb0ef41Sopenharmony_ci    start(c) {
1221cb0ef41Sopenharmony_ci      controller = c;
1231cb0ef41Sopenharmony_ci    },
1241cb0ef41Sopenharmony_ci    cancel: t.step_func(reason => {
1251cb0ef41Sopenharmony_ci      assert_equals(reason, error1, 'reason should be error1');
1261cb0ef41Sopenharmony_ci      assert_throws_js(TypeError, () => controller.enqueue(), 'enqueue() should throw');
1271cb0ef41Sopenharmony_ci    })
1281cb0ef41Sopenharmony_ci  }, {
1291cb0ef41Sopenharmony_ci    size() {
1301cb0ef41Sopenharmony_ci      cancelPromise = rs.cancel(error1);
1311cb0ef41Sopenharmony_ci      return 1;
1321cb0ef41Sopenharmony_ci    },
1331cb0ef41Sopenharmony_ci    highWaterMark: Infinity
1341cb0ef41Sopenharmony_ci  });
1351cb0ef41Sopenharmony_ci  controller.enqueue('a');
1361cb0ef41Sopenharmony_ci  const reader = rs.getReader();
1371cb0ef41Sopenharmony_ci  return Promise.all([
1381cb0ef41Sopenharmony_ci    reader.closed,
1391cb0ef41Sopenharmony_ci    cancelPromise
1401cb0ef41Sopenharmony_ci  ]);
1411cb0ef41Sopenharmony_ci}, 'cancel() inside size() should work');
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_cipromise_test(() => {
1441cb0ef41Sopenharmony_ci  let controller;
1451cb0ef41Sopenharmony_ci  let pipeToPromise;
1461cb0ef41Sopenharmony_ci  const ws = recordingWritableStream();
1471cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
1481cb0ef41Sopenharmony_ci    start(c) {
1491cb0ef41Sopenharmony_ci      controller = c;
1501cb0ef41Sopenharmony_ci    }
1511cb0ef41Sopenharmony_ci  }, {
1521cb0ef41Sopenharmony_ci    size() {
1531cb0ef41Sopenharmony_ci      if (!pipeToPromise) {
1541cb0ef41Sopenharmony_ci        pipeToPromise = rs.pipeTo(ws);
1551cb0ef41Sopenharmony_ci      }
1561cb0ef41Sopenharmony_ci      return 1;
1571cb0ef41Sopenharmony_ci    },
1581cb0ef41Sopenharmony_ci    highWaterMark: 1
1591cb0ef41Sopenharmony_ci  });
1601cb0ef41Sopenharmony_ci  controller.enqueue('a');
1611cb0ef41Sopenharmony_ci  assert_not_equals(pipeToPromise, undefined);
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci  // Some pipeTo() implementations need an additional chunk enqueued in order for the first one to be processed. See
1641cb0ef41Sopenharmony_ci  // https://github.com/whatwg/streams/issues/794 for background.
1651cb0ef41Sopenharmony_ci  controller.enqueue('a');
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci  // Give pipeTo() a chance to process the queued chunks.
1681cb0ef41Sopenharmony_ci  return delay(0).then(() => {
1691cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['write', 'a', 'write', 'a'], 'ws should contain two chunks');
1701cb0ef41Sopenharmony_ci    controller.close();
1711cb0ef41Sopenharmony_ci    return pipeToPromise;
1721cb0ef41Sopenharmony_ci  }).then(() => {
1731cb0ef41Sopenharmony_ci    assert_array_equals(ws.events, ['write', 'a', 'write', 'a', 'close'], 'target should have been closed');
1741cb0ef41Sopenharmony_ci  });
1751cb0ef41Sopenharmony_ci}, 'pipeTo() inside size() should behave as expected');
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_cipromise_test(() => {
1781cb0ef41Sopenharmony_ci  let controller;
1791cb0ef41Sopenharmony_ci  let readPromise;
1801cb0ef41Sopenharmony_ci  let calls = 0;
1811cb0ef41Sopenharmony_ci  let readResolved = false;
1821cb0ef41Sopenharmony_ci  let reader;
1831cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
1841cb0ef41Sopenharmony_ci    start(c) {
1851cb0ef41Sopenharmony_ci      controller = c;
1861cb0ef41Sopenharmony_ci    }
1871cb0ef41Sopenharmony_ci  }, {
1881cb0ef41Sopenharmony_ci    size() {
1891cb0ef41Sopenharmony_ci      // This is triggered by controller.enqueue(). The queue is empty and there are no pending reads. This read is
1901cb0ef41Sopenharmony_ci      // added to the list of pending reads.
1911cb0ef41Sopenharmony_ci      readPromise = reader.read();
1921cb0ef41Sopenharmony_ci      ++calls;
1931cb0ef41Sopenharmony_ci      return 1;
1941cb0ef41Sopenharmony_ci    },
1951cb0ef41Sopenharmony_ci    highWaterMark: 0
1961cb0ef41Sopenharmony_ci  });
1971cb0ef41Sopenharmony_ci  reader = rs.getReader();
1981cb0ef41Sopenharmony_ci  controller.enqueue('a');
1991cb0ef41Sopenharmony_ci  readPromise.then(() => {
2001cb0ef41Sopenharmony_ci    readResolved = true;
2011cb0ef41Sopenharmony_ci  });
2021cb0ef41Sopenharmony_ci  return flushAsyncEvents().then(() => {
2031cb0ef41Sopenharmony_ci    assert_false(readResolved);
2041cb0ef41Sopenharmony_ci    controller.enqueue('b');
2051cb0ef41Sopenharmony_ci    assert_equals(calls, 1, 'size() should have been called once');
2061cb0ef41Sopenharmony_ci    return delay(0);
2071cb0ef41Sopenharmony_ci  }).then(() => {
2081cb0ef41Sopenharmony_ci    assert_true(readResolved);
2091cb0ef41Sopenharmony_ci    assert_equals(calls, 1, 'size() should only be called once');
2101cb0ef41Sopenharmony_ci    return readPromise;
2111cb0ef41Sopenharmony_ci  }).then(({ value, done }) => {
2121cb0ef41Sopenharmony_ci    assert_false(done, 'done should be false');
2131cb0ef41Sopenharmony_ci    // See https://github.com/whatwg/streams/issues/794 for why this chunk is not 'a'.
2141cb0ef41Sopenharmony_ci    assert_equals(value, 'b', 'chunk should have been read');
2151cb0ef41Sopenharmony_ci    assert_equals(calls, 1, 'calls should still be 1');
2161cb0ef41Sopenharmony_ci    return reader.read();
2171cb0ef41Sopenharmony_ci  }).then(({ value, done }) => {
2181cb0ef41Sopenharmony_ci    assert_false(done, 'done should be false again');
2191cb0ef41Sopenharmony_ci    assert_equals(value, 'a', 'chunk a should come after b');
2201cb0ef41Sopenharmony_ci  });
2211cb0ef41Sopenharmony_ci}, 'read() inside of size() should behave as expected');
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_cipromise_test(() => {
2241cb0ef41Sopenharmony_ci  let controller;
2251cb0ef41Sopenharmony_ci  let reader;
2261cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
2271cb0ef41Sopenharmony_ci    start(c) {
2281cb0ef41Sopenharmony_ci      controller = c;
2291cb0ef41Sopenharmony_ci    }
2301cb0ef41Sopenharmony_ci  }, {
2311cb0ef41Sopenharmony_ci    size() {
2321cb0ef41Sopenharmony_ci      reader = rs.getReader();
2331cb0ef41Sopenharmony_ci      return 1;
2341cb0ef41Sopenharmony_ci    }
2351cb0ef41Sopenharmony_ci  });
2361cb0ef41Sopenharmony_ci  controller.enqueue('a');
2371cb0ef41Sopenharmony_ci  return reader.read().then(({ value, done }) => {
2381cb0ef41Sopenharmony_ci    assert_false(done, 'done should be false');
2391cb0ef41Sopenharmony_ci    assert_equals(value, 'a', 'value should be a');
2401cb0ef41Sopenharmony_ci  });
2411cb0ef41Sopenharmony_ci}, 'getReader() inside size() should work');
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_cipromise_test(() => {
2441cb0ef41Sopenharmony_ci  let controller;
2451cb0ef41Sopenharmony_ci  let branch1;
2461cb0ef41Sopenharmony_ci  let branch2;
2471cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
2481cb0ef41Sopenharmony_ci    start(c) {
2491cb0ef41Sopenharmony_ci      controller = c;
2501cb0ef41Sopenharmony_ci    }
2511cb0ef41Sopenharmony_ci  }, {
2521cb0ef41Sopenharmony_ci    size() {
2531cb0ef41Sopenharmony_ci      [branch1, branch2] = rs.tee();
2541cb0ef41Sopenharmony_ci      return 1;
2551cb0ef41Sopenharmony_ci    }
2561cb0ef41Sopenharmony_ci  });
2571cb0ef41Sopenharmony_ci  controller.enqueue('a');
2581cb0ef41Sopenharmony_ci  assert_true(rs.locked, 'rs should be locked');
2591cb0ef41Sopenharmony_ci  controller.close();
2601cb0ef41Sopenharmony_ci  return Promise.all([
2611cb0ef41Sopenharmony_ci    readableStreamToArray(branch1).then(array => assert_array_equals(array, ['a'], 'branch1 should have one chunk')),
2621cb0ef41Sopenharmony_ci    readableStreamToArray(branch2).then(array => assert_array_equals(array, ['a'], 'branch2 should have one chunk'))
2631cb0ef41Sopenharmony_ci  ]);
2641cb0ef41Sopenharmony_ci}, 'tee() inside size() should work');
265