Skip to content

Commit

Permalink
added tests for check handler, all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
iangeckeler committed Aug 9, 2019
1 parent 78d91e2 commit 9c1edf9
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 25 deletions.
28 changes: 8 additions & 20 deletions __tests__/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var testProto = protoDescriptor.test;

const testService = testProto.Test;

testService._serviceName = 'Test';
testService._serviceName = "Test";

xdescribe("Unit tests for Server", () => {
it("Constructor extends the grpc server class.", () => {
Expand Down Expand Up @@ -130,29 +130,17 @@ xdescribe("Uncaught Error Handling.", () => {
describe("Server tests for health check", () => {
const server = new Server();

it("Has a health check Service.",()=>{

});

it("getStatus method returns the full status map if not passed params")

it("getStatus method returns an error if passed params",()={

})

it("getStatus method returns specific status",()=>{
it("Has a health check Service.", () => {});

})
it("getStatus method returns the full status map if not passed params");

it("Health check implementation handler takes in call and calls getStatus",()=>{
it("getStatus method returns null if passed params", () => {});

})
it("getStatus method returns specific status", () => {});

it("setStatus method alters status map",()=>{

})
it("Health check implementation handler takes in call and calls getStatus", () => {});

it("Server adds health check automatically", ()=>{
it("setStatus method alters status map", () => {});

})
it("Server adds health check automatically", () => {});
});
61 changes: 61 additions & 0 deletions __tests__/custom-services/healthcheck-handlers/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const checkHandler = require("../../../lib/custom-services/healthcheck-handlers/check");

const _statusMap = {
service1: "serving",
service2: "notserving"
};

const mockSend = jest.fn();
const mockThrow = jest.fn();
const mockGetStatus = jest.fn(x => {
if (!x) {
return mockServer._statusMap;
}
return mockServer._statusMap[x];
});

const mockServer = {
_statusMap,
getStatus: mockGetStatus,
check: checkHandler
};

function callBuilder(payload) {
return {
body: payload,
send: mockSend,
throw: mockThrow
};
}

describe("Tests for health check handler", () => {
beforeEach(() => {
mockSend.mockClear();
mockThrow.mockClear();
});

it("Passes back whole status map if no services passed", () => {
mockServer.check(callBuilder({ services: [] }));
expect(mockSend.mock.calls[0][0].hasOwnProperty("health")).toBeTruthy();
expect(
mockSend.mock.calls[0][0]["health"][0].service === "service1" ||
mockSend.mock.calls[0][0]["health"][0].service === "service2"
).toBeTruthy();
});

it("uses getStatus method on Server", () => {
mockServer.check(callBuilder({ services: ["service1"] }));
expect(mockGetStatus.mock.calls.length).toBeGreaterThanOrEqual(1);
});

it("handles if specific service name passed", () => {
mockServer.check(callBuilder({ services: ["service1"] }));
expect(mockSend.mock.calls[0][0].hasOwnProperty("health")).toBeTruthy();
expect(mockSend.mock.calls[0][0]["health"].length).toEqual(1);
});

it("emits an error if wrong service name passed", () => {
mockServer.check(callBuilder({ services: ["service3"] }));
expect(mockThrow.mock.calls[0][0]).toBeInstanceOf(Error);
});
});
11 changes: 8 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,18 @@ server.addService(
this._statusMap = status;
}

/**
*
* @param {string=} serviceName
* If serviceName not provided, returns whole status map.
* If serviceName passed, returns the status of that service or null if it is not found in the statusMap
*
*/
getStatus(serviceName) {
if (serviceName === undefined) {
return { ...this._statusMap };
} else if (!this._statusMap.hasOwnProperty(serviceName)) {
throw new Error(
"Status map does not have the service name you requested."
);
return null;
} else {
return this._statusMap[serviceName];
}
Expand Down
35 changes: 33 additions & 2 deletions lib/custom-services/healthcheck-handlers/check.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
module.exports = function(call) {
console.log(call.body);
console.log("this inside of check", this);
const { services: requestedServices } = call.body;
const healthArray = [];
if (!requestedServices.length) {
const statusMap = this.getStatus();
const services = Object.keys(statusMap);
for (let i = 0; i < services.length; i++) {
healthArray.push({
service: services[i],
status: statusMap[services[i]]
});
}
return call.send({ health: healthArray });
}
// loop through statusMap append the ones in the services array
// if at any point it is not possible
for (let i = 0; i < requestedServices.length; i++) {
const curStatus = this.getStatus(requestedServices[i]);
if (curStatus === null || curStatus === undefined) {
call.throw(
new Error(
"Requested a health check for service not in Server statusMap."
)
);
}
healthArray.push({
service: requestedServices,
status: curStatus
});
}
return call.send({
health: healthArray
});
// return health array
};

0 comments on commit 9c1edf9

Please sign in to comment.