11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ciconst { describe, it, test } = require('node:test');
61cb0ef41Sopenharmony_ciconst assert = require('node:assert');
71cb0ef41Sopenharmony_ciconst path = require('node:path');
81cb0ef41Sopenharmony_ciconst fs = require('node:fs/promises');
91cb0ef41Sopenharmony_ciconst os = require('node:os');
101cb0ef41Sopenharmony_ciconst timers = require('node:timers/promises');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_citmpdir.refresh();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cidescribe('Concurrency option (boolean) = true', { concurrency: true }, () => {
151cb0ef41Sopenharmony_ci  let isFirstTestOver = false;
161cb0ef41Sopenharmony_ci  it('should start the first test', () => new Promise((resolve) => {
171cb0ef41Sopenharmony_ci    setImmediate(() => { isFirstTestOver = true; resolve(); });
181cb0ef41Sopenharmony_ci  }));
191cb0ef41Sopenharmony_ci  it('should start before the previous test ends', () => {
201cb0ef41Sopenharmony_ci    // Should work even on single core CPUs
211cb0ef41Sopenharmony_ci    assert.strictEqual(isFirstTestOver, false);
221cb0ef41Sopenharmony_ci  });
231cb0ef41Sopenharmony_ci});
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cidescribe(
261cb0ef41Sopenharmony_ci  'Concurrency option (boolean) = false',
271cb0ef41Sopenharmony_ci  { concurrency: false },
281cb0ef41Sopenharmony_ci  () => {
291cb0ef41Sopenharmony_ci    let isFirstTestOver = false;
301cb0ef41Sopenharmony_ci    it('should start the first test', () => new Promise((resolve) => {
311cb0ef41Sopenharmony_ci      setImmediate(() => { isFirstTestOver = true; resolve(); });
321cb0ef41Sopenharmony_ci    }));
331cb0ef41Sopenharmony_ci    it('should start after the previous test ends', () => {
341cb0ef41Sopenharmony_ci      assert.strictEqual(isFirstTestOver, true);
351cb0ef41Sopenharmony_ci    });
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci// Despite the docs saying so at some point, setting concurrency to true should
401cb0ef41Sopenharmony_ci// not limit concurrency to the number of available CPU cores.
411cb0ef41Sopenharmony_cidescribe('concurrency: true implies Infinity', { concurrency: true }, () => {
421cb0ef41Sopenharmony_ci  // The factor 5 is intentionally chosen to be higher than the default libuv
431cb0ef41Sopenharmony_ci  // thread pool size.
441cb0ef41Sopenharmony_ci  const nTests = 5 * os.availableParallelism();
451cb0ef41Sopenharmony_ci  let nStarted = 0;
461cb0ef41Sopenharmony_ci  for (let i = 0; i < nTests; i++) {
471cb0ef41Sopenharmony_ci    it(`should run test ${i} concurrently`, async () => {
481cb0ef41Sopenharmony_ci      assert.strictEqual(nStarted++, i);
491cb0ef41Sopenharmony_ci      await timers.setImmediate();
501cb0ef41Sopenharmony_ci      assert.strictEqual(nStarted, nTests);
511cb0ef41Sopenharmony_ci    });
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci});
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci{
561cb0ef41Sopenharmony_ci  // Make sure tests run in order when root concurrency is 1 (default)
571cb0ef41Sopenharmony_ci  const tree = [];
581cb0ef41Sopenharmony_ci  const expectedTestTree = common.mustCall(() => {
591cb0ef41Sopenharmony_ci    assert.deepStrictEqual(tree, [
601cb0ef41Sopenharmony_ci      'suite 1', 'nested', 'suite 2',
611cb0ef41Sopenharmony_ci      '1', '2', 'nested 1', 'nested 2',
621cb0ef41Sopenharmony_ci      'test', 'test 1', 'test 2',
631cb0ef41Sopenharmony_ci    ]);
641cb0ef41Sopenharmony_ci  });
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  describe('suite 1', () => {
671cb0ef41Sopenharmony_ci    tree.push('suite 1');
681cb0ef41Sopenharmony_ci    it('1', () => tree.push('1'));
691cb0ef41Sopenharmony_ci    it('2', () => tree.push('2'));
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci    describe('nested', () => {
721cb0ef41Sopenharmony_ci      tree.push('nested');
731cb0ef41Sopenharmony_ci      it('nested 1', () => tree.push('nested 1'));
741cb0ef41Sopenharmony_ci      it('nested 2', () => tree.push('nested 2'));
751cb0ef41Sopenharmony_ci    });
761cb0ef41Sopenharmony_ci  });
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  test('test', async (t) => {
791cb0ef41Sopenharmony_ci    tree.push('test');
801cb0ef41Sopenharmony_ci    await t.test('test1', () => tree.push('test 1'));
811cb0ef41Sopenharmony_ci    await t.test('test 2', () => tree.push('test 2'));
821cb0ef41Sopenharmony_ci  });
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  describe('suite 2', () => {
851cb0ef41Sopenharmony_ci    tree.push('suite 2');
861cb0ef41Sopenharmony_ci    it('should run after other suites', expectedTestTree);
871cb0ef41Sopenharmony_ci  });
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_citest('--test multiple files', { skip: os.availableParallelism() < 3 }, async () => {
911cb0ef41Sopenharmony_ci  await fs.writeFile(path.resolve(tmpdir.path, 'test-runner-concurrency'), '');
921cb0ef41Sopenharmony_ci  const { code, stderr } = await common.spawnPromisified(process.execPath, [
931cb0ef41Sopenharmony_ci    '--test',
941cb0ef41Sopenharmony_ci    fixtures.path('test-runner', 'concurrency', 'a.mjs'),
951cb0ef41Sopenharmony_ci    fixtures.path('test-runner', 'concurrency', 'b.mjs'),
961cb0ef41Sopenharmony_ci  ]);
971cb0ef41Sopenharmony_ci  assert.strictEqual(stderr, '');
981cb0ef41Sopenharmony_ci  assert.strictEqual(code, 0);
991cb0ef41Sopenharmony_ci});
100