Skip to content

Commit

Permalink
telemetry - stub httpClient, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ngmachado committed Oct 9, 2023
1 parent 5b1e4e5 commit f3e0987
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"node-telegram-bot-api": "^0.61.0",
"prom-client": "^14.2.0",
"sequelize": "^6.32.1",
"sinon": "^15.2.0",
"sqlite3": "^5.1.6",
"web3": "4.0.3",
"winston": "^3.10.0"
Expand All @@ -55,6 +54,7 @@
"hardhat": "^2.17.0",
"husky": "^8.0.3",
"mocha": "^10.2.0",
"sinon": "^15.2.0",
"typechain": "^8.1.0"
},
"husky": {
Expand Down
5 changes: 3 additions & 2 deletions src/services/telemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const UUID_FILE = "data/uuid.txt";
// Implements functionality for creating prometheus formatted reports and sending them to a telemetry endpoint.
class Telemetry {

constructor(app) {
constructor(app, httpClient) {
this.app = app;
this._isShutdown = false;
this.uuid = undefined;
this.httpClient = httpClient === undefined ? require("axios") : httpClient;
}

async start() {
Expand Down Expand Up @@ -39,7 +40,7 @@ class Telemetry {
if(this.app.config.TELEMETRY_URL) {
const reportData = this.createReport(await this.app.healthReport.fullReport());
this.app.logger.info(`sending data to telemetry with uuid ${this.uuid}`);
const resp = await axios({
const resp = await this.httpClient({
method: 'post',
url: this.app.config.TELEMETRY_URL,
data: reportData,
Expand Down
33 changes: 25 additions & 8 deletions test/unit-tests/services/telemetry.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const sinon = require("sinon");
const chai = require("chai");
const fs = require("fs");
const axios = require("axios");
const Telemetry = require("../../../src/services/telemetry");

const { expect } = chai;

describe("Telemetry", () => {
let appMock, telemetry, fsReadStub, fsWriteStub, uuidStub;
let appMock;
let fsReadStub;
let fsWriteStub;
let axiosPostStub;
let telemetry;

beforeEach(() => {
// Mocking app object
axiosPostStub = sinon.stub(axios, 'post').resolves({ data: 'ok' });
fsReadStub = sinon.stub(fs, "readFileSync");
fsWriteStub = sinon.stub(fs, "writeFileSync");
appMock = {
_isShutdown: false,
logger: {
Expand All @@ -21,14 +28,19 @@ describe("Telemetry", () => {
TELEMETRY_URL: "http://fake.telemetry",
},
healthReport: {
fullReport: sinon.stub().resolves({}),
},
fullReport: sinon.stub().resolves({
network: {
chainId: '1',
rpc: { totalRequests: 100 }
},
process: { uptime: 1000 },
healthy: true,
account: { balance: '1000000000000000000' }
})
}
};

fsReadStub = sinon.stub(fs, "readFileSync");
fsWriteStub = sinon.stub(fs, "writeFileSync");

telemetry = new Telemetry(appMock);
telemetry = new Telemetry(appMock, axiosPostStub);
});

afterEach(() => {
Expand All @@ -46,4 +58,9 @@ describe("Telemetry", () => {
await telemetry.start();
expect(fsWriteStub.calledOnceWith("data/uuid.txt", sinon.match.string)).to.be.true;
});

it('should post data to telemetry endpoint', async () => {
await telemetry.start();
expect(axiosPostStub.calledOnce).to.be.true;
});
});

0 comments on commit f3e0987

Please sign in to comment.