Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and enable additional streams tests in ShadowRealm #49321

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion streams/readable-streams/owning-type-message-port.any.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// META: global=window,worker,shadowrealm
// META: global=window,worker
// META: script=../resources/test-utils.js
// META: script=../resources/rs-utils.js
'use strict';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<!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/recording-streams.js"></script>
<script src="../resources/test-utils.js"></script>
<script>
// META: global=window,dedicatedworker,shadowrealm
// META: script=resources/helpers.js
// META: script=../resources/recording-streams.js
// META: script=../resources/test-utils.js

'use strict';

promise_test(async () => {
Expand Down Expand Up @@ -94,11 +91,15 @@
const rs = recordingReadableStream({}, { highWaterMark: 0 });
await delay(0);
assert_array_equals(rs.events, [], 'pull() should not have been called');
// Eat the message so it can't interfere with other tests.
addEventListener('message', () => {}, {once: true});
// The transfer is done manually to verify that it is posting the stream that
// relieves backpressure, not receiving it.
postMessage(rs, '*', [rs]);
if (GLOBAL.isShadowRealm()) {
structuredClone(rs, { transfer: [rs] });
} else {
// Eat the message so it can't interfere with other tests.
addEventListener('message', () => {}, {once: true});
// The transfer is done manually to verify that it is posting the stream that
// relieves backpressure, not receiving it.
postMessage(rs, '*', [rs]);
}
await delay(0);
assert_array_equals(rs.events, ['pull'], 'pull() should have been called');
}, 'transferring a stream should relieve backpressure');
Expand Down Expand Up @@ -256,5 +257,3 @@
const {done} = await reader.read();
assert_true(done, 'should be done');
}, 'race between cancel() and enqueue() should be benign');

</script>
12 changes: 10 additions & 2 deletions streams/transferable/resources/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@
}
}, {once: true});
});
postMessage(original, '*', [original]);
if (GLOBAL.isShadowRealm()) {
structuredClone(original, {transfer: [original]});
} else {
postMessage(original, '*', [original]);
}
return promise;
}

Expand All @@ -117,7 +121,11 @@
}
}, {once: true});
});
postMessage(original, '*', [original]);
if (GLOBAL.isShadowRealm()) {
structuredClone(original, {transfer: [original]});
} else {
postMessage(original, '*', [original]);
}
return promise;
}

Expand Down
117 changes: 117 additions & 0 deletions streams/transferable/transform-stream.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// META: global=window,dedicatedworker,shadowrealm
// META: script=../resources/test-utils.js

'use strict';

function transfer(t, obj, transfers) {
if (GLOBAL.isShadowRealm()) {
const transferred = structuredClone(obj, {transfer: transfers});
return Promise.resolve(transferred);
}
return new Promise(resolve => {
addEventListener('message', t.step_func(evt => {
const transferred = evt.data;
resolve(transferred);
}), {once: true});
postMessage(obj, '*', transfers);
});
}

function failToTransfer(obj) {
if (GLOBAL.isShadowRealm()) {
structuredClone(obj, {transfer: [obj]});
} else {
postMessage(obj, '*', [obj]);
}
}

promise_test(t => {
const orig = new TransformStream();
const promise = transfer(t, orig, [orig]);
assert_true(orig.readable.locked, 'the readable side should be locked');
assert_true(orig.writable.locked, 'the writable side should be locked');
return promise.then(transferred => {
assert_equals(transferred.constructor, TransformStream,
'transferred should be a TransformStream in this realm');
assert_true(transferred instanceof TransformStream,
'instanceof check should pass');

// Perform a brand-check on |transferred|.
const readableGetter = Object.getOwnPropertyDescriptor(
TransformStream.prototype, 'readable').get;
assert_true(readableGetter.call(transferred) instanceof ReadableStream,
'brand check should pass and readable stream should result');
const writableGetter = Object.getOwnPropertyDescriptor(
TransformStream.prototype, 'writable').get;
assert_true(writableGetter.call(transferred) instanceof WritableStream,
'brand check should pass and writable stream should result');
});
}, `should be able to transfer a TransformStream`);

test(() => {
const ts = new TransformStream();
const writer = ts.writable.getWriter();
assert_throws_dom('DataCloneError', () => failToTransfer(ts),
'transferring should throw');
assert_false(ts.readable.locked, 'readable side should not get locked');
}, 'a TransformStream with a locked writable should not be transferable');

test(() => {
const ts = new TransformStream();
const reader = ts.readable.getReader();
assert_throws_dom('DataCloneError', () => failToTransfer(ts),
'transferring should throw');
assert_false(ts.writable.locked, 'writable side should not get locked');
}, 'a TransformStream with a locked readable should not be transferable');

test(() => {
const ts = new TransformStream();
const reader = ts.readable.getReader();
const writer = ts.writable.getWriter();
assert_throws_dom('DataCloneError', () => failToTransfer(ts),
'transferring should throw');
}, 'a TransformStream with both sides locked should not be transferable');

promise_test(t => {
const source = new ReadableStream({
start(controller) {
controller.enqueue('hello ');
controller.enqueue('there ');
controller.close();
}
});
let resolve;
const ready = new Promise(r => resolve = r);
let result = '';
const sink = new WritableStream({
write(chunk) {
if (result) {
resolve();
}
result += chunk;
}
});
const transform1 = new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk.toUpperCase());
}
});
const transform2 = new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk + chunk);
}
});
return transfer(t, {source, sink, transform1, transform2},
[source, transform1, sink, transform2])
.then(data => {
resolve(data.source
.pipeThrough(data.transform1)
.pipeThrough(data.transform2)
.pipeTo(data.sink));
})
.then(() => ready)
.then(() => {
assert_equals(result, 'HELLO HELLO THERE THERE ',
'transforms should have been applied');
});
}, 'piping through transferred transforms should work');
108 changes: 0 additions & 108 deletions streams/transferable/transform-stream.html

This file was deleted.

Loading