Skip to content

Commit

Permalink
test: monitoring coverage fix
Browse files Browse the repository at this point in the history
  • Loading branch information
this-is-shivamsingh committed Jan 26, 2025
1 parent b9524e3 commit e498058
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/monitoring/src/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ async function getCPULoad() {
// ideally it shouldn't happen
if (deltaTick > 0) {
cpuUsagePercent = (1 - deltaIdle / deltaTick) * 100;
}
} else cpuUsagePercent = 0;

let cores = await getTotalCores();
return {
Expand Down
35 changes: 32 additions & 3 deletions packages/monitoring/test/cpu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ describe('getCPULoadInfo: Linux', () => {

describe('when c_group exists', () => {
let cpuMax, cpuStatCount;

// test_key is given for a coverage
// condition either of key or value if not
// present then don't consider it
let cpuStatsBefore = `usage_usec 330229677740
user_usec 206352296619
system_usec 122171004028
nr_periods 0
test_key
nr_throttled 0
throttled_usec 0`;
let cpuStatsAfter = `usage_usec 330229777740
Expand Down Expand Up @@ -42,9 +47,9 @@ describe('getCPULoadInfo: Linux', () => {
if (path === '/sys/fs/cgroup/cpu.stat') {
let res = (cpuStatCount === 0) ? cpuStatsBefore : cpuStatsAfter;
cpuStatCount++;
return Promise.resolve(res); // Mocked cpu.stat content
return Promise.resolve(res);
}
return Promise.reject(new Error(`File not readable: ${path}`)); // Default
return Promise.reject(new Error(`File not readable: ${path}`));
});

mockOs = spyOn(os, 'cpus').and.callThrough();
Expand Down Expand Up @@ -94,11 +99,35 @@ describe('getCPULoadInfo: Linux', () => {
});
});

describe('when cpu usage_usec do not changed', () => {
beforeEach(() => {
cpuMax = '3500000 1000000';
mockFsRead = spyOn(fs, 'readFile').and.callFake((path) => {
if (path === '/sys/fs/cgroup/cpu.max') {
return Promise.resolve(cpuMax);
}
if (path === '/sys/fs/cgroup/cpu.stat') {
return Promise.resolve(cpuStatsBefore);
}
return Promise.reject(new Error(`File not readable: ${path}`));
});
});

it('return cpu usage as 0%', async () => {
const cpuInfo = await getCPULoadInfo(platform);
expect(cpuInfo).toEqual({
cores: 3.5,
currentUsagePercent: 0,
cgroupExists: true
});
});
});

describe('throws unexpected error', () => {
beforeEach(() => {
mockFsRead = spyOn(fs, 'readFile').and.callFake((path) => {
if (path === '/sys/fs/cgroup/cpu.max') {
return Promise.resolve(cpuMax); // Mocked cpu.max content
return Promise.resolve(undefined);
}

if (path === '/sys/fs/cgroup/cpu.stat') {
Expand Down

0 comments on commit e498058

Please sign in to comment.