blob: 45e8ad90623b9a2994d2d32aae5766619d09ec58 [file] [log] [blame]
Jan Tattermusch52534672015-07-14 20:29:21 -07001#region Copyright notice and license
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002// Copyright 2015 gRPC authors.
Jan Tattermusch52534672015-07-14 20:29:21 -07003//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004// 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 Tattermusch52534672015-07-14 20:29:21 -07007//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch52534672015-07-14 20:29:21 -07009//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010// 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 Tattermusch52534672015-07-14 20:29:21 -070015#endregion
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Threading.Tasks;
22
23using Grpc.Core;
yang-ge17116242016-02-19 13:06:37 -080024using Grpc.Health.V1;
Jan Tattermusch52534672015-07-14 20:29:21 -070025using NUnit.Framework;
26
27namespace 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 Tattermusch809148d2016-03-22 15:09:41 -070037 Grpc.Health.V1.Health.HealthClient client;
Jan Tattermusch52534672015-07-14 20:29:21 -070038 Grpc.HealthCheck.HealthServiceImpl serviceImpl;
39
Jan Tattermuschb8c77c52017-08-09 09:05:54 +020040 [OneTimeSetUp]
Jan Tattermusch52534672015-07-14 20:29:21 -070041 public void Init()
42 {
43 serviceImpl = new HealthServiceImpl();
44
Jan Tattermusch09d2f552017-04-20 11:39:37 +020045 // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755
46 server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
Jan Tattermusch021df8a2015-08-04 20:31:11 -070047 {
yang-ge17116242016-02-19 13:06:37 -080048 Services = { Grpc.Health.V1.Health.BindService(serviceImpl) },
Jan Tattermusch31ba0632015-08-04 22:02:55 -070049 Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
Jan Tattermusch021df8a2015-08-04 20:31:11 -070050 };
Jan Tattermusch52534672015-07-14 20:29:21 -070051 server.Start();
Jan Tattermusch5bd70052015-10-06 16:47:49 -070052 channel = new Channel(Host, server.Ports.Single().BoundPort, ChannelCredentials.Insecure);
Jan Tattermusch52534672015-07-14 20:29:21 -070053
Jan Tattermuschf41ebc32016-06-22 12:47:14 -070054 client = new Grpc.Health.V1.Health.HealthClient(channel);
Jan Tattermusch52534672015-07-14 20:29:21 -070055 }
56
Jan Tattermuschb8c77c52017-08-09 09:05:54 +020057 [OneTimeTearDown]
Jan Tattermusch52534672015-07-14 20:29:21 -070058 public void Cleanup()
59 {
Jan Tattermusch2b357952015-08-20 14:54:33 -070060 channel.ShutdownAsync().Wait();
Jan Tattermusch52534672015-07-14 20:29:21 -070061
62 server.ShutdownAsync().Wait();
Jan Tattermusch52534672015-07-14 20:29:21 -070063 }
64
65 [Test]
66 public void ServiceIsRunning()
67 {
Jan Tattermusch87ba2942016-05-16 17:18:00 -070068 serviceImpl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving);
Jan Tattermusch52534672015-07-14 20:29:21 -070069
yang-ga4598b42016-02-19 13:30:23 -080070 var response = client.Check(new HealthCheckRequest { Service = "" });
Jan Tattermusch87ba2942016-05-16 17:18:00 -070071 Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.Serving, response.Status);
Jan Tattermusch52534672015-07-14 20:29:21 -070072 }
73
74 [Test]
75 public void ServiceDoesntExist()
76 {
Jan Tattermusch87ba2942016-05-16 17:18:00 -070077 var ex = Assert.Throws<RpcException>(() => client.Check(new HealthCheckRequest { Service = "nonexistent.service" }));
78 Assert.AreEqual(StatusCode.NotFound, ex.Status.StatusCode);
Jan Tattermusch52534672015-07-14 20:29:21 -070079 }
80
81 // TODO(jtattermusch): add test with timeout once timeouts are supported
82 }
David Garcia Quintas45484b32016-01-14 18:59:20 -080083}