summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorlegendecas <legendecas@gmail.com>2019-11-17 20:02:01 +0800
committerBeth Griggs <Bethany.Griggs@uk.ibm.com>2020-02-06 02:49:12 +0000
commit8dc4e4ecb7aeb64b3dae9ad02ac305929e00bdb9 (patch)
treeb6625691e43082ffeaaf106188c78fb6fdfd05aa /test
parent1bcbc70ea8267ecc9a21570c841c2b5bdcc3fde3 (diff)
downloadnode-new-8dc4e4ecb7aeb64b3dae9ad02ac305929e00bdb9.tar.gz
cli: add --trace-exit cli option
It could be convenient to trace abnormal exit of the Node.js processes that printing stacktrace on each `process.exit` call with a cli option. This also takes effects on worker threads. PR-URL: https://github.com/nodejs/node/pull/30516 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-trace-exit.js59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/parallel/test-trace-exit.js b/test/parallel/test-trace-exit.js
new file mode 100644
index 0000000000..237512ac27
--- /dev/null
+++ b/test/parallel/test-trace-exit.js
@@ -0,0 +1,59 @@
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+const { promisify } = require('util');
+const execFile = promisify(require('child_process').execFile);
+const { Worker, isMainThread, workerData } = require('worker_threads');
+
+const variant = process.argv[process.argv.length - 1];
+switch (true) {
+ case variant === 'main-thread': {
+ return;
+ }
+ case variant === 'main-thread-exit': {
+ return process.exit(0);
+ }
+ case variant.startsWith('worker-thread'): {
+ const worker = new Worker(__filename, { workerData: variant });
+ worker.on('error', common.mustNotCall());
+ worker.on('exit', common.mustCall((code) => {
+ assert.strictEqual(code, 0);
+ }));
+ return;
+ }
+ case !isMainThread: {
+ if (workerData === 'worker-thread-exit') {
+ process.exit(0);
+ }
+ return;
+ }
+}
+
+(async function() {
+ for (const { execArgv, variant, warnings } of [
+ { execArgv: ['--trace-exit'], variant: 'main-thread-exit', warnings: 1 },
+ { execArgv: [], variant: 'main-thread-exit', warnings: 0 },
+ { execArgv: ['--trace-exit'], variant: 'main-thread', warnings: 0 },
+ { execArgv: [], variant: 'main-thread', warnings: 0 },
+ { execArgv: ['--trace-exit'], variant: 'worker-thread-exit', warnings: 1 },
+ { execArgv: [], variant: 'worker-thread-exit', warnings: 0 },
+ { execArgv: ['--trace-exit'], variant: 'worker-thread', warnings: 0 },
+ { execArgv: [], variant: 'worker-thread', warnings: 0 },
+ ]) {
+ const { stdout, stderr } =
+ await execFile(process.execPath, [...execArgv, __filename, variant]);
+ assert.strictEqual(stdout, '');
+ const actualWarnings =
+ stderr.match(/WARNING: Exited the environment with code 0/g);
+ if (warnings === 0) {
+ assert.strictEqual(actualWarnings, null);
+ return;
+ }
+ assert.strictEqual(actualWarnings.length, warnings);
+
+ if (variant.startsWith('worker')) {
+ const workerIds = stderr.match(/\(node:\d+, thread:\d+)/g);
+ assert.strictEqual(workerIds.length, warnings);
+ }
+ }
+})().then(common.mustCall());