Skip to content
Snippets Groups Projects
Commit a5b482a8 authored by murgatroid99's avatar murgatroid99
Browse files

Updated health check service with new changes

parent 2be6ac0d
No related branches found
No related tags found
No related merge requests found
...@@ -45,13 +45,22 @@ function HealthImplementation(statusMap) { ...@@ -45,13 +45,22 @@ function HealthImplementation(statusMap) {
this.statusMap = _.clone(statusMap); this.statusMap = _.clone(statusMap);
} }
HealthImplementation.prototype.setStatus = function(service, status) { HealthImplementation.prototype.setStatus = function(host, service, status) {
this.statusMap[service] = status; if (!this.statusMap[host]) {
this.statusMap[host] = {};
}
this.statusMap[host][service] = status;
}; };
HealthImplementation.prototype.check = function(call, callback){ HealthImplementation.prototype.check = function(call, callback){
var host = call.request.host;
var service = call.request.service; var service = call.request.service;
callback(null, {status: _.get(this.statusMap, service, 'UNKNOWN')}); var status = _.get(this.statusMap, [host, service], null);
if (status === null) {
callback({code:grpc.status.NOT_FOUND});
} else {
callback(null, {status: status});
}
}; };
module.exports = { module.exports = {
......
...@@ -32,7 +32,8 @@ syntax = "proto3"; ...@@ -32,7 +32,8 @@ syntax = "proto3";
package grpc.health.v1alpha; package grpc.health.v1alpha;
message HealthCheckRequest { message HealthCheckRequest {
string service = 1; string host = 1;
string service = 2;
} }
message HealthCheckResponse { message HealthCheckResponse {
......
...@@ -40,13 +40,18 @@ var health = require('../health_check/health.js'); ...@@ -40,13 +40,18 @@ var health = require('../health_check/health.js');
var grpc = require('../'); var grpc = require('../');
describe('Health Checking', function() { describe('Health Checking', function() {
var statusMap = {
'': {
'': 'SERVING',
'grpc.test.TestService': 'NOT_SERVING',
},
virtual_host: {
'grpc.test.TestService': 'SERVING'
}
};
var HealthServer = grpc.buildServer([health.service]); var HealthServer = grpc.buildServer([health.service]);
var healthServer = new HealthServer({ var healthServer = new HealthServer({
'grpc.health.v1alpha.Health': new health.Implementation({ 'grpc.health.v1alpha.Health': new health.Implementation(statusMap)
'': 'SERVING',
'grpc.health.v1alpha.Health': 'SERVING',
'not.serving.Service': 'NOT_SERVING'
})
}); });
var healthClient; var healthClient;
before(function() { before(function() {
...@@ -57,34 +62,41 @@ describe('Health Checking', function() { ...@@ -57,34 +62,41 @@ describe('Health Checking', function() {
after(function() { after(function() {
healthServer.shutdown(); healthServer.shutdown();
}); });
it('should respond with SERVING with no service specified', function(done) { it('should say an enabled service is SERVING', function(done) {
healthClient.check({}, function(err, response) { healthClient.check({service: ''}, function(err, response) {
assert.ifError(err); assert.ifError(err);
assert.strictEqual(response.status, 'SERVING'); assert.strictEqual(response.status, 'SERVING');
done(); done();
}); });
}); });
it('should respond that the health check service is SERVING', function(done) { it('should say that a disabled service is NOT_SERVING', function(done) {
healthClient.check({service: 'grpc.health.v1alpha.Health'}, healthClient.check({service: 'grpc.test.TestService'},
function(err, response) { function(err, response) {
assert.ifError(err); assert.ifError(err);
assert.strictEqual(response.status, 'SERVING'); assert.strictEqual(response.status, 'NOT_SERVING');
done(); done();
}); });
}); });
it('should respond that a disabled service is NOT_SERVING', function(done) { it('should say that a service on another host is SERVING', function(done) {
healthClient.check({service: 'not.serving.Service'}, healthClient.check({host: 'virtual_host', service: 'grpc.test.TestService'},
function(err, response) { function(err, response) {
assert.ifError(err); assert.ifError(err);
assert.strictEqual(response.status, 'NOT_SERVING'); assert.strictEqual(response.status, 'SERVING');
done(); done();
}); });
}); });
it('should respond with UNKNOWN for an unknown service', function(done) { it('should get NOT_FOUND if the service is not registered', function(done) {
healthClient.check({service: 'unknown.service.Name'}, healthClient.check({service: 'not_registered'}, function(err, response) {
assert(err);
assert.strictEqual(err.code, grpc.status.NOT_FOUND);
done();
});
});
it('should get NOT_FOUND if the host is not registered', function(done) {
healthClient.check({host: 'wrong_host', service: 'grpc.test.TestService'},
function(err, response) { function(err, response) {
assert.ifError(err); assert(err);
assert.strictEqual(response.status, 'UNKNOWN'); assert.strictEqual(err.code, grpc.status.NOT_FOUND);
done(); done();
}); });
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment