|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const { Writable } = require('stream'); |
| 6 | + |
| 7 | +async function checkReusableBatches() { |
| 8 | + const consumed = []; |
| 9 | + let batches = 0; |
| 10 | + let drains = 0; |
| 11 | + const writable = new Writable({ |
| 12 | + highWaterMark: 64 * 1024, |
| 13 | + write(chunk, encoding, callback) { |
| 14 | + setImmediate(() => { |
| 15 | + consumed.push(Buffer.from(chunk)); |
| 16 | + callback(); |
| 17 | + }); |
| 18 | + }, |
| 19 | + writev(chunks, callback) { |
| 20 | + batches++; |
| 21 | + setImmediate(() => { |
| 22 | + consumed.push(...chunks.map(({ chunk }) => Buffer.from(chunk))); |
| 23 | + callback(); |
| 24 | + }); |
| 25 | + }, |
| 26 | + }); |
| 27 | + writable.on('error', common.mustNotCall()); |
| 28 | + writable.on('drain', () => { drains++; }); |
| 29 | + const writer = Writable.toWeb(writable).getWriter(); |
| 30 | + const input = Buffer.alloc(1024); |
| 31 | + for (let i = 1; i <= 128; i++) { |
| 32 | + input.fill(i); |
| 33 | + await writer.write(input); |
| 34 | + if (i === 1) assert.strictEqual(consumed.length, 1); |
| 35 | + input.fill(255); |
| 36 | + } |
| 37 | + await writer.close(); |
| 38 | + |
| 39 | + assert(batches > 0, 'small awaited writes should reach native writev'); |
| 40 | + assert(drains > 0, 'buffered small writes should observe native backpressure'); |
| 41 | + assert.strictEqual(consumed.length, 128); |
| 42 | + for (let i = 1; i <= 128; i++) { |
| 43 | + assert.deepStrictEqual(consumed[i - 1], Buffer.alloc(1024, i)); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +async function checkLateWriteError() { |
| 48 | + const error = new Error('late native write failure'); |
| 49 | + const started = Promise.withResolvers(); |
| 50 | + let finishWrite; |
| 51 | + const writable = new Writable({ |
| 52 | + write: common.mustCall((chunk, encoding, callback) => { |
| 53 | + if (chunk[0] === 1) { |
| 54 | + setImmediate(callback); |
| 55 | + } else { |
| 56 | + finishWrite = callback; |
| 57 | + started.resolve(); |
| 58 | + } |
| 59 | + }, 2), |
| 60 | + }); |
| 61 | + const writer = Writable.toWeb(writable).getWriter(); |
| 62 | + await writer.write(Buffer.from([1])); |
| 63 | + |
| 64 | + const closed = assert.rejects(writer.closed, (actual) => actual === error); |
| 65 | + let fulfilled = false; |
| 66 | + const smallWrite = writer.write(Buffer.from([2])).then(() => { |
| 67 | + fulfilled = true; |
| 68 | + return true; |
| 69 | + }, (actual) => { |
| 70 | + assert.strictEqual(actual, error); |
| 71 | + return false; |
| 72 | + }); |
| 73 | + await started.promise; |
| 74 | + await new Promise(setImmediate); |
| 75 | + const fulfilledBeforeError = fulfilled; |
| 76 | + const close = assert.rejects(writer.close(), (actual) => actual === error); |
| 77 | + finishWrite(error); |
| 78 | + const accepted = await smallWrite; |
| 79 | + await Promise.all([closed, close]); |
| 80 | + |
| 81 | + assert.strictEqual(fulfilledBeforeError, true); |
| 82 | + assert.strictEqual(accepted, true); |
| 83 | +} |
| 84 | + |
| 85 | +async function checkLargeWrite(size) { |
| 86 | + const started = Promise.withResolvers(); |
| 87 | + const input = new Uint8Array(new ArrayBuffer(size + 8), 4, size); |
| 88 | + input.fill(3); |
| 89 | + let consumed; |
| 90 | + let finishWrite; |
| 91 | + const writable = new Writable({ |
| 92 | + highWaterMark: 4, |
| 93 | + write: common.mustCall((chunk, encoding, callback) => { |
| 94 | + if (chunk[0] === 1) { |
| 95 | + setImmediate(callback); |
| 96 | + } else { |
| 97 | + assert.strictEqual(chunk.buffer, input.buffer); |
| 98 | + assert.strictEqual(chunk.byteOffset, input.byteOffset); |
| 99 | + finishWrite = () => { |
| 100 | + consumed = Buffer.from(chunk); |
| 101 | + callback(); |
| 102 | + }; |
| 103 | + started.resolve(); |
| 104 | + } |
| 105 | + }, 2), |
| 106 | + }); |
| 107 | + writable.on('error', common.mustNotCall()); |
| 108 | + const writer = Writable.toWeb(writable).getWriter(); |
| 109 | + await writer.write(Buffer.from([1])); |
| 110 | + |
| 111 | + let fulfilled = false; |
| 112 | + const pending = writer.write(input).then(() => { fulfilled = true; }); |
| 113 | + await started.promise; |
| 114 | + await new Promise(setImmediate); |
| 115 | + const fulfilledBeforeCompletion = fulfilled; |
| 116 | + finishWrite(); |
| 117 | + await pending; |
| 118 | + input.fill(255); |
| 119 | + await writer.close(); |
| 120 | + |
| 121 | + assert.strictEqual(fulfilledBeforeCompletion, false); |
| 122 | + assert.deepStrictEqual(consumed, Buffer.alloc(size, 3)); |
| 123 | +} |
| 124 | + |
| 125 | +async function checkLargeWriteAfterCopiedBatch() { |
| 126 | + const consumed = []; |
| 127 | + const batchStarted = Promise.withResolvers(); |
| 128 | + const input = Buffer.alloc(1); |
| 129 | + const large = Buffer.alloc(4, 5); |
| 130 | + let finishInitial; |
| 131 | + let finishBatch; |
| 132 | + const writable = new Writable({ |
| 133 | + highWaterMark: 4, |
| 134 | + write: common.mustCall((chunk, encoding, callback) => { |
| 135 | + const consume = () => { |
| 136 | + consumed.push(Buffer.from(chunk)); |
| 137 | + callback(); |
| 138 | + }; |
| 139 | + if (chunk[0] === 1) setImmediate(consume); |
| 140 | + else finishInitial = consume; |
| 141 | + }, 2), |
| 142 | + writev: common.mustCall((chunks, callback) => { |
| 143 | + assert.strictEqual(chunks.length, 3); |
| 144 | + assert.strictEqual(chunks[2].chunk.buffer, large.buffer); |
| 145 | + finishBatch = () => { |
| 146 | + consumed.push(...chunks.map(({ chunk }) => Buffer.from(chunk))); |
| 147 | + callback(); |
| 148 | + }; |
| 149 | + batchStarted.resolve(); |
| 150 | + }), |
| 151 | + }); |
| 152 | + writable.on('error', common.mustNotCall()); |
| 153 | + const writer = Writable.toWeb(writable).getWriter(); |
| 154 | + await writer.write(Buffer.from([1])); |
| 155 | + for (const value of [2, 3, 4]) { |
| 156 | + input[0] = value; |
| 157 | + await writer.write(input); |
| 158 | + input[0] = 255; |
| 159 | + } |
| 160 | + |
| 161 | + let fulfilled = false; |
| 162 | + const pending = writer.write(large).then(() => { fulfilled = true; }); |
| 163 | + await new Promise(setImmediate); |
| 164 | + assert.strictEqual(fulfilled, false); |
| 165 | + finishInitial(); |
| 166 | + await batchStarted.promise; |
| 167 | + await new Promise(setImmediate); |
| 168 | + const fulfilledBeforeBatchCompletion = fulfilled; |
| 169 | + finishBatch(); |
| 170 | + await pending; |
| 171 | + large.fill(255); |
| 172 | + await writer.close(); |
| 173 | + |
| 174 | + assert.strictEqual(fulfilledBeforeBatchCompletion, false); |
| 175 | + assert.deepStrictEqual(consumed, [ |
| 176 | + Buffer.from([1]), Buffer.from([2]), Buffer.from([3]), |
| 177 | + Buffer.from([4]), Buffer.from([5, 5, 5, 5]), |
| 178 | + ]); |
| 179 | +} |
| 180 | + |
| 181 | +async function checkQueuedWritevError() { |
| 182 | + const error = new Error('queued native writev failure'); |
| 183 | + const batchStarted = Promise.withResolvers(); |
| 184 | + let finishInitial; |
| 185 | + let finishBatch; |
| 186 | + const writable = new Writable({ |
| 187 | + highWaterMark: 4, |
| 188 | + write: common.mustCall((chunk, encoding, callback) => { |
| 189 | + if (chunk[0] === 1) setImmediate(callback); |
| 190 | + else finishInitial = callback; |
| 191 | + }, 2), |
| 192 | + writev: common.mustCall((chunks, callback) => { |
| 193 | + assert.deepStrictEqual(chunks.map(({ chunk }) => Array.from(chunk)), [[3], [4]]); |
| 194 | + finishBatch = callback; |
| 195 | + batchStarted.resolve(); |
| 196 | + }), |
| 197 | + }); |
| 198 | + const writer = Writable.toWeb(writable).getWriter(); |
| 199 | + await writer.write(Buffer.from([1])); |
| 200 | + for (const value of [2, 3, 4]) await writer.write(Buffer.from([value])); |
| 201 | + |
| 202 | + const closed = assert.rejects(writer.closed, (actual) => actual === error); |
| 203 | + const close = assert.rejects(writer.close(), (actual) => actual === error); |
| 204 | + finishInitial(); |
| 205 | + await batchStarted.promise; |
| 206 | + setImmediate(finishBatch, error); |
| 207 | + await Promise.all([closed, close]); |
| 208 | +} |
| 209 | + |
| 210 | +async function checkSyncWriteErrorAfterAsyncWrite() { |
| 211 | + const error = new Error('synchronous native write failure'); |
| 212 | + const writable = new Writable({ |
| 213 | + write: common.mustCall((chunk, encoding, callback) => { |
| 214 | + if (chunk[0] === 1) setImmediate(callback); |
| 215 | + else callback(error); |
| 216 | + }, 2), |
| 217 | + }); |
| 218 | + const writer = Writable.toWeb(writable).getWriter(); |
| 219 | + await writer.write(Buffer.from([1])); |
| 220 | + await Promise.all([ |
| 221 | + assert.rejects(writer.write(Buffer.from([2])), (actual) => actual === error), |
| 222 | + assert.rejects(writer.closed, (actual) => actual === error), |
| 223 | + ]); |
| 224 | +} |
| 225 | + |
| 226 | +async function main() { |
| 227 | + await checkReusableBatches(); |
| 228 | + await checkLateWriteError(); |
| 229 | + for (const size of [4, 5]) await checkLargeWrite(size); |
| 230 | + await checkLargeWriteAfterCopiedBatch(); |
| 231 | + await checkQueuedWritevError(); |
| 232 | + await checkSyncWriteErrorAfterAsyncWrite(); |
| 233 | +} |
| 234 | + |
| 235 | +main().then(common.mustCall()); |
0 commit comments