OLD | NEW |
(Empty) | |
| 1 'use strict'; |
| 2 |
| 3 // These tests exercise the pathological case of calling WritableStream* methods
from within the strategy.size() |
| 4 // callback. This is not something any real code should ever do. Failures here i
ndicate subtle deviations from the |
| 5 // standard that may affect real, non-pathological code. |
| 6 |
| 7 if (self.importScripts) { |
| 8 self.importScripts('/resources/testharness.js'); |
| 9 self.importScripts('../resources/test-utils.js'); |
| 10 self.importScripts('../resources/recording-streams.js'); |
| 11 } |
| 12 |
| 13 const error1 = { name: 'error1' }; |
| 14 |
| 15 promise_test(() => { |
| 16 let writer; |
| 17 const strategy = { |
| 18 size(chunk) { |
| 19 if (chunk > 0) { |
| 20 writer.write(chunk - 1); |
| 21 } |
| 22 return chunk; |
| 23 } |
| 24 }; |
| 25 |
| 26 const ws = recordingWritableStream({}, strategy); |
| 27 writer = ws.getWriter(); |
| 28 return writer.write(2) |
| 29 .then(() => { |
| 30 assert_array_equals(ws.events, ['write', 0, 'write', 1, 'write', 2], 'wr
ites should appear in order'); |
| 31 }); |
| 32 }, 'writes should be written in the standard order'); |
| 33 |
| 34 promise_test(() => { |
| 35 let writer; |
| 36 const events = []; |
| 37 const strategy = { |
| 38 size(chunk) { |
| 39 events.push('size', chunk); |
| 40 if (chunk > 0) { |
| 41 writer.write(chunk - 1) |
| 42 .then(() => events.push('writer.write done', chunk - 1)); |
| 43 } |
| 44 return chunk; |
| 45 } |
| 46 }; |
| 47 const ws = new WritableStream({ |
| 48 write(chunk) { |
| 49 events.push('sink.write', chunk); |
| 50 } |
| 51 }, strategy); |
| 52 writer = ws.getWriter(); |
| 53 return writer.write(2) |
| 54 .then(() => events.push('writer.write done', 2)) |
| 55 .then(() => flushAsyncEvents()) |
| 56 .then(() => { |
| 57 assert_array_equals(events, ['size', 2, 'size', 1, 'size', 0, |
| 58 'sink.write', 0, 'sink.write', 1, 'writer.w
rite done', 0, |
| 59 'sink.write', 2, 'writer.write done', 1, |
| 60 'writer.write done', 2], |
| 61 'events should happen in standard order'); |
| 62 }); |
| 63 }, 'writer.write() promises should resolve in the standard order'); |
| 64 |
| 65 promise_test(t => { |
| 66 let controller; |
| 67 const strategy = { |
| 68 size() { |
| 69 controller.error(error1); |
| 70 return 1; |
| 71 } |
| 72 }; |
| 73 const ws = recordingWritableStream({ |
| 74 start(c) { |
| 75 controller = c; |
| 76 } |
| 77 }, strategy); |
| 78 const resolved = []; |
| 79 const writer = ws.getWriter(); |
| 80 const readyPromise1 = writer.ready.then(() => resolved.push('ready1')); |
| 81 const writePromise = promise_rejects(t, new TypeError(), writer.write(), |
| 82 'write() should reject with a TypeError') |
| 83 .then(() => resolved.push('write')); |
| 84 const readyPromise2 = promise_rejects(t, error1, writer.ready, 'ready should r
eject with error1') |
| 85 .then(() => resolved.push('ready2')); |
| 86 const closedPromise = promise_rejects(t, error1, writer.closed, 'closed should
reject with error1') |
| 87 .then(() => resolved.push('closed')); |
| 88 return Promise.all([readyPromise1, writePromise, readyPromise2, closedPromise]
) |
| 89 .then(() => { |
| 90 assert_array_equals(resolved, ['ready1', 'write', 'ready2', 'closed'], |
| 91 'promises should resolve in standard order'); |
| 92 assert_array_equals(ws.events, [], 'underlying sink write should not be
called'); |
| 93 }); |
| 94 }, 'controller.error() should work when called from within strategy.size()'); |
| 95 |
| 96 promise_test(t => { |
| 97 let writer; |
| 98 const strategy = { |
| 99 size() { |
| 100 writer.close(); |
| 101 return 1; |
| 102 } |
| 103 }; |
| 104 |
| 105 const ws = recordingWritableStream({}, strategy); |
| 106 writer = ws.getWriter(); |
| 107 return promise_rejects(t, new TypeError(), writer.write('a'), 'write() promise
should reject') |
| 108 .then(() => { |
| 109 assert_array_equals(ws.events, ['close'], 'sink.write() should not be ca
lled'); |
| 110 }); |
| 111 }, 'close() should work when called from within strategy.size()'); |
| 112 |
| 113 promise_test(t => { |
| 114 let writer; |
| 115 const strategy = { |
| 116 size() { |
| 117 writer.abort('nice'); |
| 118 return 1; |
| 119 } |
| 120 }; |
| 121 |
| 122 const ws = recordingWritableStream({}, strategy); |
| 123 writer = ws.getWriter(); |
| 124 return promise_rejects(t, new TypeError(), writer.write('a'), 'write() promise
should reject') |
| 125 .then(() => { |
| 126 assert_array_equals(ws.events, ['abort', 'nice'], 'sink.write() should n
ot be called'); |
| 127 }); |
| 128 }, 'abort() should work when called from within strategy.size()'); |
| 129 |
| 130 promise_test(t => { |
| 131 let writer; |
| 132 const strategy = { |
| 133 size() { |
| 134 writer.releaseLock(); |
| 135 return 1; |
| 136 } |
| 137 }; |
| 138 |
| 139 const ws = recordingWritableStream({}, strategy); |
| 140 writer = ws.getWriter(); |
| 141 const writePromise = promise_rejects(t, new TypeError(), writer.write('a'), 'w
rite() promise should reject'); |
| 142 const readyPromise = promise_rejects(t, new TypeError(), writer.ready, 'ready
promise should reject'); |
| 143 const closedPromise = promise_rejects(t, new TypeError(), writer.closed, 'clos
ed promise should reject'); |
| 144 return Promise.all([writePromise, readyPromise, closedPromise]) |
| 145 .then(() => { |
| 146 assert_array_equals(ws.events, [], 'sink.write() should not be called'); |
| 147 }); |
| 148 }, 'releaseLock() should abort the write() when called within strategy.size()'); |
| 149 |
| 150 promise_test(t => { |
| 151 let writer1; |
| 152 let ws; |
| 153 let writePromise2; |
| 154 let closePromise; |
| 155 let closedPromise2; |
| 156 const strategy = { |
| 157 size(chunk) { |
| 158 if (chunk > 0) { |
| 159 writer1.releaseLock(); |
| 160 const writer2 = ws.getWriter(); |
| 161 writePromise2 = writer2.write(0); |
| 162 closePromise = writer2.close(); |
| 163 closedPromise2 = writer2.closed; |
| 164 } |
| 165 return 1; |
| 166 } |
| 167 }; |
| 168 ws = recordingWritableStream({}, strategy); |
| 169 writer1 = ws.getWriter(); |
| 170 const writePromise1 = promise_rejects(t, new TypeError(), writer1.write(1), 'w
rite() promise should reject'); |
| 171 const readyPromise = promise_rejects(t, new TypeError(), writer1.ready, 'ready
promise should reject'); |
| 172 const closedPromise1 = promise_rejects(t, new TypeError(), writer1.closed, 'cl
osed promise should reject'); |
| 173 return Promise.all([writePromise1, readyPromise, closedPromise1, writePromise2
, closePromise, closedPromise2]) |
| 174 .then(() => { |
| 175 assert_array_equals(ws.events, ['write', 0, 'close'], 'sink.write() shou
ld only be called once'); |
| 176 }); |
| 177 }, 'original reader should error when new reader is created within strategy.size
()'); |
| 178 |
| 179 done(); |
OLD | NEW |