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 | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 36 | using System.IO; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 37 | using System.Text.RegularExpressions; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 38 | using System.Threading; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 39 | using System.Threading.Tasks; |
Jan Tattermusch | 1ca56b9 | 2015-04-27 11:03:06 -0700 | [diff] [blame] | 40 | |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 41 | using CommandLine; |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 42 | using CommandLine.Text; |
Jan Tattermusch | 67c4587 | 2015-08-27 18:12:39 -0700 | [diff] [blame] | 43 | using Google.Apis.Auth.OAuth2; |
| 44 | using Google.Protobuf; |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 45 | using Grpc.Auth; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 46 | using Grpc.Core; |
| 47 | using Grpc.Core.Utils; |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 48 | using Grpc.Testing; |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 49 | using Newtonsoft.Json.Linq; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 50 | using NUnit.Framework; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 51 | |
Jan Tattermusch | 8b86b15 | 2015-02-19 21:01:05 -0800 | [diff] [blame] | 52 | namespace Grpc.IntegrationTesting |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 53 | { |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 54 | public class InteropClient |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 55 | { |
| 56 | private class ClientOptions |
| 57 | { |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 58 | [Option("server_host", DefaultValue = "127.0.0.1")] |
| 59 | public string ServerHost { get; set; } |
| 60 | |
| 61 | [Option("server_host_override", DefaultValue = TestCredentials.DefaultHostOverride)] |
| 62 | public string ServerHostOverride { get; set; } |
| 63 | |
| 64 | [Option("server_port", Required = true)] |
| 65 | public int ServerPort { get; set; } |
| 66 | |
| 67 | [Option("test_case", DefaultValue = "large_unary")] |
| 68 | public string TestCase { get; set; } |
| 69 | |
Jan Tattermusch | 7828e81 | 2015-10-07 17:27:48 -0700 | [diff] [blame] | 70 | // Deliberately using nullable bool type to allow --use_tls=true syntax (as opposed to --use_tls) |
| 71 | [Option("use_tls", DefaultValue = false)] |
| 72 | public bool? UseTls { get; set; } |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 73 | |
Jan Tattermusch | 7828e81 | 2015-10-07 17:27:48 -0700 | [diff] [blame] | 74 | // Deliberately using nullable bool type to allow --use_test_ca=true syntax (as opposed to --use_test_ca) |
| 75 | [Option("use_test_ca", DefaultValue = false)] |
| 76 | public bool? UseTestCa { get; set; } |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 77 | |
| 78 | [Option("default_service_account", Required = false)] |
| 79 | public string DefaultServiceAccount { get; set; } |
| 80 | |
| 81 | [Option("oauth_scope", Required = false)] |
| 82 | public string OAuthScope { get; set; } |
| 83 | |
| 84 | [Option("service_account_key_file", Required = false)] |
| 85 | public string ServiceAccountKeyFile { get; set; } |
| 86 | |
| 87 | [HelpOption] |
| 88 | public string GetUsage() |
| 89 | { |
| 90 | var help = new HelpText |
| 91 | { |
| 92 | Heading = "gRPC C# interop testing client", |
| 93 | AddDashesToOption = true |
| 94 | }; |
| 95 | help.AddPreOptionsLine("Usage:"); |
| 96 | help.AddOptions(this); |
| 97 | return help; |
| 98 | } |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | ClientOptions options; |
| 102 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 103 | private InteropClient(ClientOptions options) |
Jan Tattermusch | 392d1e0 | 2015-02-09 11:13:51 -0800 | [diff] [blame] | 104 | { |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 105 | this.options = options; |
| 106 | } |
| 107 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 108 | public static void Run(string[] args) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 109 | { |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 110 | var options = new ClientOptions(); |
| 111 | if (!Parser.Default.ParseArguments(args, options)) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 112 | { |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 113 | Environment.Exit(1); |
| 114 | } |
| 115 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 116 | var interopClient = new InteropClient(options); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 117 | interopClient.Run().Wait(); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 120 | private async Task Run() |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 121 | { |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 122 | var credentials = await CreateCredentialsAsync(); |
| 123 | |
| 124 | List<ChannelOption> channelOptions = null; |
| 125 | if (!string.IsNullOrEmpty(options.ServerHostOverride)) |
| 126 | { |
| 127 | channelOptions = new List<ChannelOption> |
| 128 | { |
| 129 | new ChannelOption(ChannelOptions.SslTargetNameOverride, options.ServerHostOverride) |
| 130 | }; |
| 131 | } |
| 132 | var channel = new Channel(options.ServerHost, options.ServerPort, credentials, channelOptions); |
| 133 | TestService.TestServiceClient client = new TestService.TestServiceClient(channel); |
| 134 | await RunTestCaseAsync(client, options); |
| 135 | await channel.ShutdownAsync(); |
| 136 | } |
| 137 | |
Jan Tattermusch | 5bd7005 | 2015-10-06 16:47:49 -0700 | [diff] [blame] | 138 | private async Task<ChannelCredentials> CreateCredentialsAsync() |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 139 | { |
Jan Tattermusch | beffc77 | 2015-10-22 13:28:22 -0700 | [diff] [blame^] | 140 | var credentials = ChannelCredentials.Insecure; |
| 141 | if (options.UseTls.Value) |
| 142 | { |
| 143 | credentials = options.UseTestCa.Value ? TestCredentials.CreateSslCredentials() : new SslCredentials(); |
| 144 | } |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 145 | |
| 146 | if (options.TestCase == "jwt_token_creds") |
| 147 | { |
| 148 | var googleCredential = await GoogleCredential.GetApplicationDefaultAsync(); |
| 149 | Assert.IsTrue(googleCredential.IsCreateScopedRequired); |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 150 | credentials = ChannelCredentials.Create(credentials, googleCredential.ToCallCredentials()); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (options.TestCase == "compute_engine_creds") |
| 154 | { |
| 155 | var googleCredential = await GoogleCredential.GetApplicationDefaultAsync(); |
| 156 | Assert.IsFalse(googleCredential.IsCreateScopedRequired); |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 157 | credentials = ChannelCredentials.Create(credentials, googleCredential.ToCallCredentials()); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 158 | } |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 159 | return credentials; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 162 | private async Task RunTestCaseAsync(TestService.TestServiceClient client, ClientOptions options) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 163 | { |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 164 | switch (options.TestCase) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 165 | { |
| 166 | case "empty_unary": |
| 167 | RunEmptyUnary(client); |
| 168 | break; |
| 169 | case "large_unary": |
| 170 | RunLargeUnary(client); |
| 171 | break; |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 172 | case "client_streaming": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 173 | await RunClientStreamingAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 174 | break; |
| 175 | case "server_streaming": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 176 | await RunServerStreamingAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 177 | break; |
| 178 | case "ping_pong": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 179 | await RunPingPongAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 180 | break; |
| 181 | case "empty_stream": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 182 | await RunEmptyStreamAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 183 | break; |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 184 | case "compute_engine_creds": |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 185 | RunComputeEngineCreds(client, options.DefaultServiceAccount, options.OAuthScope); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 186 | break; |
| 187 | case "jwt_token_creds": |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 188 | RunJwtTokenCreds(client); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 189 | break; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 190 | case "oauth2_auth_token": |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 191 | await RunOAuth2AuthTokenAsync(client, options.OAuthScope); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 192 | break; |
| 193 | case "per_rpc_creds": |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 194 | await RunPerRpcCredsAsync(client, options.OAuthScope); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 195 | break; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 196 | case "cancel_after_begin": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 197 | await RunCancelAfterBeginAsync(client); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 198 | break; |
| 199 | case "cancel_after_first_response": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 200 | await RunCancelAfterFirstResponseAsync(client); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 201 | break; |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 202 | case "timeout_on_sleeping_server": |
| 203 | await RunTimeoutOnSleepingServerAsync(client); |
| 204 | break; |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 205 | case "benchmark_empty_unary": |
| 206 | RunBenchmarkEmptyUnary(client); |
| 207 | break; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 208 | default: |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 209 | throw new ArgumentException("Unknown test case " + options.TestCase); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 213 | public static void RunEmptyUnary(TestService.ITestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 214 | { |
| 215 | Console.WriteLine("running empty_unary"); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 216 | var response = client.EmptyCall(new Empty()); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 217 | Assert.IsNotNull(response); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 218 | Console.WriteLine("Passed!"); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 221 | public static void RunLargeUnary(TestService.ITestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 222 | { |
| 223 | Console.WriteLine("running large_unary"); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 224 | var request = new SimpleRequest |
| 225 | { |
| 226 | ResponseType = PayloadType.COMPRESSABLE, |
| 227 | ResponseSize = 314159, |
| 228 | Payload = CreateZerosPayload(271828) |
| 229 | }; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 230 | |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 231 | var response = client.UnaryCall(request); |
| 232 | |
| 233 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 234 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 235 | Console.WriteLine("Passed!"); |
| 236 | } |
| 237 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 238 | public static async Task RunClientStreamingAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 239 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 240 | Console.WriteLine("running client_streaming"); |
| 241 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 242 | var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => new StreamingInputCallRequest { Payload = CreateZerosPayload(size) }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 243 | |
| 244 | using (var call = client.StreamingInputCall()) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 245 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 246 | await call.RequestStream.WriteAllAsync(bodySizes); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 247 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 248 | var response = await call.ResponseAsync; |
| 249 | Assert.AreEqual(74922, response.AggregatedPayloadSize); |
| 250 | } |
| 251 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 254 | public static async Task RunServerStreamingAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 255 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 256 | Console.WriteLine("running server_streaming"); |
| 257 | |
| 258 | var bodySizes = new List<int> { 31415, 9, 2653, 58979 }; |
| 259 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 260 | var request = new StreamingOutputCallRequest |
| 261 | { |
| 262 | ResponseType = PayloadType.COMPRESSABLE, |
| 263 | ResponseParameters = { bodySizes.ConvertAll((size) => new ResponseParameters { Size = size }) } |
| 264 | }; |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 265 | |
| 266 | using (var call = client.StreamingOutputCall(request)) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 267 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 268 | var responseList = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 269 | foreach (var res in responseList) |
| 270 | { |
| 271 | Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type); |
| 272 | } |
| 273 | CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length)); |
| 274 | } |
| 275 | Console.WriteLine("Passed!"); |
| 276 | } |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 277 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 278 | public static async Task RunPingPongAsync(TestService.ITestServiceClient client) |
| 279 | { |
| 280 | Console.WriteLine("running ping_pong"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 281 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 282 | using (var call = client.FullDuplexCall()) |
| 283 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 284 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 285 | { |
| 286 | ResponseType = PayloadType.COMPRESSABLE, |
| 287 | ResponseParameters = { new ResponseParameters { Size = 31415 } }, |
| 288 | Payload = CreateZerosPayload(27182) |
| 289 | }); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 290 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 291 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 292 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 293 | Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length); |
| 294 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 295 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 296 | { |
| 297 | ResponseType = PayloadType.COMPRESSABLE, |
| 298 | ResponseParameters = { new ResponseParameters { Size = 9 } }, |
| 299 | Payload = CreateZerosPayload(8) |
| 300 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 301 | |
| 302 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 303 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 304 | Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length); |
| 305 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 306 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 307 | { |
| 308 | ResponseType = PayloadType.COMPRESSABLE, |
| 309 | ResponseParameters = { new ResponseParameters { Size = 2653 } }, |
| 310 | Payload = CreateZerosPayload(1828) |
| 311 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 312 | |
| 313 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 314 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 315 | Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length); |
| 316 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 317 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 318 | { |
| 319 | ResponseType = PayloadType.COMPRESSABLE, |
| 320 | ResponseParameters = { new ResponseParameters { Size = 58979 } }, |
| 321 | Payload = CreateZerosPayload(45904) |
| 322 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 323 | |
| 324 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 325 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 326 | Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length); |
| 327 | |
| 328 | await call.RequestStream.CompleteAsync(); |
| 329 | |
| 330 | Assert.IsFalse(await call.ResponseStream.MoveNext()); |
| 331 | } |
| 332 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 335 | public static async Task RunEmptyStreamAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 336 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 337 | Console.WriteLine("running empty_stream"); |
| 338 | using (var call = client.FullDuplexCall()) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 339 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 340 | await call.RequestStream.CompleteAsync(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 341 | |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 342 | var responseList = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 343 | Assert.AreEqual(0, responseList.Count); |
| 344 | } |
| 345 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 348 | public static void RunComputeEngineCreds(TestService.TestServiceClient client, string defaultServiceAccount, string oauthScope) |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 349 | { |
| 350 | Console.WriteLine("running compute_engine_creds"); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 351 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 352 | var request = new SimpleRequest |
| 353 | { |
| 354 | ResponseType = PayloadType.COMPRESSABLE, |
| 355 | ResponseSize = 314159, |
| 356 | Payload = CreateZerosPayload(271828), |
| 357 | FillUsername = true, |
| 358 | FillOauthScope = true |
| 359 | }; |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 360 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 361 | // not setting credentials here because they were set on channel already |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 362 | var response = client.UnaryCall(request); |
| 363 | |
| 364 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 365 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 366 | Assert.False(string.IsNullOrEmpty(response.OauthScope)); |
| 367 | Assert.True(oauthScope.Contains(response.OauthScope)); |
| 368 | Assert.AreEqual(defaultServiceAccount, response.Username); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 369 | Console.WriteLine("Passed!"); |
| 370 | } |
| 371 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 372 | public static void RunJwtTokenCreds(TestService.TestServiceClient client) |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 373 | { |
| 374 | Console.WriteLine("running jwt_token_creds"); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 375 | |
Jan Tattermusch | 46e85b0 | 2015-08-13 10:31:05 -0700 | [diff] [blame] | 376 | var request = new SimpleRequest |
| 377 | { |
| 378 | ResponseType = PayloadType.COMPRESSABLE, |
| 379 | ResponseSize = 314159, |
| 380 | Payload = CreateZerosPayload(271828), |
| 381 | FillUsername = true, |
Jan Tattermusch | 46e85b0 | 2015-08-13 10:31:05 -0700 | [diff] [blame] | 382 | }; |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 383 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 384 | // not setting credentials here because they were set on channel already |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 385 | var response = client.UnaryCall(request); |
| 386 | |
| 387 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 388 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 389 | Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 390 | Console.WriteLine("Passed!"); |
| 391 | } |
| 392 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 393 | public static async Task RunOAuth2AuthTokenAsync(TestService.TestServiceClient client, string oauthScope) |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 394 | { |
| 395 | Console.WriteLine("running oauth2_auth_token"); |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 396 | ITokenAccess credential = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { oauthScope }); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 397 | string oauth2Token = await credential.GetAccessTokenForRequestAsync(); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 398 | |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 399 | var credentials = GoogleGrpcCredentials.FromAccessToken(oauth2Token); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 400 | var request = new SimpleRequest |
| 401 | { |
| 402 | FillUsername = true, |
| 403 | FillOauthScope = true |
| 404 | }; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 405 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 406 | var response = client.UnaryCall(request, new CallOptions(credentials: credentials)); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 407 | |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 408 | Assert.False(string.IsNullOrEmpty(response.OauthScope)); |
| 409 | Assert.True(oauthScope.Contains(response.OauthScope)); |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 410 | Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 411 | Console.WriteLine("Passed!"); |
| 412 | } |
| 413 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 414 | public static async Task RunPerRpcCredsAsync(TestService.TestServiceClient client, string oauthScope) |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 415 | { |
| 416 | Console.WriteLine("running per_rpc_creds"); |
Jan Tattermusch | cf72a3a | 2015-10-08 08:44:20 -0700 | [diff] [blame] | 417 | ITokenAccess googleCredential = await GoogleCredential.GetApplicationDefaultAsync(); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 418 | |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 419 | var credentials = googleCredential.ToCallCredentials(); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 420 | var request = new SimpleRequest |
| 421 | { |
| 422 | FillUsername = true, |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 423 | }; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 424 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 425 | var response = client.UnaryCall(request, new CallOptions(credentials: credentials)); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 426 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 427 | Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 428 | Console.WriteLine("Passed!"); |
| 429 | } |
| 430 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 431 | public static async Task RunCancelAfterBeginAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 432 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 433 | Console.WriteLine("running cancel_after_begin"); |
| 434 | |
| 435 | var cts = new CancellationTokenSource(); |
| 436 | using (var call = client.StreamingInputCall(cancellationToken: cts.Token)) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 437 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 438 | // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it. |
| 439 | await Task.Delay(1000); |
| 440 | cts.Cancel(); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 441 | |
Jan Tattermusch | c8d7b84 | 2015-08-07 20:52:21 -0700 | [diff] [blame] | 442 | var ex = Assert.Throws<RpcException>(async () => await call.ResponseAsync); |
| 443 | Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 444 | } |
| 445 | Console.WriteLine("Passed!"); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 448 | public static async Task RunCancelAfterFirstResponseAsync(TestService.ITestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 449 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 450 | Console.WriteLine("running cancel_after_first_response"); |
| 451 | |
| 452 | var cts = new CancellationTokenSource(); |
| 453 | using (var call = client.FullDuplexCall(cancellationToken: cts.Token)) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 454 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 455 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 456 | { |
| 457 | ResponseType = PayloadType.COMPRESSABLE, |
| 458 | ResponseParameters = { new ResponseParameters { Size = 31415 } }, |
| 459 | Payload = CreateZerosPayload(27182) |
| 460 | }); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 461 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 462 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 463 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 464 | Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length); |
| 465 | |
| 466 | cts.Cancel(); |
| 467 | |
Jan Tattermusch | c8d7b84 | 2015-08-07 20:52:21 -0700 | [diff] [blame] | 468 | var ex = Assert.Throws<RpcException>(async () => await call.ResponseStream.MoveNext()); |
| 469 | Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 470 | } |
| 471 | Console.WriteLine("Passed!"); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 474 | public static async Task RunTimeoutOnSleepingServerAsync(TestService.ITestServiceClient client) |
| 475 | { |
| 476 | Console.WriteLine("running timeout_on_sleeping_server"); |
| 477 | |
| 478 | var deadline = DateTime.UtcNow.AddMilliseconds(1); |
| 479 | using (var call = client.FullDuplexCall(deadline: deadline)) |
| 480 | { |
| 481 | try |
| 482 | { |
Jan Tattermusch | 0608a00 | 2015-08-26 08:50:19 -0700 | [diff] [blame] | 483 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest { Payload = CreateZerosPayload(27182) }); |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 484 | } |
| 485 | catch (InvalidOperationException) |
| 486 | { |
| 487 | // Deadline was reached before write has started. Eat the exception and continue. |
| 488 | } |
| 489 | |
| 490 | var ex = Assert.Throws<RpcException>(async () => await call.ResponseStream.MoveNext()); |
| 491 | Assert.AreEqual(StatusCode.DeadlineExceeded, ex.Status.StatusCode); |
| 492 | } |
| 493 | Console.WriteLine("Passed!"); |
| 494 | } |
| 495 | |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 496 | // This is not an official interop test, but it's useful. |
Jan Tattermusch | 7eb3a76 | 2015-05-07 14:26:13 -0700 | [diff] [blame] | 497 | public static void RunBenchmarkEmptyUnary(TestService.ITestServiceClient client) |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 498 | { |
| 499 | BenchmarkUtil.RunBenchmark(10000, 10000, |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 500 | () => { client.EmptyCall(new Empty()); }); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 501 | } |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 502 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 503 | private static Payload CreateZerosPayload(int size) |
| 504 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 505 | return new Payload { Body = ByteString.CopyFrom(new byte[size]) }; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 506 | } |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 507 | |
| 508 | // extracts the client_email field from service account file used for auth test cases |
| 509 | private static string GetEmailFromServiceAccountFile() |
| 510 | { |
| 511 | string keyFile = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"); |
| 512 | Assert.IsNotNull(keyFile); |
| 513 | |
| 514 | var jobject = JObject.Parse(File.ReadAllText(keyFile)); |
| 515 | string email = jobject.GetValue("client_email").Value<string>(); |
| 516 | Assert.IsTrue(email.Length > 0); // spec requires nonempty client email. |
| 517 | return email; |
| 518 | } |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 519 | } |
| 520 | } |