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 | cd7a405 | 2015-07-25 01:47:25 -0700 | [diff] [blame] | 130 | client.HeaderInterceptor = OAuth2Interceptors.FromCredential(credential); |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 133 | RunTestCaseAsync(options.testCase, client).Wait(); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 134 | } |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 135 | GrpcEnvironment.Shutdown(); |
| 136 | } |
| 137 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 138 | private async Task RunTestCaseAsync(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": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 149 | await RunClientStreamingAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 150 | break; |
| 151 | case "server_streaming": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 152 | await RunServerStreamingAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 153 | break; |
| 154 | case "ping_pong": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 155 | await RunPingPongAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 156 | break; |
| 157 | case "empty_stream": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 158 | await RunEmptyStreamAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 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": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 173 | await RunCancelAfterBeginAsync(client); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 174 | break; |
| 175 | case "cancel_after_first_response": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 176 | await RunCancelAfterFirstResponseAsync(client); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 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 | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 210 | public static async Task RunClientStreamingAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 211 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 212 | Console.WriteLine("running client_streaming"); |
| 213 | |
| 214 | var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => StreamingInputCallRequest.CreateBuilder().SetPayload(CreateZerosPayload(size)).Build()); |
| 215 | |
| 216 | using (var call = client.StreamingInputCall()) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 217 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame^] | 218 | await call.RequestStream.WriteAllAsync(bodySizes); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 219 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 220 | var response = await call.ResponseAsync; |
| 221 | Assert.AreEqual(74922, response.AggregatedPayloadSize); |
| 222 | } |
| 223 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 226 | public static async Task RunServerStreamingAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 227 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 228 | Console.WriteLine("running server_streaming"); |
| 229 | |
| 230 | var bodySizes = new List<int> { 31415, 9, 2653, 58979 }; |
| 231 | |
| 232 | var request = StreamingOutputCallRequest.CreateBuilder() |
| 233 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 234 | .AddRangeResponseParameters(bodySizes.ConvertAll( |
| 235 | (size) => ResponseParameters.CreateBuilder().SetSize(size).Build())) |
| 236 | .Build(); |
| 237 | |
| 238 | using (var call = client.StreamingOutputCall(request)) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 239 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame^] | 240 | var responseList = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 241 | foreach (var res in responseList) |
| 242 | { |
| 243 | Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type); |
| 244 | } |
| 245 | CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length)); |
| 246 | } |
| 247 | Console.WriteLine("Passed!"); |
| 248 | } |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 249 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 250 | public static async Task RunPingPongAsync(TestService.ITestServiceClient client) |
| 251 | { |
| 252 | Console.WriteLine("running ping_pong"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 253 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 254 | using (var call = client.FullDuplexCall()) |
| 255 | { |
| 256 | await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder() |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 257 | .SetResponseType(PayloadType.COMPRESSABLE) |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 258 | .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415)) |
| 259 | .SetPayload(CreateZerosPayload(27182)).Build()); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 260 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 261 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 262 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 263 | Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length); |
| 264 | |
| 265 | await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder() |
| 266 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 267 | .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(9)) |
| 268 | .SetPayload(CreateZerosPayload(8)).Build()); |
| 269 | |
| 270 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 271 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 272 | Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length); |
| 273 | |
| 274 | await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder() |
| 275 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 276 | .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(2653)) |
| 277 | .SetPayload(CreateZerosPayload(1828)).Build()); |
| 278 | |
| 279 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 280 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 281 | Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length); |
| 282 | |
| 283 | await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder() |
| 284 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 285 | .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(58979)) |
| 286 | .SetPayload(CreateZerosPayload(45904)).Build()); |
| 287 | |
| 288 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 289 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 290 | Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length); |
| 291 | |
| 292 | await call.RequestStream.CompleteAsync(); |
| 293 | |
| 294 | Assert.IsFalse(await call.ResponseStream.MoveNext()); |
| 295 | } |
| 296 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 299 | public static async Task RunEmptyStreamAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 300 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 301 | Console.WriteLine("running empty_stream"); |
| 302 | using (var call = client.FullDuplexCall()) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 303 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 304 | await call.RequestStream.CompleteAsync(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 305 | |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame^] | 306 | var responseList = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 307 | Assert.AreEqual(0, responseList.Count); |
| 308 | } |
| 309 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 312 | public static void RunServiceAccountCreds(TestService.ITestServiceClient client) |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 313 | { |
| 314 | Console.WriteLine("running service_account_creds"); |
| 315 | var request = SimpleRequest.CreateBuilder() |
| 316 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 317 | .SetResponseSize(314159) |
| 318 | .SetPayload(CreateZerosPayload(271828)) |
| 319 | .SetFillUsername(true) |
| 320 | .SetFillOauthScope(true) |
| 321 | .Build(); |
| 322 | |
| 323 | var response = client.UnaryCall(request); |
| 324 | |
| 325 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 326 | Assert.AreEqual(314159, response.Payload.Body.Length); |
| 327 | Assert.AreEqual(AuthScopeResponse, response.OauthScope); |
| 328 | Assert.AreEqual(ServiceAccountUser, response.Username); |
| 329 | Console.WriteLine("Passed!"); |
| 330 | } |
| 331 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 332 | public static void RunComputeEngineCreds(TestService.ITestServiceClient client) |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 333 | { |
| 334 | Console.WriteLine("running compute_engine_creds"); |
| 335 | var request = SimpleRequest.CreateBuilder() |
| 336 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 337 | .SetResponseSize(314159) |
| 338 | .SetPayload(CreateZerosPayload(271828)) |
| 339 | .SetFillUsername(true) |
| 340 | .SetFillOauthScope(true) |
| 341 | .Build(); |
| 342 | |
| 343 | var response = client.UnaryCall(request); |
| 344 | |
| 345 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 346 | Assert.AreEqual(314159, response.Payload.Body.Length); |
| 347 | Assert.AreEqual(AuthScopeResponse, response.OauthScope); |
Jan Tattermusch | aa5fba4 | 2015-04-27 21:28:58 -0700 | [diff] [blame] | 348 | Assert.AreEqual(ComputeEngineUser, response.Username); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 349 | Console.WriteLine("Passed!"); |
| 350 | } |
| 351 | |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 352 | public static void RunOAuth2AuthToken(TestService.TestServiceClient client) |
| 353 | { |
| 354 | Console.WriteLine("running oauth2_auth_token"); |
| 355 | var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope }); |
| 356 | Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result); |
| 357 | string oauth2Token = credential.Token.AccessToken; |
| 358 | |
Jan Tattermusch | cd7a405 | 2015-07-25 01:47:25 -0700 | [diff] [blame] | 359 | client.HeaderInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 360 | |
| 361 | var request = SimpleRequest.CreateBuilder() |
| 362 | .SetFillUsername(true) |
| 363 | .SetFillOauthScope(true) |
| 364 | .Build(); |
| 365 | |
| 366 | var response = client.UnaryCall(request); |
| 367 | |
| 368 | Assert.AreEqual(AuthScopeResponse, response.OauthScope); |
| 369 | Assert.AreEqual(ServiceAccountUser, response.Username); |
| 370 | Console.WriteLine("Passed!"); |
| 371 | } |
| 372 | |
| 373 | public static void RunPerRpcCreds(TestService.TestServiceClient client) |
| 374 | { |
| 375 | Console.WriteLine("running per_rpc_creds"); |
| 376 | |
| 377 | var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope }); |
| 378 | Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result); |
| 379 | string oauth2Token = credential.Token.AccessToken; |
Jan Tattermusch | cd7a405 | 2015-07-25 01:47:25 -0700 | [diff] [blame] | 380 | var headerInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 381 | |
| 382 | var request = SimpleRequest.CreateBuilder() |
| 383 | .SetFillUsername(true) |
| 384 | .SetFillOauthScope(true) |
| 385 | .Build(); |
| 386 | |
Jan Tattermusch | cd7a405 | 2015-07-25 01:47:25 -0700 | [diff] [blame] | 387 | var headers = new Metadata(); |
| 388 | headerInterceptor(headers); |
| 389 | var response = client.UnaryCall(request, headers: headers); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 390 | |
| 391 | Assert.AreEqual(AuthScopeResponse, response.OauthScope); |
| 392 | Assert.AreEqual(ServiceAccountUser, response.Username); |
| 393 | Console.WriteLine("Passed!"); |
| 394 | } |
| 395 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 396 | public static async Task RunCancelAfterBeginAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 397 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 398 | Console.WriteLine("running cancel_after_begin"); |
| 399 | |
| 400 | var cts = new CancellationTokenSource(); |
| 401 | using (var call = client.StreamingInputCall(cancellationToken: cts.Token)) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 402 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 403 | // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it. |
| 404 | await Task.Delay(1000); |
| 405 | cts.Cancel(); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 406 | |
Jan Tattermusch | c8d7b84 | 2015-08-07 20:52:21 -0700 | [diff] [blame] | 407 | var ex = Assert.Throws<RpcException>(async () => await call.ResponseAsync); |
| 408 | Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 409 | } |
| 410 | Console.WriteLine("Passed!"); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 413 | public static async Task RunCancelAfterFirstResponseAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 414 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 415 | Console.WriteLine("running cancel_after_first_response"); |
| 416 | |
| 417 | var cts = new CancellationTokenSource(); |
| 418 | using (var call = client.FullDuplexCall(cancellationToken: cts.Token)) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 419 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 420 | await call.RequestStream.WriteAsync(StreamingOutputCallRequest.CreateBuilder() |
| 421 | .SetResponseType(PayloadType.COMPRESSABLE) |
| 422 | .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415)) |
| 423 | .SetPayload(CreateZerosPayload(27182)).Build()); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 424 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 425 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 426 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 427 | Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length); |
| 428 | |
| 429 | cts.Cancel(); |
| 430 | |
Jan Tattermusch | c8d7b84 | 2015-08-07 20:52:21 -0700 | [diff] [blame] | 431 | var ex = Assert.Throws<RpcException>(async () => await call.ResponseStream.MoveNext()); |
| 432 | Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 433 | } |
| 434 | Console.WriteLine("Passed!"); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 437 | // This is not an official interop test, but it's useful. |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 438 | public static void RunBenchmarkEmptyUnary(TestService.ITestServiceClient client) |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 439 | { |
| 440 | BenchmarkUtil.RunBenchmark(10000, 10000, |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 441 | () => { client.EmptyCall(Empty.DefaultInstance); }); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 442 | } |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 443 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 444 | private static Payload CreateZerosPayload(int size) |
| 445 | { |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 446 | return Payload.CreateBuilder().SetBody(ByteString.CopyFrom(new byte[size])).Build(); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | private static ClientOptions ParseArguments(string[] args) |
| 450 | { |
| 451 | var options = new ClientOptions(); |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 452 | foreach (string arg in args) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 453 | { |
| 454 | ParseArgument(arg, options); |
| 455 | if (options.help) |
| 456 | { |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | return options; |
| 461 | } |
| 462 | |
| 463 | private static void ParseArgument(string arg, ClientOptions options) |
| 464 | { |
| 465 | Match match; |
| 466 | match = Regex.Match(arg, "--server_host=(.*)"); |
| 467 | if (match.Success) |
| 468 | { |
| 469 | options.serverHost = match.Groups[1].Value.Trim(); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | match = Regex.Match(arg, "--server_host_override=(.*)"); |
| 474 | if (match.Success) |
| 475 | { |
| 476 | options.serverHostOverride = match.Groups[1].Value.Trim(); |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | match = Regex.Match(arg, "--server_port=(.*)"); |
| 481 | if (match.Success) |
| 482 | { |
| 483 | options.serverPort = int.Parse(match.Groups[1].Value.Trim()); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | match = Regex.Match(arg, "--test_case=(.*)"); |
| 488 | if (match.Success) |
| 489 | { |
| 490 | options.testCase = match.Groups[1].Value.Trim(); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | match = Regex.Match(arg, "--use_tls=(.*)"); |
| 495 | if (match.Success) |
| 496 | { |
| 497 | options.useTls = bool.Parse(match.Groups[1].Value.Trim()); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | match = Regex.Match(arg, "--use_test_ca=(.*)"); |
| 502 | if (match.Success) |
| 503 | { |
| 504 | options.useTestCa = bool.Parse(match.Groups[1].Value.Trim()); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | Console.WriteLine(string.Format("Unrecognized argument \"{0}\"", arg)); |
| 509 | options.help = true; |
| 510 | } |
| 511 | } |
| 512 | } |