| 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 test(() => { |
| 10 |
| 11 const rs = new ReadableStream(); |
| 12 const ws = new WritableStream(); |
| 13 |
| 14 assert_false(rs.locked, 'sanity check: the ReadableStream must not start locke
d'); |
| 15 assert_false(ws.locked, 'sanity check: the WritableStream must not start locke
d'); |
| 16 |
| 17 rs.pipeTo(ws); |
| 18 |
| 19 assert_true(rs.locked, 'the ReadableStream must become locked'); |
| 20 assert_true(ws.locked, 'the WritableStream must become locked'); |
| 21 |
| 22 }, 'Piping must lock both the ReadableStream and WritableStream'); |
| 23 |
| 24 promise_test(() => { |
| 25 |
| 26 const rs = new ReadableStream({ |
| 27 start(controller) { |
| 28 controller.close(); |
| 29 } |
| 30 }); |
| 31 const ws = new WritableStream(); |
| 32 |
| 33 return rs.pipeTo(ws).then(() => { |
| 34 assert_false(rs.locked, 'the ReadableStream must become unlocked'); |
| 35 assert_false(ws.locked, 'the WritableStream must become unlocked'); |
| 36 }); |
| 37 |
| 38 }, 'Piping finishing must unlock both the ReadableStream and WritableStream'); |
| 39 |
| 40 promise_test(t => { |
| 41 |
| 42 const fakeRS = Object.create(ReadableStream.prototype); |
| 43 const ws = new WritableStream(); |
| 44 |
| 45 return methodRejects(t, ReadableStream.prototype, 'pipeTo', fakeRS, [ws]); |
| 46 |
| 47 }, 'pipeTo must check the brand of its ReadableStream this value'); |
| 48 |
| 49 promise_test(t => { |
| 50 |
| 51 const rs = new ReadableStream(); |
| 52 const fakeWS = Object.create(WritableStream.prototype); |
| 53 |
| 54 return methodRejects(t, ReadableStream.prototype, 'pipeTo', rs, [fakeWS]); |
| 55 |
| 56 }, 'pipeTo must check the brand of its WritableStream argument'); |
| 57 |
| 58 promise_test(t => { |
| 59 |
| 60 const rs = new ReadableStream(); |
| 61 const ws = new WritableStream(); |
| 62 |
| 63 rs.getReader(); |
| 64 |
| 65 assert_true(rs.locked, 'sanity check: the ReadableStream starts locked'); |
| 66 assert_false(ws.locked, 'sanity check: the WritableStream does not start locke
d'); |
| 67 |
| 68 return promise_rejects(t, new TypeError(), rs.pipeTo(ws)).then(() => { |
| 69 assert_false(ws.locked, 'the WritableStream must still be unlocked'); |
| 70 }); |
| 71 |
| 72 }, 'pipeTo must fail if the ReadableStream is locked, and not lock the WritableS
tream'); |
| 73 |
| 74 promise_test(t => { |
| 75 |
| 76 const rs = new ReadableStream(); |
| 77 const ws = new WritableStream(); |
| 78 |
| 79 ws.getWriter(); |
| 80 |
| 81 assert_false(rs.locked, 'sanity check: the ReadableStream does not start locke
d'); |
| 82 assert_true(ws.locked, 'sanity check: the WritableStream starts locked'); |
| 83 |
| 84 return promise_rejects(t, new TypeError(), rs.pipeTo(ws)).then(() => { |
| 85 assert_false(rs.locked, 'the ReadableStream must still be unlocked'); |
| 86 }); |
| 87 |
| 88 }, 'pipeTo must fail if the WritableStream is locked, and not lock the ReadableS
tream'); |
| 89 |
| 90 promise_test(() => { |
| 91 |
| 92 const CHUNKS = 10; |
| 93 |
| 94 const rs = new ReadableStream({ |
| 95 start(c) { |
| 96 for (let i = 0; i < CHUNKS; ++i) { |
| 97 c.enqueue(i); |
| 98 } |
| 99 c.close(); |
| 100 } |
| 101 }); |
| 102 |
| 103 const written = []; |
| 104 const ws = new WritableStream({ |
| 105 write(chunk) { |
| 106 written.push(chunk); |
| 107 }, |
| 108 close() { |
| 109 written.push('closed'); |
| 110 } |
| 111 }, new CountQueuingStrategy({ highWaterMark: CHUNKS })); |
| 112 |
| 113 return rs.pipeTo(ws).then(() => { |
| 114 const targetValues = []; |
| 115 for (let i = 0; i < CHUNKS; ++i) { |
| 116 targetValues.push(i); |
| 117 } |
| 118 targetValues.push('closed'); |
| 119 |
| 120 assert_array_equals(written, targetValues, 'the correct values must be writt
en'); |
| 121 |
| 122 // Ensure both readable and writable are closed by the time the pipe finishe
s. |
| 123 return Promise.all([ |
| 124 rs.getReader().closed, |
| 125 ws.getWriter().closed |
| 126 ]); |
| 127 }); |
| 128 |
| 129 // NOTE: no requirement on *when* the pipe finishes; that is left to implement
ations. |
| 130 |
| 131 }, 'Piping from a ReadableStream from which lots of chunks are synchronously rea
dable'); |
| 132 |
| 133 promise_test(() => { |
| 134 |
| 135 let controller; |
| 136 const rs = recordingReadableStream({ |
| 137 start(c) { |
| 138 controller = c; |
| 139 } |
| 140 }); |
| 141 |
| 142 const ws = recordingWritableStream(); |
| 143 |
| 144 const pipePromise = rs.pipeTo(ws).then(() => { |
| 145 assert_array_equals(ws.events, ['write', 'Hello', 'close']); |
| 146 }); |
| 147 |
| 148 setTimeout(() => { |
| 149 controller.enqueue('Hello'); |
| 150 setTimeout(() => controller.close(), 10); |
| 151 }, 10); |
| 152 |
| 153 return pipePromise; |
| 154 |
| 155 }, 'Piping from a ReadableStream for which a chunk becomes asynchronously readab
le after the pipeTo'); |
| 156 |
| 157 done(); |
| OLD | NEW |