Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add throat when running spec files #1482

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"micromatch": "^4.0.5",
"mlly": "^0.5.2",
"natural-compare": "^1.4.0",
"p-limit": "^4.0.0",
"pathe": "^0.2.0",
"picocolors": "^1.0.0",
"pkg-types": "^0.3.2",
Expand Down
20 changes: 17 additions & 3 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { cpus } from 'os'
import { resolve } from 'pathe'
import type { Options as TinypoolOptions } from 'tinypool'
import { Tinypool } from 'tinypool'
import limit from 'p-limit'
import { createBirpc } from 'birpc'
import type { RawSourceMap } from 'vite-node'
import type { WorkerContext, WorkerRPC } from '../types'
Expand Down Expand Up @@ -67,21 +68,28 @@ export function createPool(ctx: Vitest): WorkerPool {
let id = 0
const config = ctx.getSerializableConfig()

const freePoolId = new Set<number>(
new Array(maxThreads).fill(0).map((_, i) => (i % maxThreads)),
)

async function runFiles(files: string[], invalidates: string[] = []) {
const { workerPort, port } = createChannel(ctx)
const workerId = ++id
const poolId = !ctx.config.threads ? 0 : freePoolId.values().next().value
const data: WorkerContext = {
port: workerPort,
config,
files,
invalidates,
workerId,
poolId: !ctx.config.threads ? 1 : ((workerId - 1) % maxThreads) + 1,
poolId: poolId + 1,
}
freePoolId.delete(poolId)
try {
await pool.run(data, { transferList: [workerPort], name })
}
finally {
freePoolId.add(poolId)
port.close()
workerPort.close()
}
Expand All @@ -92,8 +100,14 @@ export function createPool(ctx: Vitest): WorkerPool {
await runFiles(files)
}
else {
const results = await Promise.allSettled(files
.map(file => runFiles([file], invalidates)))
const mutex = limit(maxThreads)

const promises: Promise<void>[] = []

for (const file of files)
promises.push(mutex(() => runFiles([file], invalidates)))

const results = await Promise.allSettled(promises)

const errors = results.filter((r): r is PromiseRejectedResult => r.status === 'rejected').map(r => r.reason)
if (errors.length > 0)
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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