Skip to content

Commit 8cb81db

Browse files
committed
Add unit tests for getCgroupCpuCountFromCpus
1 parent ef0a773 commit 8cb81db

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

lib/util.js

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.test.js

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/util.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,36 @@ for (const [
449449
versionStub.restore();
450450
});
451451
}
452+
453+
test("getCgroupCpuCountFromCpus calculates the number of CPUs correctly", async (t) => {
454+
await util.withTmpDir(async (tmpDir: string) => {
455+
const testCpuFile = `${tmpDir}/cpus-file`;
456+
fs.writeFileSync(testCpuFile, "1, 9-10\n", "utf-8");
457+
t.deepEqual(
458+
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
459+
3,
460+
);
461+
});
462+
});
463+
464+
test("getCgroupCpuCountFromCpus returns undefined if the CPU file doesn't exist", async (t) => {
465+
await util.withTmpDir(async (tmpDir: string) => {
466+
const testCpuFile = `${tmpDir}/cpus-file`;
467+
t.false(fs.existsSync(testCpuFile));
468+
t.deepEqual(
469+
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
470+
undefined,
471+
);
472+
});
473+
});
474+
475+
test("getCgroupCpuCountFromCpus returns undefined if the CPU file exists but is empty", async (t) => {
476+
await util.withTmpDir(async (tmpDir: string) => {
477+
const testCpuFile = `${tmpDir}/cpus-file`;
478+
fs.writeFileSync(testCpuFile, "\n", "utf-8");
479+
t.deepEqual(
480+
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
481+
undefined,
482+
);
483+
});
484+
});

src/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ function getCgroupCpuCountFromCpuMax(
440440
/**
441441
* Gets the number of available cores listed in the cgroup cpuset.cpus file at the given path.
442442
*/
443-
function getCgroupCpuCountFromCpus(
443+
export function getCgroupCpuCountFromCpus(
444444
cpusFile: string,
445445
logger: Logger,
446446
): number | undefined {

0 commit comments

Comments
 (0)