summaryrefslogtreecommitdiff
path: root/test/parallel/test-net-socket-write-after-close.js
blob: e3593a3a09a146d4a4b298c42dbba45564b601db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'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.expectsError({
        code: 'ERR_SOCKET_CLOSED',
        message: 'Socket is closed',
        type: Error
      }));

      server.close();

      client._handle.close();
      client._handle = null;
      client.write('foo');
    }));
  }));
}