1 #region Copyright notice and license 2 // Copyright 2015 gRPC authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 #endregion 16 17 using System; 18 using System.Collections.Generic; 19 using System.Linq; 20 using System.Text; 21 using System.Threading.Tasks; 22 23 using Grpc.Core; 24 using Grpc.Health.V1; 25 using NUnit.Framework; 26 27 namespace Grpc.HealthCheck.Tests 28 { 29 /// <summary> 30 /// Tests for HealthCheckServiceImpl 31 /// </summary> 32 public class HealthServiceImplTest 33 { 34 [Test] SetStatus()35 public void SetStatus() 36 { 37 var impl = new HealthServiceImpl(); 38 impl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving); 39 Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, GetStatusHelper(impl, "")); 40 41 impl.SetStatus("", HealthCheckResponse.Types.ServingStatus.NotServing); 42 Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.NotServing, GetStatusHelper(impl, "")); 43 44 impl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Unknown); 45 Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Unknown, GetStatusHelper(impl, "")); 46 47 impl.SetStatus("grpc.test.TestService", HealthCheckResponse.Types.ServingStatus.Serving); 48 Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, GetStatusHelper(impl, "grpc.test.TestService")); 49 } 50 51 [Test] ClearStatus()52 public void ClearStatus() 53 { 54 var impl = new HealthServiceImpl(); 55 impl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving); 56 impl.SetStatus("grpc.test.TestService", HealthCheckResponse.Types.ServingStatus.Unknown); 57 58 impl.ClearStatus(""); 59 60 var ex = Assert.Throws<RpcException>(() => GetStatusHelper(impl, "")); 61 Assert.AreEqual(StatusCode.NotFound, ex.Status.StatusCode); 62 Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Unknown, GetStatusHelper(impl, "grpc.test.TestService")); 63 } 64 65 [Test] ClearAll()66 public void ClearAll() 67 { 68 var impl = new HealthServiceImpl(); 69 impl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving); 70 impl.SetStatus("grpc.test.TestService", HealthCheckResponse.Types.ServingStatus.Unknown); 71 72 impl.ClearAll(); 73 Assert.Throws(typeof(RpcException), () => GetStatusHelper(impl, "")); 74 Assert.Throws(typeof(RpcException), () => GetStatusHelper(impl, "grpc.test.TestService")); 75 } 76 77 [Test] NullsRejected()78 public void NullsRejected() 79 { 80 var impl = new HealthServiceImpl(); 81 Assert.Throws(typeof(ArgumentNullException), () => impl.SetStatus(null, HealthCheckResponse.Types.ServingStatus.Serving)); 82 83 Assert.Throws(typeof(ArgumentNullException), () => impl.ClearStatus(null)); 84 } 85 GetStatusHelper(HealthServiceImpl impl, string service)86 private static HealthCheckResponse.Types.ServingStatus GetStatusHelper(HealthServiceImpl impl, string service) 87 { 88 return impl.Check(new HealthCheckRequest { Service = service }, null).Result.Status; 89 } 90 } 91 } 92