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 | { |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 59 | [Option("server_host", DefaultValue = "127.0.0.1")] |
| 60 | public string ServerHost { get; set; } |
| 61 | |
| 62 | [Option("server_host_override", DefaultValue = TestCredentials.DefaultHostOverride)] |
| 63 | public string ServerHostOverride { get; set; } |
| 64 | |
| 65 | [Option("server_port", Required = true)] |
| 66 | public int ServerPort { get; set; } |
| 67 | |
| 68 | [Option("test_case", DefaultValue = "large_unary")] |
| 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) |
| 72 | [Option("use_tls", DefaultValue = false)] |
| 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) |
| 76 | [Option("use_test_ca", DefaultValue = false)] |
| 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; } |
| 87 | |
| 88 | [HelpOption] |
| 89 | public string GetUsage() |
| 90 | { |
| 91 | var help = new HelpText |
| 92 | { |
| 93 | Heading = "gRPC C# interop testing client", |
| 94 | AddDashesToOption = true |
| 95 | }; |
| 96 | help.AddPreOptionsLine("Usage:"); |
| 97 | help.AddOptions(this); |
| 98 | return help; |
| 99 | } |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | ClientOptions options; |
| 103 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 104 | private InteropClient(ClientOptions options) |
Jan Tattermusch | 392d1e0 | 2015-02-09 11:13:51 -0800 | [diff] [blame] | 105 | { |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 106 | this.options = options; |
| 107 | } |
| 108 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 109 | public static void Run(string[] args) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 110 | { |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 111 | var options = new ClientOptions(); |
| 112 | if (!Parser.Default.ParseArguments(args, options)) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 113 | { |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 114 | Environment.Exit(1); |
| 115 | } |
| 116 | |
Jan Tattermusch | 503bbac | 2015-02-26 18:19:47 -0800 | [diff] [blame] | 117 | var interopClient = new InteropClient(options); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 118 | interopClient.Run().Wait(); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 121 | private async Task Run() |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 122 | { |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 123 | var credentials = await CreateCredentialsAsync(); |
| 124 | |
| 125 | List<ChannelOption> channelOptions = null; |
| 126 | if (!string.IsNullOrEmpty(options.ServerHostOverride)) |
| 127 | { |
| 128 | channelOptions = new List<ChannelOption> |
| 129 | { |
| 130 | new ChannelOption(ChannelOptions.SslTargetNameOverride, options.ServerHostOverride) |
| 131 | }; |
| 132 | } |
| 133 | var channel = new Channel(options.ServerHost, options.ServerPort, credentials, channelOptions); |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 134 | await RunTestCaseAsync(channel, options); |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 135 | await channel.ShutdownAsync(); |
| 136 | } |
| 137 | |
Jan Tattermusch | 5bd7005 | 2015-10-06 16:47:49 -0700 | [diff] [blame] | 138 | private async Task<ChannelCredentials> CreateCredentialsAsync() |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 139 | { |
Jan Tattermusch | beffc77 | 2015-10-22 13:28:22 -0700 | [diff] [blame] | 140 | var credentials = ChannelCredentials.Insecure; |
| 141 | if (options.UseTls.Value) |
| 142 | { |
| 143 | credentials = options.UseTestCa.Value ? TestCredentials.CreateSslCredentials() : new SslCredentials(); |
| 144 | } |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 145 | |
| 146 | if (options.TestCase == "jwt_token_creds") |
| 147 | { |
| 148 | var googleCredential = await GoogleCredential.GetApplicationDefaultAsync(); |
| 149 | Assert.IsTrue(googleCredential.IsCreateScopedRequired); |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 150 | credentials = ChannelCredentials.Create(credentials, googleCredential.ToCallCredentials()); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (options.TestCase == "compute_engine_creds") |
| 154 | { |
| 155 | var googleCredential = await GoogleCredential.GetApplicationDefaultAsync(); |
| 156 | Assert.IsFalse(googleCredential.IsCreateScopedRequired); |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 157 | credentials = ChannelCredentials.Create(credentials, googleCredential.ToCallCredentials()); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 158 | } |
Jan Tattermusch | 9e5e7e9 | 2015-09-24 10:34:05 -0700 | [diff] [blame] | 159 | return credentials; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 162 | private async Task RunTestCaseAsync(Channel channel, ClientOptions options) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 163 | { |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 164 | var client = new TestService.TestServiceClient(channel); |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 165 | switch (options.TestCase) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 166 | { |
| 167 | case "empty_unary": |
| 168 | RunEmptyUnary(client); |
| 169 | break; |
| 170 | case "large_unary": |
| 171 | RunLargeUnary(client); |
| 172 | break; |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 173 | case "client_streaming": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 174 | await RunClientStreamingAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 175 | break; |
| 176 | case "server_streaming": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 177 | await RunServerStreamingAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 178 | break; |
| 179 | case "ping_pong": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 180 | await RunPingPongAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 181 | break; |
| 182 | case "empty_stream": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 183 | await RunEmptyStreamAsync(client); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 184 | break; |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 185 | case "compute_engine_creds": |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 186 | RunComputeEngineCreds(client, options.DefaultServiceAccount, options.OAuthScope); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 187 | break; |
| 188 | case "jwt_token_creds": |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 189 | RunJwtTokenCreds(client); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 190 | break; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 191 | case "oauth2_auth_token": |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 192 | await RunOAuth2AuthTokenAsync(client, options.OAuthScope); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 193 | break; |
| 194 | case "per_rpc_creds": |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 195 | await RunPerRpcCredsAsync(client, options.OAuthScope); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 196 | break; |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 197 | case "cancel_after_begin": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 198 | await RunCancelAfterBeginAsync(client); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 199 | break; |
| 200 | case "cancel_after_first_response": |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 201 | await RunCancelAfterFirstResponseAsync(client); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 202 | break; |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 203 | case "timeout_on_sleeping_server": |
| 204 | await RunTimeoutOnSleepingServerAsync(client); |
| 205 | break; |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 206 | case "custom_metadata": |
| 207 | await RunCustomMetadataAsync(client); |
| 208 | break; |
| 209 | case "status_code_and_message": |
| 210 | await RunStatusCodeAndMessageAsync(client); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 211 | break; |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 212 | case "unimplemented_method": |
| 213 | RunUnimplementedMethod(new UnimplementedService.UnimplementedServiceClient(channel)); |
| 214 | break; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 215 | default: |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 216 | throw new ArgumentException("Unknown test case " + options.TestCase); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 220 | public static void RunEmptyUnary(TestService.TestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 221 | { |
| 222 | Console.WriteLine("running empty_unary"); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 223 | var response = client.EmptyCall(new Empty()); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 224 | Assert.IsNotNull(response); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 225 | Console.WriteLine("Passed!"); |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 228 | public static void RunLargeUnary(TestService.TestServiceClient client) |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 229 | { |
| 230 | Console.WriteLine("running large_unary"); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 231 | var request = new SimpleRequest |
| 232 | { |
| 233 | ResponseType = PayloadType.COMPRESSABLE, |
| 234 | ResponseSize = 314159, |
| 235 | Payload = CreateZerosPayload(271828) |
| 236 | }; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 237 | var response = client.UnaryCall(request); |
| 238 | |
| 239 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 240 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 241 | Console.WriteLine("Passed!"); |
| 242 | } |
| 243 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 244 | public static async Task RunClientStreamingAsync(TestService.TestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 245 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 246 | Console.WriteLine("running client_streaming"); |
| 247 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 248 | var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => new StreamingInputCallRequest { Payload = CreateZerosPayload(size) }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 249 | |
| 250 | using (var call = client.StreamingInputCall()) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 251 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 252 | await call.RequestStream.WriteAllAsync(bodySizes); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 253 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 254 | var response = await call.ResponseAsync; |
| 255 | Assert.AreEqual(74922, response.AggregatedPayloadSize); |
| 256 | } |
| 257 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 260 | public static async Task RunServerStreamingAsync(TestService.TestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 261 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 262 | Console.WriteLine("running server_streaming"); |
| 263 | |
| 264 | var bodySizes = new List<int> { 31415, 9, 2653, 58979 }; |
| 265 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 266 | var request = new StreamingOutputCallRequest |
| 267 | { |
| 268 | ResponseType = PayloadType.COMPRESSABLE, |
| 269 | ResponseParameters = { bodySizes.ConvertAll((size) => new ResponseParameters { Size = size }) } |
| 270 | }; |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 271 | |
| 272 | using (var call = client.StreamingOutputCall(request)) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 273 | { |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 274 | var responseList = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 275 | foreach (var res in responseList) |
| 276 | { |
| 277 | Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type); |
| 278 | } |
| 279 | CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length)); |
| 280 | } |
| 281 | Console.WriteLine("Passed!"); |
| 282 | } |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 283 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 284 | public static async Task RunPingPongAsync(TestService.TestServiceClient client) |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 285 | { |
| 286 | Console.WriteLine("running ping_pong"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 287 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 288 | using (var call = client.FullDuplexCall()) |
| 289 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 290 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 291 | { |
| 292 | ResponseType = PayloadType.COMPRESSABLE, |
| 293 | ResponseParameters = { new ResponseParameters { Size = 31415 } }, |
| 294 | Payload = CreateZerosPayload(27182) |
| 295 | }); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 296 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 297 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 298 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 299 | Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length); |
| 300 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 301 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 302 | { |
| 303 | ResponseType = PayloadType.COMPRESSABLE, |
| 304 | ResponseParameters = { new ResponseParameters { Size = 9 } }, |
| 305 | Payload = CreateZerosPayload(8) |
| 306 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 307 | |
| 308 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 309 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 310 | Assert.AreEqual(9, call.ResponseStream.Current.Payload.Body.Length); |
| 311 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 312 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 313 | { |
| 314 | ResponseType = PayloadType.COMPRESSABLE, |
| 315 | ResponseParameters = { new ResponseParameters { Size = 2653 } }, |
| 316 | Payload = CreateZerosPayload(1828) |
| 317 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 318 | |
| 319 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 320 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 321 | Assert.AreEqual(2653, call.ResponseStream.Current.Payload.Body.Length); |
| 322 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 323 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 324 | { |
| 325 | ResponseType = PayloadType.COMPRESSABLE, |
| 326 | ResponseParameters = { new ResponseParameters { Size = 58979 } }, |
| 327 | Payload = CreateZerosPayload(45904) |
| 328 | }); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 329 | |
| 330 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 331 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 332 | Assert.AreEqual(58979, call.ResponseStream.Current.Payload.Body.Length); |
| 333 | |
| 334 | await call.RequestStream.CompleteAsync(); |
| 335 | |
| 336 | Assert.IsFalse(await call.ResponseStream.MoveNext()); |
| 337 | } |
| 338 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 341 | public static async Task RunEmptyStreamAsync(TestService.TestServiceClient client) |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 342 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 343 | Console.WriteLine("running empty_stream"); |
| 344 | using (var call = client.FullDuplexCall()) |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 345 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 346 | await call.RequestStream.CompleteAsync(); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 347 | |
Jan Tattermusch | f22abfb | 2015-08-09 16:15:34 -0700 | [diff] [blame] | 348 | var responseList = await call.ResponseStream.ToListAsync(); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 349 | Assert.AreEqual(0, responseList.Count); |
| 350 | } |
| 351 | Console.WriteLine("Passed!"); |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 354 | public static void RunComputeEngineCreds(TestService.TestServiceClient client, string defaultServiceAccount, string oauthScope) |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 355 | { |
| 356 | Console.WriteLine("running compute_engine_creds"); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 357 | |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 358 | var request = new SimpleRequest |
| 359 | { |
| 360 | ResponseType = PayloadType.COMPRESSABLE, |
| 361 | ResponseSize = 314159, |
| 362 | Payload = CreateZerosPayload(271828), |
| 363 | FillUsername = true, |
| 364 | FillOauthScope = true |
| 365 | }; |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -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 | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 368 | var response = client.UnaryCall(request); |
| 369 | |
| 370 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 371 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 372 | Assert.False(string.IsNullOrEmpty(response.OauthScope)); |
| 373 | Assert.True(oauthScope.Contains(response.OauthScope)); |
| 374 | Assert.AreEqual(defaultServiceAccount, response.Username); |
Jan Tattermusch | 0bbfa38 | 2015-04-27 16:11:59 -0700 | [diff] [blame] | 375 | Console.WriteLine("Passed!"); |
| 376 | } |
| 377 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 378 | public static void RunJwtTokenCreds(TestService.TestServiceClient client) |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 379 | { |
| 380 | Console.WriteLine("running jwt_token_creds"); |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 381 | |
Jan Tattermusch | 46e85b0 | 2015-08-13 10:31:05 -0700 | [diff] [blame] | 382 | var request = new SimpleRequest |
| 383 | { |
| 384 | ResponseType = PayloadType.COMPRESSABLE, |
| 385 | ResponseSize = 314159, |
| 386 | Payload = CreateZerosPayload(271828), |
| 387 | FillUsername = true, |
Jan Tattermusch | 46e85b0 | 2015-08-13 10:31:05 -0700 | [diff] [blame] | 388 | }; |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 389 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 390 | // not setting credentials here because they were set on channel already |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 391 | var response = client.UnaryCall(request); |
| 392 | |
| 393 | Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type); |
| 394 | Assert.AreEqual(314159, response.Payload.Body.Length); |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 395 | Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 396 | Console.WriteLine("Passed!"); |
| 397 | } |
| 398 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 399 | public static async Task RunOAuth2AuthTokenAsync(TestService.TestServiceClient client, string oauthScope) |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 400 | { |
| 401 | Console.WriteLine("running oauth2_auth_token"); |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 402 | ITokenAccess credential = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { oauthScope }); |
Jan Tattermusch | 0c140a8 | 2015-08-02 00:54:02 -0700 | [diff] [blame] | 403 | string oauth2Token = await credential.GetAccessTokenForRequestAsync(); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 404 | |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 405 | var credentials = GoogleGrpcCredentials.FromAccessToken(oauth2Token); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 406 | var request = new SimpleRequest |
| 407 | { |
| 408 | FillUsername = true, |
| 409 | FillOauthScope = true |
| 410 | }; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 411 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 412 | var response = client.UnaryCall(request, new CallOptions(credentials: credentials)); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 413 | |
Jan Tattermusch | b26972f | 2015-09-03 17:47:14 -0700 | [diff] [blame] | 414 | Assert.False(string.IsNullOrEmpty(response.OauthScope)); |
| 415 | Assert.True(oauthScope.Contains(response.OauthScope)); |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 416 | Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 417 | Console.WriteLine("Passed!"); |
| 418 | } |
| 419 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 420 | public static async Task RunPerRpcCredsAsync(TestService.TestServiceClient client, string oauthScope) |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 421 | { |
| 422 | Console.WriteLine("running per_rpc_creds"); |
Jan Tattermusch | cf72a3a | 2015-10-08 08:44:20 -0700 | [diff] [blame] | 423 | ITokenAccess googleCredential = await GoogleCredential.GetApplicationDefaultAsync(); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 424 | |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 425 | var credentials = googleCredential.ToCallCredentials(); |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 426 | var request = new SimpleRequest |
| 427 | { |
| 428 | FillUsername = true, |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 429 | }; |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 430 | |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 431 | var response = client.UnaryCall(request, new CallOptions(credentials: credentials)); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 432 | |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 433 | Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username); |
Jan Tattermusch | 7b4a31f | 2015-07-20 17:08:13 -0700 | [diff] [blame] | 434 | Console.WriteLine("Passed!"); |
| 435 | } |
| 436 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 437 | public static async Task RunCancelAfterBeginAsync(TestService.TestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 438 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 439 | Console.WriteLine("running cancel_after_begin"); |
| 440 | |
| 441 | var cts = new CancellationTokenSource(); |
| 442 | using (var call = client.StreamingInputCall(cancellationToken: cts.Token)) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 443 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 444 | // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it. |
| 445 | await Task.Delay(1000); |
| 446 | cts.Cancel(); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 447 | |
Jan Tattermusch | 9f19bd3 | 2016-04-08 13:32:30 -0700 | [diff] [blame] | 448 | var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseAsync); |
Jan Tattermusch | c8d7b84 | 2015-08-07 20:52:21 -0700 | [diff] [blame] | 449 | Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 450 | } |
| 451 | Console.WriteLine("Passed!"); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 454 | public static async Task RunCancelAfterFirstResponseAsync(TestService.TestServiceClient client) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 455 | { |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 456 | Console.WriteLine("running cancel_after_first_response"); |
| 457 | |
| 458 | var cts = new CancellationTokenSource(); |
| 459 | using (var call = client.FullDuplexCall(cancellationToken: cts.Token)) |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 460 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 461 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest |
| 462 | { |
| 463 | ResponseType = PayloadType.COMPRESSABLE, |
| 464 | ResponseParameters = { new ResponseParameters { Size = 31415 } }, |
| 465 | Payload = CreateZerosPayload(27182) |
| 466 | }); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 467 | |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 468 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 469 | Assert.AreEqual(PayloadType.COMPRESSABLE, call.ResponseStream.Current.Payload.Type); |
| 470 | Assert.AreEqual(31415, call.ResponseStream.Current.Payload.Body.Length); |
| 471 | |
| 472 | cts.Cancel(); |
| 473 | |
Jan Tattermusch | 9f19bd3 | 2016-04-08 13:32:30 -0700 | [diff] [blame] | 474 | var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.MoveNext()); |
Jan Tattermusch | c8d7b84 | 2015-08-07 20:52:21 -0700 | [diff] [blame] | 475 | Assert.AreEqual(StatusCode.Cancelled, ex.Status.StatusCode); |
Jan Tattermusch | b98e1dd | 2015-07-24 21:30:14 -0700 | [diff] [blame] | 476 | } |
| 477 | Console.WriteLine("Passed!"); |
Jan Tattermusch | e5c4460 | 2015-05-01 11:12:34 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 480 | public static async Task RunTimeoutOnSleepingServerAsync(TestService.TestServiceClient client) |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 481 | { |
| 482 | Console.WriteLine("running timeout_on_sleeping_server"); |
| 483 | |
| 484 | var deadline = DateTime.UtcNow.AddMilliseconds(1); |
| 485 | using (var call = client.FullDuplexCall(deadline: deadline)) |
| 486 | { |
| 487 | try |
| 488 | { |
Jan Tattermusch | 0608a00 | 2015-08-26 08:50:19 -0700 | [diff] [blame] | 489 | await call.RequestStream.WriteAsync(new StreamingOutputCallRequest { Payload = CreateZerosPayload(27182) }); |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 490 | } |
| 491 | catch (InvalidOperationException) |
| 492 | { |
| 493 | // Deadline was reached before write has started. Eat the exception and continue. |
| 494 | } |
Jan Tattermusch | a46d21d | 2016-05-10 09:57:51 -0700 | [diff] [blame] | 495 | catch (RpcException) |
| 496 | { |
| 497 | // Deadline was reached before write has started. Eat the exception and continue. |
| 498 | } |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 499 | |
Jan Tattermusch | 9f19bd3 | 2016-04-08 13:32:30 -0700 | [diff] [blame] | 500 | var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.MoveNext()); |
Jan Tattermusch | 85c3ab0 | 2016-04-26 17:17:02 -0700 | [diff] [blame] | 501 | // We can't guarantee the status code always DeadlineExceeded. See issue #2685. |
| 502 | Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExceeded, StatusCode.Internal }); |
Jan Tattermusch | e4134dd | 2015-08-12 14:54:40 -0700 | [diff] [blame] | 503 | } |
| 504 | Console.WriteLine("Passed!"); |
| 505 | } |
| 506 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 507 | public static async Task RunCustomMetadataAsync(TestService.TestServiceClient client) |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 508 | { |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 509 | Console.WriteLine("running custom_metadata"); |
| 510 | { |
| 511 | // step 1: test unary call |
| 512 | var request = new SimpleRequest |
| 513 | { |
| 514 | ResponseType = PayloadType.COMPRESSABLE, |
| 515 | ResponseSize = 314159, |
| 516 | Payload = CreateZerosPayload(271828) |
| 517 | }; |
| 518 | |
| 519 | var call = client.UnaryCallAsync(request, headers: CreateTestMetadata()); |
| 520 | await call.ResponseAsync; |
| 521 | |
| 522 | var responseHeaders = await call.ResponseHeadersAsync; |
| 523 | var responseTrailers = call.GetTrailers(); |
| 524 | |
| 525 | Assert.AreEqual("test_initial_metadata_value", responseHeaders.First((entry) => entry.Key == "x-grpc-test-echo-initial").Value); |
| 526 | CollectionAssert.AreEqual(new byte[] { 0xab, 0xab, 0xab }, responseTrailers.First((entry) => entry.Key == "x-grpc-test-echo-trailing-bin").ValueBytes); |
| 527 | } |
| 528 | |
| 529 | { |
| 530 | // step 2: test full duplex call |
| 531 | var request = new StreamingOutputCallRequest |
| 532 | { |
| 533 | ResponseType = PayloadType.COMPRESSABLE, |
| 534 | ResponseParameters = { new ResponseParameters { Size = 31415 } }, |
| 535 | Payload = CreateZerosPayload(27182) |
| 536 | }; |
| 537 | |
| 538 | var call = client.FullDuplexCall(headers: CreateTestMetadata()); |
| 539 | var responseHeaders = await call.ResponseHeadersAsync; |
| 540 | |
| 541 | await call.RequestStream.WriteAsync(request); |
| 542 | await call.RequestStream.CompleteAsync(); |
| 543 | await call.ResponseStream.ToListAsync(); |
| 544 | |
| 545 | var responseTrailers = call.GetTrailers(); |
| 546 | |
| 547 | Assert.AreEqual("test_initial_metadata_value", responseHeaders.First((entry) => entry.Key == "x-grpc-test-echo-initial").Value); |
| 548 | CollectionAssert.AreEqual(new byte[] { 0xab, 0xab, 0xab }, responseTrailers.First((entry) => entry.Key == "x-grpc-test-echo-trailing-bin").ValueBytes); |
| 549 | } |
| 550 | |
| 551 | Console.WriteLine("Passed!"); |
| 552 | } |
| 553 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 554 | public static async Task RunStatusCodeAndMessageAsync(TestService.TestServiceClient client) |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 555 | { |
| 556 | Console.WriteLine("running status_code_and_message"); |
| 557 | var echoStatus = new EchoStatus |
| 558 | { |
| 559 | Code = 2, |
| 560 | Message = "test status message" |
| 561 | }; |
| 562 | |
| 563 | { |
| 564 | // step 1: test unary call |
| 565 | var request = new SimpleRequest { ResponseStatus = echoStatus }; |
| 566 | |
| 567 | var e = Assert.Throws<RpcException>(() => client.UnaryCall(request)); |
| 568 | Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); |
| 569 | Assert.AreEqual(echoStatus.Message, e.Status.Detail); |
| 570 | } |
| 571 | |
| 572 | { |
| 573 | // step 2: test full duplex call |
| 574 | var request = new StreamingOutputCallRequest { ResponseStatus = echoStatus }; |
| 575 | |
| 576 | var call = client.FullDuplexCall(); |
| 577 | await call.RequestStream.WriteAsync(request); |
| 578 | await call.RequestStream.CompleteAsync(); |
| 579 | |
Jan Tattermusch | 9f19bd3 | 2016-04-08 13:32:30 -0700 | [diff] [blame] | 580 | var e = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.ToListAsync()); |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 581 | Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode); |
| 582 | Assert.AreEqual(echoStatus.Message, e.Status.Detail); |
| 583 | } |
| 584 | |
| 585 | Console.WriteLine("Passed!"); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 586 | } |
Jan Tattermusch | d233d3a | 2015-02-06 14:15:00 -0800 | [diff] [blame] | 587 | |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 588 | public static void RunUnimplementedMethod(UnimplementedService.UnimplementedServiceClient client) |
Jan Tattermusch | fa20ebc | 2015-12-09 18:38:10 -0800 | [diff] [blame] | 589 | { |
| 590 | Console.WriteLine("running unimplemented_method"); |
| 591 | var e = Assert.Throws<RpcException>(() => client.UnimplementedCall(new Empty())); |
| 592 | |
| 593 | Assert.AreEqual(StatusCode.Unimplemented, e.Status.StatusCode); |
| 594 | Assert.AreEqual("", e.Status.Detail); |
| 595 | Console.WriteLine("Passed!"); |
| 596 | } |
| 597 | |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 598 | private static Payload CreateZerosPayload(int size) |
| 599 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 600 | return new Payload { Body = ByteString.CopyFrom(new byte[size]) }; |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 601 | } |
Jan Tattermusch | 64d7c24 | 2015-10-08 08:02:27 -0700 | [diff] [blame] | 602 | |
| 603 | // extracts the client_email field from service account file used for auth test cases |
| 604 | private static string GetEmailFromServiceAccountFile() |
| 605 | { |
| 606 | string keyFile = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"); |
| 607 | Assert.IsNotNull(keyFile); |
| 608 | |
| 609 | var jobject = JObject.Parse(File.ReadAllText(keyFile)); |
| 610 | string email = jobject.GetValue("client_email").Value<string>(); |
| 611 | Assert.IsTrue(email.Length > 0); // spec requires nonempty client email. |
| 612 | return email; |
| 613 | } |
Jan Tattermusch | 0ed7315 | 2015-12-09 18:21:11 -0800 | [diff] [blame] | 614 | |
| 615 | private static Metadata CreateTestMetadata() |
| 616 | { |
| 617 | return new Metadata |
| 618 | { |
| 619 | {"x-grpc-test-echo-initial", "test_initial_metadata_value"}, |
| 620 | {"x-grpc-test-echo-trailing-bin", new byte[] {0xab, 0xab, 0xab}} |
| 621 | }; |
| 622 | } |
Jan Tattermusch | eb3e76e | 2015-02-06 11:43:13 -0800 | [diff] [blame] | 623 | } |
| 624 | } |