1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const initHooks = require('./init-hooks'); 6const { checkInvocations } = require('./hook-checks'); 7 8const net = require('net'); 9 10const hooks = initHooks(); 11hooks.enable(); 12 13const server = net 14 .createServer(onconnection) 15 .on('listening', common.mustCall(onlistening)); 16server.listen(); 17function onlistening() { 18 net.connect(server.address().port, common.mustCall(onconnected)); 19} 20 21// It is non-deterministic in which order onconnection and onconnected fire. 22// Therefore we track here if we ended the connection already or not. 23let endedConnection = false; 24function onconnection(c) { 25 assert.strictEqual(hooks.activitiesOfTypes('SHUTDOWNWRAP').length, 0); 26 c.end(); 27 process.nextTick(() => { 28 endedConnection = true; 29 const as = hooks.activitiesOfTypes('SHUTDOWNWRAP'); 30 assert.strictEqual(as.length, 1); 31 checkInvocations(as[0], { init: 1 }, 'after ending client connection'); 32 this.close(onserverClosed); 33 }); 34} 35 36function onconnected() { 37 if (endedConnection) { 38 assert.strictEqual(hooks.activitiesOfTypes('SHUTDOWNWRAP').length, 1); 39 40 } else { 41 assert.strictEqual(hooks.activitiesOfTypes('SHUTDOWNWRAP').length, 0); 42 } 43} 44 45function onserverClosed() { 46 const as = hooks.activitiesOfTypes('SHUTDOWNWRAP'); 47 checkInvocations(as[0], { init: 1, before: 1, after: 1, destroy: 1 }, 48 'when server closed'); 49} 50 51process.on('exit', onexit); 52 53function onexit() { 54 hooks.disable(); 55 hooks.sanityCheck('SHUTDOWNWRAP'); 56 const as = hooks.activitiesOfTypes('SHUTDOWNWRAP'); 57 const a = as[0]; 58 assert.strictEqual(a.type, 'SHUTDOWNWRAP'); 59 assert.strictEqual(typeof a.uid, 'number'); 60 assert.strictEqual(typeof a.triggerAsyncId, 'number'); 61 checkInvocations(as[0], { init: 1, before: 1, after: 1, destroy: 1 }, 62 'when process exits'); 63} 64