Skip to content

Commit 5146df8

Browse files
fix: revive global concurrency limit for test lifecycle [backport to v4] (#10992)
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
1 parent fe5a11d commit 5146df8

2 files changed

Lines changed: 49 additions & 67 deletions

File tree

packages/runner/src/run.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const now = globalThis.performance ? globalThis.performance.now.bind(globalThis.
3737
const unixNow = Date.now
3838
const { clearTimeout, setTimeout } = getSafeTimers()
3939
let limitMaxConcurrency: ConcurrencyLimiter
40+
let limitTestConcurrency: ConcurrencyLimiter
4041

4142
/**
4243
* Normalizes retry configuration to extract individual values.
@@ -987,7 +988,7 @@ async function runSuiteChild(c: Task, runner: VitestRunner) {
987988
'code.line.number': c.location?.line,
988989
'code.column.number': c.location?.column,
989990
},
990-
() => runTest(c, runner),
991+
() => limitTestConcurrency(() => runTest(c, runner)),
991992
)
992993
}
993994
else if (c.type === 'suite') {
@@ -1008,6 +1009,7 @@ async function runSuiteChild(c: Task, runner: VitestRunner) {
10081009

10091010
export async function runFiles(files: File[], runner: VitestRunner): Promise<void> {
10101011
limitMaxConcurrency ??= limitConcurrency(runner.config.maxConcurrency)
1012+
limitTestConcurrency ??= limitConcurrency(runner.config.maxConcurrency)
10111013

10121014
for (const file of files) {
10131015
if (!file.tasks.length && !runner.config.passWithNoTests) {

test/cli/test/concurrent.test.ts

Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ test('aroundAll enforces teardown timeout when inner error is caught', async ()
10761076
})
10771077

10781078
function extractLogs(log: string) {
1079-
const result = log.split('\n').filter(line => line.match(/^![<>]/)).join('\n')
1079+
const result = log.split('\n').filter(line => line.match(/^(?:![<>]|\d+ -> \d+)/)).join('\n')
10801080
return `\n${result.trim()}\n`
10811081
}
10821082

@@ -1194,33 +1194,33 @@ describe.for(["a", "b"])("%s", { concurrent: true }, () => {
11941194
`)
11951195
})
11961196

1197-
// we could enforce this by adding yet another limit globally at `runTest`
1198-
// (like we originally had before https://github.com/vitest-dev/vitest/pull/9653)
1199-
// but there's no way to achieve the same for deep suite-level hooks anyways,
1200-
// so we don't do that (yet).
1201-
test('non-sibling test sequential lifecycle non-guarantee', async () => {
1197+
test('non-sibling test sequential lifecycle guarantee', async () => {
12021198
const result = await runInlineTests({
12031199
'basic.test.ts': `
12041200
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
1201+
let inFlight = 0
1202+
1203+
function logInFlight(change: number, ...names: string[]) {
1204+
const previous = inFlight
1205+
inFlight += change
1206+
console.log(previous, "->", inFlight, ...names)
1207+
}
12051208
12061209
describe.for(["a0", "a1"])("%s", { concurrent: true }, () => {
12071210
describe.for(["b0", "b1"])("%s", { concurrent: true }, () => {
12081211
beforeEach(async ({ task }) => {
1209-
console.log("!> beforeEach", task.suite.suite.name, task.suite.name, task.name)
1212+
logInFlight(1, "beforeEach", task.suite.suite.name, task.suite.name, task.name)
12101213
await sleep(10)
1211-
console.log("!< beforeEach", task.suite.suite.name, task.suite.name, task.name)
12121214
})
12131215
12141216
afterEach(async ({ task }) => {
1215-
console.log("!> afterEach", task.suite.suite.name, task.suite.name, task.name)
12161217
await sleep(10)
1217-
console.log("!< afterEach", task.suite.suite.name, task.suite.name, task.name)
1218+
logInFlight(-1, "afterEach", task.suite.suite.name, task.suite.name, task.name)
12181219
})
12191220
12201221
test("test", async ({ task }) => {
1221-
console.log("!> test", task.suite.suite.name,task.suite.name, task.name)
1222+
logInFlight(0, "test", task.suite.suite.name, task.suite.name, task.name)
12221223
await sleep(10)
1223-
console.log("!< test", task.suite.suite.name,task.suite.name, task.name)
12241224
})
12251225
})
12261226
})
@@ -1232,30 +1232,18 @@ describe.for(["a0", "a1"])("%s", { concurrent: true }, () => {
12321232

12331233
expect(extractLogs(result.stdout)).toMatchInlineSnapshot(`
12341234
"
1235-
!> beforeEach a0 b0 test
1236-
!> beforeEach a0 b1 test
1237-
!< beforeEach a0 b0 test
1238-
!> beforeEach a1 b0 test
1239-
!< beforeEach a0 b1 test
1240-
!> beforeEach a1 b1 test
1241-
!< beforeEach a1 b0 test
1242-
!> test a0 b0 test
1243-
!< beforeEach a1 b1 test
1244-
!> test a0 b1 test
1245-
!< test a0 b0 test
1246-
!> test a1 b0 test
1247-
!< test a0 b1 test
1248-
!> test a1 b1 test
1249-
!< test a1 b0 test
1250-
!> afterEach a0 b0 test
1251-
!< test a1 b1 test
1252-
!> afterEach a0 b1 test
1253-
!< afterEach a0 b0 test
1254-
!> afterEach a1 b0 test
1255-
!< afterEach a0 b1 test
1256-
!> afterEach a1 b1 test
1257-
!< afterEach a1 b0 test
1258-
!< afterEach a1 b1 test
1235+
0 -> 1 beforeEach a0 b0 test
1236+
1 -> 2 beforeEach a0 b1 test
1237+
2 -> 2 test a0 b0 test
1238+
2 -> 2 test a0 b1 test
1239+
2 -> 1 afterEach a0 b0 test
1240+
1 -> 2 beforeEach a1 b0 test
1241+
2 -> 1 afterEach a0 b1 test
1242+
1 -> 2 beforeEach a1 b1 test
1243+
2 -> 2 test a1 b0 test
1244+
2 -> 2 test a1 b1 test
1245+
2 -> 1 afterEach a1 b0 test
1246+
1 -> 0 afterEach a1 b1 test
12591247
"
12601248
`)
12611249

@@ -1287,25 +1275,29 @@ test('non-sibling suite sequential lifecycle non-guarantee', async () => {
12871275
const result = await runInlineTests({
12881276
'basic.test.ts': `
12891277
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
1278+
let inFlight = 0
1279+
1280+
function logInFlight(change: number, ...names: string[]) {
1281+
const previous = inFlight
1282+
inFlight += change
1283+
console.log(previous, "->", inFlight, ...names)
1284+
}
12901285
12911286
describe.for(["a0", "a1"])("%s", { concurrent: true }, () => {
12921287
describe.for(["b0", "b1"])("%s", { concurrent: true }, () => {
12931288
beforeAll(async ({}, suite) => {
1294-
console.log("!> beforeAll", suite.suite.name, suite.name)
1289+
logInFlight(1, "beforeAll", suite.suite.name, suite.name)
12951290
await sleep(10)
1296-
console.log("!< beforeAll", suite.suite.name, suite.name)
12971291
})
12981292
12991293
afterAll(async ({}, suite) => {
1300-
console.log("!> afterAll", suite.suite.name, suite.name)
13011294
await sleep(10)
1302-
console.log("!< afterAll", suite.suite.name, suite.name)
1295+
logInFlight(-1, "afterAll", suite.suite.name, suite.name)
13031296
})
13041297
13051298
test("test", async ({ task }) => {
1306-
console.log("!> test", task.suite.suite.name, task.suite.name, task.name)
1299+
logInFlight(0, "test", task.suite.suite.name, task.suite.name, task.name)
13071300
await sleep(10)
1308-
console.log("!< test", task.suite.suite.name, task.suite.name, task.name)
13091301
})
13101302
})
13111303
})
@@ -1317,30 +1309,18 @@ describe.for(["a0", "a1"])("%s", { concurrent: true }, () => {
13171309

13181310
expect(extractLogs(result.stdout)).toMatchInlineSnapshot(`
13191311
"
1320-
!> beforeAll a0 b0
1321-
!> beforeAll a0 b1
1322-
!< beforeAll a0 b0
1323-
!> beforeAll a1 b0
1324-
!< beforeAll a0 b1
1325-
!> beforeAll a1 b1
1326-
!< beforeAll a1 b0
1327-
!> test a0 b0 test
1328-
!< beforeAll a1 b1
1329-
!> test a0 b1 test
1330-
!< test a0 b0 test
1331-
!> test a1 b0 test
1332-
!< test a0 b1 test
1333-
!> test a1 b1 test
1334-
!< test a1 b0 test
1335-
!> afterAll a0 b0
1336-
!< test a1 b1 test
1337-
!> afterAll a0 b1
1338-
!< afterAll a0 b0
1339-
!> afterAll a1 b0
1340-
!< afterAll a0 b1
1341-
!> afterAll a1 b1
1342-
!< afterAll a1 b0
1343-
!< afterAll a1 b1
1312+
0 -> 1 beforeAll a0 b0
1313+
1 -> 2 beforeAll a0 b1
1314+
2 -> 3 beforeAll a1 b0
1315+
3 -> 4 beforeAll a1 b1
1316+
4 -> 4 test a0 b0 test
1317+
4 -> 4 test a0 b1 test
1318+
4 -> 4 test a1 b0 test
1319+
4 -> 4 test a1 b1 test
1320+
4 -> 3 afterAll a0 b0
1321+
3 -> 2 afterAll a0 b1
1322+
2 -> 1 afterAll a1 b0
1323+
1 -> 0 afterAll a1 b1
13441324
"
13451325
`)
13461326

0 commit comments

Comments
 (0)