summaryrefslogtreecommitdiff
path: root/test/parallel/test-net-connect-reset-until-connected.js
blob: 9c2493eaaf0598378f285a19edfd4ad60af3e35e (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
'use strict';

const common = require('../common');
const net = require('net');

function barrier(count, cb) {
  return function() {
    if (--count === 0)
      cb();
  };
}

const server = net.createServer();
server.listen(0, common.mustCall(function() {
  const port = server.address().port;
  const conn = net.createConnection(port);
  const connok = barrier(2, () => conn.resetAndDestroy());
  conn.on('close', common.mustCall());
  server.on('connection', (socket) => {
    connok();
    socket.on('error', common.expectsError({
      code: 'ECONNRESET',
      message: 'read ECONNRESET',
      name: 'Error'
    }));
    server.close();
  });
  conn.on('connect', connok);
}));