Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 3 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 7 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 9 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 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; |
yang-g | e1711624 | 2016-02-19 13:06:37 -0800 | [diff] [blame] | 24 | using Grpc.Health.V1; |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 25 | using NUnit.Framework; |
| 26 | |
| 27 | namespace Grpc.HealthCheck.Tests |
| 28 | { |
| 29 | /// <summary> |
| 30 | /// Health client talks to health server. |
| 31 | /// </summary> |
| 32 | public class HealthClientServerTest |
| 33 | { |
| 34 | const string Host = "localhost"; |
| 35 | Server server; |
| 36 | Channel channel; |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 37 | Grpc.Health.V1.Health.HealthClient client; |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 38 | Grpc.HealthCheck.HealthServiceImpl serviceImpl; |
| 39 | |
Jan Tattermusch | b8c77c5 | 2017-08-09 09:05:54 +0200 | [diff] [blame] | 40 | [OneTimeSetUp] |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 41 | public void Init() |
| 42 | { |
| 43 | serviceImpl = new HealthServiceImpl(); |
| 44 | |
Jan Tattermusch | 09d2f55 | 2017-04-20 11:39:37 +0200 | [diff] [blame] | 45 | // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755 |
| 46 | server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) }) |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 47 | { |
yang-g | e1711624 | 2016-02-19 13:06:37 -0800 | [diff] [blame] | 48 | Services = { Grpc.Health.V1.Health.BindService(serviceImpl) }, |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 49 | Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } } |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 50 | }; |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 51 | server.Start(); |
Jan Tattermusch | 5bd7005 | 2015-10-06 16:47:49 -0700 | [diff] [blame] | 52 | channel = new Channel(Host, server.Ports.Single().BoundPort, ChannelCredentials.Insecure); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 53 | |
Jan Tattermusch | f41ebc3 | 2016-06-22 12:47:14 -0700 | [diff] [blame] | 54 | client = new Grpc.Health.V1.Health.HealthClient(channel); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Jan Tattermusch | b8c77c5 | 2017-08-09 09:05:54 +0200 | [diff] [blame] | 57 | [OneTimeTearDown] |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 58 | public void Cleanup() |
| 59 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 60 | channel.ShutdownAsync().Wait(); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 61 | |
| 62 | server.ShutdownAsync().Wait(); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | [Test] |
| 66 | public void ServiceIsRunning() |
| 67 | { |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 68 | serviceImpl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 69 | |
yang-g | a4598b4 | 2016-02-19 13:30:23 -0800 | [diff] [blame] | 70 | var response = client.Check(new HealthCheckRequest { Service = "" }); |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 71 | Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, response.Status); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | [Test] |
| 75 | public void ServiceDoesntExist() |
| 76 | { |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 77 | var ex = Assert.Throws<RpcException>(() => client.Check(new HealthCheckRequest { Service = "nonexistent.service" })); |
| 78 | Assert.AreEqual(StatusCode.NotFound, ex.Status.StatusCode); |
Jan Tattermusch | 5253467 | 2015-07-14 20:29:21 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // TODO(jtattermusch): add test with timeout once timeouts are supported |
| 82 | } |
David Garcia Quintas | 45484b3 | 2016-01-14 18:59:20 -0800 | [diff] [blame] | 83 | } |