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