From 6a18c1d8dd034767f86bc496e292ecb0dddf944f Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 23 May 2017 11:51:41 -0400 Subject: [PATCH] test: add coverage for socket write after close This commit adds test coverage for the scenario where a socket's handle has been closed prior to writing. PR-URL: https://github.com/nodejs/node/pull/13171 Reviewed-By: Luigi Pinca Reviewed-By: Michael Dawson Reviewed-By: James M Snell Reviewed-By: Refael Ackermann --- .../test-net-socket-write-after-close.js | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/parallel/test-net-socket-write-after-close.js diff --git a/test/parallel/test-net-socket-write-after-close.js b/test/parallel/test-net-socket-write-after-close.js new file mode 100644 index 00000000000000..6d2d2fdd026333 --- /dev/null +++ b/test/parallel/test-net-socket-write-after-close.js @@ -0,0 +1,39 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +{ + const server = net.createServer(); + + server.listen(common.mustCall(() => { + const port = server.address().port; + const client = net.connect({port}, common.mustCall(() => { + client.on('error', common.mustCall((err) => { + server.close(); + assert.strictEqual(err.constructor, Error); + assert.strictEqual(err.message, 'write EBADF'); + })); + client._handle.close(); + client.write('foo'); + })); + })); +} + +{ + const server = net.createServer(); + + server.listen(common.mustCall(() => { + const port = server.address().port; + const client = net.connect({port}, common.mustCall(() => { + client.on('error', common.mustCall((err) => { + server.close(); + assert.strictEqual(err.constructor, Error); + assert.strictEqual(err.message, 'This socket is closed'); + })); + client._handle.close(); + client._handle = null; + client.write('foo'); + })); + })); +}