|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const net = require('net'); |
| 5 | + |
| 6 | +// Regression test for https://github.com/nodejs/node/issues/19562: |
| 7 | +// Writing to a socket first tries to push through as much data as possible |
| 8 | +// without blocking synchronously, and, if that is not enough, queues more |
| 9 | +// data up for asynchronous writing. |
| 10 | +// Check that `bytesWritten` accounts for both parts of a write. |
| 11 | + |
| 12 | +const N = 10000000; |
| 13 | +{ |
| 14 | + // Variant 1: Write a Buffer. |
| 15 | + const server = net.createServer(common.mustCall((socket) => { |
| 16 | + socket.end(Buffer.alloc(N), common.mustCall(() => { |
| 17 | + assert.strictEqual(socket.bytesWritten, N); |
| 18 | + })); |
| 19 | + assert.strictEqual(socket.bytesWritten, N); |
| 20 | + })).listen(0, common.mustCall(() => { |
| 21 | + const client = net.connect(server.address().port); |
| 22 | + client.resume(); |
| 23 | + client.on('close', common.mustCall(() => { |
| 24 | + assert.strictEqual(client.bytesRead, N); |
| 25 | + server.close(); |
| 26 | + })); |
| 27 | + })); |
| 28 | +} |
| 29 | + |
| 30 | +{ |
| 31 | + // Variant 2: Write a string. |
| 32 | + const server = net.createServer(common.mustCall((socket) => { |
| 33 | + socket.end('a'.repeat(N), common.mustCall(() => { |
| 34 | + assert.strictEqual(socket.bytesWritten, N); |
| 35 | + })); |
| 36 | + assert.strictEqual(socket.bytesWritten, N); |
| 37 | + })).listen(0, common.mustCall(() => { |
| 38 | + const client = net.connect(server.address().port); |
| 39 | + client.resume(); |
| 40 | + client.on('close', common.mustCall(() => { |
| 41 | + assert.strictEqual(client.bytesRead, N); |
| 42 | + server.close(); |
| 43 | + })); |
| 44 | + })); |
| 45 | +} |
| 46 | + |
| 47 | +{ |
| 48 | + // Variant 2: writev() with mixed data. |
| 49 | + const server = net.createServer(common.mustCall((socket) => { |
| 50 | + socket.cork(); |
| 51 | + socket.write('a'.repeat(N)); |
| 52 | + assert.strictEqual(socket.bytesWritten, N); |
| 53 | + socket.write(Buffer.alloc(N)); |
| 54 | + assert.strictEqual(socket.bytesWritten, 2 * N); |
| 55 | + socket.end('', common.mustCall(() => { |
| 56 | + assert.strictEqual(socket.bytesWritten, 2 * N); |
| 57 | + })); |
| 58 | + socket.uncork(); |
| 59 | + })).listen(0, common.mustCall(() => { |
| 60 | + const client = net.connect(server.address().port); |
| 61 | + client.resume(); |
| 62 | + client.on('close', common.mustCall(() => { |
| 63 | + assert.strictEqual(client.bytesRead, 2 * N); |
| 64 | + server.close(); |
| 65 | + })); |
| 66 | + })); |
| 67 | +} |
0 commit comments