1'use strict';
2const common = require('../common');
3const net = require('net');
4const assert = require('assert');
5
6const server = net.createServer();
7server.listen(0, common.mustCall(function() {
8  const port = server.address().port;
9  const conn = net.createConnection(port);
10  server.on('connection', (socket) => {
11    socket.on('error', common.expectsError({
12      code: 'ECONNRESET',
13      message: 'read ECONNRESET',
14      name: 'Error'
15    }));
16  });
17
18  conn.on('connect', common.mustCall(function() {
19    assert.strictEqual(conn, conn.resetAndDestroy().destroy());
20    conn.on('error', common.mustNotCall());
21
22    conn.write(Buffer.from('fzfzfzfzfz'), common.expectsError({
23      code: 'ERR_STREAM_DESTROYED',
24      message: 'Cannot call write after a stream was destroyed',
25      name: 'Error'
26    }));
27    server.close();
28  }));
29}));
30