blob: ddc36acc5a4e7d32025bb188ec22d9288d4e757b [file] [log] [blame]
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001// 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
18using System;
19using System.Collections.Generic;
20using System.Diagnostics;
21using System.IO;
22using System.Linq;
23using System.Net;
24using System.Reflection;
25using System.Security.Authentication;
26using System.Security.Cryptography.X509Certificates;
27using System.ServiceModel;
28using System.Text;
29using System.Threading;
30using System.Threading.Tasks;
31using Thrift.Collections;
32using Thrift.Protocol;
33using Thrift.Transport;
34using Thrift.Transport.Client;
35
36namespace ThriftTest
37{
Jens Geyeradde44b2019-02-05 01:00:02 +010038 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,
58 NamedPipe
59 }
60
Jens Geyeraa0c8b32019-01-28 23:27:45 +010061 public class TestClient
62 {
63 private class TestParams
64 {
65 public int numIterations = 1;
Jens Geyer22c412e2019-03-12 01:06:25 +010066 public string host = "localhost";
Jens Geyeraa0c8b32019-01-28 23:27:45 +010067 public int port = 9090;
68 public int numThreads = 1;
69 public string url;
70 public string pipe;
Jens Geyeradde44b2019-02-05 01:00:02 +010071 public LayeredChoice layered = LayeredChoice.None;
72 public ProtocolChoice protocol = ProtocolChoice.Binary;
73 public TransportChoice transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010074
75 internal void Parse( List<string> args)
76 {
77 for (var i = 0; i < args.Count; ++i)
78 {
79 if (args[i] == "-u")
80 {
81 url = args[++i];
82 }
83 else if (args[i] == "-n")
84 {
85 numIterations = Convert.ToInt32(args[++i]);
86 }
87 else if (args[i].StartsWith("--pipe="))
88 {
89 pipe = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +010090 transport = TransportChoice.NamedPipe;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010091 }
92 else if (args[i].StartsWith("--host="))
93 {
94 // check there for ipaddress
Jens Geyer22c412e2019-03-12 01:06:25 +010095 host = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +010096 if (transport != TransportChoice.TlsSocket)
97 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010098 }
99 else if (args[i].StartsWith("--port="))
100 {
101 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyeradde44b2019-02-05 01:00:02 +0100102 if (transport != TransportChoice.TlsSocket)
103 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100104 }
105 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
106 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100107 layered = LayeredChoice.Buffered;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100108 }
109 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
110 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100111 layered = LayeredChoice.Framed;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100112 }
113 else if (args[i] == "-t")
114 {
115 numThreads = Convert.ToInt32(args[++i]);
116 }
117 else if (args[i] == "--binary" || args[i] == "--protocol=binary")
118 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100119 protocol = ProtocolChoice.Binary;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100120 }
121 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
122 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100123 protocol = ProtocolChoice.Compact;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100124 }
125 else if (args[i] == "--json" || args[i] == "--protocol=json")
126 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100127 protocol = ProtocolChoice.Json;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100128 }
129 else if (args[i] == "--ssl")
130 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100131 transport = TransportChoice.TlsSocket;
132 }
133 else if (args[i] == "--help")
134 {
135 PrintOptionsHelp();
136 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100137 }
138 else
139 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100140 Console.WriteLine("Invalid argument: {0}", args[i]);
141 PrintOptionsHelp();
142 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100143 }
144 }
Jens Geyeradde44b2019-02-05 01:00:02 +0100145
146 switch (transport)
147 {
148 case TransportChoice.Socket:
149 Console.WriteLine("Using socket transport");
150 break;
151 case TransportChoice.TlsSocket:
152 Console.WriteLine("Using encrypted transport");
153 break;
154 case TransportChoice.NamedPipe:
155 Console.WriteLine("Using named pipes transport");
156 break;
157 default: // unhandled case
158 Debug.Assert(false);
159 break;
160 }
161
162 switch (layered)
163 {
164 case LayeredChoice.Framed:
165 Console.WriteLine("Using framed transport");
166 break;
167 case LayeredChoice.Buffered:
168 Console.WriteLine("Using buffered transport");
169 break;
170 default: // unhandled case?
171 Debug.Assert(layered == LayeredChoice.None);
172 break;
173 }
174
175 switch (protocol)
176 {
177 case ProtocolChoice.Binary:
178 Console.WriteLine("Using binary protocol");
179 break;
180 case ProtocolChoice.Compact:
181 Console.WriteLine("Using compact protocol");
182 break;
183 case ProtocolChoice.Json:
184 Console.WriteLine("Using JSON protocol");
185 break;
186 default: // unhandled case?
187 Debug.Assert(false);
188 break;
189 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100190 }
191
192 private static X509Certificate2 GetClientCert()
193 {
194 var clientCertName = "client.p12";
195 var possiblePaths = new List<string>
196 {
197 "../../../keys/",
198 "../../keys/",
199 "../keys/",
200 "keys/",
201 };
202
203 string existingPath = null;
204 foreach (var possiblePath in possiblePaths)
205 {
206 var path = Path.GetFullPath(possiblePath + clientCertName);
207 if (File.Exists(path))
208 {
209 existingPath = path;
210 break;
211 }
212 }
213
214 if (string.IsNullOrEmpty(existingPath))
215 {
216 throw new FileNotFoundException($"Cannot find file: {clientCertName}");
217 }
218
219 var cert = new X509Certificate2(existingPath, "thrift");
220
221 return cert;
222 }
223
224 public TTransport CreateTransport()
225 {
226 if (url == null)
227 {
228 // endpoint transport
229 TTransport trans = null;
230
Jens Geyeradde44b2019-02-05 01:00:02 +0100231 switch(transport)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100232 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100233 case TransportChoice.NamedPipe:
234 Debug.Assert(pipe != null);
235 trans = new TNamedPipeTransport(pipe);
236 break;
237
238 case TransportChoice.TlsSocket:
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100239 var cert = GetClientCert();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100240 if (cert == null || !cert.HasPrivateKey)
241 {
242 throw new InvalidOperationException("Certificate doesn't contain private key");
243 }
244
245 trans = new TTlsSocketTransport(host, port, 0, cert,
246 (sender, certificate, chain, errors) => true,
247 null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
Jens Geyeradde44b2019-02-05 01:00:02 +0100248 break;
249
250 case TransportChoice.Socket:
251 default:
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100252 trans = new TSocketTransport(host, port);
Jens Geyeradde44b2019-02-05 01:00:02 +0100253 break;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100254 }
255
Jens Geyeradde44b2019-02-05 01:00:02 +0100256
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100257 // layered transport
Jens Geyeradde44b2019-02-05 01:00:02 +0100258 switch(layered)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100259 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100260 case LayeredChoice.Buffered:
261 trans = new TBufferedTransport(trans);
262 break;
263 case LayeredChoice.Framed:
264 trans = new TFramedTransport(trans);
265 break;
266 default:
267 Debug.Assert(layered == LayeredChoice.None);
268 break;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100269 }
270
271 return trans;
272 }
273
274 return new THttpTransport(new Uri(url), null);
275 }
276
277 public TProtocol CreateProtocol(TTransport transport)
278 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100279 switch (protocol)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100280 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100281 case ProtocolChoice.Compact:
282 return new TCompactProtocol(transport);
283 case ProtocolChoice.Json:
284 return new TJsonProtocol(transport);
285 case ProtocolChoice.Binary:
286 default:
287 return new TBinaryProtocol(transport);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100288 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100289 }
290 }
291
292
293 private const int ErrorBaseTypes = 1;
294 private const int ErrorStructs = 2;
295 private const int ErrorContainers = 4;
296 private const int ErrorExceptions = 8;
297 private const int ErrorUnknown = 64;
298
299 private class ClientTest
300 {
301 private readonly TTransport transport;
302 private readonly ThriftTest.Client client;
303 private readonly int numIterations;
304 private bool done;
305
306 public int ReturnCode { get; set; }
307
308 public ClientTest(TestParams param)
309 {
310 transport = param.CreateTransport();
311 client = new ThriftTest.Client(param.CreateProtocol(transport));
312 numIterations = param.numIterations;
313 }
314
315 public void Execute()
316 {
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100317 if (done)
318 {
319 Console.WriteLine("Execute called more than once");
320 throw new InvalidOperationException();
321 }
322
323 for (var i = 0; i < numIterations; i++)
324 {
325 try
326 {
327 if (!transport.IsOpen)
Jens Geyer1b770f22019-03-12 01:19:43 +0100328 transport.OpenAsync(MakeTimeoutToken()).GetAwaiter().GetResult();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100329 }
330 catch (TTransportException ex)
331 {
332 Console.WriteLine("*** FAILED ***");
333 Console.WriteLine("Connect failed: " + ex.Message);
334 ReturnCode |= ErrorUnknown;
335 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
336 continue;
337 }
338 catch (Exception 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
347 try
348 {
349 ReturnCode |= ExecuteClientTestAsync(client).GetAwaiter().GetResult(); ;
350 }
351 catch (Exception ex)
352 {
353 Console.WriteLine("*** FAILED ***");
354 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
355 ReturnCode |= ErrorUnknown;
356 }
357 }
358 try
359 {
360 transport.Close();
361 }
362 catch (Exception ex)
363 {
364 Console.WriteLine("Error while closing transport");
365 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
366 }
367 done = true;
368 }
369 }
370
371 internal static void PrintOptionsHelp()
372 {
373 Console.WriteLine("Client options:");
374 Console.WriteLine(" -u <URL>");
375 Console.WriteLine(" -t <# of threads to run> default = 1");
376 Console.WriteLine(" -n <# of iterations> per thread");
377 Console.WriteLine(" --pipe=<pipe name>");
378 Console.WriteLine(" --host=<IP address>");
379 Console.WriteLine(" --port=<port number>");
380 Console.WriteLine(" --transport=<transport name> one of buffered,framed (defaults to none)");
381 Console.WriteLine(" --protocol=<protocol name> one of compact,json (defaults to binary)");
382 Console.WriteLine(" --ssl");
383 Console.WriteLine();
384 }
385
386 public static int Execute(List<string> args)
387 {
388 try
389 {
390 var param = new TestParams();
391
392 try
393 {
394 param.Parse(args);
395 }
396 catch (Exception ex)
397 {
398 Console.WriteLine("*** FAILED ***");
399 Console.WriteLine("Error while parsing arguments");
400 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
401 return ErrorUnknown;
402 }
403
404 var tests = Enumerable.Range(0, param.numThreads).Select(_ => new ClientTest(param)).ToArray();
405
406 //issue tests on separate threads simultaneously
407 var threads = tests.Select(test => new Task(test.Execute)).ToArray();
408 var start = DateTime.Now;
409 foreach (var t in threads)
410 {
411 t.Start();
412 }
413
414 Task.WaitAll(threads);
415
416 Console.WriteLine("Total time: " + (DateTime.Now - start));
417 Console.WriteLine();
418 return tests.Select(t => t.ReturnCode).Aggregate((r1, r2) => r1 | r2);
419 }
420 catch (Exception outerEx)
421 {
422 Console.WriteLine("*** FAILED ***");
423 Console.WriteLine("Unexpected error");
424 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
425 return ErrorUnknown;
426 }
427 }
428
429 public static string BytesToHex(byte[] data)
430 {
431 return BitConverter.ToString(data).Replace("-", string.Empty);
432 }
433
434 public static byte[] PrepareTestData(bool randomDist)
435 {
436 var retval = new byte[0x100];
437 var initLen = Math.Min(0x100, retval.Length);
438
439 // linear distribution, unless random is requested
440 if (!randomDist)
441 {
442 for (var i = 0; i < initLen; ++i)
443 {
444 retval[i] = (byte)i;
445 }
446 return retval;
447 }
448
449 // random distribution
450 for (var i = 0; i < initLen; ++i)
451 {
452 retval[i] = (byte)0;
453 }
454 var rnd = new Random();
455 for (var i = 1; i < initLen; ++i)
456 {
457 while (true)
458 {
459 var nextPos = rnd.Next() % initLen;
460 if (retval[nextPos] == 0)
461 {
462 retval[nextPos] = (byte)i;
463 break;
464 }
465 }
466 }
467 return retval;
468 }
469
Jens Geyer1b770f22019-03-12 01:19:43 +0100470 private static CancellationToken MakeTimeoutToken(int msec = 5000)
471 {
472 var token = new CancellationTokenSource(msec);
473 return token.Token;
474 }
475
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100476 public static async Task<int> ExecuteClientTestAsync(ThriftTest.Client client)
477 {
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100478 var returnCode = 0;
479
480 Console.Write("testVoid()");
Jens Geyer1b770f22019-03-12 01:19:43 +0100481 await client.testVoidAsync(MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100482 Console.WriteLine(" = void");
483
484 Console.Write("testString(\"Test\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100485 var s = await client.testStringAsync("Test", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100486 Console.WriteLine(" = \"" + s + "\"");
487 if ("Test" != s)
488 {
489 Console.WriteLine("*** FAILED ***");
490 returnCode |= ErrorBaseTypes;
491 }
492
493 Console.Write("testBool(true)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100494 var t = await client.testBoolAsync((bool)true, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100495 Console.WriteLine(" = " + t);
496 if (!t)
497 {
498 Console.WriteLine("*** FAILED ***");
499 returnCode |= ErrorBaseTypes;
500 }
501 Console.Write("testBool(false)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100502 var f = await client.testBoolAsync((bool)false, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100503 Console.WriteLine(" = " + f);
504 if (f)
505 {
506 Console.WriteLine("*** FAILED ***");
507 returnCode |= ErrorBaseTypes;
508 }
509
510 Console.Write("testByte(1)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100511 var i8 = await client.testByteAsync((sbyte)1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100512 Console.WriteLine(" = " + i8);
513 if (1 != i8)
514 {
515 Console.WriteLine("*** FAILED ***");
516 returnCode |= ErrorBaseTypes;
517 }
518
519 Console.Write("testI32(-1)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100520 var i32 = await client.testI32Async(-1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100521 Console.WriteLine(" = " + i32);
522 if (-1 != i32)
523 {
524 Console.WriteLine("*** FAILED ***");
525 returnCode |= ErrorBaseTypes;
526 }
527
528 Console.Write("testI64(-34359738368)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100529 var i64 = await client.testI64Async(-34359738368, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100530 Console.WriteLine(" = " + i64);
531 if (-34359738368 != i64)
532 {
533 Console.WriteLine("*** FAILED ***");
534 returnCode |= ErrorBaseTypes;
535 }
536
537 // TODO: Validate received message
538 Console.Write("testDouble(5.325098235)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100539 var dub = await client.testDoubleAsync(5.325098235, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100540 Console.WriteLine(" = " + dub);
541 if (5.325098235 != dub)
542 {
543 Console.WriteLine("*** FAILED ***");
544 returnCode |= ErrorBaseTypes;
545 }
546 Console.Write("testDouble(-0.000341012439638598279)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100547 dub = await client.testDoubleAsync(-0.000341012439638598279, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100548 Console.WriteLine(" = " + dub);
549 if (-0.000341012439638598279 != dub)
550 {
551 Console.WriteLine("*** FAILED ***");
552 returnCode |= ErrorBaseTypes;
553 }
554
555 var binOut = PrepareTestData(true);
556 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
557 try
558 {
Jens Geyer1b770f22019-03-12 01:19:43 +0100559 var binIn = await client.testBinaryAsync(binOut, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100560 Console.WriteLine(" = " + BytesToHex(binIn));
561 if (binIn.Length != binOut.Length)
562 {
563 Console.WriteLine("*** FAILED ***");
564 returnCode |= ErrorBaseTypes;
565 }
566 for (var ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
567 if (binIn[ofs] != binOut[ofs])
568 {
569 Console.WriteLine("*** FAILED ***");
570 returnCode |= ErrorBaseTypes;
571 }
572 }
573 catch (Thrift.TApplicationException ex)
574 {
575 Console.WriteLine("*** FAILED ***");
576 returnCode |= ErrorBaseTypes;
577 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
578 }
579
580 // binary equals?
581 Console.WriteLine("Test CrazyNesting");
582 var one = new CrazyNesting();
583 var two = new CrazyNesting();
584 one.String_field = "crazy";
585 two.String_field = "crazy";
586 one.Binary_field = new byte[] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
587 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
588 if (typeof(CrazyNesting).GetMethod("Equals")?.DeclaringType == typeof(CrazyNesting))
589 {
590 if (!one.Equals(two))
591 {
592 Console.WriteLine("*** FAILED ***");
593 returnCode |= ErrorContainers;
594 throw new Exception("CrazyNesting.Equals failed");
595 }
596 }
597
598 // TODO: Validate received message
599 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
600 var o = new Xtruct();
601 o.String_thing = "Zero";
602 o.Byte_thing = (sbyte)1;
603 o.I32_thing = -3;
604 o.I64_thing = -5;
Jens Geyer1b770f22019-03-12 01:19:43 +0100605 var i = await client.testStructAsync(o, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100606 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
607
608 // TODO: Validate received message
609 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
610 var o2 = new Xtruct2();
611 o2.Byte_thing = (sbyte)1;
612 o2.Struct_thing = o;
613 o2.I32_thing = 5;
Jens Geyer1b770f22019-03-12 01:19:43 +0100614 var i2 = await client.testNestAsync(o2, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100615 i = i2.Struct_thing;
616 Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");
617
618 var mapout = new Dictionary<int, int>();
619 for (var j = 0; j < 5; j++)
620 {
621 mapout[j] = j - 10;
622 }
623 Console.Write("testMap({");
624 var first = true;
625 foreach (var key in mapout.Keys)
626 {
627 if (first)
628 {
629 first = false;
630 }
631 else
632 {
633 Console.Write(", ");
634 }
635 Console.Write(key + " => " + mapout[key]);
636 }
637 Console.Write("})");
638
Jens Geyer1b770f22019-03-12 01:19:43 +0100639 var mapin = await client.testMapAsync(mapout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100640
641 Console.Write(" = {");
642 first = true;
643 foreach (var key in mapin.Keys)
644 {
645 if (first)
646 {
647 first = false;
648 }
649 else
650 {
651 Console.Write(", ");
652 }
653 Console.Write(key + " => " + mapin[key]);
654 }
655 Console.WriteLine("}");
656
657 // TODO: Validate received message
658 var listout = new List<int>();
659 for (var j = -2; j < 3; j++)
660 {
661 listout.Add(j);
662 }
663 Console.Write("testList({");
664 first = true;
665 foreach (var j in listout)
666 {
667 if (first)
668 {
669 first = false;
670 }
671 else
672 {
673 Console.Write(", ");
674 }
675 Console.Write(j);
676 }
677 Console.Write("})");
678
Jens Geyer1b770f22019-03-12 01:19:43 +0100679 var listin = await client.testListAsync(listout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100680
681 Console.Write(" = {");
682 first = true;
683 foreach (var j in listin)
684 {
685 if (first)
686 {
687 first = false;
688 }
689 else
690 {
691 Console.Write(", ");
692 }
693 Console.Write(j);
694 }
695 Console.WriteLine("}");
696
697 //set
698 // TODO: Validate received message
699 var setout = new THashSet<int>();
700 for (var j = -2; j < 3; j++)
701 {
702 setout.Add(j);
703 }
704 Console.Write("testSet({");
705 first = true;
706 foreach (int j in setout)
707 {
708 if (first)
709 {
710 first = false;
711 }
712 else
713 {
714 Console.Write(", ");
715 }
716 Console.Write(j);
717 }
718 Console.Write("})");
719
Jens Geyer1b770f22019-03-12 01:19:43 +0100720 var setin = await client.testSetAsync(setout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100721
722 Console.Write(" = {");
723 first = true;
724 foreach (int j in setin)
725 {
726 if (first)
727 {
728 first = false;
729 }
730 else
731 {
732 Console.Write(", ");
733 }
734 Console.Write(j);
735 }
736 Console.WriteLine("}");
737
738
739 Console.Write("testEnum(ONE)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100740 var ret = await client.testEnumAsync(Numberz.ONE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100741 Console.WriteLine(" = " + ret);
742 if (Numberz.ONE != ret)
743 {
744 Console.WriteLine("*** FAILED ***");
745 returnCode |= ErrorStructs;
746 }
747
748 Console.Write("testEnum(TWO)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100749 ret = await client.testEnumAsync(Numberz.TWO, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100750 Console.WriteLine(" = " + ret);
751 if (Numberz.TWO != ret)
752 {
753 Console.WriteLine("*** FAILED ***");
754 returnCode |= ErrorStructs;
755 }
756
757 Console.Write("testEnum(THREE)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100758 ret = await client.testEnumAsync(Numberz.THREE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100759 Console.WriteLine(" = " + ret);
760 if (Numberz.THREE != ret)
761 {
762 Console.WriteLine("*** FAILED ***");
763 returnCode |= ErrorStructs;
764 }
765
766 Console.Write("testEnum(FIVE)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100767 ret = await client.testEnumAsync(Numberz.FIVE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100768 Console.WriteLine(" = " + ret);
769 if (Numberz.FIVE != ret)
770 {
771 Console.WriteLine("*** FAILED ***");
772 returnCode |= ErrorStructs;
773 }
774
775 Console.Write("testEnum(EIGHT)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100776 ret = await client.testEnumAsync(Numberz.EIGHT, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100777 Console.WriteLine(" = " + ret);
778 if (Numberz.EIGHT != ret)
779 {
780 Console.WriteLine("*** FAILED ***");
781 returnCode |= ErrorStructs;
782 }
783
784 Console.Write("testTypedef(309858235082523)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100785 var uid = await client.testTypedefAsync(309858235082523L, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100786 Console.WriteLine(" = " + uid);
787 if (309858235082523L != uid)
788 {
789 Console.WriteLine("*** FAILED ***");
790 returnCode |= ErrorStructs;
791 }
792
793 // TODO: Validate received message
794 Console.Write("testMapMap(1)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100795 var mm = await client.testMapMapAsync(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100796 Console.Write(" = {");
797 foreach (var key in mm.Keys)
798 {
799 Console.Write(key + " => {");
800 var m2 = mm[key];
801 foreach (var k2 in m2.Keys)
802 {
803 Console.Write(k2 + " => " + m2[k2] + ", ");
804 }
805 Console.Write("}, ");
806 }
807 Console.WriteLine("}");
808
809 // TODO: Validate received message
810 var insane = new Insanity();
811 insane.UserMap = new Dictionary<Numberz, long>();
812 insane.UserMap[Numberz.FIVE] = 5000L;
813 var truck = new Xtruct();
814 truck.String_thing = "Truck";
815 truck.Byte_thing = (sbyte)8;
816 truck.I32_thing = 8;
817 truck.I64_thing = 8;
818 insane.Xtructs = new List<Xtruct>();
819 insane.Xtructs.Add(truck);
820 Console.Write("testInsanity()");
Jens Geyer1b770f22019-03-12 01:19:43 +0100821 var whoa = await client.testInsanityAsync(insane, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100822 Console.Write(" = {");
823 foreach (var key in whoa.Keys)
824 {
825 var val = whoa[key];
826 Console.Write(key + " => {");
827
828 foreach (var k2 in val.Keys)
829 {
830 var v2 = val[k2];
831
832 Console.Write(k2 + " => {");
833 var userMap = v2.UserMap;
834
835 Console.Write("{");
836 if (userMap != null)
837 {
838 foreach (var k3 in userMap.Keys)
839 {
840 Console.Write(k3 + " => " + userMap[k3] + ", ");
841 }
842 }
843 else
844 {
845 Console.Write("null");
846 }
847 Console.Write("}, ");
848
849 var xtructs = v2.Xtructs;
850
851 Console.Write("{");
852 if (xtructs != null)
853 {
854 foreach (var x in xtructs)
855 {
856 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
857 }
858 }
859 else
860 {
861 Console.Write("null");
862 }
863 Console.Write("}");
864
865 Console.Write("}, ");
866 }
867 Console.Write("}, ");
868 }
869 Console.WriteLine("}");
870
871 sbyte arg0 = 1;
872 var arg1 = 2;
873 var arg2 = long.MaxValue;
874 var multiDict = new Dictionary<short, string>();
875 multiDict[1] = "one";
876
877 var tmpMultiDict = new List<string>();
878 foreach (var pair in multiDict)
879 tmpMultiDict.Add(pair.Key +" => "+ pair.Value);
880
881 var arg4 = Numberz.FIVE;
882 long arg5 = 5000000;
883 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + ",{" + string.Join(",", tmpMultiDict) + "}," + arg4 + "," + arg5 + ")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100884 var multiResponse = await client.testMultiAsync(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100885 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
886 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
887
888 try
889 {
890 Console.WriteLine("testException(\"Xception\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100891 await client.testExceptionAsync("Xception", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100892 Console.WriteLine("*** FAILED ***");
893 returnCode |= ErrorExceptions;
894 }
895 catch (Xception ex)
896 {
897 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
898 {
899 Console.WriteLine("*** FAILED ***");
900 returnCode |= ErrorExceptions;
901 }
902 }
903 catch (Exception ex)
904 {
905 Console.WriteLine("*** FAILED ***");
906 returnCode |= ErrorExceptions;
907 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
908 }
909 try
910 {
911 Console.WriteLine("testException(\"TException\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100912 await client.testExceptionAsync("TException", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100913 Console.WriteLine("*** FAILED ***");
914 returnCode |= ErrorExceptions;
915 }
916 catch (Thrift.TException)
917 {
918 // OK
919 }
920 catch (Exception ex)
921 {
922 Console.WriteLine("*** FAILED ***");
923 returnCode |= ErrorExceptions;
924 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
925 }
926 try
927 {
928 Console.WriteLine("testException(\"ok\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100929 await client.testExceptionAsync("ok", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100930 // OK
931 }
932 catch (Exception ex)
933 {
934 Console.WriteLine("*** FAILED ***");
935 returnCode |= ErrorExceptions;
936 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
937 }
938
939 try
940 {
941 Console.WriteLine("testMultiException(\"Xception\", ...)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100942 await client.testMultiExceptionAsync("Xception", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100943 Console.WriteLine("*** FAILED ***");
944 returnCode |= ErrorExceptions;
945 }
946 catch (Xception ex)
947 {
948 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
949 {
950 Console.WriteLine("*** FAILED ***");
951 returnCode |= ErrorExceptions;
952 }
953 }
954 catch (Exception ex)
955 {
956 Console.WriteLine("*** FAILED ***");
957 returnCode |= ErrorExceptions;
958 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
959 }
960 try
961 {
962 Console.WriteLine("testMultiException(\"Xception2\", ...)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100963 await client.testMultiExceptionAsync("Xception2", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100964 Console.WriteLine("*** FAILED ***");
965 returnCode |= ErrorExceptions;
966 }
967 catch (Xception2 ex)
968 {
969 if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2")
970 {
971 Console.WriteLine("*** FAILED ***");
972 returnCode |= ErrorExceptions;
973 }
974 }
975 catch (Exception ex)
976 {
977 Console.WriteLine("*** FAILED ***");
978 returnCode |= ErrorExceptions;
979 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
980 }
981 try
982 {
983 Console.WriteLine("testMultiException(\"success\", \"OK\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100984 if ("OK" != (await client.testMultiExceptionAsync("success", "OK", MakeTimeoutToken())).String_thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100985 {
986 Console.WriteLine("*** FAILED ***");
987 returnCode |= ErrorExceptions;
988 }
989 }
990 catch (Exception ex)
991 {
992 Console.WriteLine("*** FAILED ***");
993 returnCode |= ErrorExceptions;
994 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
995 }
996
Jens Geyerb11f63c2019-03-14 21:12:38 +0100997 Console.WriteLine("Test Oneway(1)");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100998 var sw = new Stopwatch();
999 sw.Start();
Jens Geyer1b770f22019-03-12 01:19:43 +01001000 await client.testOnewayAsync(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001001 sw.Stop();
1002 if (sw.ElapsedMilliseconds > 1000)
1003 {
1004 Console.WriteLine("*** FAILED ***");
1005 returnCode |= ErrorBaseTypes;
1006 }
1007
1008 Console.Write("Test Calltime()");
1009 var times = 50;
1010 sw.Reset();
1011 sw.Start();
Jens Geyer1b770f22019-03-12 01:19:43 +01001012 var token = MakeTimeoutToken(20000);
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001013 for (var k = 0; k < times; ++k)
1014 await client.testVoidAsync(token);
1015 sw.Stop();
1016 Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times);
1017 return returnCode;
1018 }
1019 }
1020}