@@ -12,25 +12,28 @@ import { vi } from "vite-plus/test";
1212
1313import * as HostResources from "./HostResources.ts" ;
1414
15- // An 8-CPU host; every cpus() call advances each CPU by 100 ms at its `host.busy` utilization.
16- const host = vi . hoisted ( ( ) => ( { busy : [ ] as ReadonlyArray < number > , reads : 0 } ) ) ;
15+ // An 8-CPU host; every cpus() call advances each CPU by 100 ms at `host.busy` utilization.
16+ const host = vi . hoisted ( ( ) => ( { busy : 0 , reads : 0 } ) ) ;
1717vi . mock ( "node:os" , async ( importOriginal ) => {
1818 const actual = await importOriginal < typeof NodeOS > ( ) ;
1919 return {
2020 ...actual ,
2121 totalmem : ( ) => 4 * 1024 ** 3 ,
2222 cpus : ( ) => {
2323 const elapsed = 100 * host . reads ++ ;
24- return host . busy . map ( ( busy ) => ( {
25- model : "test" ,
26- speed : 1 ,
27- times : { user : elapsed * busy , nice : 0 , sys : 0 , idle : elapsed * ( 1 - busy ) , irq : 0 } ,
28- } ) ) ;
24+ const times = {
25+ user : elapsed * host . busy ,
26+ nice : 0 ,
27+ sys : 0 ,
28+ idle : elapsed * ( 1 - host . busy ) ,
29+ irq : 0 ,
30+ } ;
31+ return Array . from ( { length : 8 } , ( ) => ( { model : "test" , speed : 1 , times } ) ) ;
2932 } ,
3033 } ;
3134} ) ;
32- const setup = ( busy : number | ReadonlyArray < number > = 0 ) => {
33- host . busy = typeof busy === "number" ? Array . from ( { length : 8 } , ( ) => busy ) : busy ;
35+ const setup = ( busy = 0 ) => {
36+ host . busy = busy ;
3437 host . reads = 0 ;
3538} ;
3639
@@ -148,20 +151,28 @@ describe("HostResources.layer", () => {
148151 ) ,
149152 ) ;
150153
151- it . effect ( "counts only the CPUs the server is pinned to as host idle " , ( ) =>
154+ it . effect ( "samples the CPUs the server is pinned to by id, not by os.cpus() index " , ( ) =>
152155 Effect . gen ( function * ( ) {
153- // A neighbour saturates CPUs 0-1, the server's whole cpuset; CPUs 2-7 sit idle.
154- setup ( [ 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ;
156+ setup ( ) ;
155157 const snapshot = yield * read ;
156158
157- assert . strictEqual ( snapshot . cpuCount , 2 ) ;
159+ assert . strictEqual ( snapshot . cpuCount , 1 ) ;
158160 assert . strictEqual ( snapshot . cpuUtilization , 1 ) ;
159161 } ) . pipe (
160162 Effect . provide (
161163 linuxHost ( {
162164 ...unlimitedCgroup ,
163- "/proc/self/status" : [ "Name:\tnode\nCpus_allowed_list:\t0-1\n" ] ,
164- [ `${ cgroup } /cpu.max` ] : [ "400000 100000\n" ] ,
165+ "/proc/self/status" : [ "Name:\tnode\nCpus_allowed_list:\t4\n" ] ,
166+ // CPU 1 is offline; the server's CPU 4 is saturated, everything else is idle.
167+ "/proc/stat" : [ 0 , 100 ] . map ( ( elapsed ) =>
168+ [ 0 , 2 , 3 , 4 , 5 , 6 , 7 ]
169+ . map ( ( id ) =>
170+ id === 4
171+ ? `cpu${ id } ${ elapsed } 0 0 0 0 0 0 0`
172+ : `cpu${ id } 0 0 0 ${ elapsed } 0 0 0 0` ,
173+ )
174+ . join ( "\n" ) ,
175+ ) ,
165176 } ) ,
166177 ) ,
167178 ) ,
0 commit comments