Skip to content

Commit

Permalink
Add unit tests for getCgroupCpuCountFromCpus
Browse files Browse the repository at this point in the history
  • Loading branch information
Angela P Wen committed Feb 16, 2024
1 parent ef0a773 commit 8cb81db
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/util.js.map

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions lib/util.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/util.test.js.map

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
});
});
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8cb81db

Please sign in to comment.