Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
| 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | using System; |
| 19 | using System.Collections.Generic; |
| 20 | using System.Diagnostics; |
| 21 | using System.IO; |
| 22 | using System.Linq; |
| 23 | using System.Net; |
| 24 | using System.Reflection; |
| 25 | using System.Security.Authentication; |
| 26 | using System.Security.Cryptography.X509Certificates; |
| 27 | using System.ServiceModel; |
| 28 | using System.Text; |
| 29 | using System.Threading; |
| 30 | using System.Threading.Tasks; |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 31 | using Thrift; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 32 | using Thrift.Collections; |
| 33 | using Thrift.Protocol; |
| 34 | using Thrift.Transport; |
| 35 | using Thrift.Transport.Client; |
| 36 | |
| 37 | namespace ThriftTest |
| 38 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 39 | internal enum ProtocolChoice |
| 40 | { |
| 41 | Binary, |
| 42 | Compact, |
| 43 | Json |
| 44 | } |
| 45 | |
| 46 | // it does not make much sense to use buffered when we already use framed |
| 47 | internal enum LayeredChoice |
| 48 | { |
| 49 | None, |
| 50 | Buffered, |
| 51 | Framed |
| 52 | } |
| 53 | |
| 54 | |
| 55 | internal enum TransportChoice |
| 56 | { |
| 57 | Socket, |
| 58 | TlsSocket, |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 59 | Http, |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 60 | NamedPipe |
| 61 | } |
| 62 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 63 | public class TestClient |
| 64 | { |
| 65 | private class TestParams |
| 66 | { |
| 67 | public int numIterations = 1; |
Jens Geyer | 22c412e | 2019-03-12 01:06:25 +0100 | [diff] [blame] | 68 | public string host = "localhost"; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 69 | public int port = 9090; |
| 70 | public int numThreads = 1; |
| 71 | public string url; |
| 72 | public string pipe; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 73 | public LayeredChoice layered = LayeredChoice.None; |
| 74 | public ProtocolChoice protocol = ProtocolChoice.Binary; |
| 75 | public TransportChoice transport = TransportChoice.Socket; |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 76 | private readonly TConfiguration Configuration = null; // or new TConfiguration() if needed |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 77 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 78 | internal void Parse(List<string> args) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 79 | { |
| 80 | for (var i = 0; i < args.Count; ++i) |
| 81 | { |
| 82 | if (args[i] == "-u") |
| 83 | { |
| 84 | url = args[++i]; |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 85 | transport = TransportChoice.Http; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 86 | } |
| 87 | else if (args[i] == "-n") |
| 88 | { |
| 89 | numIterations = Convert.ToInt32(args[++i]); |
| 90 | } |
| 91 | else if (args[i].StartsWith("--pipe=")) |
| 92 | { |
| 93 | pipe = args[i].Substring(args[i].IndexOf("=") + 1); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 94 | transport = TransportChoice.NamedPipe; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 95 | } |
| 96 | else if (args[i].StartsWith("--host=")) |
| 97 | { |
| 98 | // check there for ipaddress |
Jens Geyer | 22c412e | 2019-03-12 01:06:25 +0100 | [diff] [blame] | 99 | host = args[i].Substring(args[i].IndexOf("=") + 1); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 100 | if (transport != TransportChoice.TlsSocket) |
| 101 | transport = TransportChoice.Socket; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 102 | } |
| 103 | else if (args[i].StartsWith("--port=")) |
| 104 | { |
| 105 | port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1)); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 106 | if (transport != TransportChoice.TlsSocket) |
| 107 | transport = TransportChoice.Socket; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 108 | } |
| 109 | else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered") |
| 110 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 111 | layered = LayeredChoice.Buffered; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 112 | } |
| 113 | else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed") |
| 114 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 115 | layered = LayeredChoice.Framed; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 116 | } |
| 117 | else if (args[i] == "-t") |
| 118 | { |
| 119 | numThreads = Convert.ToInt32(args[++i]); |
| 120 | } |
| 121 | else if (args[i] == "--binary" || args[i] == "--protocol=binary") |
| 122 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 123 | protocol = ProtocolChoice.Binary; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 124 | } |
| 125 | else if (args[i] == "--compact" || args[i] == "--protocol=compact") |
| 126 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 127 | protocol = ProtocolChoice.Compact; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 128 | } |
| 129 | else if (args[i] == "--json" || args[i] == "--protocol=json") |
| 130 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 131 | protocol = ProtocolChoice.Json; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 132 | } |
| 133 | else if (args[i] == "--ssl") |
| 134 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 135 | transport = TransportChoice.TlsSocket; |
| 136 | } |
| 137 | else if (args[i] == "--help") |
| 138 | { |
| 139 | PrintOptionsHelp(); |
| 140 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 141 | } |
| 142 | else |
| 143 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 144 | Console.WriteLine("Invalid argument: {0}", args[i]); |
| 145 | PrintOptionsHelp(); |
| 146 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 147 | } |
| 148 | } |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 149 | |
| 150 | switch (transport) |
| 151 | { |
| 152 | case TransportChoice.Socket: |
| 153 | Console.WriteLine("Using socket transport"); |
| 154 | break; |
| 155 | case TransportChoice.TlsSocket: |
| 156 | Console.WriteLine("Using encrypted transport"); |
| 157 | break; |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 158 | case TransportChoice.Http: |
| 159 | Console.WriteLine("Using HTTP transport"); |
| 160 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 161 | case TransportChoice.NamedPipe: |
| 162 | Console.WriteLine("Using named pipes transport"); |
| 163 | break; |
| 164 | default: // unhandled case |
| 165 | Debug.Assert(false); |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | switch (layered) |
| 170 | { |
| 171 | case LayeredChoice.Framed: |
| 172 | Console.WriteLine("Using framed transport"); |
| 173 | break; |
| 174 | case LayeredChoice.Buffered: |
| 175 | Console.WriteLine("Using buffered transport"); |
| 176 | break; |
| 177 | default: // unhandled case? |
| 178 | Debug.Assert(layered == LayeredChoice.None); |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | switch (protocol) |
| 183 | { |
| 184 | case ProtocolChoice.Binary: |
| 185 | Console.WriteLine("Using binary protocol"); |
| 186 | break; |
| 187 | case ProtocolChoice.Compact: |
| 188 | Console.WriteLine("Using compact protocol"); |
| 189 | break; |
| 190 | case ProtocolChoice.Json: |
| 191 | Console.WriteLine("Using JSON protocol"); |
| 192 | break; |
| 193 | default: // unhandled case? |
| 194 | Debug.Assert(false); |
| 195 | break; |
| 196 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | private static X509Certificate2 GetClientCert() |
| 200 | { |
| 201 | var clientCertName = "client.p12"; |
| 202 | var possiblePaths = new List<string> |
| 203 | { |
| 204 | "../../../keys/", |
| 205 | "../../keys/", |
| 206 | "../keys/", |
| 207 | "keys/", |
| 208 | }; |
| 209 | |
| 210 | string existingPath = null; |
| 211 | foreach (var possiblePath in possiblePaths) |
| 212 | { |
| 213 | var path = Path.GetFullPath(possiblePath + clientCertName); |
| 214 | if (File.Exists(path)) |
| 215 | { |
| 216 | existingPath = path; |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (string.IsNullOrEmpty(existingPath)) |
| 222 | { |
| 223 | throw new FileNotFoundException($"Cannot find file: {clientCertName}"); |
| 224 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 225 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 226 | var cert = new X509Certificate2(existingPath, "thrift"); |
| 227 | |
| 228 | return cert; |
| 229 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 230 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 231 | public TTransport CreateTransport() |
| 232 | { |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 233 | // endpoint transport |
| 234 | TTransport trans = null; |
| 235 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 236 | switch (transport) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 237 | { |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 238 | case TransportChoice.Http: |
| 239 | Debug.Assert(url != null); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 240 | trans = new THttpTransport(new Uri(url), Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 241 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 242 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 243 | case TransportChoice.NamedPipe: |
| 244 | Debug.Assert(pipe != null); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 245 | trans = new TNamedPipeTransport(pipe,Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 246 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 247 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 248 | case TransportChoice.TlsSocket: |
| 249 | var cert = GetClientCert(); |
| 250 | if (cert == null || !cert.HasPrivateKey) |
| 251 | { |
| 252 | throw new InvalidOperationException("Certificate doesn't contain private key"); |
| 253 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 254 | |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 255 | trans = new TTlsSocketTransport(host, port, Configuration, 0, |
| 256 | cert, |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 257 | (sender, certificate, chain, errors) => true, |
| 258 | null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12); |
| 259 | break; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 260 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 261 | case TransportChoice.Socket: |
| 262 | default: |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 263 | trans = new TSocketTransport(host, port, Configuration); |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 264 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 267 | |
| 268 | // layered transport |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 269 | switch (layered) |
Jens Geyer | 96c6113 | 2019-06-14 22:39:56 +0200 | [diff] [blame] | 270 | { |
| 271 | case LayeredChoice.Buffered: |
| 272 | trans = new TBufferedTransport(trans); |
| 273 | break; |
| 274 | case LayeredChoice.Framed: |
| 275 | trans = new TFramedTransport(trans); |
| 276 | break; |
| 277 | default: |
| 278 | Debug.Assert(layered == LayeredChoice.None); |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | return trans; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | public TProtocol CreateProtocol(TTransport transport) |
| 286 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 287 | switch (protocol) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 288 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 289 | case ProtocolChoice.Compact: |
| 290 | return new TCompactProtocol(transport); |
| 291 | case ProtocolChoice.Json: |
| 292 | return new TJsonProtocol(transport); |
| 293 | case ProtocolChoice.Binary: |
| 294 | default: |
| 295 | return new TBinaryProtocol(transport); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 296 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | |
| 301 | private const int ErrorBaseTypes = 1; |
| 302 | private const int ErrorStructs = 2; |
| 303 | private const int ErrorContainers = 4; |
| 304 | private const int ErrorExceptions = 8; |
| 305 | private const int ErrorUnknown = 64; |
| 306 | |
| 307 | private class ClientTest |
| 308 | { |
| 309 | private readonly TTransport transport; |
| 310 | private readonly ThriftTest.Client client; |
| 311 | private readonly int numIterations; |
| 312 | private bool done; |
| 313 | |
| 314 | public int ReturnCode { get; set; } |
| 315 | |
| 316 | public ClientTest(TestParams param) |
| 317 | { |
| 318 | transport = param.CreateTransport(); |
| 319 | client = new ThriftTest.Client(param.CreateProtocol(transport)); |
| 320 | numIterations = param.numIterations; |
| 321 | } |
| 322 | |
| 323 | public void Execute() |
| 324 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 325 | if (done) |
| 326 | { |
| 327 | Console.WriteLine("Execute called more than once"); |
| 328 | throw new InvalidOperationException(); |
| 329 | } |
| 330 | |
| 331 | for (var i = 0; i < numIterations; i++) |
| 332 | { |
| 333 | try |
| 334 | { |
| 335 | if (!transport.IsOpen) |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 336 | transport.OpenAsync(MakeTimeoutToken()).GetAwaiter().GetResult(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 337 | } |
| 338 | catch (TTransportException ex) |
| 339 | { |
| 340 | Console.WriteLine("*** FAILED ***"); |
| 341 | Console.WriteLine("Connect failed: " + ex.Message); |
| 342 | ReturnCode |= ErrorUnknown; |
| 343 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 344 | continue; |
| 345 | } |
| 346 | catch (Exception ex) |
| 347 | { |
| 348 | Console.WriteLine("*** FAILED ***"); |
| 349 | Console.WriteLine("Connect failed: " + ex.Message); |
| 350 | ReturnCode |= ErrorUnknown; |
| 351 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 352 | continue; |
| 353 | } |
| 354 | |
| 355 | try |
| 356 | { |
| 357 | ReturnCode |= ExecuteClientTestAsync(client).GetAwaiter().GetResult(); ; |
| 358 | } |
| 359 | catch (Exception ex) |
| 360 | { |
| 361 | Console.WriteLine("*** FAILED ***"); |
| 362 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 363 | ReturnCode |= ErrorUnknown; |
| 364 | } |
| 365 | } |
| 366 | try |
| 367 | { |
| 368 | transport.Close(); |
| 369 | } |
| 370 | catch (Exception ex) |
| 371 | { |
| 372 | Console.WriteLine("Error while closing transport"); |
| 373 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 374 | } |
| 375 | done = true; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | internal static void PrintOptionsHelp() |
| 380 | { |
| 381 | Console.WriteLine("Client options:"); |
| 382 | Console.WriteLine(" -u <URL>"); |
| 383 | Console.WriteLine(" -t <# of threads to run> default = 1"); |
| 384 | Console.WriteLine(" -n <# of iterations> per thread"); |
| 385 | Console.WriteLine(" --pipe=<pipe name>"); |
| 386 | Console.WriteLine(" --host=<IP address>"); |
| 387 | Console.WriteLine(" --port=<port number>"); |
| 388 | Console.WriteLine(" --transport=<transport name> one of buffered,framed (defaults to none)"); |
| 389 | Console.WriteLine(" --protocol=<protocol name> one of compact,json (defaults to binary)"); |
| 390 | Console.WriteLine(" --ssl"); |
| 391 | Console.WriteLine(); |
| 392 | } |
| 393 | |
| 394 | public static int Execute(List<string> args) |
| 395 | { |
| 396 | try |
| 397 | { |
| 398 | var param = new TestParams(); |
| 399 | |
| 400 | try |
| 401 | { |
| 402 | param.Parse(args); |
| 403 | } |
| 404 | catch (Exception ex) |
| 405 | { |
| 406 | Console.WriteLine("*** FAILED ***"); |
| 407 | Console.WriteLine("Error while parsing arguments"); |
| 408 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 409 | return ErrorUnknown; |
| 410 | } |
| 411 | |
| 412 | var tests = Enumerable.Range(0, param.numThreads).Select(_ => new ClientTest(param)).ToArray(); |
| 413 | |
| 414 | //issue tests on separate threads simultaneously |
| 415 | var threads = tests.Select(test => new Task(test.Execute)).ToArray(); |
| 416 | var start = DateTime.Now; |
| 417 | foreach (var t in threads) |
| 418 | { |
| 419 | t.Start(); |
| 420 | } |
| 421 | |
| 422 | Task.WaitAll(threads); |
| 423 | |
| 424 | Console.WriteLine("Total time: " + (DateTime.Now - start)); |
| 425 | Console.WriteLine(); |
| 426 | return tests.Select(t => t.ReturnCode).Aggregate((r1, r2) => r1 | r2); |
| 427 | } |
| 428 | catch (Exception outerEx) |
| 429 | { |
| 430 | Console.WriteLine("*** FAILED ***"); |
| 431 | Console.WriteLine("Unexpected error"); |
| 432 | Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace); |
| 433 | return ErrorUnknown; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | public static string BytesToHex(byte[] data) |
| 438 | { |
| 439 | return BitConverter.ToString(data).Replace("-", string.Empty); |
| 440 | } |
| 441 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 442 | |
| 443 | public enum BinaryTestSize |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 444 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 445 | Empty, // Edge case: the zero-length empty binary |
| 446 | Normal, // Fairly small array of usual size (256 bytes) |
| 447 | Large, // Large writes/reads may cause range check errors |
| 448 | PipeWriteLimit, // Windows Limit: Pipe write operations across a network are limited to 65,535 bytes per write. |
| 449 | TwentyMB // that's quite a bit of data |
| 450 | }; |
| 451 | |
| 452 | public static byte[] PrepareTestData(bool randomDist, BinaryTestSize testcase) |
| 453 | { |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 454 | int amount; |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 455 | switch (testcase) |
| 456 | { |
| 457 | case BinaryTestSize.Empty: |
| 458 | amount = 0; |
| 459 | break; |
| 460 | case BinaryTestSize.Normal: |
| 461 | amount = 0x100; |
| 462 | break; |
| 463 | case BinaryTestSize.Large: |
| 464 | amount = 0x8000 + 128; |
| 465 | break; |
| 466 | case BinaryTestSize.PipeWriteLimit: |
| 467 | amount = 0xFFFF + 128; |
| 468 | break; |
| 469 | case BinaryTestSize.TwentyMB: |
| 470 | amount = 20 * 1024 * 1024; |
| 471 | break; |
| 472 | default: |
| 473 | throw new ArgumentException(nameof(testcase)); |
| 474 | } |
| 475 | |
| 476 | var retval = new byte[amount]; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 477 | |
| 478 | // linear distribution, unless random is requested |
| 479 | if (!randomDist) |
| 480 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 481 | for (var i = 0; i < retval.Length; ++i) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 482 | { |
| 483 | retval[i] = (byte)i; |
| 484 | } |
| 485 | return retval; |
| 486 | } |
| 487 | |
| 488 | // random distribution |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 489 | var rnd = new Random(); |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 490 | for (var i = 1; i < retval.Length; ++i) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 491 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 492 | retval[i] = (byte)rnd.Next(0x100); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 493 | } |
| 494 | return retval; |
| 495 | } |
| 496 | |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 497 | private static CancellationToken MakeTimeoutToken(int msec = 5000) |
| 498 | { |
| 499 | var token = new CancellationTokenSource(msec); |
| 500 | return token.Token; |
| 501 | } |
| 502 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 503 | public static async Task<int> ExecuteClientTestAsync(ThriftTest.Client client) |
| 504 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 505 | var returnCode = 0; |
| 506 | |
| 507 | Console.Write("testVoid()"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 508 | await client.testVoidAsync(MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 509 | Console.WriteLine(" = void"); |
| 510 | |
| 511 | Console.Write("testString(\"Test\")"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 512 | var s = await client.testStringAsync("Test", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 513 | Console.WriteLine(" = \"" + s + "\""); |
| 514 | if ("Test" != s) |
| 515 | { |
| 516 | Console.WriteLine("*** FAILED ***"); |
| 517 | returnCode |= ErrorBaseTypes; |
| 518 | } |
| 519 | |
| 520 | Console.Write("testBool(true)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 521 | var t = await client.testBoolAsync((bool)true, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 522 | Console.WriteLine(" = " + t); |
| 523 | if (!t) |
| 524 | { |
| 525 | Console.WriteLine("*** FAILED ***"); |
| 526 | returnCode |= ErrorBaseTypes; |
| 527 | } |
| 528 | Console.Write("testBool(false)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 529 | var f = await client.testBoolAsync((bool)false, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 530 | Console.WriteLine(" = " + f); |
| 531 | if (f) |
| 532 | { |
| 533 | Console.WriteLine("*** FAILED ***"); |
| 534 | returnCode |= ErrorBaseTypes; |
| 535 | } |
| 536 | |
| 537 | Console.Write("testByte(1)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 538 | var i8 = await client.testByteAsync((sbyte)1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 539 | Console.WriteLine(" = " + i8); |
| 540 | if (1 != i8) |
| 541 | { |
| 542 | Console.WriteLine("*** FAILED ***"); |
| 543 | returnCode |= ErrorBaseTypes; |
| 544 | } |
| 545 | |
| 546 | Console.Write("testI32(-1)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 547 | var i32 = await client.testI32Async(-1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 548 | Console.WriteLine(" = " + i32); |
| 549 | if (-1 != i32) |
| 550 | { |
| 551 | Console.WriteLine("*** FAILED ***"); |
| 552 | returnCode |= ErrorBaseTypes; |
| 553 | } |
| 554 | |
| 555 | Console.Write("testI64(-34359738368)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 556 | var i64 = await client.testI64Async(-34359738368, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 557 | Console.WriteLine(" = " + i64); |
| 558 | if (-34359738368 != i64) |
| 559 | { |
| 560 | Console.WriteLine("*** FAILED ***"); |
| 561 | returnCode |= ErrorBaseTypes; |
| 562 | } |
| 563 | |
| 564 | // TODO: Validate received message |
| 565 | Console.Write("testDouble(5.325098235)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 566 | var dub = await client.testDoubleAsync(5.325098235, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 567 | Console.WriteLine(" = " + dub); |
| 568 | if (5.325098235 != dub) |
| 569 | { |
| 570 | Console.WriteLine("*** FAILED ***"); |
| 571 | returnCode |= ErrorBaseTypes; |
| 572 | } |
| 573 | Console.Write("testDouble(-0.000341012439638598279)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 574 | dub = await client.testDoubleAsync(-0.000341012439638598279, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 575 | Console.WriteLine(" = " + dub); |
| 576 | if (-0.000341012439638598279 != dub) |
| 577 | { |
| 578 | Console.WriteLine("*** FAILED ***"); |
| 579 | returnCode |= ErrorBaseTypes; |
| 580 | } |
| 581 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 582 | // testBinary() |
| 583 | foreach(BinaryTestSize binTestCase in Enum.GetValues(typeof(BinaryTestSize))) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 584 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 585 | var binOut = PrepareTestData(true, binTestCase); |
| 586 | |
| 587 | Console.Write("testBinary({0} bytes)", binOut.Length); |
| 588 | try |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 589 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 590 | var binIn = await client.testBinaryAsync(binOut, MakeTimeoutToken()); |
| 591 | Console.WriteLine(" = {0} bytes", binIn.Length); |
| 592 | if (binIn.Length != binOut.Length) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 593 | { |
| 594 | Console.WriteLine("*** FAILED ***"); |
| 595 | returnCode |= ErrorBaseTypes; |
| 596 | } |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 597 | for (var ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs) |
| 598 | { |
| 599 | if (binIn[ofs] != binOut[ofs]) |
| 600 | { |
| 601 | Console.WriteLine("*** FAILED ***"); |
| 602 | returnCode |= ErrorBaseTypes; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | catch (Thrift.TApplicationException ex) |
| 607 | { |
| 608 | Console.WriteLine("*** FAILED ***"); |
| 609 | returnCode |= ErrorBaseTypes; |
| 610 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 611 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 614 | // CrazyNesting |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 615 | Console.WriteLine("Test CrazyNesting"); |
| 616 | var one = new CrazyNesting(); |
| 617 | var two = new CrazyNesting(); |
| 618 | one.String_field = "crazy"; |
| 619 | two.String_field = "crazy"; |
| 620 | one.Binary_field = new byte[] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF }; |
| 621 | two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF }; |
| 622 | if (typeof(CrazyNesting).GetMethod("Equals")?.DeclaringType == typeof(CrazyNesting)) |
| 623 | { |
| 624 | if (!one.Equals(two)) |
| 625 | { |
| 626 | Console.WriteLine("*** FAILED ***"); |
| 627 | returnCode |= ErrorContainers; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | // TODO: Validate received message |
| 632 | Console.Write("testStruct({\"Zero\", 1, -3, -5})"); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 633 | var o = new Xtruct |
| 634 | { |
| 635 | String_thing = "Zero", |
| 636 | Byte_thing = (sbyte)1, |
| 637 | I32_thing = -3, |
| 638 | I64_thing = -5 |
| 639 | }; |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 640 | var i = await client.testStructAsync(o, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 641 | Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}"); |
| 642 | |
| 643 | // TODO: Validate received message |
| 644 | Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})"); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 645 | var o2 = new Xtruct2 |
| 646 | { |
| 647 | Byte_thing = (sbyte)1, |
| 648 | Struct_thing = o, |
| 649 | I32_thing = 5 |
| 650 | }; |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 651 | var i2 = await client.testNestAsync(o2, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 652 | i = i2.Struct_thing; |
| 653 | Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}"); |
| 654 | |
| 655 | var mapout = new Dictionary<int, int>(); |
| 656 | for (var j = 0; j < 5; j++) |
| 657 | { |
| 658 | mapout[j] = j - 10; |
| 659 | } |
| 660 | Console.Write("testMap({"); |
| 661 | var first = true; |
| 662 | foreach (var key in mapout.Keys) |
| 663 | { |
| 664 | if (first) |
| 665 | { |
| 666 | first = false; |
| 667 | } |
| 668 | else |
| 669 | { |
| 670 | Console.Write(", "); |
| 671 | } |
| 672 | Console.Write(key + " => " + mapout[key]); |
| 673 | } |
| 674 | Console.Write("})"); |
| 675 | |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 676 | var mapin = await client.testMapAsync(mapout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 677 | |
| 678 | Console.Write(" = {"); |
| 679 | first = true; |
| 680 | foreach (var key in mapin.Keys) |
| 681 | { |
| 682 | if (first) |
| 683 | { |
| 684 | first = false; |
| 685 | } |
| 686 | else |
| 687 | { |
| 688 | Console.Write(", "); |
| 689 | } |
| 690 | Console.Write(key + " => " + mapin[key]); |
| 691 | } |
| 692 | Console.WriteLine("}"); |
| 693 | |
| 694 | // TODO: Validate received message |
| 695 | var listout = new List<int>(); |
| 696 | for (var j = -2; j < 3; j++) |
| 697 | { |
| 698 | listout.Add(j); |
| 699 | } |
| 700 | Console.Write("testList({"); |
| 701 | first = true; |
| 702 | foreach (var j in listout) |
| 703 | { |
| 704 | if (first) |
| 705 | { |
| 706 | first = false; |
| 707 | } |
| 708 | else |
| 709 | { |
| 710 | Console.Write(", "); |
| 711 | } |
| 712 | Console.Write(j); |
| 713 | } |
| 714 | Console.Write("})"); |
| 715 | |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 716 | var listin = await client.testListAsync(listout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 717 | |
| 718 | Console.Write(" = {"); |
| 719 | first = true; |
| 720 | foreach (var j in listin) |
| 721 | { |
| 722 | if (first) |
| 723 | { |
| 724 | first = false; |
| 725 | } |
| 726 | else |
| 727 | { |
| 728 | Console.Write(", "); |
| 729 | } |
| 730 | Console.Write(j); |
| 731 | } |
| 732 | Console.WriteLine("}"); |
| 733 | |
| 734 | //set |
| 735 | // TODO: Validate received message |
| 736 | var setout = new THashSet<int>(); |
| 737 | for (var j = -2; j < 3; j++) |
| 738 | { |
| 739 | setout.Add(j); |
| 740 | } |
| 741 | Console.Write("testSet({"); |
| 742 | first = true; |
| 743 | foreach (int j in setout) |
| 744 | { |
| 745 | if (first) |
| 746 | { |
| 747 | first = false; |
| 748 | } |
| 749 | else |
| 750 | { |
| 751 | Console.Write(", "); |
| 752 | } |
| 753 | Console.Write(j); |
| 754 | } |
| 755 | Console.Write("})"); |
| 756 | |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 757 | var setin = await client.testSetAsync(setout, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 758 | |
| 759 | Console.Write(" = {"); |
| 760 | first = true; |
| 761 | foreach (int j in setin) |
| 762 | { |
| 763 | if (first) |
| 764 | { |
| 765 | first = false; |
| 766 | } |
| 767 | else |
| 768 | { |
| 769 | Console.Write(", "); |
| 770 | } |
| 771 | Console.Write(j); |
| 772 | } |
| 773 | Console.WriteLine("}"); |
| 774 | |
| 775 | |
| 776 | Console.Write("testEnum(ONE)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 777 | var ret = await client.testEnumAsync(Numberz.ONE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 778 | Console.WriteLine(" = " + ret); |
| 779 | if (Numberz.ONE != ret) |
| 780 | { |
| 781 | Console.WriteLine("*** FAILED ***"); |
| 782 | returnCode |= ErrorStructs; |
| 783 | } |
| 784 | |
| 785 | Console.Write("testEnum(TWO)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 786 | ret = await client.testEnumAsync(Numberz.TWO, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 787 | Console.WriteLine(" = " + ret); |
| 788 | if (Numberz.TWO != ret) |
| 789 | { |
| 790 | Console.WriteLine("*** FAILED ***"); |
| 791 | returnCode |= ErrorStructs; |
| 792 | } |
| 793 | |
| 794 | Console.Write("testEnum(THREE)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 795 | ret = await client.testEnumAsync(Numberz.THREE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 796 | Console.WriteLine(" = " + ret); |
| 797 | if (Numberz.THREE != ret) |
| 798 | { |
| 799 | Console.WriteLine("*** FAILED ***"); |
| 800 | returnCode |= ErrorStructs; |
| 801 | } |
| 802 | |
| 803 | Console.Write("testEnum(FIVE)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 804 | ret = await client.testEnumAsync(Numberz.FIVE, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 805 | Console.WriteLine(" = " + ret); |
| 806 | if (Numberz.FIVE != ret) |
| 807 | { |
| 808 | Console.WriteLine("*** FAILED ***"); |
| 809 | returnCode |= ErrorStructs; |
| 810 | } |
| 811 | |
| 812 | Console.Write("testEnum(EIGHT)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 813 | ret = await client.testEnumAsync(Numberz.EIGHT, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 814 | Console.WriteLine(" = " + ret); |
| 815 | if (Numberz.EIGHT != ret) |
| 816 | { |
| 817 | Console.WriteLine("*** FAILED ***"); |
| 818 | returnCode |= ErrorStructs; |
| 819 | } |
| 820 | |
| 821 | Console.Write("testTypedef(309858235082523)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 822 | var uid = await client.testTypedefAsync(309858235082523L, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 823 | Console.WriteLine(" = " + uid); |
| 824 | if (309858235082523L != uid) |
| 825 | { |
| 826 | Console.WriteLine("*** FAILED ***"); |
| 827 | returnCode |= ErrorStructs; |
| 828 | } |
| 829 | |
| 830 | // TODO: Validate received message |
| 831 | Console.Write("testMapMap(1)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 832 | var mm = await client.testMapMapAsync(1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 833 | Console.Write(" = {"); |
| 834 | foreach (var key in mm.Keys) |
| 835 | { |
| 836 | Console.Write(key + " => {"); |
| 837 | var m2 = mm[key]; |
| 838 | foreach (var k2 in m2.Keys) |
| 839 | { |
| 840 | Console.Write(k2 + " => " + m2[k2] + ", "); |
| 841 | } |
| 842 | Console.Write("}, "); |
| 843 | } |
| 844 | Console.WriteLine("}"); |
| 845 | |
| 846 | // TODO: Validate received message |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 847 | var insane = new Insanity |
| 848 | { |
| 849 | UserMap = new Dictionary<Numberz, long> |
| 850 | { |
| 851 | [Numberz.FIVE] = 5000L |
| 852 | } |
| 853 | }; |
| 854 | var truck = new Xtruct |
| 855 | { |
| 856 | String_thing = "Truck", |
| 857 | Byte_thing = (sbyte)8, |
| 858 | I32_thing = 8, |
| 859 | I64_thing = 8 |
| 860 | }; |
| 861 | insane.Xtructs = new List<Xtruct> |
| 862 | { |
| 863 | truck |
| 864 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 865 | Console.Write("testInsanity()"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 866 | var whoa = await client.testInsanityAsync(insane, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 867 | Console.Write(" = {"); |
| 868 | foreach (var key in whoa.Keys) |
| 869 | { |
| 870 | var val = whoa[key]; |
| 871 | Console.Write(key + " => {"); |
| 872 | |
| 873 | foreach (var k2 in val.Keys) |
| 874 | { |
| 875 | var v2 = val[k2]; |
| 876 | |
| 877 | Console.Write(k2 + " => {"); |
| 878 | var userMap = v2.UserMap; |
| 879 | |
| 880 | Console.Write("{"); |
| 881 | if (userMap != null) |
| 882 | { |
| 883 | foreach (var k3 in userMap.Keys) |
| 884 | { |
| 885 | Console.Write(k3 + " => " + userMap[k3] + ", "); |
| 886 | } |
| 887 | } |
| 888 | else |
| 889 | { |
| 890 | Console.Write("null"); |
| 891 | } |
| 892 | Console.Write("}, "); |
| 893 | |
| 894 | var xtructs = v2.Xtructs; |
| 895 | |
| 896 | Console.Write("{"); |
| 897 | if (xtructs != null) |
| 898 | { |
| 899 | foreach (var x in xtructs) |
| 900 | { |
| 901 | Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, "); |
| 902 | } |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | Console.Write("null"); |
| 907 | } |
| 908 | Console.Write("}"); |
| 909 | |
| 910 | Console.Write("}, "); |
| 911 | } |
| 912 | Console.Write("}, "); |
| 913 | } |
| 914 | Console.WriteLine("}"); |
| 915 | |
| 916 | sbyte arg0 = 1; |
| 917 | var arg1 = 2; |
| 918 | var arg2 = long.MaxValue; |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 919 | var multiDict = new Dictionary<short, string> |
| 920 | { |
| 921 | [1] = "one" |
| 922 | }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 923 | |
| 924 | var tmpMultiDict = new List<string>(); |
| 925 | foreach (var pair in multiDict) |
| 926 | tmpMultiDict.Add(pair.Key +" => "+ pair.Value); |
| 927 | |
| 928 | var arg4 = Numberz.FIVE; |
| 929 | long arg5 = 5000000; |
| 930 | Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + ",{" + string.Join(",", tmpMultiDict) + "}," + arg4 + "," + arg5 + ")"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 931 | var multiResponse = await client.testMultiAsync(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 932 | Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing |
| 933 | + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n"); |
| 934 | |
| 935 | try |
| 936 | { |
| 937 | Console.WriteLine("testException(\"Xception\")"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 938 | await client.testExceptionAsync("Xception", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 939 | Console.WriteLine("*** FAILED ***"); |
| 940 | returnCode |= ErrorExceptions; |
| 941 | } |
| 942 | catch (Xception ex) |
| 943 | { |
| 944 | if (ex.ErrorCode != 1001 || ex.Message != "Xception") |
| 945 | { |
| 946 | Console.WriteLine("*** FAILED ***"); |
| 947 | returnCode |= ErrorExceptions; |
| 948 | } |
| 949 | } |
| 950 | catch (Exception ex) |
| 951 | { |
| 952 | Console.WriteLine("*** FAILED ***"); |
| 953 | returnCode |= ErrorExceptions; |
| 954 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 955 | } |
| 956 | try |
| 957 | { |
| 958 | Console.WriteLine("testException(\"TException\")"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 959 | await client.testExceptionAsync("TException", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 960 | Console.WriteLine("*** FAILED ***"); |
| 961 | returnCode |= ErrorExceptions; |
| 962 | } |
| 963 | catch (Thrift.TException) |
| 964 | { |
| 965 | // OK |
| 966 | } |
| 967 | catch (Exception ex) |
| 968 | { |
| 969 | Console.WriteLine("*** FAILED ***"); |
| 970 | returnCode |= ErrorExceptions; |
| 971 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 972 | } |
| 973 | try |
| 974 | { |
| 975 | Console.WriteLine("testException(\"ok\")"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 976 | await client.testExceptionAsync("ok", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 977 | // OK |
| 978 | } |
| 979 | catch (Exception ex) |
| 980 | { |
| 981 | Console.WriteLine("*** FAILED ***"); |
| 982 | returnCode |= ErrorExceptions; |
| 983 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 984 | } |
| 985 | |
| 986 | try |
| 987 | { |
| 988 | Console.WriteLine("testMultiException(\"Xception\", ...)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 989 | await client.testMultiExceptionAsync("Xception", "ignore", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 990 | Console.WriteLine("*** FAILED ***"); |
| 991 | returnCode |= ErrorExceptions; |
| 992 | } |
| 993 | catch (Xception ex) |
| 994 | { |
| 995 | if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception") |
| 996 | { |
| 997 | Console.WriteLine("*** FAILED ***"); |
| 998 | returnCode |= ErrorExceptions; |
| 999 | } |
| 1000 | } |
| 1001 | catch (Exception ex) |
| 1002 | { |
| 1003 | Console.WriteLine("*** FAILED ***"); |
| 1004 | returnCode |= ErrorExceptions; |
| 1005 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 1006 | } |
| 1007 | try |
| 1008 | { |
| 1009 | Console.WriteLine("testMultiException(\"Xception2\", ...)"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 1010 | await client.testMultiExceptionAsync("Xception2", "ignore", MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1011 | Console.WriteLine("*** FAILED ***"); |
| 1012 | returnCode |= ErrorExceptions; |
| 1013 | } |
| 1014 | catch (Xception2 ex) |
| 1015 | { |
| 1016 | if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2") |
| 1017 | { |
| 1018 | Console.WriteLine("*** FAILED ***"); |
| 1019 | returnCode |= ErrorExceptions; |
| 1020 | } |
| 1021 | } |
| 1022 | catch (Exception ex) |
| 1023 | { |
| 1024 | Console.WriteLine("*** FAILED ***"); |
| 1025 | returnCode |= ErrorExceptions; |
| 1026 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 1027 | } |
| 1028 | try |
| 1029 | { |
| 1030 | Console.WriteLine("testMultiException(\"success\", \"OK\")"); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 1031 | if ("OK" != (await client.testMultiExceptionAsync("success", "OK", MakeTimeoutToken())).String_thing) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1032 | { |
| 1033 | Console.WriteLine("*** FAILED ***"); |
| 1034 | returnCode |= ErrorExceptions; |
| 1035 | } |
| 1036 | } |
| 1037 | catch (Exception ex) |
| 1038 | { |
| 1039 | Console.WriteLine("*** FAILED ***"); |
| 1040 | returnCode |= ErrorExceptions; |
| 1041 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 1042 | } |
| 1043 | |
Jens Geyer | b11f63c | 2019-03-14 21:12:38 +0100 | [diff] [blame] | 1044 | Console.WriteLine("Test Oneway(1)"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1045 | var sw = new Stopwatch(); |
| 1046 | sw.Start(); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 1047 | await client.testOnewayAsync(1, MakeTimeoutToken()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1048 | sw.Stop(); |
| 1049 | if (sw.ElapsedMilliseconds > 1000) |
| 1050 | { |
| 1051 | Console.WriteLine("*** FAILED ***"); |
| 1052 | returnCode |= ErrorBaseTypes; |
| 1053 | } |
| 1054 | |
| 1055 | Console.Write("Test Calltime()"); |
| 1056 | var times = 50; |
| 1057 | sw.Reset(); |
| 1058 | sw.Start(); |
Jens Geyer | 1b770f2 | 2019-03-12 01:19:43 +0100 | [diff] [blame] | 1059 | var token = MakeTimeoutToken(20000); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1060 | for (var k = 0; k < times; ++k) |
| 1061 | await client.testVoidAsync(token); |
| 1062 | sw.Stop(); |
| 1063 | Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times); |
| 1064 | return returnCode; |
| 1065 | } |
| 1066 | } |
| 1067 | } |