blob: 02f8a369defa37d64f0b39288fe5ee1749f515bb [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * 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.
Craig Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// 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
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080034using System;
Jan Tattermuschd233d3a2015-02-06 14:15:00 -080035using System.Collections.Generic;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080036using System.Text.RegularExpressions;
Jan Tattermusche5c44602015-05-01 11:12:34 -070037using System.Threading;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070038using System.Threading.Tasks;
Jan Tattermusch1ca56b92015-04-27 11:03:06 -070039
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080040using Google.ProtocolBuffers;
Jan Tattermusch075dde42015-03-11 18:21:00 -070041using grpc.testing;
Jan Tattermuschdca6e882015-04-22 16:56:27 -070042using Grpc.Auth;
Jan Tattermusch30868622015-02-19 09:22:33 -080043using Grpc.Core;
44using Grpc.Core.Utils;
45using NUnit.Framework;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080046
Jan Tattermusch8b86b152015-02-19 21:01:05 -080047namespace Grpc.IntegrationTesting
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080048{
Jan Tattermusch503bbac2015-02-26 18:19:47 -080049 public class InteropClient
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080050 {
Jan Tattermuschdca6e882015-04-22 16:56:27 -070051 private const string ServiceAccountUser = "155450119199-3psnrh1sdr3d8cpj1v46naggf81mhdnk@developer.gserviceaccount.com";
52 private const string ComputeEngineUser = "155450119199-r5aaqa2vqoa9g5mv2m6s3m1l293rlmel@developer.gserviceaccount.com";
53 private const string AuthScope = "https://www.googleapis.com/auth/xapi.zoo";
54 private const string AuthScopeResponse = "xapi.zoo";
55
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080056 private class ClientOptions
57 {
58 public bool help;
Jan Tattermusch075dde42015-03-11 18:21:00 -070059 public string serverHost = "127.0.0.1";
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080060 public string serverHostOverride = TestCredentials.DefaultHostOverride;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080061 public int? serverPort;
Jan Tattermusch15329232015-03-02 15:32:47 -080062 public string testCase = "large_unary";
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080063 public bool useTls;
64 public bool useTestCa;
65 }
66
67 ClientOptions options;
68
Jan Tattermusch503bbac2015-02-26 18:19:47 -080069 private InteropClient(ClientOptions options)
Jan Tattermusch392d1e02015-02-09 11:13:51 -080070 {
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080071 this.options = options;
72 }
73
Jan Tattermusch503bbac2015-02-26 18:19:47 -080074 public static void Run(string[] args)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080075 {
76 Console.WriteLine("gRPC C# interop testing client");
77 ClientOptions options = ParseArguments(args);
78
79 if (options.serverHost == null || !options.serverPort.HasValue || options.testCase == null)
80 {
81 Console.WriteLine("Missing required argument.");
82 Console.WriteLine();
83 options.help = true;
84 }
85
86 if (options.help)
87 {
88 Console.WriteLine("Usage:");
89 Console.WriteLine(" --server_host=HOSTNAME");
90 Console.WriteLine(" --server_host_override=HOSTNAME");
91 Console.WriteLine(" --server_port=PORT");
92 Console.WriteLine(" --test_case=TESTCASE");
93 Console.WriteLine(" --use_tls=BOOLEAN");
94 Console.WriteLine(" --use_test_ca=BOOLEAN");
Craig Tiller190d3602015-02-18 09:23:38 -080095 Console.WriteLine();
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -080096 Environment.Exit(1);
97 }
98
Jan Tattermusch503bbac2015-02-26 18:19:47 -080099 var interopClient = new InteropClient(options);
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800100 interopClient.Run();
101 }
102
103 private void Run()
104 {
Jan Tattermusch23821ce2015-02-13 10:46:02 -0800105 GrpcEnvironment.Initialize();
106
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800107 string addr = string.Format("{0}:{1}", options.serverHost, options.serverPort);
Jan Tattermusch15329232015-03-02 15:32:47 -0800108
109 Credentials credentials = null;
110 if (options.useTls)
111 {
Jan Tattermuschb0829eb2015-03-03 09:30:55 -0800112 credentials = TestCredentials.CreateTestClientCredentials(options.useTestCa);
Jan Tattermusch15329232015-03-02 15:32:47 -0800113 }
114
115 ChannelArgs channelArgs = null;
116 if (!string.IsNullOrEmpty(options.serverHostOverride))
117 {
Jan Tattermusch286975f2015-03-12 14:04:36 -0700118 channelArgs = ChannelArgs.CreateBuilder()
Jan Tattermusch15329232015-03-02 15:32:47 -0800119 .AddString(ChannelArgs.SslTargetNameOverrideKey, options.serverHostOverride).Build();
120 }
121
122 using (Channel channel = new Channel(addr, credentials, channelArgs))
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800123 {
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700124 var stubConfig = StubConfiguration.Default;
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700125 if (options.testCase == "service_account_creds" || options.testCase == "compute_engine_creds")
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700126 {
127 var credential = GoogleCredential.GetApplicationDefault();
128 if (credential.IsCreateScopedRequired)
129 {
130 credential = credential.CreateScoped(new[] { AuthScope });
131 }
132 stubConfig = new StubConfiguration(OAuth2InterceptorFactory.Create(credential));
133 }
134
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700135 TestService.ITestServiceClient client = new TestService.TestServiceClient(channel, stubConfig);
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800136 RunTestCase(options.testCase, client);
137 }
138
139 GrpcEnvironment.Shutdown();
140 }
141
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700142 private void RunTestCase(string testCase, TestService.ITestServiceClient client)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800143 {
144 switch (testCase)
145 {
146 case "empty_unary":
147 RunEmptyUnary(client);
148 break;
149 case "large_unary":
150 RunLargeUnary(client);
151 break;
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800152 case "client_streaming":
153 RunClientStreaming(client);
154 break;
155 case "server_streaming":
156 RunServerStreaming(client);
157 break;
158 case "ping_pong":
159 RunPingPong(client);
160 break;
161 case "empty_stream":
162 RunEmptyStream(client);
163 break;
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700164 case "service_account_creds":
165 RunServiceAccountCreds(client);
166 break;
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700167 case "compute_engine_creds":
168 RunComputeEngineCreds(client);
169 break;
Jan Tattermusche5c44602015-05-01 11:12:34 -0700170 case "cancel_after_begin":
171 RunCancelAfterBegin(client);
172 break;
173 case "cancel_after_first_response":
174 RunCancelAfterFirstResponse(client);
175 break;
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800176 case "benchmark_empty_unary":
177 RunBenchmarkEmptyUnary(client);
178 break;
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800179 default:
180 throw new ArgumentException("Unknown test case " + testCase);
181 }
182 }
183
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700184 public static void RunEmptyUnary(TestService.ITestServiceClient client)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800185 {
186 Console.WriteLine("running empty_unary");
187 var response = client.EmptyCall(Empty.DefaultInstance);
188 Assert.IsNotNull(response);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800189 Console.WriteLine("Passed!");
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800190 }
191
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700192 public static void RunLargeUnary(TestService.ITestServiceClient client)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800193 {
194 Console.WriteLine("running large_unary");
195 var request = SimpleRequest.CreateBuilder()
196 .SetResponseType(PayloadType.COMPRESSABLE)
197 .SetResponseSize(314159)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800198 .SetPayload(CreateZerosPayload(271828))
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800199 .Build();
Craig Tiller190d3602015-02-18 09:23:38 -0800200
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800201 var response = client.UnaryCall(request);
202
203 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
204 Assert.AreEqual(314159, response.Payload.Body.Length);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800205 Console.WriteLine("Passed!");
206 }
207
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700208 public static void RunClientStreaming(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800209 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700210 Task.Run(async () =>
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800211 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700212 Console.WriteLine("running client_streaming");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800213
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700214 var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => StreamingInputCallRequest.CreateBuilder().SetPayload(CreateZerosPayload(size)).Build());
215
216 var call = client.StreamingInputCall();
217 await call.RequestStream.WriteAll(bodySizes);
218
219 var response = await call.Result;
220 Assert.AreEqual(74922, response.AggregatedPayloadSize);
221 Console.WriteLine("Passed!");
222 }).Wait();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800223 }
224
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700225 public static void RunServerStreaming(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800226 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700227 Task.Run(async () =>
228 {
229 Console.WriteLine("running server_streaming");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800230
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700231 var bodySizes = new List<int> { 31415, 9, 2653, 58979 };
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800232
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700233 var request = StreamingOutputCallRequest.CreateBuilder()
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800234 .SetResponseType(PayloadType.COMPRESSABLE)
235 .AddRangeResponseParameters(bodySizes.ConvertAll(
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700236 (size) => ResponseParameters.CreateBuilder().SetSize(size).Build()))
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800237 .Build();
238
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700239 var call = client.StreamingOutputCall(request);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800240
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700241 var responseList = await call.ResponseStream.ToList();
242 foreach (var res in responseList)
243 {
244 Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type);
245 }
246 CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length));
247 Console.WriteLine("Passed!");
248 }).Wait();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800249 }
250
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700251 public static void RunPingPong(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800252 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700253 Task.Run(async () =>
254 {
255 Console.WriteLine("running ping_pong");
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800256
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700257 var call = client.FullDuplexCall();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800258
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700259 StreamingOutputCallResponse response;
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800260
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700261 await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800262 .SetResponseType(PayloadType.COMPRESSABLE)
263 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
264 .SetPayload(CreateZerosPayload(27182)).Build());
Craig Tiller190d3602015-02-18 09:23:38 -0800265
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700266 response = await call.ResponseStream.ReadNext();
267 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
268 Assert.AreEqual(31415, response.Payload.Body.Length);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800269
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700270 await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800271 .SetResponseType(PayloadType.COMPRESSABLE)
272 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(9))
273 .SetPayload(CreateZerosPayload(8)).Build());
274
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700275 response = await call.ResponseStream.ReadNext();
276 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
277 Assert.AreEqual(9, response.Payload.Body.Length);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800278
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700279 await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800280 .SetResponseType(PayloadType.COMPRESSABLE)
Jan Tattermusch20831382015-02-24 14:16:04 -0800281 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(2653))
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800282 .SetPayload(CreateZerosPayload(1828)).Build());
283
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700284 response = await call.ResponseStream.ReadNext();
285 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
286 Assert.AreEqual(2653, response.Payload.Body.Length);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800287
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700288 await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800289 .SetResponseType(PayloadType.COMPRESSABLE)
290 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(58979))
291 .SetPayload(CreateZerosPayload(45904)).Build());
292
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700293 response = await call.ResponseStream.ReadNext();
294 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
295 Assert.AreEqual(58979, response.Payload.Body.Length);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800296
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700297 await call.RequestStream.Close();
Jan Tattermusch20831382015-02-24 14:16:04 -0800298
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700299 response = await call.ResponseStream.ReadNext();
300 Assert.AreEqual(null, response);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800301
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700302 Console.WriteLine("Passed!");
303 }).Wait();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800304 }
305
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700306 public static void RunEmptyStream(TestService.ITestServiceClient client)
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800307 {
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700308 Task.Run(async () =>
309 {
310 Console.WriteLine("running empty_stream");
311 var call = client.FullDuplexCall();
312 await call.Close();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800313
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700314 var responseList = await call.ResponseStream.ToList();
315 Assert.AreEqual(0, responseList.Count);
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800316
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700317 Console.WriteLine("Passed!");
318 }).Wait();
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800319 }
320
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700321 public static void RunServiceAccountCreds(TestService.ITestServiceClient client)
Jan Tattermuschdca6e882015-04-22 16:56:27 -0700322 {
323 Console.WriteLine("running service_account_creds");
324 var request = SimpleRequest.CreateBuilder()
325 .SetResponseType(PayloadType.COMPRESSABLE)
326 .SetResponseSize(314159)
327 .SetPayload(CreateZerosPayload(271828))
328 .SetFillUsername(true)
329 .SetFillOauthScope(true)
330 .Build();
331
332 var response = client.UnaryCall(request);
333
334 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
335 Assert.AreEqual(314159, response.Payload.Body.Length);
336 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
337 Assert.AreEqual(ServiceAccountUser, response.Username);
338 Console.WriteLine("Passed!");
339 }
340
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700341 public static void RunComputeEngineCreds(TestService.ITestServiceClient client)
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700342 {
343 Console.WriteLine("running compute_engine_creds");
344 var request = SimpleRequest.CreateBuilder()
345 .SetResponseType(PayloadType.COMPRESSABLE)
346 .SetResponseSize(314159)
347 .SetPayload(CreateZerosPayload(271828))
348 .SetFillUsername(true)
349 .SetFillOauthScope(true)
350 .Build();
351
352 var response = client.UnaryCall(request);
353
354 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
355 Assert.AreEqual(314159, response.Payload.Body.Length);
356 Assert.AreEqual(AuthScopeResponse, response.OauthScope);
Jan Tattermuschaa5fba42015-04-27 21:28:58 -0700357 Assert.AreEqual(ComputeEngineUser, response.Username);
Jan Tattermusch0bbfa382015-04-27 16:11:59 -0700358 Console.WriteLine("Passed!");
359 }
360
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700361 public static void RunCancelAfterBegin(TestService.ITestServiceClient client)
Jan Tattermusche5c44602015-05-01 11:12:34 -0700362 {
363 Task.Run(async () =>
364 {
365 Console.WriteLine("running cancel_after_begin");
366
367 var cts = new CancellationTokenSource();
368 var call = client.StreamingInputCall(cts.Token);
Jan Tattermusch8c2dd9d2015-05-04 09:20:43 -0700369 // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it.
370 await Task.Delay(1000);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700371 cts.Cancel();
372
373 try
374 {
375 var response = await call.Result;
376 Assert.Fail();
377 }
378 catch (RpcException e)
379 {
380 Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
381 }
382 Console.WriteLine("Passed!");
383 }).Wait();
384 }
385
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700386 public static void RunCancelAfterFirstResponse(TestService.ITestServiceClient client)
Jan Tattermusche5c44602015-05-01 11:12:34 -0700387 {
388 Task.Run(async () =>
389 {
390 Console.WriteLine("running cancel_after_first_response");
391
392 var cts = new CancellationTokenSource();
393 var call = client.FullDuplexCall(cts.Token);
394
395 StreamingOutputCallResponse response;
396
397 await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
398 .SetResponseType(PayloadType.COMPRESSABLE)
399 .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
400 .SetPayload(CreateZerosPayload(27182)).Build());
401
402 response = await call.ResponseStream.ReadNext();
403 Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
404 Assert.AreEqual(31415, response.Payload.Body.Length);
405
406 cts.Cancel();
407
408 try
409 {
410 response = await call.ResponseStream.ReadNext();
411 Assert.Fail();
412 }
413 catch (RpcException e)
414 {
415 Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
416 }
417 Console.WriteLine("Passed!");
418 }).Wait();
419 }
420
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800421 // This is not an official interop test, but it's useful.
Jan Tattermusch7eb3a762015-05-07 14:26:13 -0700422 public static void RunBenchmarkEmptyUnary(TestService.ITestServiceClient client)
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800423 {
424 BenchmarkUtil.RunBenchmark(10000, 10000,
Jan Tattermusch075dde42015-03-11 18:21:00 -0700425 () => { client.EmptyCall(Empty.DefaultInstance); });
Jan Tattermusch50faa8f2015-02-21 17:51:52 -0800426 }
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800427
Jan Tattermusch075dde42015-03-11 18:21:00 -0700428 private static Payload CreateZerosPayload(int size)
429 {
Jan Tattermuschd233d3a2015-02-06 14:15:00 -0800430 return Payload.CreateBuilder().SetBody(ByteString.CopyFrom(new byte[size])).Build();
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800431 }
432
433 private static ClientOptions ParseArguments(string[] args)
434 {
435 var options = new ClientOptions();
Jan Tattermusch075dde42015-03-11 18:21:00 -0700436 foreach (string arg in args)
Jan Tattermuscheb3e76e2015-02-06 11:43:13 -0800437 {
438 ParseArgument(arg, options);
439 if (options.help)
440 {
441 break;
442 }
443 }
444 return options;
445 }
446
447 private static void ParseArgument(string arg, ClientOptions options)
448 {
449 Match match;
450 match = Regex.Match(arg, "--server_host=(.*)");
451 if (match.Success)
452 {
453 options.serverHost = match.Groups[1].Value.Trim();
454 return;
455 }
456
457 match = Regex.Match(arg, "--server_host_override=(.*)");
458 if (match.Success)
459 {
460 options.serverHostOverride = match.Groups[1].Value.Trim();
461 return;
462 }
463
464 match = Regex.Match(arg, "--server_port=(.*)");
465 if (match.Success)
466 {
467 options.serverPort = int.Parse(match.Groups[1].Value.Trim());
468 return;
469 }
470
471 match = Regex.Match(arg, "--test_case=(.*)");
472 if (match.Success)
473 {
474 options.testCase = match.Groups[1].Value.Trim();
475 return;
476 }
477
478 match = Regex.Match(arg, "--use_tls=(.*)");
479 if (match.Success)
480 {
481 options.useTls = bool.Parse(match.Groups[1].Value.Trim());
482 return;
483 }
484
485 match = Regex.Match(arg, "--use_test_ca=(.*)");
486 if (match.Success)
487 {
488 options.useTestCa = bool.Parse(match.Groups[1].Value.Trim());
489 return;
490 }
491
492 Console.WriteLine(string.Format("Unrecognized argument \"{0}\"", arg));
493 options.help = true;
494 }
495 }
496}