11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst net = require('net'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/19562: 71cb0ef41Sopenharmony_ci// Writing to a socket first tries to push through as much data as possible 81cb0ef41Sopenharmony_ci// without blocking synchronously, and, if that is not enough, queues more 91cb0ef41Sopenharmony_ci// data up for asynchronous writing. 101cb0ef41Sopenharmony_ci// Check that `bytesWritten` accounts for both parts of a write. 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst N = 10000000; 131cb0ef41Sopenharmony_ci{ 141cb0ef41Sopenharmony_ci // Variant 1: Write a Buffer. 151cb0ef41Sopenharmony_ci const server = net.createServer(common.mustCall((socket) => { 161cb0ef41Sopenharmony_ci socket.end(Buffer.alloc(N), common.mustCall(() => { 171cb0ef41Sopenharmony_ci assert.strictEqual(socket.bytesWritten, N); 181cb0ef41Sopenharmony_ci })); 191cb0ef41Sopenharmony_ci assert.strictEqual(socket.bytesWritten, N); 201cb0ef41Sopenharmony_ci })).listen(0, common.mustCall(() => { 211cb0ef41Sopenharmony_ci const client = net.connect(server.address().port); 221cb0ef41Sopenharmony_ci client.resume(); 231cb0ef41Sopenharmony_ci client.on('close', common.mustCall(() => { 241cb0ef41Sopenharmony_ci assert.strictEqual(client.bytesRead, N); 251cb0ef41Sopenharmony_ci server.close(); 261cb0ef41Sopenharmony_ci })); 271cb0ef41Sopenharmony_ci })); 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci{ 311cb0ef41Sopenharmony_ci // Variant 2: Write a string. 321cb0ef41Sopenharmony_ci const server = net.createServer(common.mustCall((socket) => { 331cb0ef41Sopenharmony_ci socket.end('a'.repeat(N), common.mustCall(() => { 341cb0ef41Sopenharmony_ci assert.strictEqual(socket.bytesWritten, N); 351cb0ef41Sopenharmony_ci })); 361cb0ef41Sopenharmony_ci assert.strictEqual(socket.bytesWritten, N); 371cb0ef41Sopenharmony_ci })).listen(0, common.mustCall(() => { 381cb0ef41Sopenharmony_ci const client = net.connect(server.address().port); 391cb0ef41Sopenharmony_ci client.resume(); 401cb0ef41Sopenharmony_ci client.on('close', common.mustCall(() => { 411cb0ef41Sopenharmony_ci assert.strictEqual(client.bytesRead, N); 421cb0ef41Sopenharmony_ci server.close(); 431cb0ef41Sopenharmony_ci })); 441cb0ef41Sopenharmony_ci })); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci{ 481cb0ef41Sopenharmony_ci // Variant 2: writev() with mixed data. 491cb0ef41Sopenharmony_ci const server = net.createServer(common.mustCall((socket) => { 501cb0ef41Sopenharmony_ci socket.cork(); 511cb0ef41Sopenharmony_ci socket.write('a'.repeat(N)); 521cb0ef41Sopenharmony_ci assert.strictEqual(socket.bytesWritten, N); 531cb0ef41Sopenharmony_ci socket.write(Buffer.alloc(N)); 541cb0ef41Sopenharmony_ci assert.strictEqual(socket.bytesWritten, 2 * N); 551cb0ef41Sopenharmony_ci socket.end('', common.mustCall(() => { 561cb0ef41Sopenharmony_ci assert.strictEqual(socket.bytesWritten, 2 * N); 571cb0ef41Sopenharmony_ci })); 581cb0ef41Sopenharmony_ci socket.uncork(); 591cb0ef41Sopenharmony_ci })).listen(0, common.mustCall(() => { 601cb0ef41Sopenharmony_ci const client = net.connect(server.address().port); 611cb0ef41Sopenharmony_ci client.resume(); 621cb0ef41Sopenharmony_ci client.on('close', common.mustCall(() => { 631cb0ef41Sopenharmony_ci assert.strictEqual(client.bytesRead, 2 * N); 641cb0ef41Sopenharmony_ci server.close(); 651cb0ef41Sopenharmony_ci })); 661cb0ef41Sopenharmony_ci })); 671cb0ef41Sopenharmony_ci} 68