OLD | NEW |
| (Empty) |
1 'use strict'; | |
2 | |
3 if (self.importScripts) { | |
4 self.importScripts('/resources/testharness.js'); | |
5 self.importScripts('../resources/test-utils.js'); | |
6 self.importScripts('../resources/recording-streams.js'); | |
7 } | |
8 | |
9 const error1 = new Error('error1'); | |
10 error1.name = 'error1'; | |
11 | |
12 test(() => { | |
13 assert_throws(error1, () => { | |
14 new WritableStream({ | |
15 get start() { | |
16 throw error1; | |
17 } | |
18 }); | |
19 }, 'constructor should throw same error as throwing start getter'); | |
20 | |
21 assert_throws(error1, () => { | |
22 new WritableStream({ | |
23 start() { | |
24 throw error1; | |
25 } | |
26 }); | |
27 }, 'constructor should throw same error as throwing start method'); | |
28 | |
29 assert_throws(new TypeError(), () => { | |
30 new WritableStream({ | |
31 start: 'not a function or undefined' | |
32 }); | |
33 }, 'constructor should throw TypeError when passed a non-function start proper
ty'); | |
34 | |
35 assert_throws(new TypeError(), () => { | |
36 new WritableStream({ | |
37 start: { apply() {} } | |
38 }); | |
39 }, 'constructor should throw TypeError when passed a non-function start proper
ty with an .apply method'); | |
40 }, 'start: errors in start cause WritableStream constructor to throw'); | |
41 | |
42 promise_test(t => { | |
43 | |
44 const ws = recordingWritableStream({ | |
45 close() { | |
46 throw error1; | |
47 } | |
48 }); | |
49 | |
50 const writer = ws.getWriter(); | |
51 | |
52 return promise_rejects(t, error1, writer.close(), 'close() promise must reject
with the thrown error') | |
53 .then(() => promise_rejects(t, error1, writer.ready, 'ready promise must rejec
t with the thrown error')) | |
54 .then(() => promise_rejects(t, error1, writer.closed, 'closed promise must rej
ect with the thrown error')) | |
55 .then(() => { | |
56 assert_array_equals(ws.events, ['close']); | |
57 }); | |
58 | |
59 }, 'close: throwing method should cause writer close() and ready to reject'); | |
60 | |
61 promise_test(t => { | |
62 | |
63 const ws = recordingWritableStream({ | |
64 close() { | |
65 return Promise.reject(error1); | |
66 } | |
67 }); | |
68 | |
69 const writer = ws.getWriter(); | |
70 | |
71 return promise_rejects(t, error1, writer.close(), 'close() promise must reject
with the same error') | |
72 .then(() => promise_rejects(t, error1, writer.ready, 'ready promise must rejec
t with the same error')) | |
73 .then(() => assert_array_equals(ws.events, ['close'])); | |
74 | |
75 }, 'close: returning a rejected promise should cause writer close() and ready to
reject'); | |
76 | |
77 promise_test(t => { | |
78 const ws = new WritableStream({ | |
79 get close() { | |
80 throw error1; | |
81 } | |
82 }); | |
83 | |
84 const writer = ws.getWriter(); | |
85 | |
86 return promise_rejects(t, error1, writer.close(), 'close should reject with th
e thrown error'); | |
87 }, 'close: throwing getter should cause writer close() to reject'); | |
88 | |
89 promise_test(t => { | |
90 const ws = new WritableStream({ | |
91 get write() { | |
92 throw error1; | |
93 } | |
94 }); | |
95 | |
96 const writer = ws.getWriter(); | |
97 | |
98 return promise_rejects(t, error1, writer.write('a'), 'write should reject with
the thrown error') | |
99 .then(() => promise_rejects(t, error1, writer.closed, 'closed should reject wi
th the thrown error')); | |
100 }, 'write: throwing getter should cause write() and closed to reject'); | |
101 | |
102 promise_test(t => { | |
103 const ws = new WritableStream({ | |
104 write() { | |
105 throw error1; | |
106 } | |
107 }); | |
108 | |
109 const writer = ws.getWriter(); | |
110 | |
111 return promise_rejects(t, error1, writer.write('a'), 'write should reject with
the thrown error') | |
112 .then(() => promise_rejects(t, error1, writer.closed, 'closed should reject wi
th the thrown error')); | |
113 }, 'write: throwing method should cause write() and closed to reject'); | |
114 | |
115 promise_test(t => { | |
116 | |
117 const startPromise = Promise.resolve(); | |
118 let rejectSinkWritePromise; | |
119 const ws = recordingWritableStream({ | |
120 start() { | |
121 return startPromise; | |
122 }, | |
123 write() { | |
124 return new Promise((r, reject) => { | |
125 rejectSinkWritePromise = reject; | |
126 }); | |
127 } | |
128 }); | |
129 | |
130 return startPromise.then(() => { | |
131 const writer = ws.getWriter(); | |
132 const writePromise = writer.write('a'); | |
133 rejectSinkWritePromise(error1); | |
134 | |
135 return Promise.all([ | |
136 promise_rejects(t, error1, writePromise, 'writer write must reject with th
e same error'), | |
137 promise_rejects(t, error1, writer.ready, 'ready promise must reject with t
he same error') | |
138 ]); | |
139 }) | |
140 .then(() => { | |
141 assert_array_equals(ws.events, ['write', 'a']); | |
142 }); | |
143 | |
144 }, 'write: returning a promise that becomes rejected after the writer write() sh
ould cause writer write() and ready ' + | |
145 'to reject'); | |
146 | |
147 promise_test(t => { | |
148 | |
149 const ws = recordingWritableStream({ | |
150 write() { | |
151 if (ws.events.length === 2) { | |
152 return delay(0); | |
153 } | |
154 | |
155 return Promise.reject(error1); | |
156 } | |
157 }); | |
158 | |
159 const writer = ws.getWriter(); | |
160 | |
161 // Do not wait for this; we want to test the ready promise when the stream is
"full" (desiredSize = 0), but if we wait | |
162 // then the stream will transition back to "empty" (desiredSize = 1) | |
163 writer.write('a'); | |
164 const readyPromise = writer.ready; | |
165 | |
166 return promise_rejects(t, error1, writer.write('b'), 'second write must reject
with the same error').then(() => { | |
167 assert_equals(writer.ready, readyPromise, | |
168 'the ready promise must not change, since the queue was full after the fir
st write, so the pending one simply ' + | |
169 'transitioned'); | |
170 return promise_rejects(t, error1, writer.ready, 'ready promise must reject w
ith the same error'); | |
171 }) | |
172 .then(() => assert_array_equals(ws.events, ['write', 'a', 'write', 'b'])); | |
173 | |
174 }, 'write: returning a rejected promise (second write) should cause writer write
() and ready to reject'); | |
175 | |
176 promise_test(t => { | |
177 const ws = new WritableStream({ | |
178 abort: { apply() {} } | |
179 }); | |
180 | |
181 return promise_rejects(t, new TypeError(), ws.abort(error1), 'abort should rej
ect with TypeError').then(() => { | |
182 const writer = ws.getWriter(); | |
183 return promise_rejects(t, new TypeError(), writer.closed, 'closed should rej
ect with a TypeError'); | |
184 }); | |
185 }, 'abort: non-function abort method with .apply'); | |
186 | |
187 promise_test(t => { | |
188 const abortReason = new Error('different string'); | |
189 const ws = new WritableStream({ | |
190 get abort() { | |
191 throw error1; | |
192 } | |
193 }); | |
194 | |
195 const writer = ws.getWriter(); | |
196 | |
197 return promise_rejects(t, error1, writer.abort(abortReason), 'abort should rej
ect with the thrown error') | |
198 .then(() => promise_rejects(t, new TypeError(), writer.closed, 'closed should
reject with a TypeError')); | |
199 }, 'abort: throwing getter should cause abort() and closed to reject'); | |
200 | |
201 promise_test(t => { | |
202 const abortReason = new Error('different string'); | |
203 const ws = new WritableStream({ | |
204 abort() { | |
205 throw error1; | |
206 } | |
207 }); | |
208 | |
209 const writer = ws.getWriter(); | |
210 | |
211 return promise_rejects(t, error1, writer.abort(abortReason), 'abort should rej
ect with the thrown error') | |
212 .then(() => promise_rejects(t, new TypeError(), writer.closed, 'closed should
reject with a TypeError')); | |
213 }, 'abort: throwing method should cause abort() and closed to reject'); | |
214 | |
215 done(); | |
OLD | NEW |