Skip to content

Commit f52316b

Browse files
committed
wip: limitMaxConcurrency only test cases
1 parent 6264d10 commit f52316b

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

packages/runner/src/run.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
325325
else {
326326
for (let tasksGroup of partitionSuiteChildren(suite)) {
327327
if (tasksGroup[0].concurrent === true) {
328-
const mutex = limit(runner.config.maxConcurrency)
329-
await Promise.all(tasksGroup.map(c => mutex(() => runSuiteChild(c, runner))))
328+
await Promise.all(tasksGroup.map(c => runSuiteChild(c, runner)))
330329
}
331330
else {
332331
const { sequence } = runner.config
@@ -379,15 +378,19 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
379378
}
380379
}
381380

381+
let limitMaxConcurrency: ReturnType<typeof limit>
382+
382383
async function runSuiteChild(c: Task, runner: VitestRunner) {
383384
if (c.type === 'test' || c.type === 'custom')
384-
return runTest(c, runner)
385+
return limitMaxConcurrency(() => runTest(c, runner))
385386

386387
else if (c.type === 'suite')
387388
return runSuite(c, runner)
388389
}
389390

390391
export async function runFiles(files: File[], runner: VitestRunner) {
392+
limitMaxConcurrency ??= limit(runner.config.maxConcurrency)
393+
391394
for (const file of files) {
392395
if (!file.tasks.length && !runner.config.passWithNoTests) {
393396
if (!file.result?.errors?.length) {

pnpm-lock.yaml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/core/test/concurrent-maxConcurrency.test.ts test/cli/fixtures/fails/concurrent-suite-deadlock.test.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
import { createDefer } from '@vitest/utils'
21
import { describe, test, vi } from 'vitest'
2+
import { createDefer } from 'vitest/dist/utils.js'
33

4-
vi.setConfig({ maxConcurrency: 2 })
4+
// 3 tests depend on each other,
5+
// so they will deadlock when maxConcurrency < 3
6+
//
7+
// [a] [b] [c]
8+
// * ->
9+
// * ->
10+
// <- *
11+
// <------
512

6-
describe('limit for each suite', () => {
7-
// this test requires running 3 tests in parallel.
8-
// but, currently p-limit is applied for each suite layer,
9-
// so tests succeed.
10-
//
11-
// [0] [1] [2]
12-
// * ->
13-
// * ->
14-
// <- *
15-
// <------
13+
vi.setConfig({ maxConcurrency: 2 })
1614

15+
describe('wrapper', { concurrent: true, timeout: 500 }, () => {
1716
const defers = [
1817
createDefer<void>(),
1918
createDefer<void>(),
2019
createDefer<void>(),
2120
]
2221

23-
describe('1st suite', { concurrent: true, concurrentSuite: true }, () => {
22+
describe('1st suite', () => {
2423
test('a', async () => {
2524
defers[0].resolve()
2625
await defers[2]
@@ -33,7 +32,7 @@ describe('limit for each suite', () => {
3332
})
3433
})
3534

36-
describe('2nd suite', { concurrent: true, concurrentSuite: true }, () => {
35+
describe('2nd suite', () => {
3736
test('c', async () => {
3837
await defers[1]
3938
defers[2].resolve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { describe, test, vi } from 'vitest'
2+
import { createDefer } from 'vitest/dist/utils.js'
3+
4+
// 3 tests depend on each other,
5+
// so they will deadlock when maxConcurrency < 3
6+
//
7+
// [a] [b] [c]
8+
// * ->
9+
// * ->
10+
// <- *
11+
// <------
12+
13+
vi.setConfig({ maxConcurrency: 2 })
14+
15+
describe('wrapper', { concurrent: true, timeout: 500 }, () => {
16+
const defers = [
17+
createDefer<void>(),
18+
createDefer<void>(),
19+
createDefer<void>(),
20+
]
21+
22+
test('a', async () => {
23+
defers[0].resolve()
24+
await defers[2]
25+
})
26+
27+
test('b', async () => {
28+
await defers[0]
29+
defers[1].resolve()
30+
await defers[2]
31+
})
32+
33+
test('c', async () => {
34+
await defers[1]
35+
defers[2].resolve()
36+
})
37+
})

test/cli/test/__snapshots__/fails.test.ts.snap

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
exports[`should fail .dot-folder/dot-test.test.ts > .dot-folder/dot-test.test.ts 1`] = `"AssertionError: expected true to be false // Object.is equality"`;
44

5+
exports[`should fail concurrent-suite-deadlock.test.ts > concurrent-suite-deadlock.test.ts 1`] = `"Error: Test timed out in 500ms."`;
6+
7+
exports[`should fail concurrent-test-deadlock.test.ts > concurrent-test-deadlock.test.ts 1`] = `"Error: Test timed out in 500ms."`;
8+
59
exports[`should fail each-timeout.test.ts > each-timeout.test.ts 1`] = `"Error: Test timed out in 10ms."`;
610

711
exports[`should fail empty.test.ts > empty.test.ts 1`] = `"Error: No test suite found in file <rootDir>/empty.test.ts"`;

0 commit comments

Comments
 (0)