Skip to content

Commit 50312eb

Browse files
authored
fix(pool): preserve unique pool ids when groupOrder is set (#11392)
1 parent 07ee717 commit 50312eb

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎packages/vitest/src/node/pools/pool.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export class Pool {
8080
let isMemoryLimitReached = false
8181
const runner = this.getPoolRunner(task, method)
8282

83+
// `setMaxWorkers` can replace the map for the next group before this id is freed
84+
const workerIds = this.workerIds
8385
const poolId = runner.poolId ?? this.getConcurrencyId()
8486
runner.poolId = poolId
8587

@@ -195,7 +197,7 @@ export class Pool {
195197
)
196198
}
197199

198-
this.freeWorkerId(poolId)
200+
workerIds.set(poolId, true)
199201
}
200202

201203
// This is mostly to avoid zombie workers when/if Vitest internals run into errors

‎test/e2e/test/group-order.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,82 @@ test('tests run according to the group order', async () => {
5858
✓ |1| example.1.test.ts > 1 <time>"
5959
`)
6060
})
61+
62+
test('the next group does not reuse a pool id that is still in use', async () => {
63+
const uniquePoolId = /* ts */ `
64+
import { mkdirSync, rmSync } from 'node:fs'
65+
import { onTestFinished, test } from 'vitest'
66+
67+
test('unique pool id', async () => {
68+
const locks = import.meta.dirname + '/../.locks'
69+
mkdirSync(locks, { recursive: true })
70+
const lock = locks + '/' + process.env.VITEST_POOL_ID
71+
mkdirSync(lock)
72+
onTestFinished(() => rmSync(lock, { recursive: true }))
73+
await new Promise(resolve => setTimeout(resolve, 200))
74+
})
75+
`
76+
const { stderr, testTree } = await runInlineTests(
77+
{
78+
// starts first with pool id 1 and finishes last in its group
79+
'first/slow.test.ts': `test('slow', () => new Promise(resolve => setTimeout(resolve, 500)))`,
80+
'first/fast-1.test.ts': `test('fast', () => {})`,
81+
'first/fast-2.test.ts': `test('fast', () => {})`,
82+
'first/fast-3.test.ts': `test('fast', () => {})`,
83+
'second/1.test.ts': uniquePoolId,
84+
'second/2.test.ts': uniquePoolId,
85+
'second/3.test.ts': uniquePoolId,
86+
'second/4.test.ts': uniquePoolId,
87+
},
88+
{
89+
$cliOptions: { globals: true, maxWorkers: 4 },
90+
projects: [
91+
{
92+
test: {
93+
name: 'first',
94+
include: ['./first/*.test.ts'],
95+
},
96+
},
97+
{
98+
test: {
99+
name: 'second',
100+
include: ['./second/*.test.ts'],
101+
sequence: {
102+
groupOrder: 1,
103+
},
104+
},
105+
},
106+
],
107+
},
108+
)
109+
110+
expect(stderr).toBe('')
111+
expect(testTree()).toMatchInlineSnapshot(`
112+
{
113+
"first/fast-1.test.ts": {
114+
"fast": "passed",
115+
},
116+
"first/fast-2.test.ts": {
117+
"fast": "passed",
118+
},
119+
"first/fast-3.test.ts": {
120+
"fast": "passed",
121+
},
122+
"first/slow.test.ts": {
123+
"slow": "passed",
124+
},
125+
"second/1.test.ts": {
126+
"unique pool id": "passed",
127+
},
128+
"second/2.test.ts": {
129+
"unique pool id": "passed",
130+
},
131+
"second/3.test.ts": {
132+
"unique pool id": "passed",
133+
},
134+
"second/4.test.ts": {
135+
"unique pool id": "passed",
136+
},
137+
}
138+
`)
139+
})

0 commit comments

Comments
 (0)