murgatroid99 | d046cc6 | 2015-07-06 12:12:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 36 | var assert = require('assert'); |
| 37 | |
| 38 | var health = require('../health_check/health.js'); |
| 39 | |
| 40 | var grpc = require('../'); |
| 41 | |
| 42 | describe('Health Checking', function() { |
murgatroid99 | a5b482a | 2015-07-08 15:24:42 -0700 | [diff] [blame] | 43 | var statusMap = { |
yang-g | 3cb49e0 | 2015-08-21 10:43:02 -0700 | [diff] [blame] | 44 | '': 'SERVING', |
| 45 | 'grpc.test.TestServiceNotServing': 'NOT_SERVING', |
| 46 | 'grpc.test.TestServiceServing': 'SERVING' |
murgatroid99 | a5b482a | 2015-07-08 15:24:42 -0700 | [diff] [blame] | 47 | }; |
murgatroid99 | 6fe015e | 2015-10-12 13:18:06 -0700 | [diff] [blame] | 48 | var healthServer; |
| 49 | var healthImpl; |
murgatroid99 | d046cc6 | 2015-07-06 12:12:27 -0700 | [diff] [blame] | 50 | var healthClient; |
| 51 | before(function() { |
murgatroid99 | 6fe015e | 2015-10-12 13:18:06 -0700 | [diff] [blame] | 52 | healthServer = new grpc.Server(); |
| 53 | healthImpl = new health.Implementation(statusMap); |
| 54 | healthServer.addProtoService(health.service, healthImpl); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 55 | var port_num = healthServer.bind('0.0.0.0:0', |
| 56 | grpc.ServerCredentials.createInsecure()); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 57 | healthServer.start(); |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 58 | healthClient = new health.Client('localhost:' + port_num, |
murgatroid99 | 153b09d | 2015-09-25 16:04:03 -0700 | [diff] [blame] | 59 | grpc.credentials.createInsecure()); |
murgatroid99 | d046cc6 | 2015-07-06 12:12:27 -0700 | [diff] [blame] | 60 | }); |
| 61 | after(function() { |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 62 | healthServer.forceShutdown(); |
murgatroid99 | d046cc6 | 2015-07-06 12:12:27 -0700 | [diff] [blame] | 63 | }); |
murgatroid99 | a5b482a | 2015-07-08 15:24:42 -0700 | [diff] [blame] | 64 | it('should say an enabled service is SERVING', function(done) { |
| 65 | healthClient.check({service: ''}, function(err, response) { |
murgatroid99 | d046cc6 | 2015-07-06 12:12:27 -0700 | [diff] [blame] | 66 | assert.ifError(err); |
| 67 | assert.strictEqual(response.status, 'SERVING'); |
| 68 | done(); |
| 69 | }); |
| 70 | }); |
murgatroid99 | a5b482a | 2015-07-08 15:24:42 -0700 | [diff] [blame] | 71 | it('should say that a disabled service is NOT_SERVING', function(done) { |
yang-g | 3cb49e0 | 2015-08-21 10:43:02 -0700 | [diff] [blame] | 72 | healthClient.check({service: 'grpc.test.TestServiceNotServing'}, |
murgatroid99 | d046cc6 | 2015-07-06 12:12:27 -0700 | [diff] [blame] | 73 | function(err, response) { |
| 74 | assert.ifError(err); |
| 75 | assert.strictEqual(response.status, 'NOT_SERVING'); |
| 76 | done(); |
| 77 | }); |
| 78 | }); |
yang-g | 3cb49e0 | 2015-08-21 10:43:02 -0700 | [diff] [blame] | 79 | it('should say that an enabled service is SERVING', function(done) { |
| 80 | healthClient.check({service: 'grpc.test.TestServiceServing'}, |
murgatroid99 | d046cc6 | 2015-07-06 12:12:27 -0700 | [diff] [blame] | 81 | function(err, response) { |
| 82 | assert.ifError(err); |
murgatroid99 | a5b482a | 2015-07-08 15:24:42 -0700 | [diff] [blame] | 83 | 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 | }); |
murgatroid99 | 6fe015e | 2015-10-12 13:18:06 -0700 | [diff] [blame] | 94 | 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 | }); |
murgatroid99 | d046cc6 | 2015-07-06 12:12:27 -0700 | [diff] [blame] | 106 | }); |