Skip to content

Commit

Permalink
test_runner: add support for boolean values for concurrency option
Browse files Browse the repository at this point in the history
PR-URL: #43887
Fixes: #43837
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
  • Loading branch information
98lenvi authored and danielleadams committed Jul 26, 2022
1 parent f980201 commit cd0d9dd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
9 changes: 7 additions & 2 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,14 @@ changes:
does not have a name.
* `options` {Object} Configuration options for the test. The following
properties are supported:
* `concurrency` {number} The number of tests that can be run at the same time.
* `concurrency` {number|boolean} If a number is provided,
then that many tests would run in parallel.
If truthy, it would run (number of cpu cores - 1)
tests in parallel.
For subtests, it will be `Infinity` tests in parallel.
If falsy, it would only run one test at a time.
If unspecified, subtests inherit this value from their parent.
**Default:** `1`.
**Default:** `false`.
* `only` {boolean} If truthy, and the test context is configured to run
`only` tests, then this test will be run. Otherwise, the test is skipped.
**Default:** `false`.
Expand Down
10 changes: 8 additions & 2 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
ArrayPrototypeShift,
ArrayPrototypeUnshift,
FunctionPrototype,
MathMax,
Number,
PromisePrototypeThen,
PromiseResolve,
Expand Down Expand Up @@ -52,8 +53,7 @@ const noop = FunctionPrototype;
const isTestRunner = getOptionValue('--test');
const testOnlyFlag = !isTestRunner && getOptionValue('--test-only');
// TODO(cjihrig): Use uv_available_parallelism() once it lands.
const rootConcurrency = isTestRunner ? cpus().length : 1;

const rootConcurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : 1;
const kShouldAbort = Symbol('kShouldAbort');


Expand Down Expand Up @@ -151,6 +151,12 @@ class Test extends AsyncResource {

if (isUint32(concurrency) && concurrency !== 0) {
this.concurrency = concurrency;
} else if (typeof concurrency === 'boolean') {
if (concurrency) {
this.concurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : Infinity;
} else {
this.concurrency = 1;
}
}

if (timeout != null && timeout !== Infinity) {
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-runner-concurrency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
require('../common');
const { describe, it } = require('node:test');
const assert = require('assert');

describe('Concurrency option (boolean) = true ', { concurrency: true }, () => {
let isFirstTestOver = false;
it('should start the first test', () => new Promise((resolve) => {
setImmediate(() => { isFirstTestOver = true; resolve(); });
}));
it('should start before the previous test ends', () => {
// Should work even on single core CPUs
assert.strictEqual(isFirstTestOver, false);
});
});

describe(
'Concurrency option (boolean) = false ',
{ concurrency: false },
() => {
let isFirstTestOver = false;
it('should start the first test', () => new Promise((resolve) => {
setImmediate(() => { isFirstTestOver = true; resolve(); });
}));
it('should start after the previous test ends', () => {
assert.strictEqual(isFirstTestOver, true);
});
}
);

0 comments on commit cd0d9dd

Please sign in to comment.