Skip to content

Commit

Permalink
wip: limitMaxConcurrency only test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 30, 2024
1 parent 6264d10 commit f52316b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
9 changes: 6 additions & 3 deletions packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,7 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
else {
for (let tasksGroup of partitionSuiteChildren(suite)) {
if (tasksGroup[0].concurrent === true) {
const mutex = limit(runner.config.maxConcurrency)
await Promise.all(tasksGroup.map(c => mutex(() => runSuiteChild(c, runner))))
await Promise.all(tasksGroup.map(c => runSuiteChild(c, runner)))
}
else {
const { sequence } = runner.config
Expand Down Expand Up @@ -379,15 +378,19 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
}
}

let limitMaxConcurrency: ReturnType<typeof limit>

async function runSuiteChild(c: Task, runner: VitestRunner) {
if (c.type === 'test' || c.type === 'custom')
return runTest(c, runner)
return limitMaxConcurrency(() => runTest(c, runner))

else if (c.type === 'suite')
return runSuite(c, runner)
}

export async function runFiles(files: File[], runner: VitestRunner) {
limitMaxConcurrency ??= limit(runner.config.maxConcurrency)

for (const file of files) {
if (!file.tasks.length && !runner.config.passWithNoTests) {
if (!file.result?.errors?.length) {
Expand Down
1 change: 1 addition & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { createDefer } from '@vitest/utils'
import { describe, test, vi } from 'vitest'
import { createDefer } from 'vitest/dist/utils.js'

vi.setConfig({ maxConcurrency: 2 })
// 3 tests depend on each other,
// so they will deadlock when maxConcurrency < 3
//
// [a] [b] [c]
// * ->
// * ->
// <- *
// <------

describe('limit for each suite', () => {
// this test requires running 3 tests in parallel.
// but, currently p-limit is applied for each suite layer,
// so tests succeed.
//
// [0] [1] [2]
// * ->
// * ->
// <- *
// <------
vi.setConfig({ maxConcurrency: 2 })

describe('wrapper', { concurrent: true, timeout: 500 }, () => {
const defers = [
createDefer<void>(),
createDefer<void>(),
createDefer<void>(),
]

describe('1st suite', { concurrent: true, concurrentSuite: true }, () => {
describe('1st suite', () => {
test('a', async () => {
defers[0].resolve()
await defers[2]
Expand All @@ -33,7 +32,7 @@ describe('limit for each suite', () => {
})
})

describe('2nd suite', { concurrent: true, concurrentSuite: true }, () => {
describe('2nd suite', () => {
test('c', async () => {
await defers[1]
defers[2].resolve()
Expand Down
37 changes: 37 additions & 0 deletions test/cli/fixtures/fails/concurrent-test-deadlock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, test, vi } from 'vitest'
import { createDefer } from 'vitest/dist/utils.js'

// 3 tests depend on each other,
// so they will deadlock when maxConcurrency < 3
//
// [a] [b] [c]
// * ->
// * ->
// <- *
// <------

vi.setConfig({ maxConcurrency: 2 })

describe('wrapper', { concurrent: true, timeout: 500 }, () => {
const defers = [
createDefer<void>(),
createDefer<void>(),
createDefer<void>(),
]

test('a', async () => {
defers[0].resolve()
await defers[2]
})

test('b', async () => {
await defers[0]
defers[1].resolve()
await defers[2]
})

test('c', async () => {
await defers[1]
defers[2].resolve()
})
})
4 changes: 4 additions & 0 deletions test/cli/test/__snapshots__/fails.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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"`;

exports[`should fail concurrent-suite-deadlock.test.ts > concurrent-suite-deadlock.test.ts 1`] = `"Error: Test timed out in 500ms."`;

exports[`should fail concurrent-test-deadlock.test.ts > concurrent-test-deadlock.test.ts 1`] = `"Error: Test timed out in 500ms."`;

exports[`should fail each-timeout.test.ts > each-timeout.test.ts 1`] = `"Error: Test timed out in 10ms."`;

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

0 comments on commit f52316b

Please sign in to comment.