1'use strict';
2
3// Regression test for https://github.com/nodejs/node-v0.x-archive/issues/8897.
4
5const common = require('../common');
6const net = require('net');
7const Countdown = require('../common/countdown');
8
9const clients = [];
10
11const server = net.createServer(function onClient(client) {
12  clients.push(client);
13
14  if (clients.length === 2) {
15    // Enroll two timers, and make the one supposed to fire first
16    // unenroll the other one supposed to fire later. This mutates
17    // the list of unref timers when traversing it, and exposes the
18    // original issue in joyent/node#8897.
19    clients[0].setTimeout(1, () => {
20      clients[1].setTimeout(0);
21      clients[0].end();
22      clients[1].end();
23    });
24
25    // Use a delay that is higher than the lowest timer resolution across all
26    // supported platforms, so that the two timers don't fire at the same time.
27    clients[1].setTimeout(50);
28  }
29});
30
31server.listen(0, common.mustCall(() => {
32  const countdown = new Countdown(2, () => server.close());
33
34  {
35    const client = net.connect({ port: server.address().port });
36    client.on('end', () => countdown.dec());
37  }
38
39  {
40    const client = net.connect({ port: server.address().port });
41    client.on('end', () => countdown.dec());
42  }
43}));
44