Add unit tests for getCgroupCpuCountFromCpus

This commit is contained in:
Angela P Wen 2024-02-16 12:48:04 +00:00
parent ef0a773901
commit 8cb81db135
6 changed files with 59 additions and 4 deletions

View file

@ -449,3 +449,36 @@ for (const [
versionStub.restore();
});
}
test("getCgroupCpuCountFromCpus calculates the number of CPUs correctly", async (t) => {
await util.withTmpDir(async (tmpDir: string) => {
const testCpuFile = `${tmpDir}/cpus-file`;
fs.writeFileSync(testCpuFile, "1, 9-10\n", "utf-8");
t.deepEqual(
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
3,
);
});
});
test("getCgroupCpuCountFromCpus returns undefined if the CPU file doesn't exist", async (t) => {
await util.withTmpDir(async (tmpDir: string) => {
const testCpuFile = `${tmpDir}/cpus-file`;
t.false(fs.existsSync(testCpuFile));
t.deepEqual(
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
undefined,
);
});
});
test("getCgroupCpuCountFromCpus returns undefined if the CPU file exists but is empty", async (t) => {
await util.withTmpDir(async (tmpDir: string) => {
const testCpuFile = `${tmpDir}/cpus-file`;
fs.writeFileSync(testCpuFile, "\n", "utf-8");
t.deepEqual(
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
undefined,
);
});
});

View file

@ -440,7 +440,7 @@ function getCgroupCpuCountFromCpuMax(
/**
* Gets the number of available cores listed in the cgroup cpuset.cpus file at the given path.
*/
function getCgroupCpuCountFromCpus(
export function getCgroupCpuCountFromCpus(
cpusFile: string,
logger: Logger,
): number | undefined {