mirror of
https://github.com/grpc/grpc-node.git
synced 2025-12-08 18:23:54 +00:00
Updated health check service with new changes
This commit is contained in:
parent
e845408c68
commit
e1eb8bd7ee
@ -45,13 +45,22 @@ function HealthImplementation(statusMap) {
|
||||
this.statusMap = _.clone(statusMap);
|
||||
}
|
||||
|
||||
HealthImplementation.prototype.setStatus = function(service, status) {
|
||||
this.statusMap[service] = status;
|
||||
HealthImplementation.prototype.setStatus = function(host, service, status) {
|
||||
if (!this.statusMap[host]) {
|
||||
this.statusMap[host] = {};
|
||||
}
|
||||
this.statusMap[host][service] = status;
|
||||
};
|
||||
|
||||
HealthImplementation.prototype.check = function(call, callback){
|
||||
var host = call.request.host;
|
||||
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 = {
|
||||
|
||||
@ -32,7 +32,8 @@ syntax = "proto3";
|
||||
package grpc.health.v1alpha;
|
||||
|
||||
message HealthCheckRequest {
|
||||
string service = 1;
|
||||
string host = 1;
|
||||
string service = 2;
|
||||
}
|
||||
|
||||
message HealthCheckResponse {
|
||||
|
||||
@ -40,13 +40,18 @@ var health = require('../health_check/health.js');
|
||||
var grpc = require('../');
|
||||
|
||||
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 = new HealthServer({
|
||||
'grpc.health.v1alpha.Health': new health.Implementation({
|
||||
'': 'SERVING',
|
||||
'grpc.health.v1alpha.Health': 'SERVING',
|
||||
'not.serving.Service': 'NOT_SERVING'
|
||||
})
|
||||
'grpc.health.v1alpha.Health': new health.Implementation(statusMap)
|
||||
});
|
||||
var healthClient;
|
||||
before(function() {
|
||||
@ -57,34 +62,41 @@ describe('Health Checking', function() {
|
||||
after(function() {
|
||||
healthServer.shutdown();
|
||||
});
|
||||
it('should respond with SERVING with no service specified', function(done) {
|
||||
healthClient.check({}, function(err, response) {
|
||||
it('should say an enabled service is SERVING', function(done) {
|
||||
healthClient.check({service: ''}, function(err, response) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(response.status, 'SERVING');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should respond that the health check service is SERVING', function(done) {
|
||||
healthClient.check({service: 'grpc.health.v1alpha.Health'},
|
||||
function(err, response) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(response.status, 'SERVING');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should respond that a disabled service is NOT_SERVING', function(done) {
|
||||
healthClient.check({service: 'not.serving.Service'},
|
||||
it('should say that a disabled service is NOT_SERVING', function(done) {
|
||||
healthClient.check({service: 'grpc.test.TestService'},
|
||||
function(err, response) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(response.status, 'NOT_SERVING');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should respond with UNKNOWN for an unknown service', function(done) {
|
||||
healthClient.check({service: 'unknown.service.Name'},
|
||||
it('should say that a service on another host is SERVING', function(done) {
|
||||
healthClient.check({host: 'virtual_host', service: 'grpc.test.TestService'},
|
||||
function(err, response) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(response.status, 'UNKNOWN');
|
||||
assert.strictEqual(response.status, 'SERVING');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should get NOT_FOUND if the service is not registered', function(done) {
|
||||
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) {
|
||||
assert(err);
|
||||
assert.strictEqual(err.code, grpc.status.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user