11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst initHooks = require('./init-hooks');
61cb0ef41Sopenharmony_ciconst { checkInvocations } = require('./hook-checks');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst net = require('net');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst hooks = initHooks();
111cb0ef41Sopenharmony_cihooks.enable();
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst server = net
141cb0ef41Sopenharmony_ci  .createServer(onconnection)
151cb0ef41Sopenharmony_ci  .on('listening', common.mustCall(onlistening));
161cb0ef41Sopenharmony_ciserver.listen();
171cb0ef41Sopenharmony_cifunction onlistening() {
181cb0ef41Sopenharmony_ci  net.connect(server.address().port, common.mustCall(onconnected));
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci// It is non-deterministic in which order onconnection and onconnected fire.
221cb0ef41Sopenharmony_ci// Therefore we track here if we ended the connection already or not.
231cb0ef41Sopenharmony_cilet endedConnection = false;
241cb0ef41Sopenharmony_cifunction onconnection(c) {
251cb0ef41Sopenharmony_ci  assert.strictEqual(hooks.activitiesOfTypes('SHUTDOWNWRAP').length, 0);
261cb0ef41Sopenharmony_ci  c.end();
271cb0ef41Sopenharmony_ci  process.nextTick(() => {
281cb0ef41Sopenharmony_ci    endedConnection = true;
291cb0ef41Sopenharmony_ci    const as = hooks.activitiesOfTypes('SHUTDOWNWRAP');
301cb0ef41Sopenharmony_ci    assert.strictEqual(as.length, 1);
311cb0ef41Sopenharmony_ci    checkInvocations(as[0], { init: 1 }, 'after ending client connection');
321cb0ef41Sopenharmony_ci    this.close(onserverClosed);
331cb0ef41Sopenharmony_ci  });
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cifunction onconnected() {
371cb0ef41Sopenharmony_ci  if (endedConnection) {
381cb0ef41Sopenharmony_ci    assert.strictEqual(hooks.activitiesOfTypes('SHUTDOWNWRAP').length, 1);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  } else {
411cb0ef41Sopenharmony_ci    assert.strictEqual(hooks.activitiesOfTypes('SHUTDOWNWRAP').length, 0);
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cifunction onserverClosed() {
461cb0ef41Sopenharmony_ci  const as = hooks.activitiesOfTypes('SHUTDOWNWRAP');
471cb0ef41Sopenharmony_ci  checkInvocations(as[0], { init: 1, before: 1, after: 1, destroy: 1 },
481cb0ef41Sopenharmony_ci                   'when server closed');
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciprocess.on('exit', onexit);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cifunction onexit() {
541cb0ef41Sopenharmony_ci  hooks.disable();
551cb0ef41Sopenharmony_ci  hooks.sanityCheck('SHUTDOWNWRAP');
561cb0ef41Sopenharmony_ci  const as = hooks.activitiesOfTypes('SHUTDOWNWRAP');
571cb0ef41Sopenharmony_ci  const a = as[0];
581cb0ef41Sopenharmony_ci  assert.strictEqual(a.type, 'SHUTDOWNWRAP');
591cb0ef41Sopenharmony_ci  assert.strictEqual(typeof a.uid, 'number');
601cb0ef41Sopenharmony_ci  assert.strictEqual(typeof a.triggerAsyncId, 'number');
611cb0ef41Sopenharmony_ci  checkInvocations(as[0], { init: 1, before: 1, after: 1, destroy: 1 },
621cb0ef41Sopenharmony_ci                   'when process exits');
631cb0ef41Sopenharmony_ci}
64