blob: 6be10234e87193f0f9b9cf68a4a20bf8d6893571 [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,
Jens Geyer96c61132019-06-14 22:39:56 +020058 Http,
Jens Geyeradde44b2019-02-05 01:00:02 +010059 NamedPipe
60 }
61
Jens Geyeraa0c8b32019-01-28 23:27:45 +010062 public class TestClient
63 {
64 private class TestParams
65 {
66 public int numIterations = 1;
Jens Geyer22c412e2019-03-12 01:06:25 +010067 public string host = "localhost";
Jens Geyeraa0c8b32019-01-28 23:27:45 +010068 public int port = 9090;
69 public int numThreads = 1;
70 public string url;
71 public string pipe;
Jens Geyeradde44b2019-02-05 01:00:02 +010072 public LayeredChoice layered = LayeredChoice.None;
73 public ProtocolChoice protocol = ProtocolChoice.Binary;
74 public TransportChoice transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010075
76 internal void Parse( List<string> args)
77 {
78 for (var i = 0; i < args.Count; ++i)
79 {
80 if (args[i] == "-u")
81 {
82 url = args[++i];
Jens Geyer96c61132019-06-14 22:39:56 +020083 transport = TransportChoice.Http;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010084 }
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 Geyeradde44b2019-02-05 01:00:02 +010092 transport = TransportChoice.NamedPipe;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010093 }
94 else if (args[i].StartsWith("--host="))
95 {
96 // check there for ipaddress
Jens Geyer22c412e2019-03-12 01:06:25 +010097 host = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +010098 if (transport != TransportChoice.TlsSocket)
99 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100100 }
101 else if (args[i].StartsWith("--port="))
102 {
103 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyeradde44b2019-02-05 01:00:02 +0100104 if (transport != TransportChoice.TlsSocket)
105 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100106 }
107 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
108 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100109 layered = LayeredChoice.Buffered;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100110 }
111 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
112 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100113 layered = LayeredChoice.Framed;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100114 }
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 Geyeradde44b2019-02-05 01:00:02 +0100121 protocol = ProtocolChoice.Binary;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100122 }
123 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
124 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100125 protocol = ProtocolChoice.Compact;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100126 }
127 else if (args[i] == "--json" || args[i] == "--protocol=json")
128 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100129 protocol = ProtocolChoice.Json;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100130 }
131 else if (args[i] == "--ssl")
132 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100133 transport = TransportChoice.TlsSocket;
134 }
135 else if (args[i] == "--help")
136 {
137 PrintOptionsHelp();
138 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100139 }
140 else
141 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100142 Console.WriteLine("Invalid argument: {0}", args[i]);
143 PrintOptionsHelp();
144 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100145 }
146 }
Jens Geyeradde44b2019-02-05 01:00:02 +0100147
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 Geyer96c61132019-06-14 22:39:56 +0200156 case TransportChoice.Http:
157 Console.WriteLine("Using HTTP transport");
158 break;
Jens Geyeradde44b2019-02-05 01:00:02 +0100159 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 Geyeraa0c8b32019-01-28 23:27:45 +0100195 }
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 }
223
224 var cert = new X509Certificate2(existingPath, "thrift");
225
226 return cert;
227 }
228
229 public TTransport CreateTransport()
230 {
Jens Geyer96c61132019-06-14 22:39:56 +0200231 // endpoint transport
232 TTransport trans = null;
233
234 switch(transport)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100235 {
Jens Geyer96c61132019-06-14 22:39:56 +0200236 case TransportChoice.Http:
237 Debug.Assert(url != null);
238 trans = new THttpTransport(new Uri(url), null);
239 break;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100240
Jens Geyer96c61132019-06-14 22:39:56 +0200241 case TransportChoice.NamedPipe:
242 Debug.Assert(pipe != null);
243 trans = new TNamedPipeTransport(pipe);
244 break;
Jens Geyeradde44b2019-02-05 01:00:02 +0100245
Jens Geyer96c61132019-06-14 22:39:56 +0200246 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 Geyeraa0c8b32019-01-28 23:27:45 +0100252
Jens Geyer96c61132019-06-14 22:39:56 +0200253 trans = new TTlsSocketTransport(host, port, 0, cert,
254 (sender, certificate, chain, errors) => true,
255 null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
256 break;
Jens Geyeradde44b2019-02-05 01:00:02 +0100257
Jens Geyer96c61132019-06-14 22:39:56 +0200258 case TransportChoice.Socket:
259 default:
260 trans = new TSocketTransport(host, port);
261 break;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100262 }
263
Jens Geyer96c61132019-06-14 22:39:56 +0200264
265 // layered transport
266 switch(layered)
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 Geyeraa0c8b32019-01-28 23:27:45 +0100280 }
281
282 public TProtocol CreateProtocol(TTransport transport)
283 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100284 switch (protocol)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100285 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100286 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 Geyeraa0c8b32019-01-28 23:27:45 +0100293 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100294 }
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 Geyeraa0c8b32019-01-28 23:27:45 +0100322 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 Geyer1b770f22019-03-12 01:19:43 +0100333 transport.OpenAsync(MakeTimeoutToken()).GetAwaiter().GetResult();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100334 }
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
439 public static byte[] PrepareTestData(bool randomDist)
440 {
441 var retval = new byte[0x100];
442 var initLen = Math.Min(0x100, retval.Length);
443
444 // linear distribution, unless random is requested
445 if (!randomDist)
446 {
447 for (var i = 0; i < initLen; ++i)
448 {
449 retval[i] = (byte)i;
450 }
451 return retval;
452 }
453
454 // random distribution
455 for (var i = 0; i < initLen; ++i)
456 {
457 retval[i] = (byte)0;
458 }
459 var rnd = new Random();
460 for (var i = 1; i < initLen; ++i)
461 {
462 while (true)
463 {
464 var nextPos = rnd.Next() % initLen;
465 if (retval[nextPos] == 0)
466 {
467 retval[nextPos] = (byte)i;
468 break;
469 }
470 }
471 }
472 return retval;
473 }
474
Jens Geyer1b770f22019-03-12 01:19:43 +0100475 private static CancellationToken MakeTimeoutToken(int msec = 5000)
476 {
477 var token = new CancellationTokenSource(msec);
478 return token.Token;
479 }
480
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100481 public static async Task<int> ExecuteClientTestAsync(ThriftTest.Client client)
482 {
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100483 var returnCode = 0;
484
485 Console.Write("testVoid()");
Jens Geyer1b770f22019-03-12 01:19:43 +0100486 await client.testVoidAsync(MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100487 Console.WriteLine(" = void");
488
489 Console.Write("testString(\"Test\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100490 var s = await client.testStringAsync("Test", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100491 Console.WriteLine(" = \"" + s + "\"");
492 if ("Test" != s)
493 {
494 Console.WriteLine("*** FAILED ***");
495 returnCode |= ErrorBaseTypes;
496 }
497
498 Console.Write("testBool(true)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100499 var t = await client.testBoolAsync((bool)true, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100500 Console.WriteLine(" = " + t);
501 if (!t)
502 {
503 Console.WriteLine("*** FAILED ***");
504 returnCode |= ErrorBaseTypes;
505 }
506 Console.Write("testBool(false)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100507 var f = await client.testBoolAsync((bool)false, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100508 Console.WriteLine(" = " + f);
509 if (f)
510 {
511 Console.WriteLine("*** FAILED ***");
512 returnCode |= ErrorBaseTypes;
513 }
514
515 Console.Write("testByte(1)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100516 var i8 = await client.testByteAsync((sbyte)1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100517 Console.WriteLine(" = " + i8);
518 if (1 != i8)
519 {
520 Console.WriteLine("*** FAILED ***");
521 returnCode |= ErrorBaseTypes;
522 }
523
524 Console.Write("testI32(-1)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100525 var i32 = await client.testI32Async(-1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100526 Console.WriteLine(" = " + i32);
527 if (-1 != i32)
528 {
529 Console.WriteLine("*** FAILED ***");
530 returnCode |= ErrorBaseTypes;
531 }
532
533 Console.Write("testI64(-34359738368)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100534 var i64 = await client.testI64Async(-34359738368, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100535 Console.WriteLine(" = " + i64);
536 if (-34359738368 != i64)
537 {
538 Console.WriteLine("*** FAILED ***");
539 returnCode |= ErrorBaseTypes;
540 }
541
542 // TODO: Validate received message
543 Console.Write("testDouble(5.325098235)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100544 var dub = await client.testDoubleAsync(5.325098235, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100545 Console.WriteLine(" = " + dub);
546 if (5.325098235 != dub)
547 {
548 Console.WriteLine("*** FAILED ***");
549 returnCode |= ErrorBaseTypes;
550 }
551 Console.Write("testDouble(-0.000341012439638598279)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100552 dub = await client.testDoubleAsync(-0.000341012439638598279, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100553 Console.WriteLine(" = " + dub);
554 if (-0.000341012439638598279 != dub)
555 {
556 Console.WriteLine("*** FAILED ***");
557 returnCode |= ErrorBaseTypes;
558 }
559
560 var binOut = PrepareTestData(true);
561 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
562 try
563 {
Jens Geyer1b770f22019-03-12 01:19:43 +0100564 var binIn = await client.testBinaryAsync(binOut, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100565 Console.WriteLine(" = " + BytesToHex(binIn));
566 if (binIn.Length != binOut.Length)
567 {
568 Console.WriteLine("*** FAILED ***");
569 returnCode |= ErrorBaseTypes;
570 }
571 for (var ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
572 if (binIn[ofs] != binOut[ofs])
573 {
574 Console.WriteLine("*** FAILED ***");
575 returnCode |= ErrorBaseTypes;
576 }
577 }
578 catch (Thrift.TApplicationException ex)
579 {
580 Console.WriteLine("*** FAILED ***");
581 returnCode |= ErrorBaseTypes;
582 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
583 }
584
585 // binary equals?
586 Console.WriteLine("Test CrazyNesting");
587 var one = new CrazyNesting();
588 var two = new CrazyNesting();
589 one.String_field = "crazy";
590 two.String_field = "crazy";
591 one.Binary_field = new byte[] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
592 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
593 if (typeof(CrazyNesting).GetMethod("Equals")?.DeclaringType == typeof(CrazyNesting))
594 {
595 if (!one.Equals(two))
596 {
597 Console.WriteLine("*** FAILED ***");
598 returnCode |= ErrorContainers;
599 throw new Exception("CrazyNesting.Equals failed");
600 }
601 }
602
603 // TODO: Validate received message
604 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
605 var o = new Xtruct();
606 o.String_thing = "Zero";
607 o.Byte_thing = (sbyte)1;
608 o.I32_thing = -3;
609 o.I64_thing = -5;
Jens Geyer1b770f22019-03-12 01:19:43 +0100610 var i = await client.testStructAsync(o, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100611 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
612
613 // TODO: Validate received message
614 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
615 var o2 = new Xtruct2();
616 o2.Byte_thing = (sbyte)1;
617 o2.Struct_thing = o;
618 o2.I32_thing = 5;
Jens Geyer1b770f22019-03-12 01:19:43 +0100619 var i2 = await client.testNestAsync(o2, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100620 i = i2.Struct_thing;
621 Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");
622
623 var mapout = new Dictionary<int, int>();
624 for (var j = 0; j < 5; j++)
625 {
626 mapout[j] = j - 10;
627 }
628 Console.Write("testMap({");
629 var first = true;
630 foreach (var key in mapout.Keys)
631 {
632 if (first)
633 {
634 first = false;
635 }
636 else
637 {
638 Console.Write(", ");
639 }
640 Console.Write(key + " => " + mapout[key]);
641 }
642 Console.Write("})");
643
Jens Geyer1b770f22019-03-12 01:19:43 +0100644 var mapin = await client.testMapAsync(mapout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100645
646 Console.Write(" = {");
647 first = true;
648 foreach (var key in mapin.Keys)
649 {
650 if (first)
651 {
652 first = false;
653 }
654 else
655 {
656 Console.Write(", ");
657 }
658 Console.Write(key + " => " + mapin[key]);
659 }
660 Console.WriteLine("}");
661
662 // TODO: Validate received message
663 var listout = new List<int>();
664 for (var j = -2; j < 3; j++)
665 {
666 listout.Add(j);
667 }
668 Console.Write("testList({");
669 first = true;
670 foreach (var j in listout)
671 {
672 if (first)
673 {
674 first = false;
675 }
676 else
677 {
678 Console.Write(", ");
679 }
680 Console.Write(j);
681 }
682 Console.Write("})");
683
Jens Geyer1b770f22019-03-12 01:19:43 +0100684 var listin = await client.testListAsync(listout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100685
686 Console.Write(" = {");
687 first = true;
688 foreach (var j in listin)
689 {
690 if (first)
691 {
692 first = false;
693 }
694 else
695 {
696 Console.Write(", ");
697 }
698 Console.Write(j);
699 }
700 Console.WriteLine("}");
701
702 //set
703 // TODO: Validate received message
704 var setout = new THashSet<int>();
705 for (var j = -2; j < 3; j++)
706 {
707 setout.Add(j);
708 }
709 Console.Write("testSet({");
710 first = true;
711 foreach (int j in setout)
712 {
713 if (first)
714 {
715 first = false;
716 }
717 else
718 {
719 Console.Write(", ");
720 }
721 Console.Write(j);
722 }
723 Console.Write("})");
724
Jens Geyer1b770f22019-03-12 01:19:43 +0100725 var setin = await client.testSetAsync(setout, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100726
727 Console.Write(" = {");
728 first = true;
729 foreach (int j in setin)
730 {
731 if (first)
732 {
733 first = false;
734 }
735 else
736 {
737 Console.Write(", ");
738 }
739 Console.Write(j);
740 }
741 Console.WriteLine("}");
742
743
744 Console.Write("testEnum(ONE)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100745 var ret = await client.testEnumAsync(Numberz.ONE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100746 Console.WriteLine(" = " + ret);
747 if (Numberz.ONE != ret)
748 {
749 Console.WriteLine("*** FAILED ***");
750 returnCode |= ErrorStructs;
751 }
752
753 Console.Write("testEnum(TWO)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100754 ret = await client.testEnumAsync(Numberz.TWO, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100755 Console.WriteLine(" = " + ret);
756 if (Numberz.TWO != ret)
757 {
758 Console.WriteLine("*** FAILED ***");
759 returnCode |= ErrorStructs;
760 }
761
762 Console.Write("testEnum(THREE)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100763 ret = await client.testEnumAsync(Numberz.THREE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100764 Console.WriteLine(" = " + ret);
765 if (Numberz.THREE != ret)
766 {
767 Console.WriteLine("*** FAILED ***");
768 returnCode |= ErrorStructs;
769 }
770
771 Console.Write("testEnum(FIVE)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100772 ret = await client.testEnumAsync(Numberz.FIVE, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100773 Console.WriteLine(" = " + ret);
774 if (Numberz.FIVE != ret)
775 {
776 Console.WriteLine("*** FAILED ***");
777 returnCode |= ErrorStructs;
778 }
779
780 Console.Write("testEnum(EIGHT)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100781 ret = await client.testEnumAsync(Numberz.EIGHT, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100782 Console.WriteLine(" = " + ret);
783 if (Numberz.EIGHT != ret)
784 {
785 Console.WriteLine("*** FAILED ***");
786 returnCode |= ErrorStructs;
787 }
788
789 Console.Write("testTypedef(309858235082523)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100790 var uid = await client.testTypedefAsync(309858235082523L, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100791 Console.WriteLine(" = " + uid);
792 if (309858235082523L != uid)
793 {
794 Console.WriteLine("*** FAILED ***");
795 returnCode |= ErrorStructs;
796 }
797
798 // TODO: Validate received message
799 Console.Write("testMapMap(1)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100800 var mm = await client.testMapMapAsync(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100801 Console.Write(" = {");
802 foreach (var key in mm.Keys)
803 {
804 Console.Write(key + " => {");
805 var m2 = mm[key];
806 foreach (var k2 in m2.Keys)
807 {
808 Console.Write(k2 + " => " + m2[k2] + ", ");
809 }
810 Console.Write("}, ");
811 }
812 Console.WriteLine("}");
813
814 // TODO: Validate received message
815 var insane = new Insanity();
816 insane.UserMap = new Dictionary<Numberz, long>();
817 insane.UserMap[Numberz.FIVE] = 5000L;
818 var truck = new Xtruct();
819 truck.String_thing = "Truck";
820 truck.Byte_thing = (sbyte)8;
821 truck.I32_thing = 8;
822 truck.I64_thing = 8;
823 insane.Xtructs = new List<Xtruct>();
824 insane.Xtructs.Add(truck);
825 Console.Write("testInsanity()");
Jens Geyer1b770f22019-03-12 01:19:43 +0100826 var whoa = await client.testInsanityAsync(insane, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100827 Console.Write(" = {");
828 foreach (var key in whoa.Keys)
829 {
830 var val = whoa[key];
831 Console.Write(key + " => {");
832
833 foreach (var k2 in val.Keys)
834 {
835 var v2 = val[k2];
836
837 Console.Write(k2 + " => {");
838 var userMap = v2.UserMap;
839
840 Console.Write("{");
841 if (userMap != null)
842 {
843 foreach (var k3 in userMap.Keys)
844 {
845 Console.Write(k3 + " => " + userMap[k3] + ", ");
846 }
847 }
848 else
849 {
850 Console.Write("null");
851 }
852 Console.Write("}, ");
853
854 var xtructs = v2.Xtructs;
855
856 Console.Write("{");
857 if (xtructs != null)
858 {
859 foreach (var x in xtructs)
860 {
861 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
862 }
863 }
864 else
865 {
866 Console.Write("null");
867 }
868 Console.Write("}");
869
870 Console.Write("}, ");
871 }
872 Console.Write("}, ");
873 }
874 Console.WriteLine("}");
875
876 sbyte arg0 = 1;
877 var arg1 = 2;
878 var arg2 = long.MaxValue;
879 var multiDict = new Dictionary<short, string>();
880 multiDict[1] = "one";
881
882 var tmpMultiDict = new List<string>();
883 foreach (var pair in multiDict)
884 tmpMultiDict.Add(pair.Key +" => "+ pair.Value);
885
886 var arg4 = Numberz.FIVE;
887 long arg5 = 5000000;
888 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + ",{" + string.Join(",", tmpMultiDict) + "}," + arg4 + "," + arg5 + ")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100889 var multiResponse = await client.testMultiAsync(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100890 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
891 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
892
893 try
894 {
895 Console.WriteLine("testException(\"Xception\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100896 await client.testExceptionAsync("Xception", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100897 Console.WriteLine("*** FAILED ***");
898 returnCode |= ErrorExceptions;
899 }
900 catch (Xception ex)
901 {
902 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
903 {
904 Console.WriteLine("*** FAILED ***");
905 returnCode |= ErrorExceptions;
906 }
907 }
908 catch (Exception ex)
909 {
910 Console.WriteLine("*** FAILED ***");
911 returnCode |= ErrorExceptions;
912 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
913 }
914 try
915 {
916 Console.WriteLine("testException(\"TException\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100917 await client.testExceptionAsync("TException", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100918 Console.WriteLine("*** FAILED ***");
919 returnCode |= ErrorExceptions;
920 }
921 catch (Thrift.TException)
922 {
923 // OK
924 }
925 catch (Exception ex)
926 {
927 Console.WriteLine("*** FAILED ***");
928 returnCode |= ErrorExceptions;
929 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
930 }
931 try
932 {
933 Console.WriteLine("testException(\"ok\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100934 await client.testExceptionAsync("ok", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100935 // OK
936 }
937 catch (Exception ex)
938 {
939 Console.WriteLine("*** FAILED ***");
940 returnCode |= ErrorExceptions;
941 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
942 }
943
944 try
945 {
946 Console.WriteLine("testMultiException(\"Xception\", ...)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100947 await client.testMultiExceptionAsync("Xception", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100948 Console.WriteLine("*** FAILED ***");
949 returnCode |= ErrorExceptions;
950 }
951 catch (Xception ex)
952 {
953 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
954 {
955 Console.WriteLine("*** FAILED ***");
956 returnCode |= ErrorExceptions;
957 }
958 }
959 catch (Exception ex)
960 {
961 Console.WriteLine("*** FAILED ***");
962 returnCode |= ErrorExceptions;
963 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
964 }
965 try
966 {
967 Console.WriteLine("testMultiException(\"Xception2\", ...)");
Jens Geyer1b770f22019-03-12 01:19:43 +0100968 await client.testMultiExceptionAsync("Xception2", "ignore", MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100969 Console.WriteLine("*** FAILED ***");
970 returnCode |= ErrorExceptions;
971 }
972 catch (Xception2 ex)
973 {
974 if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2")
975 {
976 Console.WriteLine("*** FAILED ***");
977 returnCode |= ErrorExceptions;
978 }
979 }
980 catch (Exception ex)
981 {
982 Console.WriteLine("*** FAILED ***");
983 returnCode |= ErrorExceptions;
984 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
985 }
986 try
987 {
988 Console.WriteLine("testMultiException(\"success\", \"OK\")");
Jens Geyer1b770f22019-03-12 01:19:43 +0100989 if ("OK" != (await client.testMultiExceptionAsync("success", "OK", MakeTimeoutToken())).String_thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100990 {
991 Console.WriteLine("*** FAILED ***");
992 returnCode |= ErrorExceptions;
993 }
994 }
995 catch (Exception ex)
996 {
997 Console.WriteLine("*** FAILED ***");
998 returnCode |= ErrorExceptions;
999 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
1000 }
1001
Jens Geyerb11f63c2019-03-14 21:12:38 +01001002 Console.WriteLine("Test Oneway(1)");
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001003 var sw = new Stopwatch();
1004 sw.Start();
Jens Geyer1b770f22019-03-12 01:19:43 +01001005 await client.testOnewayAsync(1, MakeTimeoutToken());
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001006 sw.Stop();
1007 if (sw.ElapsedMilliseconds > 1000)
1008 {
1009 Console.WriteLine("*** FAILED ***");
1010 returnCode |= ErrorBaseTypes;
1011 }
1012
1013 Console.Write("Test Calltime()");
1014 var times = 50;
1015 sw.Reset();
1016 sw.Start();
Jens Geyer1b770f22019-03-12 01:19:43 +01001017 var token = MakeTimeoutToken(20000);
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001018 for (var k = 0; k < times; ++k)
1019 await client.testVoidAsync(token);
1020 sw.Stop();
1021 Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times);
1022 return returnCode;
1023 }
1024 }
1025}