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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
// META: global=window,worker
// META: script=../resources/test-utils.js
// META: script=../resources/recording-streams.js
'use strict';
const error1 = new Error('error1');
error1.name = 'error1';
const error2 = new Error('error2');
error2.name = 'error2';
promise_test(() => {
const ws = new WritableStream({
close() {
return 'Hello';
}
});
const writer = ws.getWriter();
const closePromise = writer.close();
return closePromise.then(value => assert_equals(value, undefined, 'fulfillment value must be undefined'));
}, 'fulfillment value of writer.close() call must be undefined even if the underlying sink returns a non-undefined ' +
'value');
promise_test(() => {
let controller;
let resolveClose;
const ws = new WritableStream({
start(c) {
controller = c;
},
close() {
return new Promise(resolve => {
resolveClose = resolve;
});
}
});
const writer = ws.getWriter();
const closePromise = writer.close();
return flushAsyncEvents().then(() => {
controller.error(error1);
return flushAsyncEvents();
}).then(() => {
resolveClose();
return Promise.all([
closePromise,
writer.closed,
flushAsyncEvents().then(() => writer.closed)]);
});
}, 'when sink calls error asynchronously while sink close is in-flight, the stream should not become errored');
promise_test(() => {
let controller;
const passedError = new Error('error me');
const ws = new WritableStream({
start(c) {
controller = c;
},
close() {
controller.error(passedError);
}
});
const writer = ws.getWriter();
return writer.close().then(() => writer.closed);
}, 'when sink calls error synchronously while closing, the stream should not become errored');
promise_test(t => {
const ws = new WritableStream({
close() {
throw error1;
}
});
const writer = ws.getWriter();
return Promise.all([
writer.write('y'),
promise_rejects_exactly(t, error1, writer.close(), 'close() must reject with the error'),
promise_rejects_exactly(t, error1, writer.closed, 'closed must reject with the error')
]);
}, 'when the sink throws during close, and the close is requested while a write is still in-flight, the stream should ' +
'become errored during the close');
promise_test(() => {
const ws = new WritableStream({
write(chunk, controller) {
controller.error(error1);
return new Promise(() => {});
}
});
const writer = ws.getWriter();
writer.write('a');
return delay(0).then(() => {
writer.releaseLock();
});
}, 'releaseLock on a stream with a pending write in which the stream has been errored');
promise_test(() => {
let controller;
const ws = new WritableStream({
start(c) {
controller = c;
},
close() {
controller.error(error1);
return new Promise(() => {});
}
});
const writer = ws.getWriter();
writer.close();
return delay(0).then(() => {
writer.releaseLock();
});
}, 'releaseLock on a stream with a pending close in which controller.error() was called');
promise_test(() => {
const ws = recordingWritableStream();
const writer = ws.getWriter();
return writer.ready.then(() => {
assert_equals(writer.desiredSize, 1, 'desiredSize should be 1');
writer.close();
assert_equals(writer.desiredSize, 1, 'desiredSize should be still 1');
return writer.ready.then(v => {
assert_equals(v, undefined, 'ready promise should be fulfilled with undefined');
assert_array_equals(ws.events, ['close'], 'write and abort should not be called');
});
});
}, 'when close is called on a WritableStream in writable state, ready should return a fulfilled promise');
promise_test(() => {
const ws = recordingWritableStream({
write() {
return new Promise(() => {});
}
});
const writer = ws.getWriter();
return writer.ready.then(() => {
writer.write('a');
assert_equals(writer.desiredSize, 0, 'desiredSize should be 0');
let calledClose = false;
return Promise.all([
writer.ready.then(v => {
assert_equals(v, undefined, 'ready promise should be fulfilled with undefined');
assert_true(calledClose, 'ready should not be fulfilled before writer.close() is called');
assert_array_equals(ws.events, ['write', 'a'], 'sink abort() should not be called');
}),
flushAsyncEvents().then(() => {
writer.close();
calledClose = true;
})
]);
});
}, 'when close is called on a WritableStream in waiting state, ready promise should be fulfilled');
promise_test(() => {
let asyncCloseFinished = false;
const ws = recordingWritableStream({
close() {
return flushAsyncEvents().then(() => {
asyncCloseFinished = true;
});
}
});
const writer = ws.getWriter();
return writer.ready.then(() => {
writer.write('a');
writer.close();
return writer.ready.then(v => {
assert_false(asyncCloseFinished, 'ready promise should be fulfilled before async close completes');
assert_equals(v, undefined, 'ready promise should be fulfilled with undefined');
assert_array_equals(ws.events, ['write', 'a', 'close'], 'sink abort() should not be called');
});
});
}, 'when close is called on a WritableStream in waiting state, ready should be fulfilled immediately even if close ' +
'takes a long time');
promise_test(t => {
const rejection = { name: 'letter' };
const ws = new WritableStream({
close() {
return {
then(onFulfilled, onRejected) { onRejected(rejection); }
};
}
});
return promise_rejects_exactly(t, rejection, ws.getWriter().close(), 'close() should return a rejection');
}, 'returning a thenable from close() should work');
promise_test(t => {
const ws = new WritableStream();
const writer = ws.getWriter();
return writer.ready.then(() => {
const closePromise = writer.close();
const closedPromise = writer.closed;
writer.releaseLock();
return Promise.all([
closePromise,
promise_rejects_js(t, TypeError, closedPromise, '.closed promise should be rejected')
]);
});
}, 'releaseLock() should not change the result of sync close()');
promise_test(t => {
const ws = new WritableStream({
close() {
return flushAsyncEvents();
}
});
const writer = ws.getWriter();
return writer.ready.then(() => {
const closePromise = writer.close();
const closedPromise = writer.closed;
writer.releaseLock();
return Promise.all([
closePromise,
promise_rejects_js(t, TypeError, closedPromise, '.closed promise should be rejected')
]);
});
}, 'releaseLock() should not change the result of async close()');
promise_test(() => {
let resolveClose;
const ws = new WritableStream({
close() {
const promise = new Promise(resolve => {
resolveClose = resolve;
});
return promise;
}
});
const writer = ws.getWriter();
const closePromise = writer.close();
writer.releaseLock();
return delay(0).then(() => {
resolveClose();
return closePromise.then(() => {
assert_equals(ws.getWriter().desiredSize, 0, 'desiredSize should be 0');
});
});
}, 'close() should set state to CLOSED even if writer has detached');
promise_test(() => {
let resolveClose;
const ws = new WritableStream({
close() {
const promise = new Promise(resolve => {
resolveClose = resolve;
});
return promise;
}
});
const writer = ws.getWriter();
writer.close();
writer.releaseLock();
return delay(0).then(() => {
const abortingWriter = ws.getWriter();
const abortPromise = abortingWriter.abort();
abortingWriter.releaseLock();
resolveClose();
return abortPromise;
});
}, 'the promise returned by async abort during close should resolve');
// Though the order in which the promises are fulfilled or rejected is arbitrary, we're checking it for
// interoperability. We can change the order as long as we file bugs on all implementers to update to the latest tests
// to keep them interoperable.
promise_test(() => {
const ws = new WritableStream({});
const writer = ws.getWriter();
const closePromise = writer.close();
const events = [];
return Promise.all([
closePromise.then(() => {
events.push('closePromise');
}),
writer.closed.then(() => {
events.push('closed');
})
]).then(() => {
assert_array_equals(events, ['closePromise', 'closed'],
'promises must fulfill/reject in the expected order');
});
}, 'promises must fulfill/reject in the expected order on closure');
promise_test(() => {
const ws = new WritableStream({});
// Wait until the WritableStream starts so that the close() call gets processed. Otherwise, abort() will be
// processed without waiting for completion of the close().
return delay(0).then(() => {
const writer = ws.getWriter();
const closePromise = writer.close();
const abortPromise = writer.abort(error1);
const events = [];
return Promise.all([
closePromise.then(() => {
events.push('closePromise');
}),
abortPromise.then(() => {
events.push('abortPromise');
}),
writer.closed.then(() => {
events.push('closed');
})
]).then(() => {
assert_array_equals(events, ['closePromise', 'abortPromise', 'closed'],
'promises must fulfill/reject in the expected order');
});
});
}, 'promises must fulfill/reject in the expected order on aborted closure');
promise_test(t => {
const ws = new WritableStream({
close() {
return Promise.reject(error1);
}
});
// Wait until the WritableStream starts so that the close() call gets processed.
return delay(0).then(() => {
const writer = ws.getWriter();
const closePromise = writer.close();
const abortPromise = writer.abort(error2);
const events = [];
closePromise.catch(() => events.push('closePromise'));
abortPromise.catch(() => events.push('abortPromise'));
writer.closed.catch(() => events.push('closed'));
return Promise.all([
promise_rejects_exactly(t, error1, closePromise,
'closePromise must reject with the error returned from the sink\'s close method'),
promise_rejects_exactly(t, error1, abortPromise,
'abortPromise must reject with the error returned from the sink\'s close method'),
promise_rejects_exactly(t, error2, writer.closed,
'writer.closed must reject with error2')
]).then(() => {
assert_array_equals(events, ['closePromise', 'abortPromise', 'closed'],
'promises must fulfill/reject in the expected order');
});
});
}, 'promises must fulfill/reject in the expected order on aborted and errored closure');
promise_test(t => {
let resolveWrite;
let controller;
const ws = new WritableStream({
write(chunk, c) {
controller = c;
return new Promise(resolve => {
resolveWrite = resolve;
});
}
});
const writer = ws.getWriter();
return writer.ready.then(() => {
const writePromise = writer.write('c');
controller.error(error1);
const closePromise = writer.close();
let closeRejected = false;
closePromise.catch(() => {
closeRejected = true;
});
return flushAsyncEvents().then(() => {
assert_false(closeRejected);
resolveWrite();
return Promise.all([
writePromise,
promise_rejects_exactly(t, error1, closePromise, 'close() should reject')
]).then(() => {
assert_true(closeRejected);
});
});
});
}, 'close() should not reject until no sink methods are in flight');
promise_test(() => {
const ws = new WritableStream();
const writer1 = ws.getWriter();
return writer1.close().then(() => {
writer1.releaseLock();
const writer2 = ws.getWriter();
const ready = writer2.ready;
assert_equals(ready.constructor, Promise);
return ready;
});
}, 'ready promise should be initialised as fulfilled for a writer on a closed stream');
promise_test(() => {
const ws = new WritableStream();
ws.close();
const writer = ws.getWriter();
return writer.closed;
}, 'close() on a writable stream should work');
promise_test(t => {
const ws = new WritableStream();
ws.getWriter();
return promise_rejects_js(t, TypeError, ws.close(), 'close should reject');
}, 'close() on a locked stream should reject');
promise_test(t => {
const ws = new WritableStream({
start(controller) {
controller.error(error1);
}
});
return promise_rejects_exactly(t, error1, ws.close(), 'close should reject with error1');
}, 'close() on an erroring stream should reject');
promise_test(t => {
const ws = new WritableStream({
start(controller) {
controller.error(error1);
}
});
const writer = ws.getWriter();
return promise_rejects_exactly(t, error1, writer.closed, 'closed should reject with the error').then(() => {
writer.releaseLock();
return promise_rejects_js(t, TypeError, ws.close(), 'close should reject');
});
}, 'close() on an errored stream should reject');
promise_test(t => {
const ws = new WritableStream();
const writer = ws.getWriter();
return writer.close().then(() => {
return promise_rejects_js(t, TypeError, ws.close(), 'close should reject');
});
}, 'close() on an closed stream should reject');
promise_test(t => {
const ws = new WritableStream({
close() {
return new Promise(() => {});
}
});
const writer = ws.getWriter();
writer.close();
writer.releaseLock();
return promise_rejects_js(t, TypeError, ws.close(), 'close should reject');
}, 'close() on a stream with a pending close should reject');
|