OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Collections.Generic; |
| 36 using System.Diagnostics; |
| 37 using System.Linq; |
| 38 using System.Threading; |
| 39 using System.Threading.Tasks; |
| 40 using Grpc.Core; |
| 41 using Grpc.Core.Internal; |
| 42 using Grpc.Core.Utils; |
| 43 using NUnit.Framework; |
| 44 |
| 45 namespace Grpc.Core.Tests |
| 46 { |
| 47 /// <summary> |
| 48 /// Allows setting up a mock service in the client-server tests easily. |
| 49 /// </summary> |
| 50 public class MockServiceHelper |
| 51 { |
| 52 public const string ServiceName = "tests.Test"; |
| 53 |
| 54 readonly string host; |
| 55 readonly ServerServiceDefinition serviceDefinition; |
| 56 readonly IEnumerable<ChannelOption> channelOptions; |
| 57 |
| 58 readonly Method<string, string> unaryMethod; |
| 59 readonly Method<string, string> clientStreamingMethod; |
| 60 readonly Method<string, string> serverStreamingMethod; |
| 61 readonly Method<string, string> duplexStreamingMethod; |
| 62 |
| 63 UnaryServerMethod<string, string> unaryHandler; |
| 64 ClientStreamingServerMethod<string, string> clientStreamingHandler; |
| 65 ServerStreamingServerMethod<string, string> serverStreamingHandler; |
| 66 DuplexStreamingServerMethod<string, string> duplexStreamingHandler; |
| 67 |
| 68 Server server; |
| 69 Channel channel; |
| 70 |
| 71 public MockServiceHelper(string host = null, Marshaller<string> marshall
er = null, IEnumerable<ChannelOption> channelOptions = null) |
| 72 { |
| 73 this.host = host ?? "localhost"; |
| 74 this.channelOptions = channelOptions; |
| 75 marshaller = marshaller ?? Marshallers.StringMarshaller; |
| 76 |
| 77 unaryMethod = new Method<string, string>( |
| 78 MethodType.Unary, |
| 79 ServiceName, |
| 80 "Unary", |
| 81 marshaller, |
| 82 marshaller); |
| 83 |
| 84 clientStreamingMethod = new Method<string, string>( |
| 85 MethodType.ClientStreaming, |
| 86 ServiceName, |
| 87 "ClientStreaming", |
| 88 marshaller, |
| 89 marshaller); |
| 90 |
| 91 serverStreamingMethod = new Method<string, string>( |
| 92 MethodType.ServerStreaming, |
| 93 ServiceName, |
| 94 "ServerStreaming", |
| 95 marshaller, |
| 96 marshaller); |
| 97 |
| 98 duplexStreamingMethod = new Method<string, string>( |
| 99 MethodType.DuplexStreaming, |
| 100 ServiceName, |
| 101 "DuplexStreaming", |
| 102 marshaller, |
| 103 marshaller); |
| 104 |
| 105 serviceDefinition = ServerServiceDefinition.CreateBuilder(ServiceNam
e) |
| 106 .AddMethod(unaryMethod, (request, context) => unaryHandler(reque
st, context)) |
| 107 .AddMethod(clientStreamingMethod, (requestStream, context) => cl
ientStreamingHandler(requestStream, context)) |
| 108 .AddMethod(serverStreamingMethod, (request, responseStream, cont
ext) => serverStreamingHandler(request, responseStream, context)) |
| 109 .AddMethod(duplexStreamingMethod, (requestStream, responseStream
, context) => duplexStreamingHandler(requestStream, responseStream, context)) |
| 110 .Build(); |
| 111 |
| 112 var defaultStatus = new Status(StatusCode.Unknown, "Default mock imp
lementation. Please provide your own."); |
| 113 |
| 114 unaryHandler = new UnaryServerMethod<string, string>(async (request,
context) => |
| 115 { |
| 116 context.Status = defaultStatus; |
| 117 return ""; |
| 118 }); |
| 119 |
| 120 clientStreamingHandler = new ClientStreamingServerMethod<string, str
ing>(async (requestStream, context) => |
| 121 { |
| 122 context.Status = defaultStatus; |
| 123 return ""; |
| 124 }); |
| 125 |
| 126 serverStreamingHandler = new ServerStreamingServerMethod<string, str
ing>(async (request, responseStream, context) => |
| 127 { |
| 128 context.Status = defaultStatus; |
| 129 }); |
| 130 |
| 131 duplexStreamingHandler = new DuplexStreamingServerMethod<string, str
ing>(async (requestStream, responseStream, context) => |
| 132 { |
| 133 context.Status = defaultStatus; |
| 134 }); |
| 135 } |
| 136 |
| 137 /// <summary> |
| 138 /// Returns the default server for this service and creates one if not y
et created. |
| 139 /// </summary> |
| 140 public Server GetServer() |
| 141 { |
| 142 if (server == null) |
| 143 { |
| 144 server = new Server |
| 145 { |
| 146 Services = { serviceDefinition }, |
| 147 Ports = { { Host, ServerPort.PickUnused, ServerCredentials.I
nsecure } } |
| 148 }; |
| 149 } |
| 150 return server; |
| 151 } |
| 152 |
| 153 /// <summary> |
| 154 /// Returns the default channel for this service and creates one if not
yet created. |
| 155 /// </summary> |
| 156 public Channel GetChannel() |
| 157 { |
| 158 if (channel == null) |
| 159 { |
| 160 channel = new Channel(Host, GetServer().Ports.Single().BoundPort
, ChannelCredentials.Insecure, channelOptions); |
| 161 } |
| 162 return channel; |
| 163 } |
| 164 |
| 165 public CallInvocationDetails<string, string> CreateUnaryCall(CallOptions
options = default(CallOptions)) |
| 166 { |
| 167 return new CallInvocationDetails<string, string>(channel, unaryMetho
d, options); |
| 168 } |
| 169 |
| 170 public CallInvocationDetails<string, string> CreateClientStreamingCall(C
allOptions options = default(CallOptions)) |
| 171 { |
| 172 return new CallInvocationDetails<string, string>(channel, clientStre
amingMethod, options); |
| 173 } |
| 174 |
| 175 public CallInvocationDetails<string, string> CreateServerStreamingCall(C
allOptions options = default(CallOptions)) |
| 176 { |
| 177 return new CallInvocationDetails<string, string>(channel, serverStre
amingMethod, options); |
| 178 } |
| 179 |
| 180 public CallInvocationDetails<string, string> CreateDuplexStreamingCall(C
allOptions options = default(CallOptions)) |
| 181 { |
| 182 return new CallInvocationDetails<string, string>(channel, duplexStre
amingMethod, options); |
| 183 } |
| 184 |
| 185 public string Host |
| 186 { |
| 187 get |
| 188 { |
| 189 return this.host; |
| 190 } |
| 191 } |
| 192 |
| 193 public ServerServiceDefinition ServiceDefinition |
| 194 { |
| 195 get |
| 196 { |
| 197 return this.serviceDefinition; |
| 198 } |
| 199 } |
| 200 |
| 201 public UnaryServerMethod<string, string> UnaryHandler |
| 202 { |
| 203 get |
| 204 { |
| 205 return this.unaryHandler; |
| 206 } |
| 207 |
| 208 set |
| 209 { |
| 210 unaryHandler = value; |
| 211 } |
| 212 } |
| 213 |
| 214 public ClientStreamingServerMethod<string, string> ClientStreamingHandle
r |
| 215 { |
| 216 get |
| 217 { |
| 218 return this.clientStreamingHandler; |
| 219 } |
| 220 |
| 221 set |
| 222 { |
| 223 clientStreamingHandler = value; |
| 224 } |
| 225 } |
| 226 |
| 227 public ServerStreamingServerMethod<string, string> ServerStreamingHandle
r |
| 228 { |
| 229 get |
| 230 { |
| 231 return this.serverStreamingHandler; |
| 232 } |
| 233 |
| 234 set |
| 235 { |
| 236 serverStreamingHandler = value; |
| 237 } |
| 238 } |
| 239 |
| 240 public DuplexStreamingServerMethod<string, string> DuplexStreamingHandle
r |
| 241 { |
| 242 get |
| 243 { |
| 244 return this.duplexStreamingHandler; |
| 245 } |
| 246 |
| 247 set |
| 248 { |
| 249 duplexStreamingHandler = value; |
| 250 } |
| 251 } |
| 252 } |
| 253 } |
OLD | NEW |