Skip to content

Commit 2310fcb

Browse files
committed
Expose methods on agent for better testability.
1 parent 397b491 commit 2310fcb

File tree

2 files changed

+56
-51
lines changed

2 files changed

+56
-51
lines changed

lib/dashboard-agent.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ const dashboardAgent = () => {
2121
high: 0
2222
};
2323

24-
blocked((delay) => {
24+
const _delayed = (delay) => {
2525
eventLoop.high = Math.max(eventLoop.high, delay);
2626
eventLoop.delay = delay;
27-
}, { threshold: options.blockedThreshold });
27+
};
28+
29+
blocked(_delayed, { threshold: options.blockedThreshold });
2830

29-
const getStats = (cb) => {
31+
const _getStats = (cb) => {
3032
const metrics = {
3133
eventLoop,
3234
mem: {
@@ -55,30 +57,36 @@ const dashboardAgent = () => {
5557
eventLoop.delay = 0;
5658
};
5759

58-
const startPump = () => {
59-
options.intervalId = setInterval(() => {
60-
61-
getStats((err, newMetrics) => {
62-
if (err) {
63-
console.error("Failed to load metrics: ", err); //eslint-disable-line
64-
socket.emit("error", JSON.stringify(err));
65-
} else {
66-
socket.emit("metrics", JSON.stringify(newMetrics));
67-
}
68-
69-
resetEventMetrics();
70-
});
71-
}, options.refreshInterval);
60+
const _emitStats = () => {
61+
_getStats((err, newMetrics) => {
62+
if (err) {
63+
console.error("Failed to load metrics: ", err); //eslint-disable-line
64+
socket.emit("error", JSON.stringify(err));
65+
} else {
66+
socket.emit("metrics", JSON.stringify(newMetrics));
67+
}
68+
69+
resetEventMetrics();
70+
});
7271
};
7372

74-
startPump();
73+
const startPump = () => {
74+
options.intervalId = setInterval(_emitStats, options.refreshInterval);
75+
};
7576

7677
const destroy = () => {
7778
socket.close();
7879
clearInterval(options.intervalId);
7980
};
8081

81-
return { destroy };
82+
startPump();
83+
84+
return {
85+
_delayed,
86+
_getStats,
87+
_emitStats,
88+
destroy
89+
};
8290
};
8391

8492
module.exports = dashboardAgent;

test/lib/dashboard-agent.spec.js

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ const sinon = require("sinon");
66
const SocketIO = require("socket.io");
77
const config = require("../../lib/config");
88
const dashboardAgent = require("../../lib/dashboard-agent");
9-
const _ = require("lodash");
9+
const pusage = require("pidusage");
1010

1111
describe("dashboard-agent", () => {
1212

1313
let server;
14-
let clock;
1514
let agent;
1615
const TEST_PORT = 12345;
17-
const REPORTING_THRESHOLD = 2000;
16+
const REPORTING_THRESHOLD = 1500;
1817

1918
before(() => {
20-
clock = sinon.useFakeTimers();
2119
process.env[config.PORT_KEY] = TEST_PORT;
2220
process.env[config.BLOCKED_THRESHOLD_KEY] = 1;
23-
2421
});
2522

2623
beforeEach(() => {
@@ -34,15 +31,23 @@ describe("dashboard-agent", () => {
3431
});
3532

3633
describe("initialization", () => {
34+
let clock;
35+
before(() => {
36+
clock = sinon.useFakeTimers();
37+
});
3738

38-
it("should use environment variables for configuration", (done) => {
39+
after(() => {
40+
clock.restore();
41+
});
3942

43+
it("should use environment variables for configuration", (done) => {
4044
const checkMetrics = (metrics) => {
4145
expect(metrics).to.be.exist;
4246
expect(metrics.eventLoop.delay).to.equal(0);
4347
};
4448

4549
clock.tick(REPORTING_THRESHOLD);
50+
4651
server.on("connection", (socket) => {
4752
expect(socket).to.be.defined;
4853
socket.on("error", done);
@@ -68,40 +73,32 @@ describe("dashboard-agent", () => {
6873
expect(metrics.cpu.utilization).to.be.above(0);
6974
};
7075

71-
clock.tick(REPORTING_THRESHOLD);
72-
server.on("connection", (socket) => {
73-
socket.on("error", done);
74-
socket.on("metrics", (data) => {
75-
checkMetrics(JSON.parse(data));
76-
done();
77-
});
76+
agent._getStats((err, metrics) => {
77+
expect(err).to.be.null;
78+
checkMetrics(metrics);
79+
done();
7880
});
7981
});
8082

81-
it("should report an event loop delay", (done) => {
82-
clock.restore();
83-
agent.destroy();
84-
agent = dashboardAgent();
85-
86-
const slowFunc = () => {
87-
const count = 10000;
88-
let values = _.times(count, () => _.random(0, count));
83+
it("should report an event loop delay and cpu stats", (done) => {
84+
const delay = { current: 100, max: 150 };
85+
const pusageResults = { cpu: 50 };
86+
const pidStub = sinon.stub(pusage, "stat").yields(null, pusageResults);
8987

90-
values = _.sortBy(values);
91-
};
88+
agent._delayed(delay.max);
89+
agent._delayed(delay.current);
9290

9391
const checkMetrics = (metrics) => {
94-
expect(metrics.eventLoop.delay).to.be.above(0);
95-
expect(metrics.eventLoop.high).to.be.above(0);
92+
expect(metrics.eventLoop.delay).to.equal(delay.current);
93+
expect(metrics.eventLoop.high).to.be.equal(delay.max);
94+
expect(metrics.cpu.utilization).to.equal(pusageResults.cpu);
9695
};
9796

98-
slowFunc();
99-
server.on("connection", (socket) => {
100-
socket.on("error", (err) => done(err));
101-
socket.on("metrics", (data) => {
102-
checkMetrics(JSON.parse(data));
103-
done();
104-
});
97+
agent._getStats((err, metrics) => {
98+
expect(err).to.be.null;
99+
checkMetrics(metrics);
100+
pidStub.restore();
101+
done();
105102
});
106103
});
107104
});

0 commit comments

Comments
 (0)