MockServiceHelper.cs 8.5 KB

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