Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 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 | |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 34 | using System; |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 35 | using System.Collections.Generic; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 36 | using System.Text.RegularExpressions; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 37 | using System.Threading; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 38 | using System.Threading.Tasks; |
Jan Tattermusch | 1ca56b9 | 2015-04-27 11:03:06 -0700 | [diff] [blame] | 39 | |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 40 | using Google.ProtocolBuffers; |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 41 | using grpc.testing; |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 42 | using Grpc.Auth; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 43 | using Grpc.Core; |
| 44 | using Grpc.Core.Utils; |
| 45 | using NUnit.Framework; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 46 | |
Jan Tattermusch | 8b86b15 | 2015-02-19 21:01:05 -0800 | [diff] [blame] | 47 | namespace Grpc.IntegrationTesting |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 48 | { |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 49 | public class InteropClient |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 50 | { |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 51 | 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 Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 56 | private class ClientOptions |
| 57 | { |
| 58 | public bool help; |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 59 | public string serverHost = "127.0.0.1"; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 60 | public string serverHostOverride = TestCredentials.DefaultHostOverride; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 61 | public int? serverPort; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 62 | public string testCase = "large_unary"; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 63 | public bool useTls; |
| 64 | public bool useTestCa; |
| 65 | } |
| 66 | |
| 67 | ClientOptions options; |
| 68 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 69 | private InteropClient(ClientOptions options) |
Jan Tattermusch | 392d1e0 | 2015-02-09 11:13:51 -0800 | [diff] [blame] | 70 | { |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 71 | this.options = options; |
| 72 | } |
| 73 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 74 | public static void Run(string[] args) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 75 | { |
| 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 Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 95 | Console.WriteLine(); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 96 | Environment.Exit(1); |
| 97 | } |
| 98 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 99 | var interopClient = new InteropClient(options); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 100 | interopClient.Run(); |
| 101 | } |
| 102 | |
| 103 | private void Run() |
| 104 | { |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 105 | Credentials credentials = null; |
| 106 | if (options.useTls) |
| 107 | { |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 108 | credentials = TestCredentials.CreateTestClientCredentials(options.useTestCa); |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 111 | List<ChannelOption> channelOptions = null; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 112 | if (!string.IsNullOrEmpty(options.serverHostOverride)) |
| 113 | { |
Jan Tattermusch | c8f7d10 | 2015-06-08 17:53:45 -0700 | [diff] [blame] | 114 | channelOptions = new List<ChannelOption> |
| 115 | { |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 116 | new ChannelOption(ChannelOptions.SslTargetNameOverride, options.serverHostOverride) |
| 117 | }; |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 120 | using (Channel channel = new Channel(options.serverHost, options.serverPort.Value, credentials, channelOptions)) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 121 | { |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 122 | TestService.TestServiceClient client = new TestService.TestServiceClient(channel); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 123 | if (options.testCase == "service_account_creds" || options.testCase == "compute_engine_creds") |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 124 | { |
| 125 | var credential = GoogleCredential.GetApplicationDefault(); |
| 126 | if (credential.IsCreateScopedRequired) |
| 127 | { |
| 128 | credential = credential.CreateScoped(new[] { AuthScope }); |
| 129 | } |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 130 | client.HeaderInterceptor = OAuth2InterceptorFactory.Create(credential); |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 133 | RunTestCase(options.testCase, client); |
| 134 | } |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 135 | GrpcEnvironment.Shutdown(); |
| 136 | } |
| 137 | |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 138 | private void RunTestCase(string testCase, TestService.TestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 139 | { |
| 140 | switch (testCase) |
| 141 | { |
| 142 | case "empty_unary": |
| 143 | RunEmptyUnary(client); |
| 144 | break; |
| 145 | case "large_unary": |
| 146 | RunLargeUnary(client); |
| 147 | break; |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 148 | case "client_streaming": |
| 149 | RunClientStreaming(client); |
| 150 | break; |
| 151 | case "server_streaming": |
| 152 | RunServerStreaming(client); |
| 153 | break; |
| 154 | case "ping_pong": |
| 155 | RunPingPong(client); |
| 156 | break; |
| 157 | case "empty_stream": |
| 158 | RunEmptyStream(client); |
| 159 | break; |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 160 | case "service_account_creds": |
| 161 | RunServiceAccountCreds(client); |
| 162 | break; |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 163 | case "compute_engine_creds": |
| 164 | RunComputeEngineCreds(client); |
| 165 | break; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 166 | case "oauth2_auth_token": |
| 167 | RunOAuth2AuthToken(client); |
| 168 | break; |
| 169 | case "per_rpc_creds": |
| 170 | RunPerRpcCreds(client); |
| 171 | break; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 172 | case "cancel_after_begin": |
| 173 | RunCancelAfterBegin(client); |
| 174 | break; |
| 175 | case "cancel_after_first_response": |
| 176 | RunCancelAfterFirstResponse(client); |
| 177 | break; |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 178 | case "benchmark_empty_unary": |
| 179 | RunBenchmarkEmptyUnary(client); |
| 180 | break; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 181 | default: |
| 182 | throw new ArgumentException("Unknown test case " + testCase); |
| 183 | } |
| 184 | } |
| 185 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 186 | public static void RunEmptyUnary(TestService.ITestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 187 | { |
| 188 | Console.WriteLine("running empty_unary"); |
| 189 | var response = client.EmptyCall(Empty.DefaultInstance); |
| 190 | Assert.IsNotNull(response); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 191 | Console.WriteLine("Passed!"); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 194 | public static void RunLargeUnary(TestService.ITestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 195 | { |
| 196 | Console.WriteLine("running large_unary"); |
| 197 | var request = SimpleRequest.CreateBuilder() |
| 198 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 199 | .SetResponseSize(314159) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 200 | .SetPayload(CreateZerosPayload(271828)) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 201 | .Build(); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 202 | |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 203 | var response = client.UnaryCall(request); |
| 204 | |
| 205 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 206 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 207 | Console.WriteLine("Passed!"); |
| 208 | } |
| 209 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 210 | public static void RunClientStreaming(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 211 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 212 | Task.Run(async () => |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 213 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 214 | Console.WriteLine("running client_streaming"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 215 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 216 | var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => StreamingInputCallRequest.CreateBuilder().SetPayload(CreateZerosPayload(size)).Build()); |
| 217 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 218 | using (var call = client.StreamingInputCall()) |
| 219 | { |
| 220 | await call.RequestStream.WriteAll(bodySizes); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 221 | |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 222 | var response = await call.ResponseAsync; |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 223 | Assert.AreEqual(74922, response.AggregatedPayloadSize); |
| 224 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 225 | Console.WriteLine("Passed!"); |
| 226 | }).Wait(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 229 | public static void RunServerStreaming(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 230 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 231 | Task.Run(async () => |
| 232 | { |
| 233 | Console.WriteLine("running server_streaming"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 234 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 235 | var bodySizes = new List<int> { 31415, 9, 2653, 58979 }; |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 236 | |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 237 | var request = StreamingOutputCallRequest.CreateBuilder() |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 238 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 239 | .AddRangeResponseParameters(bodySizes.ConvertAll( |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 240 | (size) => ResponseParameters.CreateBuilder().SetSize(size).Build())) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 241 | .Build(); |
| 242 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 243 | using (var call = client.StreamingOutputCall(request)) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 244 | { |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 245 | var responseList = await call.ResponseStream.ToList(); |
| 246 | foreach (var res in responseList) |
| 247 | { |
| 248 | Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type); |
| 249 | } |
| 250 | CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length)); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 251 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 252 | Console.WriteLine("Passed!"); |
| 253 | }).Wait(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 256 | public static void RunPingPong(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 257 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 258 | Task.Run(async () => |
| 259 | { |
| 260 | Console.WriteLine("running ping_pong"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 261 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 262 | using (var call = client.FullDuplexCall()) |
| 263 | { |
| 264 | await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder() |
| 265 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 266 | .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415)) |
| 267 | .SetPayload(CreateZerosPayload(27182)).Build()); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 268 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 269 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 270 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 271 | Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 272 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 273 | await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder() |
| 274 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 275 | .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(9)) |
| 276 | .SetPayload(CreateZerosPayload(8)).Build()); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 277 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 278 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 279 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 280 | Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 281 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 282 | await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder() |
| 283 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 284 | .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(2653)) |
| 285 | .SetPayload(CreateZerosPayload(1828)).Build()); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 286 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 287 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 288 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 289 | Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 290 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 291 | await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder() |
| 292 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 293 | .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(58979)) |
| 294 | .SetPayload(CreateZerosPayload(45904)).Build()); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 295 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 296 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 297 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 298 | Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 299 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 300 | await call.RequestStream.CompleteAsync(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 301 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 302 | Assert.IsFalse(await call.ResponseStream.MoveNext()); |
| 303 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 304 | Console.WriteLine("Passed!"); |
| 305 | }).Wait(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 308 | public static void RunEmptyStream(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 309 | { |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 310 | Task.Run(async () => |
| 311 | { |
| 312 | Console.WriteLine("running empty_stream"); |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 313 | using (var call = client.FullDuplexCall()) |
| 314 | { |
| 315 | await call.RequestStream.CompleteAsync(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 316 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 317 | var responseList = await call.ResponseStream.ToList(); |
| 318 | Assert.AreEqual(0, responseList.Count); |
| 319 | } |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 320 | Console.WriteLine("Passed!"); |
| 321 | }).Wait(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 324 | public static void RunServiceAccountCreds(TestService.ITestServiceClient client) |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 325 | { |
| 326 | Console.WriteLine("running service_account_creds"); |
| 327 | var request = SimpleRequest.CreateBuilder() |
| 328 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 329 | .SetResponseSize(314159) |
| 330 | .SetPayload(CreateZerosPayload(271828)) |
| 331 | .SetFillUsername(true) |
| 332 | .SetFillOauthScope(true) |
| 333 | .Build(); |
| 334 | |
| 335 | var response = client.UnaryCall(request); |
| 336 | |
| 337 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 338 | Assert.AreEqual(314159, response.Payload.Body.Length); |
| 339 | Assert.AreEqual(AuthScopeResponse, response.OauthScope); |
| 340 | Assert.AreEqual(ServiceAccountUser, response.Username); |
| 341 | Console.WriteLine("Passed!"); |
| 342 | } |
| 343 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 344 | public static void RunComputeEngineCreds(TestService.ITestServiceClient client) |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 345 | { |
| 346 | Console.WriteLine("running compute_engine_creds"); |
| 347 | var request = SimpleRequest.CreateBuilder() |
| 348 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 349 | .SetResponseSize(314159) |
| 350 | .SetPayload(CreateZerosPayload(271828)) |
| 351 | .SetFillUsername(true) |
| 352 | .SetFillOauthScope(true) |
| 353 | .Build(); |
| 354 | |
| 355 | var response = client.UnaryCall(request); |
| 356 | |
| 357 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 358 | Assert.AreEqual(314159, response.Payload.Body.Length); |
| 359 | Assert.AreEqual(AuthScopeResponse, response.OauthScope); |
Jan Tattermusch | aa5fba4 | 2015-04-27 21:28:58 -0700 | [diff] [blame] | 360 | Assert.AreEqual(ComputeEngineUser, response.Username); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 361 | Console.WriteLine("Passed!"); |
| 362 | } |
| 363 | |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 364 | public static void RunOAuth2AuthToken(TestService.TestServiceClient client) |
| 365 | { |
| 366 | Console.WriteLine("running oauth2_auth_token"); |
| 367 | var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope }); |
| 368 | Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result); |
| 369 | string oauth2Token = credential.Token.AccessToken; |
| 370 | |
| 371 | // Intercept calls with an OAuth2 token obtained out-of-band. |
| 372 | client.HeaderInterceptor = new MetadataInterceptorDelegate((metadata) => |
| 373 | { |
| 374 | metadata.Add(new Metadata.Entry("Authorization", "Bearer " + oauth2Token)); |
| 375 | }); |
| 376 | |
| 377 | var request = SimpleRequest.CreateBuilder() |
| 378 | .SetFillUsername(true) |
| 379 | .SetFillOauthScope(true) |
| 380 | .Build(); |
| 381 | |
| 382 | var response = client.UnaryCall(request); |
| 383 | |
| 384 | Assert.AreEqual(AuthScopeResponse, response.OauthScope); |
| 385 | Assert.AreEqual(ServiceAccountUser, response.Username); |
| 386 | Console.WriteLine("Passed!"); |
| 387 | } |
| 388 | |
| 389 | public static void RunPerRpcCreds(TestService.TestServiceClient client) |
| 390 | { |
| 391 | Console.WriteLine("running per_rpc_creds"); |
| 392 | |
| 393 | var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope }); |
| 394 | Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result); |
| 395 | string oauth2Token = credential.Token.AccessToken; |
| 396 | |
| 397 | var request = SimpleRequest.CreateBuilder() |
| 398 | .SetFillUsername(true) |
| 399 | .SetFillOauthScope(true) |
| 400 | .Build(); |
| 401 | |
Jan Tattermusch | ae01709 | 2015-07-22 11:59:13 -0700 | [diff] [blame] | 402 | var response = client.UnaryCall(request, headers: new Metadata { new Metadata.Entry("Authorization", "Bearer " + oauth2Token) }); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 403 | |
| 404 | Assert.AreEqual(AuthScopeResponse, response.OauthScope); |
| 405 | Assert.AreEqual(ServiceAccountUser, response.Username); |
| 406 | Console.WriteLine("Passed!"); |
| 407 | } |
| 408 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 409 | public static void RunCancelAfterBegin(TestService.ITestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 410 | { |
| 411 | Task.Run(async () => |
| 412 | { |
| 413 | Console.WriteLine("running cancel_after_begin"); |
| 414 | |
| 415 | var cts = new CancellationTokenSource(); |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 416 | using (var call = client.StreamingInputCall(cancellationToken: cts.Token)) |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 417 | { |
| 418 | // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it. |
| 419 | await Task.Delay(1000); |
| 420 | cts.Cancel(); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 421 | |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 422 | try |
| 423 | { |
Jan Tattermusch | a236ff2 | 2015-07-21 12:33:31 -0700 | [diff] [blame] | 424 | var response = await call.ResponseAsync; |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 425 | Assert.Fail(); |
| 426 | } |
| 427 | catch (RpcException e) |
| 428 | { |
| 429 | Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode); |
| 430 | } |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 431 | } |
| 432 | Console.WriteLine("Passed!"); |
| 433 | }).Wait(); |
| 434 | } |
| 435 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 436 | public static void RunCancelAfterFirstResponse(TestService.ITestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 437 | { |
| 438 | Task.Run(async () => |
| 439 | { |
| 440 | Console.WriteLine("running cancel_after_first_response"); |
| 441 | |
| 442 | var cts = new CancellationTokenSource(); |
Jan Tattermusch | fd953a5 | 2015-07-14 21:41:29 -0700 | [diff] [blame] | 443 | using (var call = client.FullDuplexCall(cancellationToken: cts.Token)) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 444 | { |
Jan Tattermusch | 9f550a3 | 2015-05-18 16:00:36 -0700 | [diff] [blame] | 445 | await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder() |
| 446 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 447 | .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415)) |
| 448 | .SetPayload(CreateZerosPayload(27182)).Build()); |
| 449 | |
| 450 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 451 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 452 | Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length); |
| 453 | |
| 454 | cts.Cancel(); |
| 455 | |
| 456 | try |
| 457 | { |
| 458 | await call.ResponseStream.MoveNext(); |
| 459 | Assert.Fail(); |
| 460 | } |
| 461 | catch (RpcException e) |
| 462 | { |
| 463 | Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode); |
| 464 | } |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 465 | } |
| 466 | Console.WriteLine("Passed!"); |
| 467 | }).Wait(); |
| 468 | } |
| 469 | |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 470 | // This is not an official interop test, but it's useful. |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 471 | public static void RunBenchmarkEmptyUnary(TestService.ITestServiceClient client) |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 472 | { |
| 473 | BenchmarkUtil.RunBenchmark(10000, 10000, |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 474 | () => { client.EmptyCall(Empty.DefaultInstance); }); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 475 | } |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 476 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 477 | private static Payload CreateZerosPayload(int size) |
| 478 | { |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 479 | return Payload.CreateBuilder().SetBody(ByteString.CopyFrom(new byte[size])).Build(); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | private static ClientOptions ParseArguments(string[] args) |
| 483 | { |
| 484 | var options = new ClientOptions(); |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 485 | foreach (string arg in args) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 486 | { |
| 487 | ParseArgument(arg, options); |
| 488 | if (options.help) |
| 489 | { |
| 490 | break; |
| 491 | } |
| 492 | } |
| 493 | return options; |
| 494 | } |
| 495 | |
| 496 | private static void ParseArgument(string arg, ClientOptions options) |
| 497 | { |
| 498 | Match match; |
| 499 | match = Regex.Match(arg, "--server_host=(.*)"); |
| 500 | if (match.Success) |
| 501 | { |
| 502 | options.serverHost = match.Groups[1].Value.Trim(); |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | match = Regex.Match(arg, "--server_host_override=(.*)"); |
| 507 | if (match.Success) |
| 508 | { |
| 509 | options.serverHostOverride = match.Groups[1].Value.Trim(); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | match = Regex.Match(arg, "--server_port=(.*)"); |
| 514 | if (match.Success) |
| 515 | { |
| 516 | options.serverPort = int.Parse(match.Groups[1].Value.Trim()); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | match = Regex.Match(arg, "--test_case=(.*)"); |
| 521 | if (match.Success) |
| 522 | { |
| 523 | options.testCase = match.Groups[1].Value.Trim(); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | match = Regex.Match(arg, "--use_tls=(.*)"); |
| 528 | if (match.Success) |
| 529 | { |
| 530 | options.useTls = bool.Parse(match.Groups[1].Value.Trim()); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | match = Regex.Match(arg, "--use_test_ca=(.*)"); |
| 535 | if (match.Success) |
| 536 | { |
| 537 | options.useTestCa = bool.Parse(match.Groups[1].Value.Trim()); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | Console.WriteLine(string.Format("Unrecognized argument \"{0}\"", arg)); |
| 542 | options.help = true; |
| 543 | } |
| 544 | } |
| 545 | } |