aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/node/test/health_test.js
blob: dc008644d81cc88ccdf1d5fd190e4cc653617bd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

'use strict';

var assert = require('assert');

var health = require('../health_check/health');

var health_messages = require('../health_check/v1/health_pb');

var ServingStatus = health_messages.HealthCheckResponse.ServingStatus;

var grpc = require('../');

describe('Health Checking', function() {
  var statusMap = {
    '': ServingStatus.SERVING,
    'grpc.test.TestServiceNotServing': ServingStatus.NOT_SERVING,
    'grpc.test.TestServiceServing': ServingStatus.SERVING
  };
  var healthServer;
  var healthImpl;
  var healthClient;
  before(function() {
    healthServer = new grpc.Server();
    healthImpl = new health.Implementation(statusMap);
    healthServer.addService(health.service, healthImpl);
    var port_num = healthServer.bind('0.0.0.0:0',
                                     grpc.ServerCredentials.createInsecure());
    healthServer.start();
    healthClient = new health.Client('localhost:' + port_num,
                                     grpc.credentials.createInsecure());
  });
  after(function() {
    healthServer.forceShutdown();
  });
  it('should say an enabled service is SERVING', function(done) {
    var request = new health_messages.HealthCheckRequest();
    request.setService('');
    healthClient.check(request, function(err, response) {
      assert.ifError(err);
      assert.strictEqual(response.getStatus(), ServingStatus.SERVING);
      done();
    });
  });
  it('should say that a disabled service is NOT_SERVING', function(done) {
    var request = new health_messages.HealthCheckRequest();
    request.setService('grpc.test.TestServiceNotServing');
    healthClient.check(request, function(err, response) {
      assert.ifError(err);
      assert.strictEqual(response.getStatus(), ServingStatus.NOT_SERVING);
      done();
    });
  });
  it('should say that an enabled service is SERVING', function(done) {
    var request = new health_messages.HealthCheckRequest();
    request.setService('grpc.test.TestServiceServing');
    healthClient.check(request, function(err, response) {
      assert.ifError(err);
      assert.strictEqual(response.getStatus(), ServingStatus.SERVING);
      done();
    });
  });
  it('should get NOT_FOUND if the service is not registered', function(done) {
    var request = new health_messages.HealthCheckRequest();
    request.setService('not_registered');
    healthClient.check(request, function(err, response) {
      assert(err);
      assert.strictEqual(err.code, grpc.status.NOT_FOUND);
      done();
    });
  });
  it('should get a different response if the status changes', function(done) {
    var request = new health_messages.HealthCheckRequest();
    request.setService('transient');
    healthClient.check(request, function(err, response) {
      assert(err);
      assert.strictEqual(err.code, grpc.status.NOT_FOUND);
      healthImpl.setStatus('transient', ServingStatus.SERVING);
      healthClient.check(request, function(err, response) {
        assert.ifError(err);
        assert.strictEqual(response.getStatus(), ServingStatus.SERVING);
        done();
      });
    });
  });
});