XdsInteropClientTest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using Grpc.Core;
  22. using Grpc.Core.Utils;
  23. using Grpc.Testing;
  24. using NUnit.Framework;
  25. namespace Grpc.IntegrationTesting
  26. {
  27. public class XdsInteropClientTest
  28. {
  29. const string Host = "localhost";
  30. BackendServiceImpl backendService;
  31. Server backendServer;
  32. Server lbStatsServer;
  33. Channel lbStatsChannel;
  34. LoadBalancerStatsService.LoadBalancerStatsServiceClient lbStatsClient;
  35. XdsInteropClient xdsInteropClient;
  36. [OneTimeSetUp]
  37. public void Init()
  38. {
  39. backendService = new BackendServiceImpl();
  40. // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755
  41. backendServer = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
  42. {
  43. Services = { TestService.BindService(backendService) },
  44. Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
  45. };
  46. backendServer.Start();
  47. xdsInteropClient = new XdsInteropClient(new XdsInteropClient.ClientOptions
  48. {
  49. NumChannels = 1,
  50. Qps = 1,
  51. RpcTimeoutSec = 10,
  52. Rpc = "UnaryCall",
  53. Server = $"{Host}:{backendServer.Ports.Single().BoundPort}",
  54. });
  55. // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755
  56. lbStatsServer = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
  57. {
  58. Services = { LoadBalancerStatsService.BindService(new LoadBalancerStatsServiceImpl(xdsInteropClient.StatsWatcher)) },
  59. Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
  60. };
  61. lbStatsServer.Start();
  62. int port = lbStatsServer.Ports.Single().BoundPort;
  63. lbStatsChannel = new Channel(Host, port, ChannelCredentials.Insecure);
  64. lbStatsClient = new LoadBalancerStatsService.LoadBalancerStatsServiceClient(lbStatsChannel);
  65. }
  66. [OneTimeTearDown]
  67. public void Cleanup()
  68. {
  69. lbStatsChannel.ShutdownAsync().Wait();
  70. lbStatsServer.ShutdownAsync().Wait();
  71. backendServer.ShutdownAsync().Wait();
  72. }
  73. [Test]
  74. public async Task SmokeTest()
  75. {
  76. string backendName = "backend1";
  77. backendService.UnaryHandler = (request, context) =>
  78. {
  79. return Task.FromResult(new SimpleResponse { Hostname = backendName });
  80. };
  81. var cancellationTokenSource = new CancellationTokenSource();
  82. var runChannelsTask = xdsInteropClient.RunChannelsAsync(cancellationTokenSource.Token);
  83. var stats = await lbStatsClient.GetClientStatsAsync(new LoadBalancerStatsRequest
  84. {
  85. NumRpcs = 5,
  86. TimeoutSec = 10,
  87. }, deadline: DateTime.UtcNow.AddSeconds(30));
  88. Assert.AreEqual(0, stats.NumFailures);
  89. Assert.AreEqual(backendName, stats.RpcsByPeer.Keys.Single());
  90. Assert.AreEqual(5, stats.RpcsByPeer[backendName]);
  91. Assert.AreEqual("UnaryCall", stats.RpcsByMethod.Keys.Single());
  92. Assert.AreEqual(backendName, stats.RpcsByMethod["UnaryCall"].RpcsByPeer_.Keys.Single());
  93. Assert.AreEqual(5, stats.RpcsByMethod["UnaryCall"].RpcsByPeer_[backendName]);
  94. await Task.Delay(100);
  95. var stats2 = await lbStatsClient.GetClientStatsAsync(new LoadBalancerStatsRequest
  96. {
  97. NumRpcs = 3,
  98. TimeoutSec = 10,
  99. }, deadline: DateTime.UtcNow.AddSeconds(30));
  100. Assert.AreEqual(0, stats2.NumFailures);
  101. Assert.AreEqual(backendName, stats2.RpcsByPeer.Keys.Single());
  102. Assert.AreEqual(3, stats2.RpcsByPeer[backendName]);
  103. Assert.AreEqual("UnaryCall", stats2.RpcsByMethod.Keys.Single());
  104. Assert.AreEqual(backendName, stats2.RpcsByMethod["UnaryCall"].RpcsByPeer_.Keys.Single());
  105. Assert.AreEqual(3, stats2.RpcsByMethod["UnaryCall"].RpcsByPeer_[backendName]);
  106. cancellationTokenSource.Cancel();
  107. await runChannelsTask;
  108. }
  109. [Test]
  110. public async Task HostnameReadFromResponseHeaders()
  111. {
  112. string correctBackendName = "backend1";
  113. backendService.UnaryHandler = async (request, context) =>
  114. {
  115. await context.WriteResponseHeadersAsync(new Metadata { {"hostname", correctBackendName} });
  116. return new SimpleResponse { Hostname = "wrong_hostname" };
  117. };
  118. var cancellationTokenSource = new CancellationTokenSource();
  119. var runChannelsTask = xdsInteropClient.RunChannelsAsync(cancellationTokenSource.Token);
  120. var stats = await lbStatsClient.GetClientStatsAsync(new LoadBalancerStatsRequest
  121. {
  122. NumRpcs = 3,
  123. TimeoutSec = 10,
  124. }, deadline: DateTime.UtcNow.AddSeconds(30));
  125. Assert.AreEqual(0, stats.NumFailures);
  126. Assert.AreEqual(correctBackendName, stats.RpcsByPeer.Keys.Single());
  127. Assert.AreEqual(3, stats.RpcsByPeer[correctBackendName]);
  128. cancellationTokenSource.Cancel();
  129. await runChannelsTask;
  130. }
  131. public class BackendServiceImpl : TestService.TestServiceBase
  132. {
  133. public UnaryServerMethod<SimpleRequest, SimpleResponse> UnaryHandler { get; set; }
  134. public UnaryServerMethod<Empty, Empty> EmptyHandler { get; set; }
  135. public override Task<SimpleResponse> UnaryCall(SimpleRequest request, ServerCallContext context)
  136. {
  137. return UnaryHandler(request, context);
  138. }
  139. public override Task<Empty> EmptyCall(Empty request, ServerCallContext context)
  140. {
  141. return EmptyHandler(request, context);
  142. }
  143. }
  144. }
  145. }