Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
yang-g | e171162 | 2016-02-19 13:06:37 -0800 | [diff] [blame] | 2 | // Copyright 2015-2016, Google Inc. |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | #endregion |
| 31 | |
| 32 | using System; |
| 33 | using System.Collections.Generic; |
| 34 | using System.Linq; |
| 35 | using System.Text; |
| 36 | using System.Threading.Tasks; |
| 37 | |
| 38 | using Grpc.Core; |
yang-g | e171162 | 2016-02-19 13:06:37 -0800 | [diff] [blame] | 39 | using Grpc.Health.V1; |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 40 | using NUnit.Framework; |
| 41 | |
| 42 | namespace Grpc.HealthCheck.Tests |
| 43 | { |
| 44 | /// <summary> |
| 45 | /// Tests for HealthCheckServiceImpl |
| 46 | /// </summary> |
| 47 | public class HealthServiceImplTest |
| 48 | { |
| 49 | [Test] |
| 50 | public void SetStatus() |
| 51 | { |
| 52 | var impl = new HealthServiceImpl(); |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 53 | impl.SetStatus("", HealthCheckResponse.Types.ServingStatus.SERVING); |
| 54 | Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.SERVING, GetStatusHelper(impl, "")); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 55 | |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 56 | impl.SetStatus("", HealthCheckResponse.Types.ServingStatus.NOT_SERVING); |
| 57 | Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.NOT_SERVING, GetStatusHelper(impl, "")); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 58 | |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 59 | impl.SetStatus("", HealthCheckResponse.Types.ServingStatus.UNKNOWN); |
| 60 | Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.UNKNOWN, GetStatusHelper(impl, "")); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 61 | |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 62 | impl.SetStatus("grpc.test.TestService", HealthCheckResponse.Types.ServingStatus.SERVING); |
| 63 | Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.SERVING, GetStatusHelper(impl, "grpc.test.TestService")); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | [Test] |
| 67 | public void ClearStatus() |
| 68 | { |
| 69 | var impl = new HealthServiceImpl(); |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 70 | impl.SetStatus("", HealthCheckResponse.Types.ServingStatus.SERVING); |
| 71 | impl.SetStatus("grpc.test.TestService", HealthCheckResponse.Types.ServingStatus.UNKNOWN); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 72 | |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 73 | impl.ClearStatus(""); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 74 | |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 75 | Assert.Throws(Is.TypeOf(typeof(RpcException)).And.Property("Status").Property("StatusCode").EqualTo(StatusCode.NotFound), () => GetStatusHelper(impl, "")); |
| 76 | Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.UNKNOWN, GetStatusHelper(impl, "grpc.test.TestService")); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | [Test] |
| 80 | public void ClearAll() |
| 81 | { |
| 82 | var impl = new HealthServiceImpl(); |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 83 | impl.SetStatus("", HealthCheckResponse.Types.ServingStatus.SERVING); |
| 84 | impl.SetStatus("grpc.test.TestService", HealthCheckResponse.Types.ServingStatus.UNKNOWN); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 85 | |
| 86 | impl.ClearAll(); |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 87 | Assert.Throws(typeof(RpcException), () => GetStatusHelper(impl, "")); |
| 88 | Assert.Throws(typeof(RpcException), () => GetStatusHelper(impl, "grpc.test.TestService")); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | [Test] |
| 92 | public void NullsRejected() |
| 93 | { |
| 94 | var impl = new HealthServiceImpl(); |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 95 | Assert.Throws(typeof(ArgumentNullException), () => impl.SetStatus(null, HealthCheckResponse.Types.ServingStatus.SERVING)); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 96 | |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 97 | Assert.Throws(typeof(ArgumentNullException), () => impl.ClearStatus(null)); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 98 | } |
| 99 | |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 100 | private static HealthCheckResponse.Types.ServingStatus GetStatusHelper(HealthServiceImpl impl, string service) |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 101 | { |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 102 | return impl.Check(new HealthCheckRequest { Service = service }, null).Result.Status; |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |