Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | acb842c | 2016-03-25 16:54:14 -0700 | [diff] [blame] | 3 | // Copyright 2015-2016, 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 | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 37 | using System.Linq; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 38 | using System.Text.RegularExpressions; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 39 | using System.Threading; |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 40 | using System.Threading.Tasks; |
Jan Tattermusch | 1ca56b9 | 2015-04-27 11:03:06 -0700 | [diff] [blame] | 41 | |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 42 | using CommandLine; |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 43 | using CommandLine.Text; |
Jan Tattermusch | 67c4587 | 2015-08-27 18:12:39 -0700 | [diff] [blame] | 44 | using Google.Apis.Auth.OAuth2; |
| 45 | using Google.Protobuf; |
Jan Tattermusch | dca6e88 | 2015-04-22 16:56:27 -0700 | [diff] [blame] | 46 | using Grpc.Auth; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 47 | using Grpc.Core; |
| 48 | using Grpc.Core.Utils; |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 49 | using Grpc.Testing; |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 50 | using Newtonsoft.Json.Linq; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 51 | using NUnit.Framework; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 52 | |
Jan Tattermusch | 8b86b15 | 2015-02-19 21:01:05 -0800 | [diff] [blame] | 53 | namespace Grpc.IntegrationTesting |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 54 | { |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 55 | public class InteropClient |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 56 | { |
| 57 | private class ClientOptions |
| 58 | { |
Paul Marks | 3a5bba0 | 2017-02-07 16:28:09 -0800 | [diff] [blame] | 59 | [Option("server_host", Default = "localhost")] |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 60 | public string ServerHost { get; set; } |
| 61 | |
Jan Tattermusch | 678ec90 | 2016-08-08 12:54:16 +0800 | [diff] [blame] | 62 | [Option("server_host_override", Default = TestCredentials.DefaultHostOverride)] |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 63 | public string ServerHostOverride { get; set; } |
| 64 | |
| 65 | [Option("server_port", Required = true)] |
| 66 | public int ServerPort { get; set; } |
| 67 | |
Jan Tattermusch | 678ec90 | 2016-08-08 12:54:16 +0800 | [diff] [blame] | 68 | [Option("test_case", Default = "large_unary")] |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 69 | public string TestCase { get; set; } |
| 70 | |
Jan Tattermusch | 7828e81 | 2015-10-07 17:27:48 -0700 | [diff] [blame] | 71 | // Deliberately using nullable bool type to allow --use_tls=true syntax (as opposed to --use_tls) |
Jan Tattermusch | 678ec90 | 2016-08-08 12:54:16 +0800 | [diff] [blame] | 72 | [Option("use_tls", Default = false)] |
Jan Tattermusch | 7828e81 | 2015-10-07 17:27:48 -0700 | [diff] [blame] | 73 | public bool? UseTls { get; set; } |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 74 | |
Jan Tattermusch | 7828e81 | 2015-10-07 17:27:48 -0700 | [diff] [blame] | 75 | // Deliberately using nullable bool type to allow --use_test_ca=true syntax (as opposed to --use_test_ca) |
Jan Tattermusch | 678ec90 | 2016-08-08 12:54:16 +0800 | [diff] [blame] | 76 | [Option("use_test_ca", Default = false)] |
Jan Tattermusch | 7828e81 | 2015-10-07 17:27:48 -0700 | [diff] [blame] | 77 | public bool? UseTestCa { get; set; } |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 78 | |
| 79 | [Option("default_service_account", Required = false)] |
| 80 | public string DefaultServiceAccount { get; set; } |
| 81 | |
| 82 | [Option("oauth_scope", Required = false)] |
| 83 | public string OAuthScope { get; set; } |
| 84 | |
| 85 | [Option("service_account_key_file", Required = false)] |
| 86 | public string ServiceAccountKeyFile { get; set; } |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | ClientOptions options; |
| 90 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 91 | private InteropClient(ClientOptions options) |
Jan Tattermusch | 392d1e0 | 2015-02-09 11:13:51 -0800 | [diff] [blame] | 92 | { |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 93 | this.options = options; |
| 94 | } |
| 95 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 96 | public static void Run(string[] args) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 97 | { |
Jan Tattermusch | 90cbde8 | 2016-08-08 14:33:28 +0800 | [diff] [blame] | 98 | var parserResult = Parser.Default.ParseArguments<ClientOptions>(args) |
| 99 | .WithNotParsed(errors => Environment.Exit(1)) |
| 100 | .WithParsed(options => |
| 101 | { |
| 102 | var interopClient = new InteropClient(options); |
| 103 | interopClient.Run().Wait(); |
| 104 | }); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 107 | private async Task Run() |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 108 | { |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 109 | var credentials = await CreateCredentialsAsync(); |
| 110 | |
| 111 | List<ChannelOption> channelOptions = null; |
| 112 | if (!string.IsNullOrEmpty(options.ServerHostOverride)) |
| 113 | { |
| 114 | channelOptions = new List<ChannelOption> |
| 115 | { |
| 116 | new ChannelOption(ChannelOptions.SslTargetNameOverride, options.ServerHostOverride) |
| 117 | }; |
| 118 | } |
| 119 | var channel = new Channel(options.ServerHost, options.ServerPort, credentials, channelOptions); |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 120 | await RunTestCaseAsync(channel, options); |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 121 | await channel.ShutdownAsync(); |
| 122 | } |
| 123 | |
Jan Tattermusch | 5bd7005 | 2015-10-06 16:47:49 -0700 | [diff] [blame] | 124 | private async Task<ChannelCredentials> CreateCredentialsAsync() |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 125 | { |
Jan Tattermusch | beffc77 | 2015-10-22 13:28:22 -0700 | [diff] [blame] | 126 | var credentials = ChannelCredentials.Insecure; |
| 127 | if (options.UseTls.Value) |
| 128 | { |
| 129 | credentials = options.UseTestCa.Value ? TestCredentials.CreateSslCredentials() : new SslCredentials(); |
| 130 | } |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 131 | |
| 132 | if (options.TestCase == "jwt_token_creds") |
| 133 | { |
| 134 | var googleCredential = await GoogleCredential.GetApplicationDefaultAsync(); |
| 135 | Assert.IsTrue(googleCredential.IsCreateScopedRequired); |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 136 | credentials = ChannelCredentials.Create(credentials, googleCredential.ToCallCredentials()); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | if (options.TestCase == "compute_engine_creds") |
| 140 | { |
| 141 | var googleCredential = await GoogleCredential.GetApplicationDefaultAsync(); |
| 142 | Assert.IsFalse(googleCredential.IsCreateScopedRequired); |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 143 | credentials = ChannelCredentials.Create(credentials, googleCredential.ToCallCredentials()); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 144 | } |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 145 | return credentials; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 148 | private async Task RunTestCaseAsync(Channel channel, ClientOptions options) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 149 | { |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 150 | var client = new TestService.TestServiceClient(channel); |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 151 | switch (options.TestCase) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 152 | { |
| 153 | case "empty_unary": |
| 154 | RunEmptyUnary(client); |
| 155 | break; |
| 156 | case "large_unary": |
| 157 | RunLargeUnary(client); |
| 158 | break; |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 159 | case "client_streaming": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 160 | await RunClientStreamingAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 161 | break; |
| 162 | case "server_streaming": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 163 | await RunServerStreamingAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 164 | break; |
| 165 | case "ping_pong": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 166 | await RunPingPongAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 167 | break; |
| 168 | case "empty_stream": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 169 | await RunEmptyStreamAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 170 | break; |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 171 | case "compute_engine_creds": |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 172 | RunComputeEngineCreds(client, options.DefaultServiceAccount, options.OAuthScope); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 173 | break; |
| 174 | case "jwt_token_creds": |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 175 | RunJwtTokenCreds(client); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 176 | break; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 177 | case "oauth2_auth_token": |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 178 | await RunOAuth2AuthTokenAsync(client, options.OAuthScope); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 179 | break; |
| 180 | case "per_rpc_creds": |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 181 | await RunPerRpcCredsAsync(client, options.OAuthScope); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 182 | break; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 183 | case "cancel_after_begin": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 184 | await RunCancelAfterBeginAsync(client); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 185 | break; |
| 186 | case "cancel_after_first_response": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 187 | await RunCancelAfterFirstResponseAsync(client); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 188 | break; |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 189 | case "timeout_on_sleeping_server": |
| 190 | await RunTimeoutOnSleepingServerAsync(client); |
| 191 | break; |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 192 | case "custom_metadata": |
| 193 | await RunCustomMetadataAsync(client); |
| 194 | break; |
| 195 | case "status_code_and_message": |
| 196 | await RunStatusCodeAndMessageAsync(client); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 197 | break; |
Noah Eisen | 9a171c7 | 2016-10-21 16:26:40 -0700 | [diff] [blame] | 198 | case "unimplemented_service": |
| 199 | RunUnimplementedService(new UnimplementedService.UnimplementedServiceClient(channel)); |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 200 | break; |
Noah Eisen | b2a2647 | 2016-10-21 16:43:57 -0700 | [diff] [blame] | 201 | case "unimplemented_method": |
| 202 | RunUnimplementedMethod(client); |
| 203 | break; |
Jan Tattermusch | a7daf1e | 2016-06-21 19:11:49 -0700 | [diff] [blame] | 204 | case "client_compressed_unary": |
| 205 | RunClientCompressedUnary(client); |
| 206 | break; |
| 207 | case "client_compressed_streaming": |
| 208 | await RunClientCompressedStreamingAsync(client); |
| 209 | break; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 210 | default: |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 211 | throw new ArgumentException("Unknown test case " + options.TestCase); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 215 | public static void RunEmptyUnary(TestService.TestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 216 | { |
| 217 | Console.WriteLine("running empty_unary"); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 218 | var response = client.EmptyCall(new Empty()); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 219 | Assert.IsNotNull(response); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 220 | Console.WriteLine("Passed!"); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 223 | public static void RunLargeUnary(TestService.TestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 224 | { |
| 225 | Console.WriteLine("running large_unary"); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 226 | var request = new SimpleRequest |
| 227 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 228 | ResponseSize = 314159, |
| 229 | Payload = CreateZerosPayload(271828) |
| 230 | }; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 231 | var response = client.UnaryCall(request); |
| 232 | |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 233 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 234 | Console.WriteLine("Passed!"); |
| 235 | } |
| 236 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 237 | public static async Task RunClientStreamingAsync(TestService.TestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 238 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 239 | Console.WriteLine("running client_streaming"); |
| 240 | |
Jan Tattermusch | 317b8ac | 2016-06-16 09:36:11 -0700 | [diff] [blame] | 241 | var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.Select((size) => new StreamingInputCallRequest { Payload = CreateZerosPayload(size) }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 242 | |
| 243 | using (var call = client.StreamingInputCall()) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 244 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 245 | await call.RequestStream.WriteAllAsync(bodySizes); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 246 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 247 | var response = await call.ResponseAsync; |
| 248 | Assert.AreEqual(74922, response.AggregatedPayloadSize); |
| 249 | } |
| 250 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 253 | public static async Task RunServerStreamingAsync(TestService.TestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 254 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 255 | Console.WriteLine("running server_streaming"); |
| 256 | |
| 257 | var bodySizes = new List<int> { 31415, 9, 2653, 58979 }; |
| 258 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 259 | var request = new StreamingOutputCallRequest |
| 260 | { |
Jan Tattermusch | 317b8ac | 2016-06-16 09:36:11 -0700 | [diff] [blame] | 261 | ResponseParameters = { bodySizes.Select((size) => new ResponseParameters { Size = size }) } |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 262 | }; |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 263 | |
| 264 | using (var call = client.StreamingOutputCall(request)) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 265 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 266 | var responseList = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | 317b8ac | 2016-06-16 09:36:11 -0700 | [diff] [blame] | 267 | CollectionAssert.AreEqual(bodySizes, responseList.Select((item) => item.Payload.Body.Length)); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 268 | } |
| 269 | Console.WriteLine("Passed!"); |
| 270 | } |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 271 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 272 | public static async Task RunPingPongAsync(TestService.TestServiceClient client) |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 273 | { |
| 274 | Console.WriteLine("running ping_pong"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 275 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 276 | using (var call = client.FullDuplexCall()) |
| 277 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 278 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 279 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 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()); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 285 | Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length); |
| 286 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 287 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 288 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 289 | ResponseParameters = { new ResponseParameters { Size = 9 } }, |
| 290 | Payload = CreateZerosPayload(8) |
| 291 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 292 | |
| 293 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 294 | Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length); |
| 295 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 296 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 297 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 298 | ResponseParameters = { new ResponseParameters { Size = 2653 } }, |
| 299 | Payload = CreateZerosPayload(1828) |
| 300 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 301 | |
| 302 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 303 | Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length); |
| 304 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 305 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 306 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 307 | ResponseParameters = { new ResponseParameters { Size = 58979 } }, |
| 308 | Payload = CreateZerosPayload(45904) |
| 309 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 310 | |
| 311 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 312 | Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length); |
| 313 | |
| 314 | await call.RequestStream.CompleteAsync(); |
| 315 | |
| 316 | Assert.IsFalse(await call.ResponseStream.MoveNext()); |
| 317 | } |
| 318 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 321 | public static async Task RunEmptyStreamAsync(TestService.TestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 322 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 323 | Console.WriteLine("running empty_stream"); |
| 324 | using (var call = client.FullDuplexCall()) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 325 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 326 | await call.RequestStream.CompleteAsync(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 327 | |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 328 | var responseList = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 329 | Assert.AreEqual(0, responseList.Count); |
| 330 | } |
| 331 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 334 | public static void RunComputeEngineCreds(TestService.TestServiceClient client, string defaultServiceAccount, string oauthScope) |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 335 | { |
| 336 | Console.WriteLine("running compute_engine_creds"); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 337 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 338 | var request = new SimpleRequest |
| 339 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 340 | ResponseSize = 314159, |
| 341 | Payload = CreateZerosPayload(271828), |
| 342 | FillUsername = true, |
| 343 | FillOauthScope = true |
| 344 | }; |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 345 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 346 | // not setting credentials here because they were set on channel already |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 347 | var response = client.UnaryCall(request); |
| 348 | |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 349 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 350 | Assert.False(string.IsNullOrEmpty(response.OauthScope)); |
| 351 | Assert.True(oauthScope.Contains(response.OauthScope)); |
| 352 | Assert.AreEqual(defaultServiceAccount, response.Username); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 353 | Console.WriteLine("Passed!"); |
| 354 | } |
| 355 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 356 | public static void RunJwtTokenCreds(TestService.TestServiceClient client) |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 357 | { |
| 358 | Console.WriteLine("running jwt_token_creds"); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 359 | |
Jan Tattermusch | 46e85b0 | 2015-08-13 10:31:05 -0700 | [diff] [blame] | 360 | var request = new SimpleRequest |
| 361 | { |
Jan Tattermusch | 46e85b0 | 2015-08-13 10:31:05 -0700 | [diff] [blame] | 362 | ResponseSize = 314159, |
| 363 | Payload = CreateZerosPayload(271828), |
| 364 | FillUsername = true, |
Jan Tattermusch | 46e85b0 | 2015-08-13 10:31:05 -0700 | [diff] [blame] | 365 | }; |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 366 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 367 | // not setting credentials here because they were set on channel already |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 368 | var response = client.UnaryCall(request); |
| 369 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 370 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 371 | Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 372 | Console.WriteLine("Passed!"); |
| 373 | } |
| 374 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 375 | public static async Task RunOAuth2AuthTokenAsync(TestService.TestServiceClient client, string oauthScope) |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 376 | { |
| 377 | Console.WriteLine("running oauth2_auth_token"); |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 378 | ITokenAccess credential = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { oauthScope }); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 379 | string oauth2Token = await credential.GetAccessTokenForRequestAsync(); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 380 | |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 381 | var credentials = GoogleGrpcCredentials.FromAccessToken(oauth2Token); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 382 | var request = new SimpleRequest |
| 383 | { |
| 384 | FillUsername = true, |
| 385 | FillOauthScope = true |
| 386 | }; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 387 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 388 | var response = client.UnaryCall(request, new CallOptions(credentials: credentials)); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 389 | |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 390 | Assert.False(string.IsNullOrEmpty(response.OauthScope)); |
| 391 | Assert.True(oauthScope.Contains(response.OauthScope)); |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 392 | Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 393 | Console.WriteLine("Passed!"); |
| 394 | } |
| 395 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 396 | public static async Task RunPerRpcCredsAsync(TestService.TestServiceClient client, string oauthScope) |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 397 | { |
| 398 | Console.WriteLine("running per_rpc_creds"); |
Jan Tattermusch | cf72a3a | 2015-10-08 08:44:20 -0700 | [diff] [blame] | 399 | ITokenAccess googleCredential = await GoogleCredential.GetApplicationDefaultAsync(); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 400 | |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 401 | var credentials = googleCredential.ToCallCredentials(); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 402 | var request = new SimpleRequest |
| 403 | { |
| 404 | FillUsername = true, |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 405 | }; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 406 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 407 | var response = client.UnaryCall(request, new CallOptions(credentials: credentials)); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 408 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 409 | Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 410 | Console.WriteLine("Passed!"); |
| 411 | } |
| 412 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 413 | public static async Task RunCancelAfterBeginAsync(TestService.TestServiceClient 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_begin"); |
| 416 | |
| 417 | var cts = new CancellationTokenSource(); |
| 418 | using (var call = client.StreamingInputCall(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 | // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it. |
| 421 | await Task.Delay(1000); |
| 422 | cts.Cancel(); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 423 | |
Jan Tattermusch | 9f19bd3 | 2016-04-08 13:32:30 -0700 | [diff] [blame] | 424 | var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseAsync); |
Jan Tattermusch | c8d7b84 | 2015-08-07 20:52:21 -0700 | [diff] [blame] | 425 | Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 426 | } |
| 427 | Console.WriteLine("Passed!"); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 430 | public static async Task RunCancelAfterFirstResponseAsync(TestService.TestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 431 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 432 | Console.WriteLine("running cancel_after_first_response"); |
| 433 | |
| 434 | var cts = new CancellationTokenSource(); |
| 435 | using (var call = client.FullDuplexCall(cancellationToken: cts.Token)) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 436 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 437 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 438 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 439 | ResponseParameters = { new ResponseParameters { Size = 31415 } }, |
| 440 | Payload = CreateZerosPayload(27182) |
| 441 | }); |
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 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 444 | Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length); |
| 445 | |
| 446 | cts.Cancel(); |
| 447 | |
Jan Tattermusch | 71702b1 | 2016-05-25 20:48:38 -0700 | [diff] [blame] | 448 | try |
| 449 | { |
| 450 | // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock. |
| 451 | await call.ResponseStream.MoveNext(); |
| 452 | Assert.Fail(); |
| 453 | } |
| 454 | catch (RpcException ex) |
| 455 | { |
| 456 | Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode); |
| 457 | } |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 458 | } |
| 459 | Console.WriteLine("Passed!"); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 462 | public static async Task RunTimeoutOnSleepingServerAsync(TestService.TestServiceClient client) |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 463 | { |
| 464 | Console.WriteLine("running timeout_on_sleeping_server"); |
| 465 | |
| 466 | var deadline = DateTime.UtcNow.AddMilliseconds(1); |
| 467 | using (var call = client.FullDuplexCall(deadline: deadline)) |
| 468 | { |
| 469 | try |
| 470 | { |
Jan Tattermusch | 0608a00 | 2015-08-26 08:50:19 -0700 | [diff] [blame] | 471 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest { Payload = CreateZerosPayload(27182) }); |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 472 | } |
| 473 | catch (InvalidOperationException) |
| 474 | { |
| 475 | // Deadline was reached before write has started. Eat the exception and continue. |
| 476 | } |
Jan Tattermusch | a46d21d | 2016-05-10 09:57:51 -0700 | [diff] [blame] | 477 | catch (RpcException) |
| 478 | { |
| 479 | // Deadline was reached before write has started. Eat the exception and continue. |
| 480 | } |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 481 | |
Jan Tattermusch | 1e5d9b9 | 2016-05-25 21:38:10 -0700 | [diff] [blame] | 482 | try |
| 483 | { |
| 484 | await call.ResponseStream.MoveNext(); |
| 485 | Assert.Fail(); |
| 486 | } |
| 487 | catch (RpcException ex) |
| 488 | { |
| 489 | // We can't guarantee the status code always DeadlineExceeded. See issue #2685. |
| 490 | Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExceeded, StatusCode.Internal }); |
| 491 | } |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 492 | } |
| 493 | Console.WriteLine("Passed!"); |
| 494 | } |
| 495 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 496 | public static async Task RunCustomMetadataAsync(TestService.TestServiceClient client) |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 497 | { |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 498 | Console.WriteLine("running custom_metadata"); |
| 499 | { |
| 500 | // step 1: test unary call |
| 501 | var request = new SimpleRequest |
| 502 | { |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 503 | ResponseSize = 314159, |
| 504 | Payload = CreateZerosPayload(271828) |
| 505 | }; |
| 506 | |
| 507 | var call = client.UnaryCallAsync(request, headers: CreateTestMetadata()); |
| 508 | await call.ResponseAsync; |
| 509 | |
| 510 | var responseHeaders = await call.ResponseHeadersAsync; |
| 511 | var responseTrailers = call.GetTrailers(); |
| 512 | |
| 513 | Assert.AreEqual("test_initial_metadata_value", responseHeaders.First((entry) => entry.Key == "x-grpc-test-echo-initial").Value); |
| 514 | CollectionAssert.AreEqual(new byte[] { 0xab, 0xab, 0xab }, responseTrailers.First((entry) => entry.Key == "x-grpc-test-echo-trailing-bin").ValueBytes); |
| 515 | } |
| 516 | |
| 517 | { |
| 518 | // step 2: test full duplex call |
| 519 | var request = new StreamingOutputCallRequest |
| 520 | { |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 521 | ResponseParameters = { new ResponseParameters { Size = 31415 } }, |
| 522 | Payload = CreateZerosPayload(27182) |
| 523 | }; |
| 524 | |
| 525 | var call = client.FullDuplexCall(headers: CreateTestMetadata()); |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 526 | |
| 527 | await call.RequestStream.WriteAsync(request); |
| 528 | await call.RequestStream.CompleteAsync(); |
| 529 | await call.ResponseStream.ToListAsync(); |
| 530 | |
Noah Eisen | 2d41ed1 | 2016-10-12 17:53:43 -0700 | [diff] [blame] | 531 | var responseHeaders = await call.ResponseHeadersAsync; |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 532 | var responseTrailers = call.GetTrailers(); |
| 533 | |
| 534 | Assert.AreEqual("test_initial_metadata_value", responseHeaders.First((entry) => entry.Key == "x-grpc-test-echo-initial").Value); |
| 535 | CollectionAssert.AreEqual(new byte[] { 0xab, 0xab, 0xab }, responseTrailers.First((entry) => entry.Key == "x-grpc-test-echo-trailing-bin").ValueBytes); |
| 536 | } |
| 537 | |
| 538 | Console.WriteLine("Passed!"); |
| 539 | } |
| 540 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 541 | public static async Task RunStatusCodeAndMessageAsync(TestService.TestServiceClient client) |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 542 | { |
| 543 | Console.WriteLine("running status_code_and_message"); |
| 544 | var echoStatus = new EchoStatus |
| 545 | { |
| 546 | Code = 2, |
| 547 | Message = "test status message" |
| 548 | }; |
| 549 | |
| 550 | { |
| 551 | // step 1: test unary call |
| 552 | var request = new SimpleRequest { ResponseStatus = echoStatus }; |
| 553 | |
| 554 | var e = Assert.Throws<RpcException>(() => client.UnaryCall(request)); |
| 555 | Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); |
| 556 | Assert.AreEqual(echoStatus.Message, e.Status.Detail); |
| 557 | } |
| 558 | |
| 559 | { |
| 560 | // step 2: test full duplex call |
| 561 | var request = new StreamingOutputCallRequest { ResponseStatus = echoStatus }; |
| 562 | |
| 563 | var call = client.FullDuplexCall(); |
| 564 | await call.RequestStream.WriteAsync(request); |
| 565 | await call.RequestStream.CompleteAsync(); |
| 566 | |
Jan Tattermusch | 71702b1 | 2016-05-25 20:48:38 -0700 | [diff] [blame] | 567 | try |
| 568 | { |
| 569 | // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock. |
| 570 | await call.ResponseStream.ToListAsync(); |
| 571 | Assert.Fail(); |
| 572 | } |
| 573 | catch (RpcException e) |
| 574 | { |
| 575 | Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); |
| 576 | Assert.AreEqual(echoStatus.Message, e.Status.Detail); |
| 577 | } |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | Console.WriteLine("Passed!"); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 581 | } |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 582 | |
Noah Eisen | 9a171c7 | 2016-10-21 16:26:40 -0700 | [diff] [blame] | 583 | public static void RunUnimplementedService(UnimplementedService.UnimplementedServiceClient client) |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 584 | { |
Noah Eisen | 9a171c7 | 2016-10-21 16:26:40 -0700 | [diff] [blame] | 585 | Console.WriteLine("running unimplemented_service"); |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 586 | var e = Assert.Throws<RpcException>(() => client.UnimplementedCall(new Empty())); |
| 587 | |
| 588 | Assert.AreEqual(StatusCode.Unimplemented, e.Status.StatusCode); |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 589 | Console.WriteLine("Passed!"); |
| 590 | } |
| 591 | |
Noah Eisen | b2a2647 | 2016-10-21 16:43:57 -0700 | [diff] [blame] | 592 | public static void RunUnimplementedMethod(TestService.TestServiceClient client) |
| 593 | { |
| 594 | Console.WriteLine("running unimplemented_method"); |
| 595 | var e = Assert.Throws<RpcException>(() => client.UnimplementedCall(new Empty())); |
| 596 | |
| 597 | Assert.AreEqual(StatusCode.Unimplemented, e.Status.StatusCode); |
| 598 | Console.WriteLine("Passed!"); |
| 599 | } |
| 600 | |
Jan Tattermusch | a7daf1e | 2016-06-21 19:11:49 -0700 | [diff] [blame] | 601 | public static void RunClientCompressedUnary(TestService.TestServiceClient client) |
| 602 | { |
| 603 | Console.WriteLine("running client_compressed_unary"); |
| 604 | var probeRequest = new SimpleRequest |
| 605 | { |
| 606 | ExpectCompressed = new BoolValue |
| 607 | { |
| 608 | Value = true // lie about compression |
| 609 | }, |
| 610 | ResponseSize = 314159, |
| 611 | Payload = CreateZerosPayload(271828) |
| 612 | }; |
| 613 | var e = Assert.Throws<RpcException>(() => client.UnaryCall(probeRequest, CreateClientCompressionMetadata(false))); |
| 614 | Assert.AreEqual(StatusCode.InvalidArgument, e.Status.StatusCode); |
| 615 | |
| 616 | var compressedRequest = new SimpleRequest |
| 617 | { |
| 618 | ExpectCompressed = new BoolValue |
| 619 | { |
| 620 | Value = true |
| 621 | }, |
| 622 | ResponseSize = 314159, |
| 623 | Payload = CreateZerosPayload(271828) |
| 624 | }; |
| 625 | var response1 = client.UnaryCall(compressedRequest, CreateClientCompressionMetadata(true)); |
| 626 | Assert.AreEqual(314159, response1.Payload.Body.Length); |
| 627 | |
| 628 | var uncompressedRequest = new SimpleRequest |
| 629 | { |
| 630 | ExpectCompressed = new BoolValue |
| 631 | { |
| 632 | Value = false |
| 633 | }, |
| 634 | ResponseSize = 314159, |
| 635 | Payload = CreateZerosPayload(271828) |
| 636 | }; |
| 637 | var response2 = client.UnaryCall(uncompressedRequest, CreateClientCompressionMetadata(false)); |
| 638 | Assert.AreEqual(314159, response2.Payload.Body.Length); |
| 639 | |
| 640 | Console.WriteLine("Passed!"); |
| 641 | } |
| 642 | |
| 643 | public static async Task RunClientCompressedStreamingAsync(TestService.TestServiceClient client) |
| 644 | { |
| 645 | Console.WriteLine("running client_compressed_streaming"); |
| 646 | try |
| 647 | { |
| 648 | var probeCall = client.StreamingInputCall(CreateClientCompressionMetadata(false)); |
| 649 | await probeCall.RequestStream.WriteAsync(new StreamingInputCallRequest |
| 650 | { |
| 651 | ExpectCompressed = new BoolValue |
| 652 | { |
| 653 | Value = true |
| 654 | }, |
| 655 | Payload = CreateZerosPayload(27182) |
| 656 | }); |
| 657 | |
| 658 | // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock. |
| 659 | await probeCall; |
| 660 | Assert.Fail(); |
| 661 | } |
| 662 | catch (RpcException e) |
| 663 | { |
| 664 | Assert.AreEqual(StatusCode.InvalidArgument, e.Status.StatusCode); |
| 665 | } |
| 666 | |
| 667 | var call = client.StreamingInputCall(CreateClientCompressionMetadata(true)); |
| 668 | await call.RequestStream.WriteAsync(new StreamingInputCallRequest |
| 669 | { |
| 670 | ExpectCompressed = new BoolValue |
| 671 | { |
| 672 | Value = true |
| 673 | }, |
| 674 | Payload = CreateZerosPayload(27182) |
| 675 | }); |
| 676 | |
| 677 | call.RequestStream.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
| 678 | await call.RequestStream.WriteAsync(new StreamingInputCallRequest |
| 679 | { |
| 680 | ExpectCompressed = new BoolValue |
| 681 | { |
| 682 | Value = false |
| 683 | }, |
| 684 | Payload = CreateZerosPayload(45904) |
| 685 | }); |
| 686 | await call.RequestStream.CompleteAsync(); |
| 687 | |
| 688 | var response = await call.ResponseAsync; |
| 689 | Assert.AreEqual(73086, response.AggregatedPayloadSize); |
| 690 | |
| 691 | Console.WriteLine("Passed!"); |
| 692 | } |
| 693 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 694 | private static Payload CreateZerosPayload(int size) |
| 695 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 696 | return new Payload { Body = ByteString.CopyFrom(new byte[size]) }; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 697 | } |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 698 | |
Jan Tattermusch | a7daf1e | 2016-06-21 19:11:49 -0700 | [diff] [blame] | 699 | private static Metadata CreateClientCompressionMetadata(bool compressed) |
| 700 | { |
| 701 | var algorithmName = compressed ? "gzip" : "identity"; |
| 702 | return new Metadata |
| 703 | { |
Jan Tattermusch | 606e35a | 2016-06-22 12:26:36 -0700 | [diff] [blame] | 704 | { new Metadata.Entry(Metadata.CompressionRequestAlgorithmMetadataKey, algorithmName) } |
Jan Tattermusch | a7daf1e | 2016-06-21 19:11:49 -0700 | [diff] [blame] | 705 | }; |
| 706 | } |
| 707 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 708 | // extracts the client_email field from service account file used for auth test cases |
| 709 | private static string GetEmailFromServiceAccountFile() |
| 710 | { |
| 711 | string keyFile = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"); |
| 712 | Assert.IsNotNull(keyFile); |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 713 | var jobject = JObject.Parse(File.ReadAllText(keyFile)); |
| 714 | string email = jobject.GetValue("client_email").Value<string>(); |
| 715 | Assert.IsTrue(email.Length > 0); // spec requires nonempty client email. |
| 716 | return email; |
| 717 | } |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 718 | |
| 719 | private static Metadata CreateTestMetadata() |
| 720 | { |
| 721 | return new Metadata |
| 722 | { |
| 723 | {"x-grpc-test-echo-initial", "test_initial_metadata_value"}, |
| 724 | {"x-grpc-test-echo-trailing-bin", new byte[] {0xab, 0xab, 0xab}} |
| 725 | }; |
| 726 | } |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 727 | } |
| 728 | } |