blob: 67673ec3bdb5cc8680198aa73eff9ff92cd09d9d [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Reiss63191332009-01-06 19:49:22 +000020using System;
Jens Geyer178b8132015-09-30 23:16:45 +020021using System.Linq;
22using System.Diagnostics;
David Reiss63191332009-01-06 19:49:22 +000023using System.Collections.Generic;
David Reissd831a212009-02-13 03:09:52 +000024using System.Threading;
Jens Geyer178b8132015-09-30 23:16:45 +020025using System.Security.Cryptography.X509Certificates;
David Reissd831a212009-02-13 03:09:52 +000026using Thrift.Collections;
David Reiss63191332009-01-06 19:49:22 +000027using Thrift.Protocol;
28using Thrift.Transport;
29using Thrift.Test;
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +090030using System.Security.Authentication;
David Reiss63191332009-01-06 19:49:22 +000031
32namespace Test
33{
Jens Geyerd5436f52014-10-03 19:50:38 +020034 public class TestClient
35 {
Jens Geyer178b8132015-09-30 23:16:45 +020036 private class TestParams
37 {
38 public int numIterations = 1;
39 public string host = "localhost";
40 public int port = 9090;
41 public string url;
42 public string pipe;
43 public bool buffered;
44 public bool framed;
45 public string protocol;
46 public bool encrypted = false;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000047 protected bool _isFirstTransport = true;
48
David Reiss63191332009-01-06 19:49:22 +000049
Jens Geyer178b8132015-09-30 23:16:45 +020050 public TTransport CreateTransport()
51 {
52 if (url == null)
53 {
54 // endpoint transport
55 TTransport trans = null;
56 if (pipe != null)
57 trans = new TNamedPipeClientTransport(pipe);
58 else
59 {
60 if (encrypted)
61 {
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +090062 string certPath = "../keys/client.p12";
Jens Geyer178b8132015-09-30 23:16:45 +020063 X509Certificate cert = new X509Certificate2(certPath, "thrift");
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +090064 trans = new TTLSSocket(host, port, 0, cert, (o, c, chain, errors) => true, null, SslProtocols.Tls);
Jens Geyer178b8132015-09-30 23:16:45 +020065 }
66 else
67 {
68 trans = new TSocket(host, port);
69 }
70 }
71
72 // layered transport
73 if (buffered)
74 trans = new TBufferedTransport(trans);
75 if (framed)
76 trans = new TFramedTransport(trans);
77
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000078 if (_isFirstTransport)
79 {
80 //ensure proper open/close of transport
81 trans.Open();
82 trans.Close();
83 _isFirstTransport = false;
84 }
Jens Geyer178b8132015-09-30 23:16:45 +020085 return trans;
86 }
87 else
88 {
89 return new THttpClient(new Uri(url));
90 }
91 }
92
93 public TProtocol CreateProtocol(TTransport transport)
94 {
95 if (protocol == "compact")
96 return new TCompactProtocol(transport);
97 else if (protocol == "json")
98 return new TJSONProtocol(transport);
99 else
100 return new TBinaryProtocol(transport);
101 }
102 };
103
104 private const int ErrorBaseTypes = 1;
105 private const int ErrorStructs = 2;
106 private const int ErrorContainers = 4;
107 private const int ErrorExceptions = 8;
108 private const int ErrorUnknown = 64;
109
110 private class ClientTest
111 {
112 private readonly TTransport transport;
113 private readonly ThriftTest.Client client;
114 private readonly int numIterations;
115 private bool done;
116
117 public int ReturnCode { get; set; }
118
119 public ClientTest(TestParams param)
120 {
121 transport = param.CreateTransport();
122 client = new ThriftTest.Client(param.CreateProtocol(transport));
123 numIterations = param.numIterations;
124 }
125 public void Execute()
126 {
127 if (done)
128 {
129 Console.WriteLine("Execute called more than once");
130 throw new InvalidOperationException();
131 }
132
133 for (int i = 0; i < numIterations; i++)
134 {
135 try
136 {
137 if (!transport.IsOpen)
138 transport.Open();
139 }
140 catch (TTransportException ex)
141 {
142 Console.WriteLine("*** FAILED ***");
143 Console.WriteLine("Connect failed: " + ex.Message);
144 ReturnCode |= ErrorUnknown;
145 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
146 continue;
147 }
148
149 try
150 {
151 ReturnCode |= ExecuteClientTest(client);
152 }
153 catch (Exception ex)
154 {
155 Console.WriteLine("*** FAILED ***");
156 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
157 ReturnCode |= ErrorUnknown;
158 }
159 }
160 try
161 {
162 transport.Close();
163 }
164 catch(Exception ex)
165 {
166 Console.WriteLine("Error while closing transport");
167 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
168 }
169 done = true;
170 }
171 }
172
173 public static int Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +0200174 {
175 try
176 {
Jens Geyer178b8132015-09-30 23:16:45 +0200177 TestParams param = new TestParams();
Jens Geyerd5436f52014-10-03 19:50:38 +0200178 int numThreads = 1;
Jens Geyerd5436f52014-10-03 19:50:38 +0200179 try
180 {
181 for (int i = 0; i < args.Length; i++)
182 {
183 if (args[i] == "-u")
184 {
Jens Geyer178b8132015-09-30 23:16:45 +0200185 param.url = args[++i];
Jens Geyerd5436f52014-10-03 19:50:38 +0200186 }
187 else if (args[i] == "-n")
188 {
Jens Geyer178b8132015-09-30 23:16:45 +0200189 param.numIterations = Convert.ToInt32(args[++i]);
Jens Geyerd5436f52014-10-03 19:50:38 +0200190 }
191 else if (args[i] == "-pipe") // -pipe <name>
192 {
Jens Geyer178b8132015-09-30 23:16:45 +0200193 param.pipe = args[++i];
Jens Geyerd5436f52014-10-03 19:50:38 +0200194 Console.WriteLine("Using named pipes transport");
195 }
196 else if (args[i].Contains("--host="))
197 {
Jens Geyer178b8132015-09-30 23:16:45 +0200198 param.host = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200199 }
200 else if (args[i].Contains("--port="))
201 {
Jens Geyer178b8132015-09-30 23:16:45 +0200202 param.port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
Jens Geyerd5436f52014-10-03 19:50:38 +0200203 }
204 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
205 {
Jens Geyer178b8132015-09-30 23:16:45 +0200206 param.buffered = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200207 Console.WriteLine("Using buffered sockets");
208 }
209 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
210 {
Jens Geyer178b8132015-09-30 23:16:45 +0200211 param.framed = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200212 Console.WriteLine("Using framed transport");
213 }
214 else if (args[i] == "-t")
215 {
216 numThreads = Convert.ToInt32(args[++i]);
217 }
218 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
219 {
Jens Geyer178b8132015-09-30 23:16:45 +0200220 param.protocol = "compact";
Jens Geyerd5436f52014-10-03 19:50:38 +0200221 Console.WriteLine("Using compact protocol");
222 }
223 else if (args[i] == "--json" || args[i] == "--protocol=json")
224 {
Jens Geyer178b8132015-09-30 23:16:45 +0200225 param.protocol = "json";
Jens Geyerd5436f52014-10-03 19:50:38 +0200226 Console.WriteLine("Using JSON protocol");
227 }
228 else if (args[i] == "--ssl")
229 {
Jens Geyer178b8132015-09-30 23:16:45 +0200230 param.encrypted = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200231 Console.WriteLine("Using encrypted transport");
232 }
233 }
234 }
Jens Geyer178b8132015-09-30 23:16:45 +0200235 catch (Exception ex)
Jens Geyerd5436f52014-10-03 19:50:38 +0200236 {
Jens Geyer178b8132015-09-30 23:16:45 +0200237 Console.WriteLine("*** FAILED ***");
238 Console.WriteLine("Error while parsing arguments");
239 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
240 return ErrorUnknown;
Jens Geyerd5436f52014-10-03 19:50:38 +0200241 }
David Reiss63191332009-01-06 19:49:22 +0000242
Jens Geyer178b8132015-09-30 23:16:45 +0200243 var tests = Enumerable.Range(0, numThreads).Select(_ => new ClientTest(param)).ToArray();
Jens Geyerd5436f52014-10-03 19:50:38 +0200244 //issue tests on separate threads simultaneously
Jens Geyer178b8132015-09-30 23:16:45 +0200245 var threads = tests.Select(test => new Thread(test.Execute)).ToArray();
Jens Geyerd5436f52014-10-03 19:50:38 +0200246 DateTime start = DateTime.Now;
Jens Geyer178b8132015-09-30 23:16:45 +0200247 foreach (var t in threads)
248 t.Start();
249 foreach (var t in threads)
250 t.Join();
251 Console.WriteLine("Total time: " + (DateTime.Now - start));
252 Console.WriteLine();
253 return tests.Select(t => t.ReturnCode).Aggregate((r1, r2) => r1 | r2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200254 }
255 catch (Exception outerEx)
256 {
Jens Geyer178b8132015-09-30 23:16:45 +0200257 Console.WriteLine("*** FAILED ***");
258 Console.WriteLine("Unexpected error");
Jens Geyerd5436f52014-10-03 19:50:38 +0200259 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
Jens Geyer178b8132015-09-30 23:16:45 +0200260 return ErrorUnknown;
Jens Geyerd5436f52014-10-03 19:50:38 +0200261 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200262 }
David Reiss63191332009-01-06 19:49:22 +0000263
Jens Geyer71e814a2014-12-13 23:40:35 +0100264 public static string BytesToHex(byte[] data) {
265 return BitConverter.ToString(data).Replace("-", string.Empty);
266 }
267
268 public static byte[] PrepareTestData(bool randomDist)
269 {
270 byte[] retval = new byte[0x100];
271 int initLen = Math.Min(0x100,retval.Length);
272
273 // linear distribution, unless random is requested
274 if (!randomDist) {
Jens Geyer178b8132015-09-30 23:16:45 +0200275 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100276 retval[i] = (byte)i;
277 }
278 return retval;
279 }
280
281 // random distribution
Jens Geyer178b8132015-09-30 23:16:45 +0200282 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100283 retval[i] = (byte)0;
284 }
285 var rnd = new Random();
286 for (var i = 1; i < initLen; ++i) {
287 while( true) {
288 int nextPos = rnd.Next() % initLen;
289 if (retval[nextPos] == 0) {
290 retval[nextPos] = (byte)i;
291 break;
292 }
293 }
294 }
295 return retval;
296 }
297
Jens Geyer178b8132015-09-30 23:16:45 +0200298 public static int ExecuteClientTest(ThriftTest.Client client)
Jens Geyerd5436f52014-10-03 19:50:38 +0200299 {
Jens Geyer178b8132015-09-30 23:16:45 +0200300 int returnCode = 0;
David Reiss63191332009-01-06 19:49:22 +0000301
Jens Geyerd5436f52014-10-03 19:50:38 +0200302 Console.Write("testVoid()");
303 client.testVoid();
304 Console.WriteLine(" = void");
David Reiss63191332009-01-06 19:49:22 +0000305
Jens Geyerd5436f52014-10-03 19:50:38 +0200306 Console.Write("testString(\"Test\")");
307 string s = client.testString("Test");
308 Console.WriteLine(" = \"" + s + "\"");
Jens Geyer178b8132015-09-30 23:16:45 +0200309 if ("Test" != s)
310 {
311 Console.WriteLine("*** FAILED ***");
312 returnCode |= ErrorBaseTypes;
313 }
David Reiss63191332009-01-06 19:49:22 +0000314
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900315 Console.Write("testBool(true)");
316 bool t = client.testBool((bool)true);
317 Console.WriteLine(" = " + t);
Jens Geyer178b8132015-09-30 23:16:45 +0200318 if (!t)
319 {
320 Console.WriteLine("*** FAILED ***");
321 returnCode |= ErrorBaseTypes;
322 }
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900323 Console.Write("testBool(false)");
324 bool f = client.testBool((bool)false);
325 Console.WriteLine(" = " + f);
Jens Geyer178b8132015-09-30 23:16:45 +0200326 if (f)
327 {
328 Console.WriteLine("*** FAILED ***");
329 returnCode |= ErrorBaseTypes;
330 }
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900331
Jens Geyerd5436f52014-10-03 19:50:38 +0200332 Console.Write("testByte(1)");
333 sbyte i8 = client.testByte((sbyte)1);
334 Console.WriteLine(" = " + i8);
Jens Geyer178b8132015-09-30 23:16:45 +0200335 if (1 != i8)
336 {
337 Console.WriteLine("*** FAILED ***");
338 returnCode |= ErrorBaseTypes;
339 }
David Reiss63191332009-01-06 19:49:22 +0000340
Jens Geyerd5436f52014-10-03 19:50:38 +0200341 Console.Write("testI32(-1)");
342 int i32 = client.testI32(-1);
343 Console.WriteLine(" = " + i32);
Jens Geyer178b8132015-09-30 23:16:45 +0200344 if (-1 != i32)
345 {
346 Console.WriteLine("*** FAILED ***");
347 returnCode |= ErrorBaseTypes;
348 }
David Reiss63191332009-01-06 19:49:22 +0000349
Jens Geyerd5436f52014-10-03 19:50:38 +0200350 Console.Write("testI64(-34359738368)");
351 long i64 = client.testI64(-34359738368);
352 Console.WriteLine(" = " + i64);
Jens Geyer178b8132015-09-30 23:16:45 +0200353 if (-34359738368 != i64)
354 {
355 Console.WriteLine("*** FAILED ***");
356 returnCode |= ErrorBaseTypes;
357 }
David Reiss63191332009-01-06 19:49:22 +0000358
Jens Geyer178b8132015-09-30 23:16:45 +0200359 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200360 Console.Write("testDouble(5.325098235)");
361 double dub = client.testDouble(5.325098235);
362 Console.WriteLine(" = " + dub);
Jens Geyer178b8132015-09-30 23:16:45 +0200363 if (5.325098235 != dub)
364 {
365 Console.WriteLine("*** FAILED ***");
366 returnCode |= ErrorBaseTypes;
367 }
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900368 Console.Write("testDouble(-0.000341012439638598279)");
369 dub = client.testDouble(-0.000341012439638598279);
370 Console.WriteLine(" = " + dub);
371 if (-0.000341012439638598279 != dub)
372 {
373 Console.WriteLine("*** FAILED ***");
374 returnCode |= ErrorBaseTypes;
375 }
David Reiss63191332009-01-06 19:49:22 +0000376
Jens Geyer71e814a2014-12-13 23:40:35 +0100377 byte[] binOut = PrepareTestData(true);
378 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
379 try
380 {
381 byte[] binIn = client.testBinary(binOut);
382 Console.WriteLine(" = " + BytesToHex(binIn));
383 if (binIn.Length != binOut.Length)
Jens Geyer178b8132015-09-30 23:16:45 +0200384 {
385 Console.WriteLine("*** FAILED ***");
386 returnCode |= ErrorBaseTypes;
387 }
Jens Geyer71e814a2014-12-13 23:40:35 +0100388 for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
389 if (binIn[ofs] != binOut[ofs])
Jens Geyer178b8132015-09-30 23:16:45 +0200390 {
391 Console.WriteLine("*** FAILED ***");
392 returnCode |= ErrorBaseTypes;
393 }
Jens Geyer71e814a2014-12-13 23:40:35 +0100394 }
Jens Geyer178b8132015-09-30 23:16:45 +0200395 catch (Thrift.TApplicationException ex)
Jens Geyer71e814a2014-12-13 23:40:35 +0100396 {
Jens Geyer178b8132015-09-30 23:16:45 +0200397 Console.WriteLine("*** FAILED ***");
398 returnCode |= ErrorBaseTypes;
399 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
Jens Geyer71e814a2014-12-13 23:40:35 +0100400 }
401
Jens Geyer80aa53e2015-02-18 22:04:09 +0100402 // binary equals? only with hashcode option enabled ...
Jens Geyer178b8132015-09-30 23:16:45 +0200403 Console.WriteLine("Test CrazyNesting");
404 if( typeof(CrazyNesting).GetMethod("Equals").DeclaringType == typeof(CrazyNesting))
Jens Geyer80aa53e2015-02-18 22:04:09 +0100405 {
406 CrazyNesting one = new CrazyNesting();
407 CrazyNesting two = new CrazyNesting();
408 one.String_field = "crazy";
409 two.String_field = "crazy";
410 one.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
411 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
412 if (!one.Equals(two))
Jens Geyer178b8132015-09-30 23:16:45 +0200413 {
414 Console.WriteLine("*** FAILED ***");
415 returnCode |= ErrorContainers;
Jens Geyer80aa53e2015-02-18 22:04:09 +0100416 throw new Exception("CrazyNesting.Equals failed");
Jens Geyer178b8132015-09-30 23:16:45 +0200417 }
Jens Geyer80aa53e2015-02-18 22:04:09 +0100418 }
419
Jens Geyer178b8132015-09-30 23:16:45 +0200420 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200421 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
422 Xtruct o = new Xtruct();
423 o.String_thing = "Zero";
424 o.Byte_thing = (sbyte)1;
425 o.I32_thing = -3;
426 o.I64_thing = -5;
427 Xtruct i = client.testStruct(o);
428 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000429
Jens Geyer178b8132015-09-30 23:16:45 +0200430 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200431 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
432 Xtruct2 o2 = new Xtruct2();
433 o2.Byte_thing = (sbyte)1;
434 o2.Struct_thing = o;
435 o2.I32_thing = 5;
436 Xtruct2 i2 = client.testNest(o2);
437 i = i2.Struct_thing;
438 Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000439
Jens Geyerd5436f52014-10-03 19:50:38 +0200440 Dictionary<int, int> mapout = new Dictionary<int, int>();
441 for (int j = 0; j < 5; j++)
442 {
443 mapout[j] = j - 10;
444 }
445 Console.Write("testMap({");
446 bool first = true;
447 foreach (int key in mapout.Keys)
448 {
449 if (first)
450 {
451 first = false;
452 }
453 else
454 {
455 Console.Write(", ");
456 }
457 Console.Write(key + " => " + mapout[key]);
458 }
459 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000460
Jens Geyerd5436f52014-10-03 19:50:38 +0200461 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000462
Jens Geyerd5436f52014-10-03 19:50:38 +0200463 Console.Write(" = {");
464 first = true;
465 foreach (int key in mapin.Keys)
466 {
467 if (first)
468 {
469 first = false;
470 }
471 else
472 {
473 Console.Write(", ");
474 }
475 Console.Write(key + " => " + mapin[key]);
476 }
477 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000478
Jens Geyer178b8132015-09-30 23:16:45 +0200479 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200480 List<int> listout = new List<int>();
481 for (int j = -2; j < 3; j++)
482 {
483 listout.Add(j);
484 }
485 Console.Write("testList({");
486 first = true;
487 foreach (int j in listout)
488 {
489 if (first)
490 {
491 first = false;
492 }
493 else
494 {
495 Console.Write(", ");
496 }
497 Console.Write(j);
498 }
499 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000500
Jens Geyerd5436f52014-10-03 19:50:38 +0200501 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000502
Jens Geyerd5436f52014-10-03 19:50:38 +0200503 Console.Write(" = {");
504 first = true;
505 foreach (int j in listin)
506 {
507 if (first)
508 {
509 first = false;
510 }
511 else
512 {
513 Console.Write(", ");
514 }
515 Console.Write(j);
516 }
517 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000518
Jens Geyerd5436f52014-10-03 19:50:38 +0200519 //set
Jens Geyer178b8132015-09-30 23:16:45 +0200520 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200521 THashSet<int> setout = new THashSet<int>();
522 for (int j = -2; j < 3; j++)
523 {
524 setout.Add(j);
525 }
526 Console.Write("testSet({");
527 first = true;
528 foreach (int j in setout)
529 {
530 if (first)
531 {
532 first = false;
533 }
534 else
535 {
536 Console.Write(", ");
537 }
538 Console.Write(j);
539 }
540 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000541
Jens Geyerd5436f52014-10-03 19:50:38 +0200542 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000543
Jens Geyerd5436f52014-10-03 19:50:38 +0200544 Console.Write(" = {");
545 first = true;
546 foreach (int j in setin)
547 {
548 if (first)
549 {
550 first = false;
551 }
552 else
553 {
554 Console.Write(", ");
555 }
556 Console.Write(j);
557 }
558 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000559
560
Jens Geyerd5436f52014-10-03 19:50:38 +0200561 Console.Write("testEnum(ONE)");
562 Numberz ret = client.testEnum(Numberz.ONE);
563 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200564 if (Numberz.ONE != ret)
565 {
566 Console.WriteLine("*** FAILED ***");
567 returnCode |= ErrorStructs;
568 }
David Reiss63191332009-01-06 19:49:22 +0000569
Jens Geyerd5436f52014-10-03 19:50:38 +0200570 Console.Write("testEnum(TWO)");
571 ret = client.testEnum(Numberz.TWO);
572 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200573 if (Numberz.TWO != ret)
574 {
575 Console.WriteLine("*** FAILED ***");
576 returnCode |= ErrorStructs;
577 }
David Reiss63191332009-01-06 19:49:22 +0000578
Jens Geyerd5436f52014-10-03 19:50:38 +0200579 Console.Write("testEnum(THREE)");
580 ret = client.testEnum(Numberz.THREE);
581 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200582 if (Numberz.THREE != ret)
583 {
584 Console.WriteLine("*** FAILED ***");
585 returnCode |= ErrorStructs;
586 }
David Reiss63191332009-01-06 19:49:22 +0000587
Jens Geyerd5436f52014-10-03 19:50:38 +0200588 Console.Write("testEnum(FIVE)");
589 ret = client.testEnum(Numberz.FIVE);
590 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200591 if (Numberz.FIVE != ret)
592 {
593 Console.WriteLine("*** FAILED ***");
594 returnCode |= ErrorStructs;
595 }
David Reiss63191332009-01-06 19:49:22 +0000596
Jens Geyerd5436f52014-10-03 19:50:38 +0200597 Console.Write("testEnum(EIGHT)");
598 ret = client.testEnum(Numberz.EIGHT);
599 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200600 if (Numberz.EIGHT != ret)
601 {
602 Console.WriteLine("*** FAILED ***");
603 returnCode |= ErrorStructs;
604 }
David Reiss63191332009-01-06 19:49:22 +0000605
Jens Geyerd5436f52014-10-03 19:50:38 +0200606 Console.Write("testTypedef(309858235082523)");
607 long uid = client.testTypedef(309858235082523L);
608 Console.WriteLine(" = " + uid);
Jens Geyer178b8132015-09-30 23:16:45 +0200609 if (309858235082523L != uid)
610 {
611 Console.WriteLine("*** FAILED ***");
612 returnCode |= ErrorStructs;
613 }
David Reiss63191332009-01-06 19:49:22 +0000614
Jens Geyer178b8132015-09-30 23:16:45 +0200615 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200616 Console.Write("testMapMap(1)");
617 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
618 Console.Write(" = {");
619 foreach (int key in mm.Keys)
620 {
621 Console.Write(key + " => {");
622 Dictionary<int, int> m2 = mm[key];
623 foreach (int k2 in m2.Keys)
624 {
625 Console.Write(k2 + " => " + m2[k2] + ", ");
626 }
627 Console.Write("}, ");
628 }
629 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000630
Jens Geyer178b8132015-09-30 23:16:45 +0200631 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200632 Insanity insane = new Insanity();
633 insane.UserMap = new Dictionary<Numberz, long>();
634 insane.UserMap[Numberz.FIVE] = 5000L;
635 Xtruct truck = new Xtruct();
636 truck.String_thing = "Truck";
637 truck.Byte_thing = (sbyte)8;
638 truck.I32_thing = 8;
639 truck.I64_thing = 8;
640 insane.Xtructs = new List<Xtruct>();
641 insane.Xtructs.Add(truck);
642 Console.Write("testInsanity()");
643 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
644 Console.Write(" = {");
645 foreach (long key in whoa.Keys)
646 {
647 Dictionary<Numberz, Insanity> val = whoa[key];
648 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000649
Jens Geyerd5436f52014-10-03 19:50:38 +0200650 foreach (Numberz k2 in val.Keys)
651 {
652 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000653
Jens Geyerd5436f52014-10-03 19:50:38 +0200654 Console.Write(k2 + " => {");
655 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000656
Jens Geyerd5436f52014-10-03 19:50:38 +0200657 Console.Write("{");
658 if (userMap != null)
659 {
660 foreach (Numberz k3 in userMap.Keys)
661 {
662 Console.Write(k3 + " => " + userMap[k3] + ", ");
663 }
664 }
665 else
666 {
667 Console.Write("null");
668 }
669 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000670
Jens Geyerd5436f52014-10-03 19:50:38 +0200671 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000672
Jens Geyerd5436f52014-10-03 19:50:38 +0200673 Console.Write("{");
674 if (xtructs != null)
675 {
676 foreach (Xtruct x in xtructs)
677 {
678 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
679 }
680 }
681 else
682 {
683 Console.Write("null");
684 }
685 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000686
Jens Geyerd5436f52014-10-03 19:50:38 +0200687 Console.Write("}, ");
688 }
689 Console.Write("}, ");
690 }
691 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000692
Jens Geyerd5436f52014-10-03 19:50:38 +0200693 sbyte arg0 = 1;
694 int arg1 = 2;
695 long arg2 = long.MaxValue;
696 Dictionary<short, string> multiDict = new Dictionary<short, string>();
697 multiDict[1] = "one";
698 Numberz arg4 = Numberz.FIVE;
699 long arg5 = 5000000;
700 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
701 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
702 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
703 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000704
Jens Geyer178b8132015-09-30 23:16:45 +0200705 try
706 {
707 Console.WriteLine("testException(\"Xception\")");
708 client.testException("Xception");
709 Console.WriteLine("*** FAILED ***");
710 returnCode |= ErrorExceptions;
711 }
712 catch (Xception ex)
713 {
714 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
715 {
716 Console.WriteLine("*** FAILED ***");
717 returnCode |= ErrorExceptions;
718 }
719 }
720 catch (Exception ex)
721 {
722 Console.WriteLine("*** FAILED ***");
723 returnCode |= ErrorExceptions;
724 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
725 }
726 try
727 {
728 Console.WriteLine("testException(\"TException\")");
729 client.testException("TException");
730 Console.WriteLine("*** FAILED ***");
731 returnCode |= ErrorExceptions;
732 }
733 catch (Thrift.TException)
734 {
735 // OK
736 }
737 catch (Exception ex)
738 {
739 Console.WriteLine("*** FAILED ***");
740 returnCode |= ErrorExceptions;
741 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
742 }
743 try
744 {
745 Console.WriteLine("testException(\"ok\")");
746 client.testException("ok");
747 // OK
748 }
749 catch (Exception ex)
750 {
751 Console.WriteLine("*** FAILED ***");
752 returnCode |= ErrorExceptions;
753 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
754 }
755
756 try
757 {
758 Console.WriteLine("testMultiException(\"Xception\", ...)");
759 client.testMultiException("Xception", "ignore");
760 Console.WriteLine("*** FAILED ***");
761 returnCode |= ErrorExceptions;
762 }
763 catch (Xception ex)
764 {
765 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
766 {
767 Console.WriteLine("*** FAILED ***");
768 returnCode |= ErrorExceptions;
769 }
770 }
771 catch (Exception ex)
772 {
773 Console.WriteLine("*** FAILED ***");
774 returnCode |= ErrorExceptions;
775 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
776 }
777 try
778 {
779 Console.WriteLine("testMultiException(\"Xception2\", ...)");
780 client.testMultiException("Xception2", "ignore");
781 Console.WriteLine("*** FAILED ***");
782 returnCode |= ErrorExceptions;
783 }
784 catch (Xception2 ex)
785 {
786 if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2")
787 {
788 Console.WriteLine("*** FAILED ***");
789 returnCode |= ErrorExceptions;
790 }
791 }
792 catch (Exception ex)
793 {
794 Console.WriteLine("*** FAILED ***");
795 returnCode |= ErrorExceptions;
796 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
797 }
798 try
799 {
800 Console.WriteLine("testMultiException(\"success\", \"OK\")");
801 if ("OK" != client.testMultiException("success", "OK").String_thing)
802 {
803 Console.WriteLine("*** FAILED ***");
804 returnCode |= ErrorExceptions;
805 }
806 }
807 catch (Exception ex)
808 {
809 Console.WriteLine("*** FAILED ***");
810 returnCode |= ErrorExceptions;
811 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
812 }
813
814 Stopwatch sw = new Stopwatch();
815 sw.Start();
Jens Geyerd5436f52014-10-03 19:50:38 +0200816 Console.WriteLine("Test Oneway(1)");
817 client.testOneway(1);
Jens Geyer178b8132015-09-30 23:16:45 +0200818 sw.Stop();
819 if (sw.ElapsedMilliseconds > 1000)
820 {
821 Console.WriteLine("*** FAILED ***");
822 returnCode |= ErrorBaseTypes;
823 }
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000824
Jens Geyerd5436f52014-10-03 19:50:38 +0200825 Console.Write("Test Calltime()");
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +0900826 var times = 50;
827 sw.Reset();
828 sw.Start();
829 for (int k = 0; k < times; ++k)
Jens Geyerd5436f52014-10-03 19:50:38 +0200830 client.testVoid();
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +0900831 sw.Stop();
832 Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times);
Jens Geyer178b8132015-09-30 23:16:45 +0200833 return returnCode;
Jens Geyerd5436f52014-10-03 19:50:38 +0200834 }
835 }
David Reiss63191332009-01-06 19:49:22 +0000836}