Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test concurrency flag #43887

Merged
merged 15 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have mislead you - it is currently impossible to configure concurrency in --test mode
so this docs only refers to normal tests, meaning default is Infinity

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 @@ -47,8 +48,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 @@ -146,6 +146,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 (isUint32(timeout)) {
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 }, () => {
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
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);
});
}
);