Skip to content

Commit cf9176b

Browse files
hi-ogawaOpenCodesheremet-va
authored
fix: revive global concurrency limit for test lifecycle (#10928)
Co-authored-by: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Co-authored-by: OpenCode <noreply@opencode.ai> Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
1 parent 9b4df4b commit cf9176b

2 files changed

Lines changed: 49 additions & 67 deletions

File tree

packages/vitest/src/runtime/runner/run.ts

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

4243
/**
4344
* Normalizes retry configuration to extract individual values.
@@ -988,7 +989,7 @@ async function runSuiteChild(c: Task, runner: VitestRunner) {
988989
'code.line.number': c.location?.line,
989990
'code.column.number': c.location?.column,
990991
},
991-
() => runTest(c, runner),
992+
() => limitTestConcurrency(() => runTest(c, runner)),
992993
)
993994
}
994995
else if (c.type === 'suite') {
@@ -1009,6 +1010,7 @@ async function runSuiteChild(c: Task, runner: VitestRunner) {
10091010

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

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

test/e2e/test/concurrent.test.ts

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

10981098
function extractLogs(log: string) {
1099-
const result = log.split('\n').filter(line => line.match(/^![<>]/)).join('\n')
1099+
const result = log.split('\n').filter(line => line.match(/^(?:![<>]|\d+ -> \d+)/)).join('\n')
11001100
return `\n${result.trim()}\n`
11011101
}
11021102

@@ -1214,33 +1214,33 @@ describe.for(["a", "b"])("%s", { concurrent: true }, () => {
12141214
`)
12151215
})
12161216

1217-
// we could enforce this by adding yet another limit globally at `runTest`
1218-
// (like we originally had before https://github.com/vitest-dev/vitest/pull/9653)
1219-
// but there's no way to achieve the same for deep suite-level hooks anyways,
1220-
// so we don't do that (yet).
1221-
test('non-sibling test sequential lifecycle non-guarantee', async () => {
1217+
test('non-sibling test sequential lifecycle guarantee', async () => {
12221218
const result = await runInlineTests({
12231219
'basic.test.ts': `
12241220
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
1221+
let inFlight = 0
1222+
1223+
function logInFlight(change: number, ...names: string[]) {
1224+
const previous = inFlight
1225+
inFlight += change
1226+
console.log(previous, "->", inFlight, ...names)
1227+
}
12251228
12261229
describe.for(["a0", "a1"])("%s", { concurrent: true }, () => {
12271230
describe.for(["b0", "b1"])("%s", { concurrent: true }, () => {
12281231
beforeEach(async ({ task }) => {
1229-
console.log("!> beforeEach", task.suite.suite.name, task.suite.name, task.name)
1232+
logInFlight(1, "beforeEach", task.suite.suite.name, task.suite.name, task.name)
12301233
await sleep(10)
1231-
console.log("!< beforeEach", task.suite.suite.name, task.suite.name, task.name)
12321234
})
12331235
12341236
afterEach(async ({ task }) => {
1235-
console.log("!> afterEach", task.suite.suite.name, task.suite.name, task.name)
12361237
await sleep(10)
1237-
console.log("!< afterEach", task.suite.suite.name, task.suite.name, task.name)
1238+
logInFlight(-1, "afterEach", task.suite.suite.name, task.suite.name, task.name)
12381239
})
12391240
12401241
test("test", async ({ task }) => {
1241-
console.log("!> test", task.suite.suite.name,task.suite.name, task.name)
1242+
logInFlight(0, "test", task.suite.suite.name, task.suite.name, task.name)
12421243
await sleep(10)
1243-
console.log("!< test", task.suite.suite.name,task.suite.name, task.name)
12441244
})
12451245
})
12461246
})
@@ -1252,30 +1252,18 @@ describe.for(["a0", "a1"])("%s", { concurrent: true }, () => {
12521252

12531253
expect(extractLogs(result.stdout)).toMatchInlineSnapshot(`
12541254
"
1255-
!> beforeEach a0 b0 test
1256-
!> beforeEach a0 b1 test
1257-
!< beforeEach a0 b0 test
1258-
!> beforeEach a1 b0 test
1259-
!< beforeEach a0 b1 test
1260-
!> beforeEach a1 b1 test
1261-
!< beforeEach a1 b0 test
1262-
!> test a0 b0 test
1263-
!< beforeEach a1 b1 test
1264-
!> test a0 b1 test
1265-
!< test a0 b0 test
1266-
!> test a1 b0 test
1267-
!< test a0 b1 test
1268-
!> test a1 b1 test
1269-
!< test a1 b0 test
1270-
!> afterEach a0 b0 test
1271-
!< test a1 b1 test
1272-
!> afterEach a0 b1 test
1273-
!< afterEach a0 b0 test
1274-
!> afterEach a1 b0 test
1275-
!< afterEach a0 b1 test
1276-
!> afterEach a1 b1 test
1277-
!< afterEach a1 b0 test
1278-
!< afterEach a1 b1 test
1255+
0 -> 1 beforeEach a0 b0 test
1256+
1 -> 2 beforeEach a0 b1 test
1257+
2 -> 2 test a0 b0 test
1258+
2 -> 2 test a0 b1 test
1259+
2 -> 1 afterEach a0 b0 test
1260+
1 -> 2 beforeEach a1 b0 test
1261+
2 -> 1 afterEach a0 b1 test
1262+
1 -> 2 beforeEach a1 b1 test
1263+
2 -> 2 test a1 b0 test
1264+
2 -> 2 test a1 b1 test
1265+
2 -> 1 afterEach a1 b0 test
1266+
1 -> 0 afterEach a1 b1 test
12791267
"
12801268
`)
12811269

@@ -1307,25 +1295,29 @@ test('non-sibling suite sequential lifecycle non-guarantee', async () => {
13071295
const result = await runInlineTests({
13081296
'basic.test.ts': `
13091297
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
1298+
let inFlight = 0
1299+
1300+
function logInFlight(change: number, ...names: string[]) {
1301+
const previous = inFlight
1302+
inFlight += change
1303+
console.log(previous, "->", inFlight, ...names)
1304+
}
13101305
13111306
describe.for(["a0", "a1"])("%s", { concurrent: true }, () => {
13121307
describe.for(["b0", "b1"])("%s", { concurrent: true }, () => {
13131308
beforeAll(async ({}, suite) => {
1314-
console.log("!> beforeAll", suite.suite.name, suite.name)
1309+
logInFlight(1, "beforeAll", suite.suite.name, suite.name)
13151310
await sleep(10)
1316-
console.log("!< beforeAll", suite.suite.name, suite.name)
13171311
})
13181312
13191313
afterAll(async ({}, suite) => {
1320-
console.log("!> afterAll", suite.suite.name, suite.name)
13211314
await sleep(10)
1322-
console.log("!< afterAll", suite.suite.name, suite.name)
1315+
logInFlight(-1, "afterAll", suite.suite.name, suite.name)
13231316
})
13241317
13251318
test("test", async ({ task }) => {
1326-
console.log("!> test", task.suite.suite.name, task.suite.name, task.name)
1319+
logInFlight(0, "test", task.suite.suite.name, task.suite.name, task.name)
13271320
await sleep(10)
1328-
console.log("!< test", task.suite.suite.name, task.suite.name, task.name)
13291321
})
13301322
})
13311323
})
@@ -1337,30 +1329,18 @@ describe.for(["a0", "a1"])("%s", { concurrent: true }, () => {
13371329

13381330
expect(extractLogs(result.stdout)).toMatchInlineSnapshot(`
13391331
"
1340-
!> beforeAll a0 b0
1341-
!> beforeAll a0 b1
1342-
!< beforeAll a0 b0
1343-
!> beforeAll a1 b0
1344-
!< beforeAll a0 b1
1345-
!> beforeAll a1 b1
1346-
!< beforeAll a1 b0
1347-
!> test a0 b0 test
1348-
!< beforeAll a1 b1
1349-
!> test a0 b1 test
1350-
!< test a0 b0 test
1351-
!> test a1 b0 test
1352-
!< test a0 b1 test
1353-
!> test a1 b1 test
1354-
!< test a1 b0 test
1355-
!> afterAll a0 b0
1356-
!< test a1 b1 test
1357-
!> afterAll a0 b1
1358-
!< afterAll a0 b0
1359-
!> afterAll a1 b0
1360-
!< afterAll a0 b1
1361-
!> afterAll a1 b1
1362-
!< afterAll a1 b0
1363-
!< afterAll a1 b1
1332+
0 -> 1 beforeAll a0 b0
1333+
1 -> 2 beforeAll a0 b1
1334+
2 -> 3 beforeAll a1 b0
1335+
3 -> 4 beforeAll a1 b1
1336+
4 -> 4 test a0 b0 test
1337+
4 -> 4 test a0 b1 test
1338+
4 -> 4 test a1 b0 test
1339+
4 -> 4 test a1 b1 test
1340+
4 -> 3 afterAll a0 b0
1341+
3 -> 2 afterAll a0 b1
1342+
2 -> 1 afterAll a1 b0
1343+
1 -> 0 afterAll a1 b1
13641344
"
13651345
`)
13661346

0 commit comments

Comments
 (0)
Sponsor
SponsoredKunjungi sekarang
Promo