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
|
// 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';
function writeArrayToStream(array, writableStreamWriter) {
array.forEach(chunk => writableStreamWriter.write(chunk));
return writableStreamWriter.close();
}
promise_test(() => {
let storage;
const ws = new WritableStream({
start() {
storage = [];
},
write(chunk) {
return delay(0).then(() => storage.push(chunk));
},
close() {
return delay(0);
}
});
const writer = ws.getWriter();
const input = [1, 2, 3, 4, 5];
return writeArrayToStream(input, writer)
.then(() => assert_array_equals(storage, input, 'correct data should be relayed to underlying sink'));
}, 'WritableStream should complete asynchronous writes before close resolves');
promise_test(() => {
const ws = recordingWritableStream();
const writer = ws.getWriter();
const input = [1, 2, 3, 4, 5];
return writeArrayToStream(input, writer)
.then(() => assert_array_equals(ws.events, ['write', 1, 'write', 2, 'write', 3, 'write', 4, 'write', 5, 'close'],
'correct data should be relayed to underlying sink'));
}, 'WritableStream should complete synchronous writes before close resolves');
promise_test(() => {
const ws = new WritableStream({
write() {
return 'Hello';
}
});
const writer = ws.getWriter();
const writePromise = writer.write('a');
return writePromise
.then(value => assert_equals(value, undefined, 'fulfillment value must be undefined'));
}, 'fulfillment value of ws.write() call should be undefined even if the underlying sink returns a non-undefined ' +
'value');
promise_test(() => {
let resolveSinkWritePromise;
const ws = new WritableStream({
write() {
return new Promise(resolve => {
resolveSinkWritePromise = resolve;
});
}
});
const writer = ws.getWriter();
assert_equals(writer.desiredSize, 1, 'desiredSize should be 1');
return writer.ready.then(() => {
const writePromise = writer.write('a');
let writePromiseResolved = false;
assert_not_equals(resolveSinkWritePromise, undefined, 'resolveSinkWritePromise should not be undefined');
assert_equals(writer.desiredSize, 0, 'desiredSize should be 0 after writer.write()');
return Promise.all([
writePromise.then(value => {
writePromiseResolved = true;
assert_equals(resolveSinkWritePromise, undefined, 'sinkWritePromise should be fulfilled before writePromise');
assert_equals(value, undefined, 'writePromise should be fulfilled with undefined');
}),
writer.ready.then(value => {
assert_equals(resolveSinkWritePromise, undefined, 'sinkWritePromise should be fulfilled before writer.ready');
assert_true(writePromiseResolved, 'writePromise should be fulfilled before writer.ready');
assert_equals(writer.desiredSize, 1, 'desiredSize should be 1 again');
assert_equals(value, undefined, 'writePromise should be fulfilled with undefined');
}),
flushAsyncEvents().then(() => {
resolveSinkWritePromise();
resolveSinkWritePromise = undefined;
})
]);
});
}, 'WritableStream should transition to waiting until write is acknowledged');
promise_test(t => {
let sinkWritePromiseRejectors = [];
const ws = new WritableStream({
write() {
const sinkWritePromise = new Promise((r, reject) => sinkWritePromiseRejectors.push(reject));
return sinkWritePromise;
}
});
const writer = ws.getWriter();
assert_equals(writer.desiredSize, 1, 'desiredSize should be 1');
return writer.ready.then(() => {
const writePromise = writer.write('a');
assert_equals(sinkWritePromiseRejectors.length, 1, 'there should be 1 rejector');
assert_equals(writer.desiredSize, 0, 'desiredSize should be 0');
const writePromise2 = writer.write('b');
assert_equals(sinkWritePromiseRejectors.length, 1, 'there should be still 1 rejector');
assert_equals(writer.desiredSize, -1, 'desiredSize should be -1');
const closedPromise = writer.close();
assert_equals(writer.desiredSize, -1, 'desiredSize should still be -1');
return Promise.all([
promise_rejects_exactly(t, error1, closedPromise,
'closedPromise should reject with the error returned from the sink\'s write method')
.then(() => assert_equals(sinkWritePromiseRejectors.length, 0,
'sinkWritePromise should reject before closedPromise')),
promise_rejects_exactly(t, error1, writePromise,
'writePromise should reject with the error returned from the sink\'s write method')
.then(() => assert_equals(sinkWritePromiseRejectors.length, 0,
'sinkWritePromise should reject before writePromise')),
promise_rejects_exactly(t, error1, writePromise2,
'writePromise2 should reject with the error returned from the sink\'s write method')
.then(() => assert_equals(sinkWritePromiseRejectors.length, 0,
'sinkWritePromise should reject before writePromise2')),
flushAsyncEvents().then(() => {
sinkWritePromiseRejectors[0](error1);
sinkWritePromiseRejectors = [];
})
]);
});
}, 'when write returns a rejected promise, queued writes and close should be cleared');
promise_test(t => {
const ws = new WritableStream({
write() {
throw error1;
}
});
const writer = ws.getWriter();
return promise_rejects_exactly(t, error1, writer.write('a'),
'write() should reject with the error returned from the sink\'s write method')
.then(() => promise_rejects_js(t, TypeError, writer.close(), 'close() should be rejected'));
}, 'when sink\'s write throws an error, the stream should become errored and the promise should reject');
promise_test(t => {
const ws = new WritableStream({
write(chunk, controller) {
controller.error(error1);
throw error2;
}
});
const writer = ws.getWriter();
return promise_rejects_exactly(t, error2, writer.write('a'),
'write() should reject with the error returned from the sink\'s write method ')
.then(() => {
return Promise.all([
promise_rejects_exactly(t, error1, writer.ready,
'writer.ready must reject with the error passed to the controller'),
promise_rejects_exactly(t, error1, writer.closed,
'writer.closed must reject with the error passed to the controller')
]);
});
}, 'writer.write(), ready and closed reject with the error passed to controller.error() made before sink.write' +
' rejection');
promise_test(() => {
const numberOfWrites = 1000;
let resolveFirstWritePromise;
let writeCount = 0;
const ws = new WritableStream({
write() {
++writeCount;
if (!resolveFirstWritePromise) {
return new Promise(resolve => {
resolveFirstWritePromise = resolve;
});
}
return Promise.resolve();
}
});
const writer = ws.getWriter();
return writer.ready.then(() => {
for (let i = 1; i < numberOfWrites; ++i) {
writer.write('a');
}
const writePromise = writer.write('a');
assert_equals(writeCount, 1, 'should have called sink\'s write once');
resolveFirstWritePromise();
return writePromise
.then(() =>
assert_equals(writeCount, numberOfWrites, `should have called sink's write ${numberOfWrites} times`));
});
}, 'a large queue of writes should be processed completely');
promise_test(() => {
const stream = recordingWritableStream();
const w = stream.getWriter();
const WritableStreamDefaultWriter = w.constructor;
w.releaseLock();
const writer = new WritableStreamDefaultWriter(stream);
return writer.ready.then(() => {
writer.write('a');
assert_array_equals(stream.events, ['write', 'a'], 'write() should be passed to sink');
});
}, 'WritableStreamDefaultWriter should work when manually constructed');
promise_test(() => {
let thenCalled = false;
const ws = new WritableStream({
write() {
return {
then(onFulfilled) {
thenCalled = true;
onFulfilled();
}
};
}
});
return ws.getWriter().write('a').then(() => assert_true(thenCalled, 'thenCalled should be true'));
}, 'returning a thenable from write() should work');
promise_test(() => {
const stream = new WritableStream();
const writer = stream.getWriter();
const WritableStreamDefaultWriter = writer.constructor;
assert_throws_js(TypeError, () => new WritableStreamDefaultWriter(stream),
'should not be able to construct on locked stream');
// If stream.[[writer]] no longer points to |writer| then the closed Promise
// won't work properly.
return Promise.all([writer.close(), writer.closed]);
}, 'failing DefaultWriter constructor should not release an existing writer');
promise_test(t => {
const ws = new WritableStream({
start() {
return Promise.reject(error1);
}
}, { highWaterMark: 0 });
const writer = ws.getWriter();
return Promise.all([
promise_rejects_exactly(t, error1, writer.ready, 'ready should be rejected'),
promise_rejects_exactly(t, error1, writer.write(), 'write() should be rejected')
]);
}, 'write() on a stream with HWM 0 should not cause the ready Promise to resolve');
promise_test(t => {
const ws = new WritableStream();
const writer = ws.getWriter();
writer.releaseLock();
return promise_rejects_js(t, TypeError, writer.write(), 'write should reject');
}, 'writing to a released writer should reject the returned promise');
|