-
Notifications
You must be signed in to change notification settings - Fork 63
/
memory.spec.ts
72 lines (58 loc) · 2.08 KB
/
memory.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { expect } from 'chai';
import type { SinonStub } from 'sinon';
import { stub } from 'sinon';
import * as process from 'process';
import * as memory from '~/src/memory';
import * as deviceState from '~/src/device-state';
import log from '~/lib/supervisor-console';
describe('memory.healthcheck', () => {
let uptimeStub: SinonStub;
let rssStub: SinonStub;
let isApplyInProgressStub: SinonStub;
beforeEach(() => {
uptimeStub = stub(memory, 'processUptime').returns(20);
rssStub = stub(process.memoryUsage, 'rss').returns(100);
isApplyInProgressStub = stub(deviceState, 'isApplyInProgress').returns(
false,
);
});
afterEach(() => {
uptimeStub.restore();
rssStub.restore();
isApplyInProgressStub.restore();
});
it('passes healthcheck if process has not been running for 20s', async () => {
// @ts-expect-error - initialMemory is read-only
memory.initialMemory = 0;
uptimeStub.returns(19);
expect(await memory.healthcheck()).to.be.true;
});
it('passes healthcheck while initial memory not set and sets initial memory', async () => {
// @ts-expect-error - initialMemory is read-only
memory.initialMemory = 0;
expect(await memory.healthcheck()).to.be.true;
expect(memory.initialMemory).to.equal(100);
});
it('passes healthcheck while state apply in progress', async () => {
// @ts-expect-error - initialMemory is read-only
memory.initialMemory = 100;
isApplyInProgressStub.returns(true);
expect(await memory.healthcheck()).to.be.true;
});
it('passes healthcheck if memory usage is below threshold', async () => {
// @ts-expect-error - initialMemory is read-only
memory.initialMemory = 100;
rssStub.returns(150);
expect(await memory.healthcheck(100)).to.be.true;
});
it('fails healthcheck if memory usage is above threshold', async () => {
// @ts-expect-error - initialMemory is read-only
memory.initialMemory = 100;
uptimeStub.returns(61);
rssStub.returns(250);
expect(await memory.healthcheck(100)).to.be.false;
expect(log.info).to.have.been.calledWith(
`Healthcheck failure - memory usage above threshold after 0h 1m 1s`,
);
});
});