<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/helpers.js"></script>
<script src="../resources/test-utils.js"></script>
<script>
'use strict';
promise_test(t => {
const orig = createOriginalReadableStream();
const w = new Worker('resources/receiving-worker.js');
t.add_cleanup(() => {
w.terminate();
});
const promise = new Promise((resolve, reject) => {
checkTestResults(w).then(resolve, reject);
w.onerror = () => reject('error in worker');
});
w.postMessage(orig, [orig]);
assert_true(orig.locked, 'the original stream should be locked');
return promise;
}, 'worker.postMessage should be able to transfer a ReadableStream');
promise_test(t => {
const w = new Worker('resources/sending-worker.js');
t.add_cleanup(() => {
w.terminate();
});
return new Promise((resolve, reject) => {
testMessageEvent(w).then(resolve, reject);
w.onerror = () => reject('error in worker');
});
}, 'postMessage in a worker should be able to transfer a ReadableStream');
promise_test(async t => {
const w = new Worker('resources/echo-worker.js');
let controller;
const orig = new ReadableStream({
start(c) {
controller = c;
}
});
const targetStream = await new Promise((resolve, reject) => {
w.onmessage = evt => resolve(evt.data);
w.onerror = () => reject('error in worker');
w.postMessage(orig, [orig]);
});
const reader = targetStream.getReader();
const reads = [];
// Place a lot of chunks "in transit". This should increase the likelihood
// that they is a chunk at each relevant step when the worker is terminated.
for (let i = 0; i < 50; ++i) {
await delay(0);
controller.enqueue(i);
const expected = i;
reads.push(reader.read().then(({value, done}) => {
assert_false(done, 'we should not be done');
assert_equals(value, expected, 'value should match expectation');
}));
}
w.terminate();
for (let i = 50; i < 60; ++i) {
controller.enqueue(i);
reads.push(
reader.read().then(t.unreached_func('read() should not resolve')));
await delay(0);
}
// We don't expect every read() to complete, but we want to give them a chance
// to reject if they're going to.
return Promise.race([
Promise.all(reads),
flushAsyncEvents()
]);
}, 'terminating a worker should not error the stream');
</script>