Skip to content

Commit

Permalink
WebSocketStream: Rename "connection" to "opened"
Browse files Browse the repository at this point in the history
See ricea/websocketstream-explainer#16

Also add some unit tests for use of AbortSignal. This was already
tested by wpts, but I need to keep the coverage bot happy.

Change-Id: I559553796a8d1358506d26800108105be58a0e13
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4685590
Reviewed-by: Nidhi Jaju <nidhijaju@chromium.org>
Commit-Queue: Adam Rice <ricea@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1175907}
  • Loading branch information
ricea authored and chromium-wpt-export-bot committed Jul 27, 2023
1 parent e03faa1 commit 2c8ef26
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 41 deletions.
16 changes: 9 additions & 7 deletions websockets/stream/tentative/abort.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ promise_test(async t => {
// We intentionally use the port for the HTTP server, not the WebSocket
// server, because we don't expect the connection to be performed.
const wss = new WebSocketStream(wsUrl, { signal: controller.signal });
await promise_rejects_dom(t, 'AbortError', wss.connection,
'connection should reject');
await promise_rejects_dom(t, 'AbortError', wss.closed, 'closed should reject');
await promise_rejects_dom(
t, 'AbortError', wss.opened, 'opened should reject');
await promise_rejects_dom(
t, 'AbortError', wss.closed, 'closed should reject');
// An incorrect implementation could pass this test due a race condition,
// but it is hard to completely eliminate the possibility.
const response = await fetch(`/fetch/api/resources/stash-take.py?key=${key}`);
Expand All @@ -32,15 +33,16 @@ promise_test(async t => {
// Give the connection a chance to start.
await new Promise(resolve => t.step_timeout(resolve, 0));
controller.abort();
await promise_rejects_dom(t, 'AbortError', wss.connection,
'connection should reject');
await promise_rejects_dom(t, 'AbortError', wss.closed, 'closed should reject');
await promise_rejects_dom(
t, 'AbortError', wss.opened, 'opened should reject');
await promise_rejects_dom(
t, 'AbortError', wss.closed, 'closed should reject');
}, 'abort during handshake should work');

promise_test(async t => {
const controller = new AbortController();
const wss = new WebSocketStream(ECHOURL, { signal: controller.signal });
const { readable, writable } = await wss.connection;
const { readable, writable } = await wss.opened;
controller.abort();
writable.getWriter().write('connected');
const { value } = await readable.getReader().read();
Expand Down
2 changes: 1 addition & 1 deletion websockets/stream/tentative/backpressure-receive.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const LARGE_MESSAGE_COUNT = 16;
// the large message.
promise_test(async t => {
const wss = new WebSocketStream(`${BASEURL}/send-backpressure`);
const { readable } = await wss.connection;
const { readable } = await wss.opened;
const reader = readable.getReader();

// Create backpressure for 2 seconds.
Expand Down
2 changes: 1 addition & 1 deletion websockets/stream/tentative/backpressure-send.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MESSAGE_SIZE = 8 * 1024 * 1024;
// times how long it takes to send the first message.
promise_test(async t => {
const wss = new WebSocketStream(`${BASEURL}/receive-backpressure`);
const { writable } = await wss.connection;
const { writable } = await wss.opened;
const writer = writable.getWriter();
const start = performance.now();
await writer.write(new Uint8Array(MESSAGE_SIZE));
Expand Down
43 changes: 22 additions & 21 deletions websockets/stream/tentative/close.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
await wss.opened;
wss.close({code: 3456, reason: 'pizza'});
const { code, reason } = await wss.closed;
assert_equals(code, 3456, 'code should match');
Expand All @@ -15,7 +15,7 @@ promise_test(async () => {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
await wss.opened;
wss.close();
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
Expand All @@ -24,7 +24,7 @@ promise_test(async () => {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
await wss.opened;
wss.close({});
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
Expand All @@ -33,7 +33,7 @@ promise_test(async () => {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
await wss.opened;
wss.close({reason: ''});
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
Expand All @@ -42,7 +42,7 @@ promise_test(async () => {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
await wss.opened;
wss.close({reason: 'non-empty'});
const { code, reason } = await wss.closed;
assert_equals(code, 1000, 'code should be set');
Expand All @@ -51,14 +51,14 @@ promise_test(async () => {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
await wss.opened;
assert_throws_js(TypeError, () => wss.close(true),
'close should throw a TypeError');
}, 'close(true) should throw a TypeError');

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
await wss.opened;
const reason = '.'.repeat(124);
assert_throws_dom('SyntaxError', () => wss.close({ reason }),
'close should throw a TypeError');
Expand All @@ -68,24 +68,25 @@ promise_test(t => {
const wss = new WebSocketStream(ECHOURL);
wss.close();
return Promise.all([
promise_rejects_dom(t, 'NetworkError', wss.connection,
'connection promise should reject'),
promise_rejects_dom(t, 'NetworkError', wss.closed,
'closed promise should reject')]);
promise_rejects_dom(
t, 'NetworkError', wss.opened, 'opened promise should reject'),
promise_rejects_dom(
t, 'NetworkError', wss.closed, 'closed promise should reject'),
]);
}, 'close during handshake should work');

for (const invalidCode of [999, 1001, 2999, 5000]) {
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
await wss.opened;
assert_throws_dom('InvalidAccessError', () => wss.close({ code: invalidCode }),
'close should throw a TypeError');
}, `close() with invalid code ${invalidCode} should throw`);
}

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const { writable } = await wss.connection;
const { writable } = await wss.opened;
writable.getWriter().close();
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
Expand All @@ -94,7 +95,7 @@ promise_test(async () => {

promise_test(async () => {
const wss = new WebSocketStream(`${BASEURL}/delayed-passive-close`);
const { writable } = await wss.connection;
const { writable } = await wss.opened;
const startTime = performance.now();
await writable.getWriter().close();
const elapsed = performance.now() - startTime;
Expand All @@ -120,7 +121,7 @@ for (const { method, voweling, stream } of abortOrCancel) {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
const info = await wss.opened;
info[stream][method]();
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
Expand All @@ -129,7 +130,7 @@ for (const { method, voweling, stream } of abortOrCancel) {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
const info = await wss.opened;
info[stream][method]({ code: 3333 });
const { code, reason } = await wss.closed;
assert_equals(code, 3333, 'code should be used');
Expand All @@ -138,7 +139,7 @@ for (const { method, voweling, stream } of abortOrCancel) {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
const info = await wss.opened;
info[stream][method]({ code: 3456, reason: 'set' });
const { code, reason } = await wss.closed;
assert_equals(code, 3456, 'code should be used');
Expand All @@ -147,7 +148,7 @@ for (const { method, voweling, stream } of abortOrCancel) {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
const info = await wss.opened;
info[stream][method]({ reason: 'specified' });
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
Expand All @@ -156,7 +157,7 @@ for (const { method, voweling, stream } of abortOrCancel) {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
const info = await wss.opened;
info[stream][method]({ code: 999 });
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
Expand All @@ -165,7 +166,7 @@ for (const { method, voweling, stream } of abortOrCancel) {

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
const info = await wss.opened;
info[stream][method]({ code: 1000, reason: 'x'.repeat(128) });
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
Expand All @@ -176,7 +177,7 @@ for (const { method, voweling, stream } of abortOrCancel) {
// be a valid WebSocket close code.
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
const info = await wss.opened;
info[stream][method](new DOMException('yes', 'DataCloneError'));
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
Expand Down
21 changes: 10 additions & 11 deletions websockets/stream/tentative/constructor.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ test(() => {
}, 'constructing with no URL should throw');

test(() => {
assert_throws_dom("SyntaxError", () => new WebSocketStream('invalid:'),
"constructor should throw");
assert_throws_dom('SyntaxError', () => new WebSocketStream('invalid:'),
'constructor should throw');
}, 'constructing with an invalid URL should throw');

test(() => {
assert_throws_js(TypeError,
() => new WebSocketStream(`${BASEURL}/`, true),
"constructor should throw");
'constructor should throw');
}, 'constructing with invalid options should throw');

test(() => {
assert_throws_js(TypeError,
() => new WebSocketStream(`${BASEURL}/`, {protocols: 'hi'}),
"constructor should throw");
'constructor should throw');
}, 'protocols should be required to be a list');

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
await wss.opened;
assert_equals(wss.url, ECHOURL, 'url should match');
wss.close();
}, 'constructing with a valid URL should work');

promise_test(async () => {
const wss = new WebSocketStream(`${BASEURL}/protocol_array`,
{protocols: ['alpha', 'beta']});
const { readable, protocol } = await wss.connection;
const { readable, protocol } = await wss.opened;
assert_equals(protocol, 'alpha', 'protocol should be right');
const reader = readable.getReader();
const { value, done } = await reader.read();
Expand All @@ -47,15 +47,14 @@ promise_test(async () => {
promise_test(t => {
const wss = new WebSocketStream(`${BASEURL}/404`);
return Promise.all([
promise_rejects_dom(t, 'NetworkError', wss.connection,
'connection should reject'),
promise_rejects_dom(t, 'NetworkError', wss.closed, 'closed should reject')
promise_rejects_dom(t, 'NetworkError', wss.opened, 'opened should reject'),
promise_rejects_dom(t, 'NetworkError', wss.closed, 'closed should reject'),
]);
}, 'connection failure should reject the promises');

promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const { readable, writable, protocol, extensions} = await wss.connection;
const { readable, writable, protocol, extensions} = await wss.opened;
// Verify that |readable| really is a ReadableStream using the getReader()
// brand check. If it doesn't throw the test passes.
ReadableStream.prototype.getReader.call(readable);
Expand All @@ -65,4 +64,4 @@ promise_test(async () => {
assert_equals(typeof protocol, 'string', 'protocol should be a string');
assert_equals(typeof extensions, 'string', 'extensions should be a string');
wss.close();
}, 'wss.connection should resolve to the right types');
}, 'wss.opened should resolve to the right types');

0 comments on commit 2c8ef26

Please sign in to comment.