• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// NOTE: this also covers process wrap as one is created along with the pipes
2// when we launch the sleep process
3'use strict';
4// Flags: --expose-gc
5
6const common = require('../common');
7const assert = require('assert');
8const tick = require('../common/tick');
9const initHooks = require('./init-hooks');
10const { checkInvocations } = require('./hook-checks');
11const { spawn } = require('child_process');
12
13if (!common.isMainThread)
14  common.skip('Worker bootstrapping works differently -> different async IDs');
15
16const hooks = initHooks();
17
18hooks.enable();
19const nodeVersionSpawn = spawn(process.execPath, [ '--version' ]);
20
21nodeVersionSpawn
22  .on('exit', common.mustCall(onsleepExit))
23  .on('close', common.mustCall(onsleepClose));
24
25// A process wrap and 3 pipe wraps for std{in,out,err} are initialized
26// synchronously
27const processes = hooks.activitiesOfTypes('PROCESSWRAP');
28const pipes = hooks.activitiesOfTypes('PIPEWRAP');
29assert.strictEqual(processes.length, 1);
30assert.strictEqual(pipes.length, 3);
31
32const processwrap = processes[0];
33const pipe1 = pipes[0];
34const pipe2 = pipes[1];
35const pipe3 = pipes[2];
36
37assert.strictEqual(processwrap.type, 'PROCESSWRAP');
38assert.strictEqual(processwrap.triggerAsyncId, 1);
39checkInvocations(processwrap, { init: 1 },
40                 'processwrap when sleep.spawn was called');
41
42[ pipe1, pipe2, pipe3 ].forEach((x) => {
43  assert.strictEqual(x.type, 'PIPEWRAP');
44  assert.strictEqual(x.triggerAsyncId, 1);
45  checkInvocations(x, { init: 1 }, 'pipe wrap when sleep.spawn was called');
46});
47
48function onsleepExit() {
49  checkInvocations(processwrap, { init: 1, before: 1 },
50                   'processwrap while in onsleepExit callback');
51}
52
53function onsleepClose() {
54  tick(1, () =>
55    checkInvocations(
56      processwrap,
57      { init: 1, before: 1, after: 1 },
58      'processwrap while in onsleepClose callback'),
59  );
60}
61
62process.on('exit', onexit);
63
64function onexit() {
65  hooks.disable();
66  hooks.sanityCheck('PROCESSWRAP');
67  hooks.sanityCheck('PIPEWRAP');
68
69  checkInvocations(
70    processwrap,
71    { init: 1, before: 1, after: 1 },
72    'processwrap while in onsleepClose callback');
73
74  [ pipe1, pipe2, pipe3 ].forEach((x) => {
75    assert.strictEqual(x.type, 'PIPEWRAP');
76    assert.strictEqual(x.triggerAsyncId, 1);
77  });
78
79  const ioEvents = Math.min(pipe2.before.length, pipe2.after.length);
80  // 2 events without any IO and at least one more for the node version data.
81  // Usually it is just one event, but it can be more.
82  assert.ok(ioEvents >= 3, `at least 3 stdout io events, got ${ioEvents}`);
83
84  checkInvocations(pipe1, { init: 1, before: 1, after: 1 },
85                   'pipe wrap when sleep.spawn was called');
86  checkInvocations(pipe2, { init: 1, before: ioEvents, after: ioEvents },
87                   'pipe wrap when sleep.spawn was called');
88  checkInvocations(pipe3, { init: 1, before: 2, after: 2 },
89                   'pipe wrap when sleep.spawn was called');
90}
91