Skip to content

Commit ba8fd71

Browse files
98lenviaduh95
authored andcommitted
feat: add support for boolean values for concurrency option
PR-URL: nodejs/node#43887 Fixes: nodejs/node#43837 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Jacob Smith <[email protected]> (cherry picked from commit dab492f0444b0a6ae8a41dd1d9605e036c363655)
1 parent cf78656 commit ba8fd71

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,14 @@ internally.
328328
does not have a name.
329329
- `options` {Object} Configuration options for the test. The following
330330
properties are supported:
331-
- `concurrency` {number} The number of tests that can be run at the same time.
331+
- `concurrency` {number|boolean} If a number is provided,
332+
then that many tests would run in parallel.
333+
If truthy, it would run (number of cpu cores - 1)
334+
tests in parallel.
335+
For subtests, it will be `Infinity` tests in parallel.
336+
If falsy, it would only run one test at a time.
332337
If unspecified, subtests inherit this value from their parent.
333-
**Default:** `1`.
338+
**Default:** `false`.
334339
- `only` {boolean} If truthy, and the test context is configured to run
335340
`only` tests, then this test will be run. Otherwise, the test is skipped.
336341
**Default:** `false`.

lib/internal/per_context/primordials.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ exports.Error = Error
1717
exports.ErrorCaptureStackTrace = (...args) => Error.captureStackTrace(...args)
1818
exports.FunctionPrototype = Function.prototype
1919
exports.FunctionPrototypeBind = (fn, obj, ...args) => fn.bind(obj, ...args)
20+
exports.MathMax = (...args) => Math.max(...args)
2021
exports.Number = Number
2122
exports.ObjectCreate = obj => Object.create(obj)
2223
exports.ObjectDefineProperties = (obj, props) => Object.defineProperties(obj, props)

lib/internal/test_runner/test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/d83446b4c4694322e12d2b7d22592f2be674e580/lib/internal/test_runner/test.js
1+
// https://github.com/nodejs/node/blob/dab492f0444b0a6ae8a41dd1d9605e036c363655/lib/internal/test_runner/test.js
22

33
'use strict'
44

@@ -7,6 +7,7 @@ const {
77
ArrayPrototypeShift,
88
ArrayPrototypeUnshift,
99
FunctionPrototype,
10+
MathMax,
1011
Number,
1112
PromisePrototypeThen,
1213
PromiseResolve,
@@ -55,8 +56,7 @@ const noop = FunctionPrototype
5556
const isTestRunner = getOptionValue('--test')
5657
const testOnlyFlag = !isTestRunner && getOptionValue('--test-only')
5758
// TODO(cjihrig): Use uv_available_parallelism() once it lands.
58-
const rootConcurrency = isTestRunner ? cpus().length : 1
59-
59+
const rootConcurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : 1
6060
const kShouldAbort = Symbol('kShouldAbort')
6161

6262
function stopTest (timeout, signal) {
@@ -154,6 +154,12 @@ class Test extends AsyncResource {
154154

155155
if (isUint32(concurrency) && concurrency !== 0) {
156156
this.concurrency = concurrency
157+
} else if (typeof concurrency === 'boolean') {
158+
if (concurrency) {
159+
this.concurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : Infinity
160+
} else {
161+
this.concurrency = 1
162+
}
157163
}
158164

159165
if (timeout != null && timeout !== Infinity) {

lib/test.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ interface TestOptions {
33
* The number of tests that can be run at the same time. If unspecified, subtests inherit this value from their parent.
44
* Default: 1.
55
*/
6-
concurrency?: number
6+
concurrency?: boolean | number
77

88
/**
99
* If truthy, the test is skipped. If a string is provided, that string is displayed in the test results as the reason for skipping the test.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://github.com/nodejs/node/blob/dab492f0444b0a6ae8a41dd1d9605e036c363655/test/parallel/test-runner-concurrency.js
2+
3+
'use strict'
4+
require('../common')
5+
const { describe, it } = require('node:test')
6+
const assert = require('assert')
7+
8+
describe('Concurrency option (boolean) = true ', { concurrency: true }, () => {
9+
let isFirstTestOver = false
10+
it('should start the first test', () => new Promise((resolve) => {
11+
setImmediate(() => { isFirstTestOver = true; resolve() })
12+
}))
13+
it('should start before the previous test ends', () => {
14+
// Should work even on single core CPUs
15+
assert.strictEqual(isFirstTestOver, false)
16+
})
17+
})
18+
19+
describe(
20+
'Concurrency option (boolean) = false ',
21+
{ concurrency: false },
22+
() => {
23+
let isFirstTestOver = false
24+
it('should start the first test', () => new Promise((resolve) => {
25+
setImmediate(() => { isFirstTestOver = true; resolve() })
26+
}))
27+
it('should start after the previous test ends', () => {
28+
assert.strictEqual(isFirstTestOver, true)
29+
})
30+
}
31+
)

0 commit comments

Comments
 (0)