• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /// Health client talks to health server.
31     /// </summary>
32     public class HealthClientServerTest
33     {
34         const string Host = "localhost";
35         Server server;
36         Channel channel;
37         Grpc.Health.V1.Health.HealthClient client;
38         Grpc.HealthCheck.HealthServiceImpl serviceImpl;
39 
40         [OneTimeSetUp]
Init()41         public void Init()
42         {
43             serviceImpl = new HealthServiceImpl();
44 
45             // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755
46             server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
47             {
48                 Services = { Grpc.Health.V1.Health.BindService(serviceImpl) },
49                 Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
50             };
51             server.Start();
52             channel = new Channel(Host, server.Ports.Single().BoundPort, ChannelCredentials.Insecure);
53 
54             client = new Grpc.Health.V1.Health.HealthClient(channel);
55         }
56 
57         [OneTimeTearDown]
Cleanup()58         public void Cleanup()
59         {
60             channel.ShutdownAsync().Wait();
61 
62             server.ShutdownAsync().Wait();
63         }
64 
65         [Test]
ServiceIsRunning()66         public void ServiceIsRunning()
67         {
68             serviceImpl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving);
69 
70             var response = client.Check(new HealthCheckRequest { Service = "" });
71             Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, response.Status);
72         }
73 
74         [Test]
ServiceDoesntExist()75         public void ServiceDoesntExist()
76         {
77             var ex = Assert.Throws<RpcException>(() => client.Check(new HealthCheckRequest { Service = "nonexistent.service" }));
78             Assert.AreEqual(StatusCode.NotFound, ex.Status.StatusCode);
79         }
80 
81         // TODO(jtattermusch): add test with timeout once timeouts are supported
82     }
83 }
84