11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<meta charset="utf-8">
31cb0ef41Sopenharmony_ci<script src="/resources/testharness.js"></script>
41cb0ef41Sopenharmony_ci<script src="/resources/testharnessreport.js"></script>
51cb0ef41Sopenharmony_ci<script src="resources/helpers.js"></script>
61cb0ef41Sopenharmony_ci<script src="../resources/test-utils.js"></script>
71cb0ef41Sopenharmony_ci<script>
81cb0ef41Sopenharmony_ci'use strict';
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cipromise_test(t => {
111cb0ef41Sopenharmony_ci  const orig = createOriginalReadableStream();
121cb0ef41Sopenharmony_ci  const w = new Worker('resources/receiving-worker.js');
131cb0ef41Sopenharmony_ci  t.add_cleanup(() => {
141cb0ef41Sopenharmony_ci    w.terminate();
151cb0ef41Sopenharmony_ci  });
161cb0ef41Sopenharmony_ci  const promise = new Promise((resolve, reject) => {
171cb0ef41Sopenharmony_ci    checkTestResults(w).then(resolve, reject);
181cb0ef41Sopenharmony_ci    w.onerror = () => reject('error in worker');
191cb0ef41Sopenharmony_ci  });
201cb0ef41Sopenharmony_ci  w.postMessage(orig, [orig]);
211cb0ef41Sopenharmony_ci  assert_true(orig.locked, 'the original stream should be locked');
221cb0ef41Sopenharmony_ci  return promise;
231cb0ef41Sopenharmony_ci}, 'worker.postMessage should be able to transfer a ReadableStream');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cipromise_test(t => {
261cb0ef41Sopenharmony_ci  const w = new Worker('resources/sending-worker.js');
271cb0ef41Sopenharmony_ci  t.add_cleanup(() => {
281cb0ef41Sopenharmony_ci    w.terminate();
291cb0ef41Sopenharmony_ci  });
301cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
311cb0ef41Sopenharmony_ci    testMessageEvent(w).then(resolve, reject);
321cb0ef41Sopenharmony_ci    w.onerror = () => reject('error in worker');
331cb0ef41Sopenharmony_ci  });
341cb0ef41Sopenharmony_ci}, 'postMessage in a worker should be able to transfer a ReadableStream');
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cipromise_test(async t => {
371cb0ef41Sopenharmony_ci  const w = new Worker('resources/echo-worker.js');
381cb0ef41Sopenharmony_ci  let controller;
391cb0ef41Sopenharmony_ci  const orig = new ReadableStream({
401cb0ef41Sopenharmony_ci    start(c) {
411cb0ef41Sopenharmony_ci      controller = c;
421cb0ef41Sopenharmony_ci    }
431cb0ef41Sopenharmony_ci  });
441cb0ef41Sopenharmony_ci  const targetStream = await new Promise((resolve, reject) => {
451cb0ef41Sopenharmony_ci    w.onmessage = evt => resolve(evt.data);
461cb0ef41Sopenharmony_ci    w.onerror = () => reject('error in worker');
471cb0ef41Sopenharmony_ci    w.postMessage(orig, [orig]);
481cb0ef41Sopenharmony_ci  });
491cb0ef41Sopenharmony_ci  const reader = targetStream.getReader();
501cb0ef41Sopenharmony_ci  const reads = [];
511cb0ef41Sopenharmony_ci  // Place a lot of chunks "in transit". This should increase the likelihood
521cb0ef41Sopenharmony_ci  // that they is a chunk at each relevant step when the worker is terminated.
531cb0ef41Sopenharmony_ci  for (let i = 0; i < 50; ++i) {
541cb0ef41Sopenharmony_ci    await delay(0);
551cb0ef41Sopenharmony_ci    controller.enqueue(i);
561cb0ef41Sopenharmony_ci    const expected = i;
571cb0ef41Sopenharmony_ci    reads.push(reader.read().then(({value, done}) => {
581cb0ef41Sopenharmony_ci      assert_false(done, 'we should not be done');
591cb0ef41Sopenharmony_ci      assert_equals(value, expected, 'value should match expectation');
601cb0ef41Sopenharmony_ci    }));
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci  w.terminate();
631cb0ef41Sopenharmony_ci  for (let i = 50; i < 60; ++i) {
641cb0ef41Sopenharmony_ci    controller.enqueue(i);
651cb0ef41Sopenharmony_ci    reads.push(
661cb0ef41Sopenharmony_ci      reader.read().then(t.unreached_func('read() should not resolve')));
671cb0ef41Sopenharmony_ci    await delay(0);
681cb0ef41Sopenharmony_ci  }
691cb0ef41Sopenharmony_ci  // We don't expect every read() to complete, but we want to give them a chance
701cb0ef41Sopenharmony_ci  // to reject if they're going to.
711cb0ef41Sopenharmony_ci  return Promise.race([
721cb0ef41Sopenharmony_ci    Promise.all(reads),
731cb0ef41Sopenharmony_ci    flushAsyncEvents()
741cb0ef41Sopenharmony_ci  ]);
751cb0ef41Sopenharmony_ci}, 'terminating a worker should not error the stream');
761cb0ef41Sopenharmony_ci</script>
77