MockServiceHelper.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.Diagnostics;
  19. using System.Linq;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using Grpc.Core;
  23. using Grpc.Core.Internal;
  24. using Grpc.Core.Utils;
  25. using NUnit.Framework;
  26. namespace Grpc.Core.Tests
  27. {
  28. /// <summary>
  29. /// Allows setting up a mock service in the client-server tests easily.
  30. /// </summary>
  31. public class MockServiceHelper
  32. {
  33. public const string ServiceName = "tests.Test";
  34. readonly string host;
  35. readonly ServerServiceDefinition serviceDefinition;
  36. readonly IEnumerable<ChannelOption> channelOptions;
  37. readonly Method<string, string> unaryMethod;
  38. readonly Method<string, string> clientStreamingMethod;
  39. readonly Method<string, string> serverStreamingMethod;
  40. readonly Method<string, string> duplexStreamingMethod;
  41. UnaryServerMethod<string, string> unaryHandler;
  42. ClientStreamingServerMethod<string, string> clientStreamingHandler;
  43. ServerStreamingServerMethod<string, string> serverStreamingHandler;
  44. DuplexStreamingServerMethod<string, string> duplexStreamingHandler;
  45. Server server;
  46. Channel channel;
  47. public MockServiceHelper(string host = null, Marshaller<string> marshaller = null, IEnumerable<ChannelOption> channelOptions = null)
  48. {
  49. this.host = host ?? "localhost";
  50. this.channelOptions = channelOptions;
  51. marshaller = marshaller ?? Marshallers.StringMarshaller;
  52. unaryMethod = new Method<string, string>(
  53. MethodType.Unary,
  54. ServiceName,
  55. "Unary",
  56. marshaller,
  57. marshaller);
  58. clientStreamingMethod = new Method<string, string>(
  59. MethodType.ClientStreaming,
  60. ServiceName,
  61. "ClientStreaming",
  62. marshaller,
  63. marshaller);
  64. serverStreamingMethod = new Method<string, string>(
  65. MethodType.ServerStreaming,
  66. ServiceName,
  67. "ServerStreaming",
  68. marshaller,
  69. marshaller);
  70. duplexStreamingMethod = new Method<string, string>(
  71. MethodType.DuplexStreaming,
  72. ServiceName,
  73. "DuplexStreaming",
  74. marshaller,
  75. marshaller);
  76. serviceDefinition = ServerServiceDefinition.CreateBuilder()
  77. .AddMethod(unaryMethod, (request, context) => unaryHandler(request, context))
  78. .AddMethod(clientStreamingMethod, (requestStream, context) => clientStreamingHandler(requestStream, context))
  79. .AddMethod(serverStreamingMethod, (request, responseStream, context) => serverStreamingHandler(request, responseStream, context))
  80. .AddMethod(duplexStreamingMethod, (requestStream, responseStream, context) => duplexStreamingHandler(requestStream, responseStream, context))
  81. .Build();
  82. var defaultStatus = new Status(StatusCode.Unknown, "Default mock implementation. Please provide your own.");
  83. unaryHandler = new UnaryServerMethod<string, string>(async (request, context) =>
  84. {
  85. context.Status = defaultStatus;
  86. return "";
  87. });
  88. clientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) =>
  89. {
  90. context.Status = defaultStatus;
  91. return "";
  92. });
  93. serverStreamingHandler = new ServerStreamingServerMethod<string, string>(async (request, responseStream, context) =>
  94. {
  95. context.Status = defaultStatus;
  96. });
  97. duplexStreamingHandler = new DuplexStreamingServerMethod<string, string>(async (requestStream, responseStream, context) =>
  98. {
  99. context.Status = defaultStatus;
  100. });
  101. }
  102. /// <summary>
  103. /// Returns the default server for this service and creates one if not yet created.
  104. /// </summary>
  105. public Server GetServer()
  106. {
  107. if (server == null)
  108. {
  109. // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755
  110. server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
  111. {
  112. Services = { serviceDefinition },
  113. Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
  114. };
  115. }
  116. return server;
  117. }
  118. /// <summary>
  119. /// Returns the default channel for this service and creates one if not yet created.
  120. /// </summary>
  121. public Channel GetChannel()
  122. {
  123. if (channel == null)
  124. {
  125. channel = new Channel(Host, GetServer().Ports.Single().BoundPort, ChannelCredentials.Insecure, channelOptions);
  126. }
  127. return channel;
  128. }
  129. public CallInvocationDetails<string, string> CreateUnaryCall(CallOptions options = default(CallOptions))
  130. {
  131. return new CallInvocationDetails<string, string>(channel, unaryMethod, options);
  132. }
  133. public CallInvocationDetails<string, string> CreateClientStreamingCall(CallOptions options = default(CallOptions))
  134. {
  135. return new CallInvocationDetails<string, string>(channel, clientStreamingMethod, options);
  136. }
  137. public CallInvocationDetails<string, string> CreateServerStreamingCall(CallOptions options = default(CallOptions))
  138. {
  139. return new CallInvocationDetails<string, string>(channel, serverStreamingMethod, options);
  140. }
  141. public CallInvocationDetails<string, string> CreateDuplexStreamingCall(CallOptions options = default(CallOptions))
  142. {
  143. return new CallInvocationDetails<string, string>(channel, duplexStreamingMethod, options);
  144. }
  145. public string Host
  146. {
  147. get
  148. {
  149. return this.host;
  150. }
  151. }
  152. public ServerServiceDefinition ServiceDefinition
  153. {
  154. get
  155. {
  156. return this.serviceDefinition;
  157. }
  158. }
  159. public UnaryServerMethod<string, string> UnaryHandler
  160. {
  161. get
  162. {
  163. return this.unaryHandler;
  164. }
  165. set
  166. {
  167. unaryHandler = value;
  168. }
  169. }
  170. public ClientStreamingServerMethod<string, string> ClientStreamingHandler
  171. {
  172. get
  173. {
  174. return this.clientStreamingHandler;
  175. }
  176. set
  177. {
  178. clientStreamingHandler = value;
  179. }
  180. }
  181. public ServerStreamingServerMethod<string, string> ServerStreamingHandler
  182. {
  183. get
  184. {
  185. return this.serverStreamingHandler;
  186. }
  187. set
  188. {
  189. serverStreamingHandler = value;
  190. }
  191. }
  192. public DuplexStreamingServerMethod<string, string> DuplexStreamingHandler
  193. {
  194. get
  195. {
  196. return this.duplexStreamingHandler;
  197. }
  198. set
  199. {
  200. duplexStreamingHandler = value;
  201. }
  202. }
  203. }
  204. }