Skip to content

Commit

Permalink
modified health check .proto to fit new implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
iangeckeler committed Aug 9, 2019
1 parent 04faf84 commit 29575a4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
38 changes: 35 additions & 3 deletions __tests__/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ var testProto = protoDescriptor.test;

const testService = testProto.Test;

describe("Unit tests for Server", () => {
testService._serviceName = 'Test';

xdescribe("Unit tests for Server", () => {
it("Constructor extends the grpc server class.", () => {
const server = new Server();
expect(server instanceof grpc.Server).toBeTruthy();
Expand Down Expand Up @@ -78,15 +80,15 @@ describe("Unit tests for Server", () => {
});
});

describe("Unit tests for bind.", () => {
xdescribe("Unit tests for bind.", () => {
it("Bind should support a single port insecurely if no config supplied.", () => {
const server = new Server();
const boundPorts = server.bind("0.0.0.0:3000");
expect(boundPorts[0]).toBeGreaterThanOrEqual(0);
});
});

describe("Uncaught Error Handling.", () => {
xdescribe("Uncaught Error Handling.", () => {
it("Server level error handling should receive error and context object", () => {
const mockErrorHandler = jest.fn();
const server = new Server(mockErrorHandler);
Expand Down Expand Up @@ -124,3 +126,33 @@ describe("Uncaught Error Handling.", () => {
expect(typeof mockErrorHandler.mock.calls[0][1] === "object").toBeTruthy();
});
});

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("Health check implementation handler takes in call and calls getStatus",()=>{

})

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

})

it("Server adds health check automatically", ()=>{

})
});
25 changes: 24 additions & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = class Server extends grpc.Server {
constructor(/** @type {function} */ uncaughtErrorHandler) {
super();
this.uncaughtErrorHandler = uncaughtErrorHandler;
this._statusMap = new Map();
}
/**
* Connects handlers and middleware functionality to your gRPC methods * as defined in your `.proto` file.
Expand Down Expand Up @@ -81,7 +82,8 @@ server.addService(
} else {
service = serviceDefinition;
}
// console.log(service);
// add to the status map
this._statusMap.set(service._serviceName, "SERVING");

// handlerObject ~= { unaryChat: [waitFor, unaryChat], serverStream,
// clientStream, bidiChat }
Expand Down Expand Up @@ -121,6 +123,27 @@ server.addService(
// console.log(this instanceof grpc.Server)
}

setStatus(serviceName, status) {
if (!this._statusMap.hasOwnProperty(serviceName)) {
throw new Error(
"Status map does not have the service name you requested."
);
}
this._statusMap = status;
}

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."
);
} else {
return this._statusMap[serviceName];
}
}

/**
* Invoke this method to connect your server instance to a channel.
* Allowsfor the creation of secure and insecure channels.
Expand Down
3 changes: 0 additions & 3 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ module.exports = function build(
default:
const packageDefinition = protoLoader.loadSync(PROTO_PATH, config);
const pkg = grpc.loadPackageDefinition(packageDefinition);
// console.log(Object.keys(pkg[Object.keys(pkg)[0]]));
// tag all services with hidden property
// iterate through all services and add the hidden property _servicename to them
const serviceKeys = Object.keys(pkg[Object.keys(pkg)[0]]);
for (let i = 0; i < serviceKeys.length; i++) {
console.log(serviceKeys[i]);
Expand Down
5 changes: 3 additions & 2 deletions lib/custom-services/healthcheck-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ const grpc = require("grpc");
const protoLoader = require("../build");
const PROTO_PATH = path.join(__dirname, "./healthcheck.proto");

const healthcheck = build(PROTO_PATH, {enums: Object});
module.exports = healthcheck;
const healthcheck = build(PROTO_PATH, { enums: Object });
console.log(healthcheck);
module.exports = healthcheck;
18 changes: 7 additions & 11 deletions lib/custom-services/healthcheck.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ syntax = "proto3";
package healthcheck;

service Health {
rpc Check(HealthCheckRequest) returns (HealthCheckResponse);
rpc Check(HealthCheckRequest) returns (repeated HealthCheckResponse);

rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse);
rpc Watch(HealthCheckRequest) returns (stream repeated HealthCheckResponse);
}

message HealthCheckRequest {
message HealthCheckRequest { repeated string services = 1; }

message Status {
string service = 1;
string status = 2;
}

message HealthCheckResponse {
enum ServingStatus {
UNKNOWN = 0;
SERVING = 1;
NOT_SERVING = 2;
}
ServingStatus status = 1;
}
message HealthCheckResponse { repeated Status health = 1; }

0 comments on commit 29575a4

Please sign in to comment.