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