Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/vitest/src/node/pools/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface QueuedTask {
task: PoolTask
resolver: ReturnType<typeof withResolvers>
method: 'run' | 'collect'
warmRunner?: Promise<PoolRunner>
}

interface ActiveTask extends QueuedTask {
Expand Down Expand Up @@ -67,11 +68,11 @@ export class Pool {
return
}

const { task, resolver, method } = this.queue.shift()!
const { task, resolver, method, warmRunner } = this.queue.shift()!

try {
let isMemoryLimitReached = false
const runner = this.getPoolRunner(task, method)
const runner = warmRunner ? await warmRunner : this.getPoolRunner(task, method)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the orignal PR I had this line as

const runner = (await warmRunner) || this.getPoolRunner(task, method)

... which broke everything and we had to drop this change. I though these two would be equivalent but no 🤔


const activeTask = { task, resolver, method, cancelTask }
this.activeTasks.push(activeTask)
Expand Down Expand Up @@ -123,6 +124,14 @@ export class Pool {
poolId,
})

const next = this.queue.find(entry => !entry.warmRunner && entry.task.isolate)

// Warmup the next runtime. This will start the dynamic imports inside the worker early.
if (next) {
const warmRunner = this.getPoolRunner(next.task, next.method)
next.warmRunner = warmRunner.start().then(() => warmRunner)
}

await resolver.promise

const index = this.activeTasks.indexOf(activeTask)
Expand Down Expand Up @@ -175,6 +184,7 @@ export class Pool {
if (pendingTasks.length) {
const error = new Error('Cancelled')
pendingTasks.forEach(task => task.resolver.reject(error))
await Promise.all(pendingTasks.map(task => task.warmRunner?.then(runner => runner.stop())))
}

const activeTasks = this.activeTasks.splice(0)
Expand Down
Loading