|
| 1 | +'use strict'; |
| 2 | +constcommon=require('../common'); |
| 3 | +constassert=require('assert'); |
| 4 | +const{ promisify }=require('util'); |
| 5 | +constexecFile=promisify(require('child_process').execFile); |
| 6 | +const{ Worker, isMainThread, workerData }=require('worker_threads'); |
| 7 | + |
| 8 | +constvariant=process.argv[process.argv.length-1]; |
| 9 | +switch(true){ |
| 10 | +casevariant==='main-thread': { |
| 11 | +return; |
| 12 | +} |
| 13 | +casevariant==='main-thread-exit': { |
| 14 | +returnprocess.exit(0); |
| 15 | +} |
| 16 | +casevariant.startsWith('worker-thread'): { |
| 17 | +constworker=newWorker(__filename,{workerData: variant}); |
| 18 | +worker.on('error',common.mustNotCall()); |
| 19 | +worker.on('exit',common.mustCall((code)=>{ |
| 20 | +assert.strictEqual(code,0); |
| 21 | +})); |
| 22 | +return; |
| 23 | +} |
| 24 | +case!isMainThread: { |
| 25 | +if(workerData==='worker-thread-exit'){ |
| 26 | +process.exit(0); |
| 27 | +} |
| 28 | +return; |
| 29 | +} |
| 30 | +} |
| 31 | + |
| 32 | +(asyncfunction(){ |
| 33 | +for(const{ execArgv, variant, warnings }of[ |
| 34 | +{execArgv: ['--trace-exit'],variant: 'main-thread-exit',warnings: 1}, |
| 35 | +{execArgv: [],variant: 'main-thread-exit',warnings: 0}, |
| 36 | +{execArgv: ['--trace-exit'],variant: 'main-thread',warnings: 0}, |
| 37 | +{execArgv: [],variant: 'main-thread',warnings: 0}, |
| 38 | +{execArgv: ['--trace-exit'],variant: 'worker-thread-exit',warnings: 1}, |
| 39 | +{execArgv: [],variant: 'worker-thread-exit',warnings: 0}, |
| 40 | +{execArgv: ['--trace-exit'],variant: 'worker-thread',warnings: 0}, |
| 41 | +{execArgv: [],variant: 'worker-thread',warnings: 0}, |
| 42 | +]){ |
| 43 | +const{ stdout, stderr }= |
| 44 | +awaitexecFile(process.execPath,[...execArgv,__filename,variant]); |
| 45 | +assert.strictEqual(stdout,''); |
| 46 | +constactualWarnings= |
| 47 | +stderr.match(/WARNING:Exitedtheenvironmentwithcode0/g); |
| 48 | +if(warnings===0){ |
| 49 | +assert.strictEqual(actualWarnings,null); |
| 50 | +return; |
| 51 | +} |
| 52 | +assert.strictEqual(actualWarnings.length,warnings); |
| 53 | + |
| 54 | +if(variant.startsWith('worker')){ |
| 55 | +constworkerIds=stderr.match(/\(node:\d+,thread:\d+)/g); |
| 56 | +assert.strictEqual(workerIds.length,warnings); |
| 57 | +} |
| 58 | +} |
| 59 | +})().then(common.mustCall()); |
0 commit comments