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