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