1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<!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>
|