1- import { cpus , totalmem } from "os" ;
1+ import { availableParallelism , cpus , totalmem } from "os" ;
22import { getHeapStatistics } from "v8" ;
33import { afterEach , describe , it , expect , vi } from "vitest" ;
44import {
@@ -132,7 +132,13 @@ describe("calculateOptimalWorkers", () => {
132132
133133vi . mock ( "os" , async ( importOriginal ) => {
134134 const os = await importOriginal < typeof import ( "os" ) > ( ) ;
135- return { ...os , default : os , cpus : vi . fn ( os . cpus ) , totalmem : vi . fn ( os . totalmem ) } ;
135+ return {
136+ ...os ,
137+ default : os ,
138+ availableParallelism : vi . fn ( os . availableParallelism ) ,
139+ cpus : vi . fn ( os . cpus ) ,
140+ totalmem : vi . fn ( os . totalmem ) ,
141+ } ;
136142} ) ;
137143vi . mock ( "v8" , async ( importOriginal ) => {
138144 const v8 = await importOriginal < typeof import ( "v8" ) > ( ) ;
@@ -141,16 +147,15 @@ vi.mock("v8", async (importOriginal) => {
141147
142148describe ( "computeWorkerSizing" , ( ) => {
143149 afterEach ( ( ) => {
150+ vi . mocked ( availableParallelism ) . mockRestore ( ) ;
144151 vi . mocked ( cpus ) . mockRestore ( ) ;
145152 vi . mocked ( totalmem ) . mockRestore ( ) ;
146153 vi . mocked ( getHeapStatistics ) . mockRestore ( ) ;
147154 } ) ;
148155
149156 // The field case: a 24GB 14-core Mac, Node's default ~4GB heap, a long 1080x1920 project.
150157 it ( "never auto-picks more workers than the V8 heap can feed" , ( ) => {
151- vi . mocked ( cpus ) . mockReturnValue (
152- Array . from ( { length : 14 } , ( ) => ( { } ) as ReturnType < typeof cpus > [ number ] ) ,
153- ) ;
158+ vi . mocked ( availableParallelism ) . mockReturnValue ( 14 ) ;
154159 vi . mocked ( totalmem ) . mockReturnValue ( 24 * 1024 ** 3 ) ;
155160 vi . mocked ( getHeapStatistics ) . mockReturnValue ( {
156161 heap_size_limit : 4192 * 1024 ** 2 ,
@@ -167,6 +172,14 @@ describe("computeWorkerSizing", () => {
167172 expect ( computeWorkerSizing ( 1300 , undefined , { concurrency : "auto" } ) . workers ) . toBe ( 5 ) ;
168173 } ) ;
169174
175+ it ( "sizes from the CPU quota, not the host's core count" , ( ) => {
176+ vi . mocked ( cpus ) . mockReturnValue ( Array . from ( { length : 32 } , ( ) => ( { } ) as never ) ) ;
177+ vi . mocked ( availableParallelism ) . mockReturnValue ( 6 ) ;
178+ const sizing = computeWorkerSizing ( 1300 , undefined , { concurrency : "auto" } ) ;
179+ expect ( sizing . cpuCount ) . toBe ( 6 ) ;
180+ expect ( sizing . cpuBasedWorkers ) . toBe ( 4 ) ;
181+ } ) ;
182+
170183 it ( "matches calculateOptimalWorkers and reports every constraint" , ( ) => {
171184 const config = { concurrency : "auto" as const } ;
172185 const sizing = computeWorkerSizing ( 900 , undefined , config ) ;
0 commit comments