11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst child_process = require('child_process');
51cb0ef41Sopenharmony_ciconst path = require('path');
61cb0ef41Sopenharmony_ciconst { Worker } = require('worker_threads');
71cb0ef41Sopenharmony_ciconst binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciswitch (process.argv[2]) {
101cb0ef41Sopenharmony_ci  case 'both':
111cb0ef41Sopenharmony_ci    require(binding);
121cb0ef41Sopenharmony_ci    // fallthrough
131cb0ef41Sopenharmony_ci  case 'worker-twice':
141cb0ef41Sopenharmony_ci  case 'worker': {
151cb0ef41Sopenharmony_ci    const worker = new Worker(`require(${JSON.stringify(binding)});`, {
161cb0ef41Sopenharmony_ci      eval: true,
171cb0ef41Sopenharmony_ci    });
181cb0ef41Sopenharmony_ci    if (process.argv[2] === 'worker-twice') {
191cb0ef41Sopenharmony_ci      worker.on('exit', common.mustCall(() => {
201cb0ef41Sopenharmony_ci        new Worker(`require(${JSON.stringify(binding)});`, {
211cb0ef41Sopenharmony_ci          eval: true,
221cb0ef41Sopenharmony_ci        });
231cb0ef41Sopenharmony_ci      }));
241cb0ef41Sopenharmony_ci    }
251cb0ef41Sopenharmony_ci    return;
261cb0ef41Sopenharmony_ci  }
271cb0ef41Sopenharmony_ci  case 'main-thread':
281cb0ef41Sopenharmony_ci    process.env.addExtraItemToEventLoop = 'yes';
291cb0ef41Sopenharmony_ci    require(binding);
301cb0ef41Sopenharmony_ci    return;
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci// Use process.report to figure out if we might be running under musl libc.
341cb0ef41Sopenharmony_ciconst glibc = process.report.getReport().header.glibcVersionRuntime;
351cb0ef41Sopenharmony_ciassert(typeof glibc === 'string' || glibc === undefined, glibc);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciconst libcMayBeMusl = common.isLinux && glibc === undefined;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cifor (const { test, expected } of [
401cb0ef41Sopenharmony_ci  { test: 'worker', expected: [ 'ctor cleanup dtor ' ] },
411cb0ef41Sopenharmony_ci  { test: 'main-thread', expected: [ 'ctor cleanup dtor ' ] },
421cb0ef41Sopenharmony_ci  // We always only have 1 instance of the shared object in memory, so
431cb0ef41Sopenharmony_ci  // 1 ctor and 1 dtor call. If we attach the module to 2 Environments,
441cb0ef41Sopenharmony_ci  // we expect 2 cleanup calls, otherwise one.
451cb0ef41Sopenharmony_ci  { test: 'both', expected: [ 'ctor cleanup cleanup dtor ' ] },
461cb0ef41Sopenharmony_ci  {
471cb0ef41Sopenharmony_ci    test: 'worker-twice',
481cb0ef41Sopenharmony_ci    // In this case, we load and unload an addon, then load and unload again.
491cb0ef41Sopenharmony_ci    // musl doesn't support unloading, so the output may be missing
501cb0ef41Sopenharmony_ci    // a dtor + ctor pair.
511cb0ef41Sopenharmony_ci    expected: [
521cb0ef41Sopenharmony_ci      'ctor cleanup dtor ctor cleanup dtor ',
531cb0ef41Sopenharmony_ci    ].concat(libcMayBeMusl ? [
541cb0ef41Sopenharmony_ci      'ctor cleanup cleanup dtor ',
551cb0ef41Sopenharmony_ci    ] : []),
561cb0ef41Sopenharmony_ci  },
571cb0ef41Sopenharmony_ci]) {
581cb0ef41Sopenharmony_ci  console.log('spawning test', test);
591cb0ef41Sopenharmony_ci  const proc = child_process.spawnSync(process.execPath, [
601cb0ef41Sopenharmony_ci    __filename,
611cb0ef41Sopenharmony_ci    test,
621cb0ef41Sopenharmony_ci  ]);
631cb0ef41Sopenharmony_ci  process.stderr.write(proc.stderr.toString());
641cb0ef41Sopenharmony_ci  assert.strictEqual(proc.stderr.toString(), '');
651cb0ef41Sopenharmony_ci  assert(expected.includes(proc.stdout.toString()),
661cb0ef41Sopenharmony_ci         `${proc.stdout.toString()} is not included in ${expected}`);
671cb0ef41Sopenharmony_ci  assert.strictEqual(proc.status, 0);
681cb0ef41Sopenharmony_ci}
69