11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node-v0.x-archive/issues/8897.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst net = require('net');
71cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst clients = [];
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst server = net.createServer(function onClient(client) {
121cb0ef41Sopenharmony_ci  clients.push(client);
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  if (clients.length === 2) {
151cb0ef41Sopenharmony_ci    // Enroll two timers, and make the one supposed to fire first
161cb0ef41Sopenharmony_ci    // unenroll the other one supposed to fire later. This mutates
171cb0ef41Sopenharmony_ci    // the list of unref timers when traversing it, and exposes the
181cb0ef41Sopenharmony_ci    // original issue in joyent/node#8897.
191cb0ef41Sopenharmony_ci    clients[0].setTimeout(1, () => {
201cb0ef41Sopenharmony_ci      clients[1].setTimeout(0);
211cb0ef41Sopenharmony_ci      clients[0].end();
221cb0ef41Sopenharmony_ci      clients[1].end();
231cb0ef41Sopenharmony_ci    });
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    // Use a delay that is higher than the lowest timer resolution across all
261cb0ef41Sopenharmony_ci    // supported platforms, so that the two timers don't fire at the same time.
271cb0ef41Sopenharmony_ci    clients[1].setTimeout(50);
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci});
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
321cb0ef41Sopenharmony_ci  const countdown = new Countdown(2, () => server.close());
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  {
351cb0ef41Sopenharmony_ci    const client = net.connect({ port: server.address().port });
361cb0ef41Sopenharmony_ci    client.on('end', () => countdown.dec());
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  {
401cb0ef41Sopenharmony_ci    const client = net.connect({ port: server.address().port });
411cb0ef41Sopenharmony_ci    client.on('end', () => countdown.dec());
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci}));
44