1'use strict';
2
3const common = require('../common');
4const net = require('net');
5const assert = require('assert');
6
7const server = net.createServer();
8server.listen(0, common.mustCall(function() {
9  const port = server.address().port;
10  const conn = net.createConnection(port);
11
12  conn.on('connect', common.mustCall(function() {
13    // Test destroy returns this, even on multiple calls when it short-circuits.
14    assert.strictEqual(conn, conn.destroy().destroy());
15    conn.on('error', common.mustNotCall());
16
17    conn.write(Buffer.from('kaboom'), common.expectsError({
18      code: 'ERR_STREAM_DESTROYED',
19      message: 'Cannot call write after a stream was destroyed',
20      name: 'Error'
21    }));
22    server.close();
23  }));
24}));
25