blob: 73ff0e74b57daeeb87b9fd4563b6efaf2bf71281 [file] [log] [blame]
Jan Tattermusch52534672015-07-14 20:29:21 -07001#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
32using System;
33using System.Collections.Generic;
34using System.Linq;
35using System.Text;
36using System.Threading.Tasks;
37
38using Grpc.Core;
39using Grpc.Health.V1Alpha;
40using NUnit.Framework;
41
42namespace 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
60 server = new Server();
61 server.AddServiceDefinition(Grpc.Health.V1Alpha.Health.BindService(serviceImpl));
62 int port = server.AddListeningPort(Host, Server.PickUnusedPort);
63 server.Start();
64 channel = new Channel(Host, port);
65
Jan Tattermuschb5332812015-07-14 19:29:35 -070066 client = Grpc.Health.V1Alpha.Health.NewClient(channel);
Jan Tattermusch52534672015-07-14 20:29:21 -070067 }
68
69 [TestFixtureTearDown]
70 public void Cleanup()
71 {
72 channel.Dispose();
73
74 server.ShutdownAsync().Wait();
75 GrpcEnvironment.Shutdown();
76 }
77
78 [Test]
79 public void ServiceIsRunning()
80 {
81 serviceImpl.SetStatus("", "", HealthCheckResponse.Types.ServingStatus.SERVING);
82
83 var response = client.Check(HealthCheckRequest.CreateBuilder().SetHost("").SetService("").Build());
84 Assert.AreEqual(HealthCheckResponse.Types.ServingStatus.SERVING, response.Status);
85 }
86
87 [Test]
88 public void ServiceDoesntExist()
89 {
90 // TODO(jtattermusch): currently, this returns wrong status code, because we don't enable sending arbitrary status code from
91 // server handlers yet.
92 Assert.Throws(typeof(RpcException), () => client.Check(HealthCheckRequest.CreateBuilder().SetHost("").SetService("nonexistent.service").Build()));
93 }
94
95 // TODO(jtattermusch): add test with timeout once timeouts are supported
96 }
97}