Skip to content

Commit

Permalink
feat: add support for boolean values for concurrency option
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#43887
Fixes: nodejs/node#43837
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
(cherry picked from commit dab492f0444b0a6ae8a41dd1d9605e036c363655)
  • Loading branch information
98lenvi authored and aduh95 committed Jul 24, 2022
1 parent cf78656 commit ba8fd71
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,14 @@ internally.
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
1 change: 1 addition & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exports.Error = Error
exports.ErrorCaptureStackTrace = (...args) => Error.captureStackTrace(...args)
exports.FunctionPrototype = Function.prototype
exports.FunctionPrototypeBind = (fn, obj, ...args) => fn.bind(obj, ...args)
exports.MathMax = (...args) => Math.max(...args)
exports.Number = Number
exports.ObjectCreate = obj => Object.create(obj)
exports.ObjectDefineProperties = (obj, props) => Object.defineProperties(obj, props)
Expand Down
12 changes: 9 additions & 3 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/nodejs/node/blob/d83446b4c4694322e12d2b7d22592f2be674e580/lib/internal/test_runner/test.js
// https://github.com/nodejs/node/blob/dab492f0444b0a6ae8a41dd1d9605e036c363655/lib/internal/test_runner/test.js

'use strict'

Expand All @@ -7,6 +7,7 @@ const {
ArrayPrototypeShift,
ArrayPrototypeUnshift,
FunctionPrototype,
MathMax,
Number,
PromisePrototypeThen,
PromiseResolve,
Expand Down Expand Up @@ -55,8 +56,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')

function stopTest (timeout, signal) {
Expand Down Expand Up @@ -154,6 +154,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
2 changes: 1 addition & 1 deletion lib/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ interface TestOptions {
* The number of tests that can be run at the same time. If unspecified, subtests inherit this value from their parent.
* Default: 1.
*/
concurrency?: number
concurrency?: boolean | number

/**
* 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.
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-runner-concurrency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// https://github.com/nodejs/node/blob/dab492f0444b0a6ae8a41dd1d9605e036c363655/test/parallel/test-runner-concurrency.js

'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 ba8fd71

Please sign in to comment.