summaryrefslogtreecommitdiff
path: root/test/parallel/test-net-bytes-written-large.js
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2018-03-24 17:02:34 +0100
committerAnna Henningsen <anna@addaleax.net>2018-03-30 14:20:45 +0200
commit1dc8eb4bd34383830d48a704d79a2bc9ec55152f (patch)
treeb30de4ca5080225428bfac11493a634201ee0609 /test/parallel/test-net-bytes-written-large.js
parent923fb5cc1861422291d135177770f94f473f4d6f (diff)
downloadandroid-node-v8-1dc8eb4bd34383830d48a704d79a2bc9ec55152f.tar.gz
android-node-v8-1dc8eb4bd34383830d48a704d79a2bc9ec55152f.tar.bz2
android-node-v8-1dc8eb4bd34383830d48a704d79a2bc9ec55152f.zip
test: add regression test for large write
Fixes: https://github.com/nodejs/node/issues/19562 PR-URL: https://github.com/nodejs/node/pull/19551 Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-net-bytes-written-large.js')
-rw-r--r--test/parallel/test-net-bytes-written-large.js67
1 files changed, 67 insertions, 0 deletions
diff --git a/test/parallel/test-net-bytes-written-large.js b/test/parallel/test-net-bytes-written-large.js
new file mode 100644
index 0000000000..79a997ec5a
--- /dev/null
+++ b/test/parallel/test-net-bytes-written-large.js
@@ -0,0 +1,67 @@
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+const net = require('net');
+
+// Regression test for https://github.com/nodejs/node/issues/19562:
+// Writing to a socket first tries to push through as much data as possible
+// without blocking synchronously, and, if that is not enough, queues more
+// data up for asynchronous writing.
+// Check that `bytesWritten` accounts for both parts of a write.
+
+const N = 10000000;
+{
+ // Variant 1: Write a Buffer.
+ const server = net.createServer(common.mustCall((socket) => {
+ socket.end(Buffer.alloc(N), common.mustCall(() => {
+ assert.strictEqual(socket.bytesWritten, N);
+ }));
+ assert.strictEqual(socket.bytesWritten, N);
+ })).listen(0, common.mustCall(() => {
+ const client = net.connect(server.address().port);
+ client.resume();
+ client.on('close', common.mustCall(() => {
+ assert.strictEqual(client.bytesRead, N);
+ server.close();
+ }));
+ }));
+}
+
+{
+ // Variant 2: Write a string.
+ const server = net.createServer(common.mustCall((socket) => {
+ socket.end('a'.repeat(N), common.mustCall(() => {
+ assert.strictEqual(socket.bytesWritten, N);
+ }));
+ assert.strictEqual(socket.bytesWritten, N);
+ })).listen(0, common.mustCall(() => {
+ const client = net.connect(server.address().port);
+ client.resume();
+ client.on('close', common.mustCall(() => {
+ assert.strictEqual(client.bytesRead, N);
+ server.close();
+ }));
+ }));
+}
+
+{
+ // Variant 2: writev() with mixed data.
+ const server = net.createServer(common.mustCall((socket) => {
+ socket.cork();
+ socket.write('a'.repeat(N));
+ assert.strictEqual(socket.bytesWritten, N);
+ socket.write(Buffer.alloc(N));
+ assert.strictEqual(socket.bytesWritten, 2 * N);
+ socket.end('', common.mustCall(() => {
+ assert.strictEqual(socket.bytesWritten, 2 * N);
+ }));
+ socket.uncork();
+ })).listen(0, common.mustCall(() => {
+ const client = net.connect(server.address().port);
+ client.resume();
+ client.on('close', common.mustCall(() => {
+ assert.strictEqual(client.bytesRead, 2 * N);
+ server.close();
+ }));
+ }));
+}