11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// Repro for Blink bug https://crbug.com/1255762.
61cb0ef41Sopenharmony_cipromise_test(async () => {
71cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
81cb0ef41Sopenharmony_ci    type: 'bytes',
91cb0ef41Sopenharmony_ci    autoAllocateChunkSize: 10,
101cb0ef41Sopenharmony_ci    pull(controller) {
111cb0ef41Sopenharmony_ci      controller.enqueue(new Uint8Array([1, 2, 3]));
121cb0ef41Sopenharmony_ci      controller.byobRequest.respond(10);
131cb0ef41Sopenharmony_ci    }
141cb0ef41Sopenharmony_ci  });
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  const reader = rs.getReader();
171cb0ef41Sopenharmony_ci  const {value, done} = await reader.read();
181cb0ef41Sopenharmony_ci  assert_false(done, 'done should not be true');
191cb0ef41Sopenharmony_ci  assert_array_equals(value, [1, 2, 3], 'value should be 3 bytes');
201cb0ef41Sopenharmony_ci}, 'byobRequest.respond() after enqueue() should not crash');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cipromise_test(async () => {
231cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
241cb0ef41Sopenharmony_ci    type: 'bytes',
251cb0ef41Sopenharmony_ci    autoAllocateChunkSize: 10,
261cb0ef41Sopenharmony_ci    pull(controller) {
271cb0ef41Sopenharmony_ci      const byobRequest = controller.byobRequest;
281cb0ef41Sopenharmony_ci      controller.enqueue(new Uint8Array([1, 2, 3]));
291cb0ef41Sopenharmony_ci      byobRequest.respond(10);
301cb0ef41Sopenharmony_ci    }
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  const reader = rs.getReader();
341cb0ef41Sopenharmony_ci  const {value, done} = await reader.read();
351cb0ef41Sopenharmony_ci  assert_false(done, 'done should not be true');
361cb0ef41Sopenharmony_ci  assert_array_equals(value, [1, 2, 3], 'value should be 3 bytes');
371cb0ef41Sopenharmony_ci}, 'byobRequest.respond() with cached byobRequest after enqueue() should not crash');
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cipromise_test(async () => {
401cb0ef41Sopenharmony_ci  const rs = new ReadableStream({
411cb0ef41Sopenharmony_ci    type: 'bytes',
421cb0ef41Sopenharmony_ci    autoAllocateChunkSize: 10,
431cb0ef41Sopenharmony_ci    pull(controller) {
441cb0ef41Sopenharmony_ci      controller.enqueue(new Uint8Array([1, 2, 3]));
451cb0ef41Sopenharmony_ci      controller.byobRequest.respond(2);
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  const reader = rs.getReader();
501cb0ef41Sopenharmony_ci  const [read1, read2] = await Promise.all([reader.read(), reader.read()]);
511cb0ef41Sopenharmony_ci  assert_false(read1.done, 'read1.done should not be true');
521cb0ef41Sopenharmony_ci  assert_array_equals(read1.value, [1, 2, 3], 'read1.value should be 3 bytes');
531cb0ef41Sopenharmony_ci  assert_false(read2.done, 'read2.done should not be true');
541cb0ef41Sopenharmony_ci  assert_array_equals(read2.value, [0, 0], 'read2.value should be 2 bytes');
551cb0ef41Sopenharmony_ci}, 'byobRequest.respond() after enqueue() with double read should not crash');
56