blob: c93b528d42f2a6d259ac95ef408a47a2258cef5e [file] [log] [blame]
murgatroid99d046cc62015-07-06 12:12:27 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34'use strict';
35
36var assert = require('assert');
37
38var health = require('../health_check/health.js');
39
40var grpc = require('../');
41
42describe('Health Checking', function() {
murgatroid99a5b482a2015-07-08 15:24:42 -070043 var statusMap = {
yang-g3cb49e02015-08-21 10:43:02 -070044 '': 'SERVING',
45 'grpc.test.TestServiceNotServing': 'NOT_SERVING',
46 'grpc.test.TestServiceServing': 'SERVING'
murgatroid99a5b482a2015-07-08 15:24:42 -070047 };
murgatroid996fe015e2015-10-12 13:18:06 -070048 var healthServer;
49 var healthImpl;
murgatroid99d046cc62015-07-06 12:12:27 -070050 var healthClient;
51 before(function() {
murgatroid996fe015e2015-10-12 13:18:06 -070052 healthServer = new grpc.Server();
53 healthImpl = new health.Implementation(statusMap);
54 healthServer.addProtoService(health.service, healthImpl);
murgatroid991a7dcac2015-07-27 16:13:28 -070055 var port_num = healthServer.bind('0.0.0.0:0',
56 grpc.ServerCredentials.createInsecure());
murgatroid99366e64d2015-07-15 17:01:49 -070057 healthServer.start();
murgatroid99893690f2015-07-27 14:56:40 -070058 healthClient = new health.Client('localhost:' + port_num,
murgatroid99153b09d2015-09-25 16:04:03 -070059 grpc.credentials.createInsecure());
murgatroid99d046cc62015-07-06 12:12:27 -070060 });
61 after(function() {
murgatroid99cb951f62015-08-18 17:38:11 -070062 healthServer.forceShutdown();
murgatroid99d046cc62015-07-06 12:12:27 -070063 });
murgatroid99a5b482a2015-07-08 15:24:42 -070064 it('should say an enabled service is SERVING', function(done) {
65 healthClient.check({service: ''}, function(err, response) {
murgatroid99d046cc62015-07-06 12:12:27 -070066 assert.ifError(err);
67 assert.strictEqual(response.status, 'SERVING');
68 done();
69 });
70 });
murgatroid99a5b482a2015-07-08 15:24:42 -070071 it('should say that a disabled service is NOT_SERVING', function(done) {
yang-g3cb49e02015-08-21 10:43:02 -070072 healthClient.check({service: 'grpc.test.TestServiceNotServing'},
murgatroid99d046cc62015-07-06 12:12:27 -070073 function(err, response) {
74 assert.ifError(err);
75 assert.strictEqual(response.status, 'NOT_SERVING');
76 done();
77 });
78 });
yang-g3cb49e02015-08-21 10:43:02 -070079 it('should say that an enabled service is SERVING', function(done) {
80 healthClient.check({service: 'grpc.test.TestServiceServing'},
murgatroid99d046cc62015-07-06 12:12:27 -070081 function(err, response) {
82 assert.ifError(err);
murgatroid99a5b482a2015-07-08 15:24:42 -070083 assert.strictEqual(response.status, 'SERVING');
84 done();
85 });
86 });
87 it('should get NOT_FOUND if the service is not registered', function(done) {
88 healthClient.check({service: 'not_registered'}, function(err, response) {
89 assert(err);
90 assert.strictEqual(err.code, grpc.status.NOT_FOUND);
91 done();
92 });
93 });
murgatroid996fe015e2015-10-12 13:18:06 -070094 it('should get a different response if the status changes', function(done) {
95 healthClient.check({service: 'transient'}, function(err, response) {
96 assert(err);
97 assert.strictEqual(err.code, grpc.status.NOT_FOUND);
98 healthImpl.setStatus('transient', 'SERVING');
99 healthClient.check({service: 'transient'}, function(err, response) {
100 assert.ifError(err);
101 assert.strictEqual(response.status, 'SERVING');
102 done();
103 });
104 });
105 });
murgatroid99d046cc62015-07-06 12:12:27 -0700106});