From a5b482a8ec1ce77d881024c0de28bd9a02f97959 Mon Sep 17 00:00:00 2001
From: murgatroid99 <mlumish@google.com>
Date: Wed, 8 Jul 2015 15:24:42 -0700
Subject: [PATCH] Updated health check service with new changes

---
 src/node/health_check/health.js    | 15 ++++++++--
 src/node/health_check/health.proto |  3 +-
 src/node/test/health_test.js       | 46 +++++++++++++++++++-----------
 3 files changed, 43 insertions(+), 21 deletions(-)

diff --git a/src/node/health_check/health.js b/src/node/health_check/health.js
index 09a57d38bd..87e00197fe 100644
--- a/src/node/health_check/health.js
+++ b/src/node/health_check/health.js
@@ -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 = {
diff --git a/src/node/health_check/health.proto b/src/node/health_check/health.proto
index 1f6eb6a80d..d31df1e0a7 100644
--- a/src/node/health_check/health.proto
+++ b/src/node/health_check/health.proto
@@ -32,7 +32,8 @@ syntax = "proto3";
 package grpc.health.v1alpha;
 
 message HealthCheckRequest {
-  string service = 1;
+  string host = 1;
+  string service = 2;
 }
 
 message HealthCheckResponse {
diff --git a/src/node/test/health_test.js b/src/node/test/health_test.js
index 456b686b9b..4d1a5082e0 100644
--- a/src/node/test/health_test.js
+++ b/src/node/test/health_test.js
@@ -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'},
+  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, 'SERVING');
+                         assert.strictEqual(response.status, 'NOT_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 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, 'NOT_SERVING');
+                         assert.strictEqual(response.status, 'SERVING');
                          done();
                        });
   });
-  it('should respond with UNKNOWN for an unknown service', function(done) {
-    healthClient.check({service: 'unknown.service.Name'},
+  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.ifError(err);
-                         assert.strictEqual(response.status, 'UNKNOWN');
+                         assert(err);
+                         assert.strictEqual(err.code, grpc.status.NOT_FOUND);
                          done();
                        });
   });
-- 
GitLab