-
-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #389 from faeron/379-cpu-usage
reject promise if no cpu data is returned fix #379
- Loading branch information
Showing
2 changed files
with
80 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"use strict"; | ||
|
||
const os = require('os'); | ||
jest.mock('os'); | ||
jest.useFakeTimers(); | ||
|
||
const getCpuUsage = require('../../src/cpu-usage'); | ||
|
||
describe('getCpuUsage', () => { | ||
it('should report cpu usage', () => { | ||
os.cpus = jest.fn() | ||
.mockImplementationOnce(() => [{ | ||
times: { | ||
user: 1, | ||
nice: 2, | ||
sys: 3, | ||
idle: 4, | ||
irq: 5, | ||
} | ||
}]) | ||
.mockImplementationOnce(() => [{ | ||
times: { | ||
user: 2, | ||
nice: 3, | ||
sys: 4, | ||
idle: 5, | ||
irq: 6 | ||
} | ||
}]) | ||
.mockImplementationOnce(() => [{ | ||
times: { | ||
user: 3, | ||
nice: 3, | ||
sys: 3, | ||
idle: 3, | ||
irq: 3 | ||
} | ||
}]); | ||
|
||
|
||
const result = getCpuUsage(100); | ||
jest.runAllTimers(); | ||
return expect(result).resolves.toEqual({ avg: 70, usages: [70] }); | ||
}); | ||
|
||
it('should return rejected promise on missing cpu data', () => { | ||
os.cpus = jest.fn().mockImplementationOnce(() => undefined); | ||
|
||
const result = getCpuUsage(100); | ||
jest.runAllTimers(); | ||
return expect(result).rejects.toBeUndefined(); | ||
}); | ||
}); |