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