summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webtransport/streams-echo.https.any.js
blob: 703318d0e7020af468dc3f4996b1cede78ba9258 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// META: global=window,worker
// META: script=/common/get-host-info.sub.js
// META: script=resources/webtransport-test-helpers.sub.js

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // Create a bidirectional stream.
  const bidi_stream = await wt.createBidirectionalStream();

  // Write a message to the writable end, and close it.
  const writer = bidi_stream.writable.getWriter();
  const encoder = new TextEncoder();
  await writer.write(encoder.encode('Hello World'));
  await writer.close();

  // Read the data on the readable end.
  const reply = await read_stream_as_string(bidi_stream.readable);

  // Check that the message from the readable end matches the writable end.
  assert_equals(reply, 'Hello World');
}, 'WebTransport client should be able to create and handle a bidirectional stream');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));

  // Create a bidirectional stream.
  const bidi_stream = await wt.createBidirectionalStream();

  // Write a message to the writable end, and close it.
  const writer = bidi_stream.writable.getWriter();
  const encoder = new TextEncoder();
  await writer.write(encoder.encode('Hello World'));
  await writer.close();

  // Read the data on the readable end.
  const reply = await read_stream_as_string(bidi_stream.readable);

  // Check that the message from the readable end matches the writable end.
  assert_equals(reply, 'Hello World');
}, 'WebTransport client should be able to create and handle a bidirectional stream without waiting for ready');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // The echo handler creates a bidirectional stream when a WebTransport session
  // is established. Accept the bidirectional stream.
  const stream_reader = wt.incomingBidirectionalStreams.getReader();
  const { value: bidi_stream } = await stream_reader.read();
  stream_reader.releaseLock();

  // Write a message to the writable end, and close it.
  const encoder = new TextEncoderStream();
  encoder.readable.pipeTo(bidi_stream.writable);
  const writer = encoder.writable.getWriter();
  await writer.write('Hello World');
  await writer.close();

  // Read the data on the readable end.
  const reply = await read_stream_as_string(bidi_stream.readable);

  // Check that the message from the readable end matches the writable end.
  assert_equals(reply, 'Hello World');
}, 'WebTransport server should be able to accept and handle a bidirectional stream');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // Create a unidirectional stream.
  const writable = await wt.createUnidirectionalStream();

  // Write a message to the writable end, and close it.
  const encoder = new TextEncoderStream();
  encoder.readable.pipeTo(writable);
  const writer = encoder.writable.getWriter();
  await writer.write('Hello World');
  await writer.close();

  // The echo handler creates a new unidirectional stream to echo back data from
  // the server to client. Accept the unidirectional stream.
  const readable = wt.incomingUnidirectionalStreams;
  const stream_reader = readable.getReader();
  const { value: recv_stream } = await stream_reader.read();
  stream_reader.releaseLock();

  // Read the data on the readable end.
  const reply = await read_stream_as_string(recv_stream);

  // Make sure the message on the writable and readable ends of the streams
  // match.
  assert_equals(reply, 'Hello World');
}, 'WebTransport client should be able to create, accept, and handle a unidirectional stream');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));

  // Create a unidirectional stream.
  const writable = await wt.createUnidirectionalStream();

  // Write a message to the writable end, and close it.
  const encoder = new TextEncoderStream();
  encoder.readable.pipeTo(writable);
  const writer = encoder.writable.getWriter();
  await writer.write('Hello World');
  await writer.close();

  // The echo handler creates a new unidirectional stream to echo back data from
  // the server to client. Accept the unidirectional stream.
  const readable = wt.incomingUnidirectionalStreams;
  const stream_reader = readable.getReader();
  const { value: recv_stream } = await stream_reader.read();
  stream_reader.releaseLock();

  // Read the data on the readable end.
  const reply = await read_stream_as_string(recv_stream);

  // Make sure the message on the writable and readable ends of the streams
  // match.
  assert_equals(reply, 'Hello World');
}, 'WebTransport client should be able to create, accept, and handle a unidirectional stream without waiting for ready');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // The echo handler creates a bidirectional stream when a WebTransport session
  // is established. Accept the bidirectional stream.
  const stream_reader = wt.incomingBidirectionalStreams.getReader();
  const {value: bidi_stream} = await stream_reader.read();
  stream_reader.releaseLock();

  // Write data to the writable end, and close it.
  const buffer_size = 256;
  const data = new Uint8Array(buffer_size);
  for (let i = 0; i < data.byteLength; ++i) {
    data[i] = i;
  }
  const writer = bidi_stream.writable.getWriter();
  writer.write(data);
  await writer.close();

  // Read the data on the readable end and check if it matches the writable end.
  const reader = bidi_stream.readable.getReader({mode: 'byob'});
  assert_true(reader instanceof ReadableStreamBYOBReader);
  const half_buffer_size = buffer_size / 2;
  for (let i = 0; i < 2; i++) {
    let buffer = new ArrayBuffer(half_buffer_size);
    buffer = await readInto(reader, buffer);
    assert_array_equals(
        new Uint8Array(buffer),
        data.subarray(half_buffer_size * i, half_buffer_size * (i + 1)))
  }
  reader.releaseLock();
}, 'Can read data from a bidirectional stream with BYOB reader');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // Create a unidirectional stream.
  const writable = await wt.createUnidirectionalStream();

  // Write data to the writable end, and close it.
  const buffer_size = 256;
  const data = new Uint8Array(buffer_size);
  for (let i = 0; i < data.byteLength; ++i) {
    data[i] = i;
  }
  const writer = writable.getWriter();
  writer.write(data);
  await writer.close();

  // The echo handler creates a new unidirectional stream to echo back data from
  // the server to client. Accept the unidirectional stream.
  const readable = wt.incomingUnidirectionalStreams;
  const stream_reader = readable.getReader();
  const {value: recv_stream} = await stream_reader.read();
  stream_reader.releaseLock();

  // Read the data on the readable end and check if it matches the writable end.
  const reader = recv_stream.getReader({mode: 'byob'});
  assert_true(reader instanceof ReadableStreamBYOBReader);
  const half_buffer_size = buffer_size / 2;
  let buffer = new ArrayBuffer(half_buffer_size);
  for (let i = 0; i < 2; i++) {
    buffer = await readInto(reader, buffer);
    assert_array_equals(
        new Uint8Array(buffer),
        data.subarray(half_buffer_size * i, half_buffer_size * (i + 1)))
  }
  reader.releaseLock();
}, 'Can read data from a unidirectional stream with BYOB reader');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // Create a bidirectional stream.
  const bidi_stream = await wt.createBidirectionalStream();

  // Write a message to the writable end, and close it.
  const writer = bidi_stream.writable.getWriter();
  const bytes = new Uint8Array(16384);
  const [reply] = await Promise.all([
    read_stream(bidi_stream.readable),
    writer.write(bytes),
    writer.write(bytes),
    writer.write(bytes),
    writer.close()
  ]);
  let len = 0;
  for (chunk of reply) {
    len += chunk.length;
  }
  // Check that the message from the readable end matches the writable end.
  assert_equals(len, 3*bytes.length);
}, 'Transfer large chunks of data on a bidirectional stream');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // Create a unidirectional stream.
  const uni_stream = await wt.createUnidirectionalStream();

  // Write a message to the writable end, and close it.
  const writer = uni_stream.getWriter();
  const bytes = new Uint8Array(16384);
  await Promise.all([
    writer.write(bytes),
    writer.write(bytes),
    writer.write(bytes),
    writer.close()
  ]);
  // XXX Update once chrome fixes https://crbug.com/929585
  // The echo handler creates a new unidirectional stream to echo back data from
  // the server to client. Accept the unidirectional stream.
  const readable = wt.incomingUnidirectionalStreams;
  const stream_reader = readable.getReader();
  const { value: recv_stream } = await stream_reader.read();
  stream_reader.releaseLock();

  // Read the data on the readable end.
  const reply = await read_stream(recv_stream);
  let len = 0;
  for (chunk of reply) {
    len += chunk.length;
  }
  // Check that the message from the readable end matches the writable end.
  assert_equals(len, 3*bytes.length);
}, 'Transfer large chunks of data on a unidirectional stream');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // Create a bidirectional stream.
  const bidi_stream = await wt.createBidirectionalStream();

  // Close the writable end with no data at all.
  const writer = bidi_stream.writable.getWriter();
  writer.close();

  // Read the data on the readable end.
  const chunks = await read_stream(bidi_stream.readable);
  assert_equals(chunks.length, 0);

  await bidi_stream.readable.closed;
}, 'Closing the stream with no data still resolves the read request');