1'use strict'; 2const common = require('../../common'); 3const assert = require('assert'); 4const child_process = require('child_process'); 5const path = require('path'); 6const { Worker } = require('worker_threads'); 7const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`); 8 9switch (process.argv[2]) { 10 case 'both': 11 require(binding); 12 // fallthrough 13 case 'worker-twice': 14 case 'worker': { 15 const worker = new Worker(`require(${JSON.stringify(binding)});`, { 16 eval: true, 17 }); 18 if (process.argv[2] === 'worker-twice') { 19 worker.on('exit', common.mustCall(() => { 20 new Worker(`require(${JSON.stringify(binding)});`, { 21 eval: true, 22 }); 23 })); 24 } 25 return; 26 } 27 case 'main-thread': 28 process.env.addExtraItemToEventLoop = 'yes'; 29 require(binding); 30 return; 31} 32 33// Use process.report to figure out if we might be running under musl libc. 34const glibc = process.report.getReport().header.glibcVersionRuntime; 35assert(typeof glibc === 'string' || glibc === undefined, glibc); 36 37const libcMayBeMusl = common.isLinux && glibc === undefined; 38 39for (const { test, expected } of [ 40 { test: 'worker', expected: [ 'ctor cleanup dtor ' ] }, 41 { test: 'main-thread', expected: [ 'ctor cleanup dtor ' ] }, 42 // We always only have 1 instance of the shared object in memory, so 43 // 1 ctor and 1 dtor call. If we attach the module to 2 Environments, 44 // we expect 2 cleanup calls, otherwise one. 45 { test: 'both', expected: [ 'ctor cleanup cleanup dtor ' ] }, 46 { 47 test: 'worker-twice', 48 // In this case, we load and unload an addon, then load and unload again. 49 // musl doesn't support unloading, so the output may be missing 50 // a dtor + ctor pair. 51 expected: [ 52 'ctor cleanup dtor ctor cleanup dtor ', 53 ].concat(libcMayBeMusl ? [ 54 'ctor cleanup cleanup dtor ', 55 ] : []), 56 }, 57]) { 58 console.log('spawning test', test); 59 const proc = child_process.spawnSync(process.execPath, [ 60 __filename, 61 test, 62 ]); 63 process.stderr.write(proc.stderr.toString()); 64 assert.strictEqual(proc.stderr.toString(), ''); 65 assert(expected.includes(proc.stdout.toString()), 66 `${proc.stdout.toString()} is not included in ${expected}`); 67 assert.strictEqual(proc.status, 0); 68} 69