Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Copyright 2015, Google Inc. |
| 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; |
| 39 | using Grpc.Health.V1Alpha; |
| 40 | using NUnit.Framework; |
| 41 | |
| 42 | namespace Grpc.HealthCheck.Tests |
| 43 | { |
| 44 | /// <summary> |
| 45 | /// Health client talks to health server. |
| 46 | /// </summary> |
| 47 | public class HealthClientServerTest |
| 48 | { |
| 49 | const string Host = "localhost"; |
| 50 | Server server; |
| 51 | Channel channel; |
| 52 | Grpc.Health.V1Alpha.Health.IHealthClient client; |
| 53 | Grpc.HealthCheck.HealthServiceImpl serviceImpl; |
| 54 | |
| 55 | [TestFixtureSetUp] |
| 56 | public void Init() |
| 57 | { |
| 58 | serviceImpl = new HealthServiceImpl(); |
| 59 | |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 60 | server = new Server |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 61 | { |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 62 | Services = { Grpc.Health.V1Alpha.Health.BindService(serviceImpl) }, |
| 63 | Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } } |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 64 | }; |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 65 | server.Start(); |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 66 | channel = new Channel(Host, server.Ports.Single().BoundPort, Credentials.Insecure); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 67 | |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 68 | client = Grpc.Health.V1Alpha.Health.NewClient(channel); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | [TestFixtureTearDown] |
| 72 | public void Cleanup() |
| 73 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 74 | channel.ShutdownAsync().Wait(); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 75 | |
| 76 | server.ShutdownAsync().Wait(); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | [Test] |
| 80 | public void ServiceIsRunning() |
| 81 | { |
| 82 | serviceImpl.SetStatus("", "", HealthCheckResponse.Types.ServingStatus.SERVING); |
| 83 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 84 | var response = client.Check(new HealthCheckRequest { Host = "", Service = "" }); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 85 | Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.SERVING, response.Status); |
| 86 | } |
| 87 | |
| 88 | [Test] |
| 89 | public void ServiceDoesntExist() |
| 90 | { |
Jan Tattermusch | 67c4587 | 2015-08-27 18:12:39 -0700 | [diff] [blame] | 91 | Assert.Throws(Is.TypeOf(typeof(RpcException)).And.Property("Status").Property("StatusCode").EqualTo(StatusCode.NotFound), () => client.Check(new HealthCheckRequest { Host = "", Service = "nonexistent.service" })); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // TODO(jtattermusch): add test with timeout once timeouts are supported |
| 95 | } |
| 96 | } |