blob: c09a801e4920a40771ec5f76686d45edf346e087 [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;
David Reiss63191332009-01-06 19:49:22 +000030
31namespace Test
32{
Jens Geyerd5436f52014-10-03 19:50:38 +020033 public class TestClient
34 {
Jens Geyer178b8132015-09-30 23:16:45 +020035 private class TestParams
36 {
37 public int numIterations = 1;
38 public string host = "localhost";
39 public int port = 9090;
40 public string url;
41 public string pipe;
42 public bool buffered;
43 public bool framed;
44 public string protocol;
45 public bool encrypted = false;
David Reiss63191332009-01-06 19:49:22 +000046
Jens Geyer178b8132015-09-30 23:16:45 +020047 public TTransport CreateTransport()
48 {
49 if (url == null)
50 {
51 // endpoint transport
52 TTransport trans = null;
53 if (pipe != null)
54 trans = new TNamedPipeClientTransport(pipe);
55 else
56 {
57 if (encrypted)
58 {
59 string certPath = "../../../../test/keys/client.p12";
60 X509Certificate cert = new X509Certificate2(certPath, "thrift");
61 trans = new TTLSSocket(host, port, cert, (o, c, chain, errors) => true);
62 }
63 else
64 {
65 trans = new TSocket(host, port);
66 }
67 }
68
69 // layered transport
70 if (buffered)
71 trans = new TBufferedTransport(trans);
72 if (framed)
73 trans = new TFramedTransport(trans);
74
75 //ensure proper open/close of transport
76 trans.Open();
77 trans.Close();
78 return trans;
79 }
80 else
81 {
82 return new THttpClient(new Uri(url));
83 }
84 }
85
86 public TProtocol CreateProtocol(TTransport transport)
87 {
88 if (protocol == "compact")
89 return new TCompactProtocol(transport);
90 else if (protocol == "json")
91 return new TJSONProtocol(transport);
92 else
93 return new TBinaryProtocol(transport);
94 }
95 };
96
97 private const int ErrorBaseTypes = 1;
98 private const int ErrorStructs = 2;
99 private const int ErrorContainers = 4;
100 private const int ErrorExceptions = 8;
101 private const int ErrorUnknown = 64;
102
103 private class ClientTest
104 {
105 private readonly TTransport transport;
106 private readonly ThriftTest.Client client;
107 private readonly int numIterations;
108 private bool done;
109
110 public int ReturnCode { get; set; }
111
112 public ClientTest(TestParams param)
113 {
114 transport = param.CreateTransport();
115 client = new ThriftTest.Client(param.CreateProtocol(transport));
116 numIterations = param.numIterations;
117 }
118 public void Execute()
119 {
120 if (done)
121 {
122 Console.WriteLine("Execute called more than once");
123 throw new InvalidOperationException();
124 }
125
126 for (int i = 0; i < numIterations; i++)
127 {
128 try
129 {
130 if (!transport.IsOpen)
131 transport.Open();
132 }
133 catch (TTransportException ex)
134 {
135 Console.WriteLine("*** FAILED ***");
136 Console.WriteLine("Connect failed: " + ex.Message);
137 ReturnCode |= ErrorUnknown;
138 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
139 continue;
140 }
141
142 try
143 {
144 ReturnCode |= ExecuteClientTest(client);
145 }
146 catch (Exception ex)
147 {
148 Console.WriteLine("*** FAILED ***");
149 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
150 ReturnCode |= ErrorUnknown;
151 }
152 }
153 try
154 {
155 transport.Close();
156 }
157 catch(Exception ex)
158 {
159 Console.WriteLine("Error while closing transport");
160 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
161 }
162 done = true;
163 }
164 }
165
166 public static int Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +0200167 {
168 try
169 {
Jens Geyer178b8132015-09-30 23:16:45 +0200170 TestParams param = new TestParams();
Jens Geyerd5436f52014-10-03 19:50:38 +0200171 int numThreads = 1;
Jens Geyerd5436f52014-10-03 19:50:38 +0200172 try
173 {
174 for (int i = 0; i < args.Length; i++)
175 {
176 if (args[i] == "-u")
177 {
Jens Geyer178b8132015-09-30 23:16:45 +0200178 param.url = args[++i];
Jens Geyerd5436f52014-10-03 19:50:38 +0200179 }
180 else if (args[i] == "-n")
181 {
Jens Geyer178b8132015-09-30 23:16:45 +0200182 param.numIterations = Convert.ToInt32(args[++i]);
Jens Geyerd5436f52014-10-03 19:50:38 +0200183 }
184 else if (args[i] == "-pipe") // -pipe <name>
185 {
Jens Geyer178b8132015-09-30 23:16:45 +0200186 param.pipe = args[++i];
Jens Geyerd5436f52014-10-03 19:50:38 +0200187 Console.WriteLine("Using named pipes transport");
188 }
189 else if (args[i].Contains("--host="))
190 {
Jens Geyer178b8132015-09-30 23:16:45 +0200191 param.host = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200192 }
193 else if (args[i].Contains("--port="))
194 {
Jens Geyer178b8132015-09-30 23:16:45 +0200195 param.port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
Jens Geyerd5436f52014-10-03 19:50:38 +0200196 }
197 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
198 {
Jens Geyer178b8132015-09-30 23:16:45 +0200199 param.buffered = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200200 Console.WriteLine("Using buffered sockets");
201 }
202 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
203 {
Jens Geyer178b8132015-09-30 23:16:45 +0200204 param.framed = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200205 Console.WriteLine("Using framed transport");
206 }
207 else if (args[i] == "-t")
208 {
209 numThreads = Convert.ToInt32(args[++i]);
210 }
211 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
212 {
Jens Geyer178b8132015-09-30 23:16:45 +0200213 param.protocol = "compact";
Jens Geyerd5436f52014-10-03 19:50:38 +0200214 Console.WriteLine("Using compact protocol");
215 }
216 else if (args[i] == "--json" || args[i] == "--protocol=json")
217 {
Jens Geyer178b8132015-09-30 23:16:45 +0200218 param.protocol = "json";
Jens Geyerd5436f52014-10-03 19:50:38 +0200219 Console.WriteLine("Using JSON protocol");
220 }
221 else if (args[i] == "--ssl")
222 {
Jens Geyer178b8132015-09-30 23:16:45 +0200223 param.encrypted = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200224 Console.WriteLine("Using encrypted transport");
225 }
226 }
227 }
Jens Geyer178b8132015-09-30 23:16:45 +0200228 catch (Exception ex)
Jens Geyerd5436f52014-10-03 19:50:38 +0200229 {
Jens Geyer178b8132015-09-30 23:16:45 +0200230 Console.WriteLine("*** FAILED ***");
231 Console.WriteLine("Error while parsing arguments");
232 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
233 return ErrorUnknown;
Jens Geyerd5436f52014-10-03 19:50:38 +0200234 }
David Reiss63191332009-01-06 19:49:22 +0000235
Jens Geyer178b8132015-09-30 23:16:45 +0200236 var tests = Enumerable.Range(0, numThreads).Select(_ => new ClientTest(param)).ToArray();
Jens Geyerd5436f52014-10-03 19:50:38 +0200237 //issue tests on separate threads simultaneously
Jens Geyer178b8132015-09-30 23:16:45 +0200238 var threads = tests.Select(test => new Thread(test.Execute)).ToArray();
Jens Geyerd5436f52014-10-03 19:50:38 +0200239 DateTime start = DateTime.Now;
Jens Geyer178b8132015-09-30 23:16:45 +0200240 foreach (var t in threads)
241 t.Start();
242 foreach (var t in threads)
243 t.Join();
244 Console.WriteLine("Total time: " + (DateTime.Now - start));
245 Console.WriteLine();
246 return tests.Select(t => t.ReturnCode).Aggregate((r1, r2) => r1 | r2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200247 }
248 catch (Exception outerEx)
249 {
Jens Geyer178b8132015-09-30 23:16:45 +0200250 Console.WriteLine("*** FAILED ***");
251 Console.WriteLine("Unexpected error");
Jens Geyerd5436f52014-10-03 19:50:38 +0200252 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
Jens Geyer178b8132015-09-30 23:16:45 +0200253 return ErrorUnknown;
Jens Geyerd5436f52014-10-03 19:50:38 +0200254 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200255 }
David Reiss63191332009-01-06 19:49:22 +0000256
Jens Geyer71e814a2014-12-13 23:40:35 +0100257 public static string BytesToHex(byte[] data) {
258 return BitConverter.ToString(data).Replace("-", string.Empty);
259 }
260
261 public static byte[] PrepareTestData(bool randomDist)
262 {
263 byte[] retval = new byte[0x100];
264 int initLen = Math.Min(0x100,retval.Length);
265
266 // linear distribution, unless random is requested
267 if (!randomDist) {
Jens Geyer178b8132015-09-30 23:16:45 +0200268 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100269 retval[i] = (byte)i;
270 }
271 return retval;
272 }
273
274 // random distribution
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)0;
277 }
278 var rnd = new Random();
279 for (var i = 1; i < initLen; ++i) {
280 while( true) {
281 int nextPos = rnd.Next() % initLen;
282 if (retval[nextPos] == 0) {
283 retval[nextPos] = (byte)i;
284 break;
285 }
286 }
287 }
288 return retval;
289 }
290
Jens Geyer178b8132015-09-30 23:16:45 +0200291 public static int ExecuteClientTest(ThriftTest.Client client)
Jens Geyerd5436f52014-10-03 19:50:38 +0200292 {
Jens Geyer178b8132015-09-30 23:16:45 +0200293 int returnCode = 0;
David Reiss63191332009-01-06 19:49:22 +0000294
Jens Geyerd5436f52014-10-03 19:50:38 +0200295 Console.Write("testVoid()");
296 client.testVoid();
297 Console.WriteLine(" = void");
David Reiss63191332009-01-06 19:49:22 +0000298
Jens Geyerd5436f52014-10-03 19:50:38 +0200299 Console.Write("testString(\"Test\")");
300 string s = client.testString("Test");
301 Console.WriteLine(" = \"" + s + "\"");
Jens Geyer178b8132015-09-30 23:16:45 +0200302 if ("Test" != s)
303 {
304 Console.WriteLine("*** FAILED ***");
305 returnCode |= ErrorBaseTypes;
306 }
David Reiss63191332009-01-06 19:49:22 +0000307
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900308 Console.Write("testBool(true)");
309 bool t = client.testBool((bool)true);
310 Console.WriteLine(" = " + t);
Jens Geyer178b8132015-09-30 23:16:45 +0200311 if (!t)
312 {
313 Console.WriteLine("*** FAILED ***");
314 returnCode |= ErrorBaseTypes;
315 }
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900316 Console.Write("testBool(false)");
317 bool f = client.testBool((bool)false);
318 Console.WriteLine(" = " + f);
Jens Geyer178b8132015-09-30 23:16:45 +0200319 if (f)
320 {
321 Console.WriteLine("*** FAILED ***");
322 returnCode |= ErrorBaseTypes;
323 }
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900324
Jens Geyerd5436f52014-10-03 19:50:38 +0200325 Console.Write("testByte(1)");
326 sbyte i8 = client.testByte((sbyte)1);
327 Console.WriteLine(" = " + i8);
Jens Geyer178b8132015-09-30 23:16:45 +0200328 if (1 != i8)
329 {
330 Console.WriteLine("*** FAILED ***");
331 returnCode |= ErrorBaseTypes;
332 }
David Reiss63191332009-01-06 19:49:22 +0000333
Jens Geyerd5436f52014-10-03 19:50:38 +0200334 Console.Write("testI32(-1)");
335 int i32 = client.testI32(-1);
336 Console.WriteLine(" = " + i32);
Jens Geyer178b8132015-09-30 23:16:45 +0200337 if (-1 != i32)
338 {
339 Console.WriteLine("*** FAILED ***");
340 returnCode |= ErrorBaseTypes;
341 }
David Reiss63191332009-01-06 19:49:22 +0000342
Jens Geyerd5436f52014-10-03 19:50:38 +0200343 Console.Write("testI64(-34359738368)");
344 long i64 = client.testI64(-34359738368);
345 Console.WriteLine(" = " + i64);
Jens Geyer178b8132015-09-30 23:16:45 +0200346 if (-34359738368 != i64)
347 {
348 Console.WriteLine("*** FAILED ***");
349 returnCode |= ErrorBaseTypes;
350 }
David Reiss63191332009-01-06 19:49:22 +0000351
Jens Geyer178b8132015-09-30 23:16:45 +0200352 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200353 Console.Write("testDouble(5.325098235)");
354 double dub = client.testDouble(5.325098235);
355 Console.WriteLine(" = " + dub);
Jens Geyer178b8132015-09-30 23:16:45 +0200356 if (5.325098235 != dub)
357 {
358 Console.WriteLine("*** FAILED ***");
359 returnCode |= ErrorBaseTypes;
360 }
David Reiss63191332009-01-06 19:49:22 +0000361
Jens Geyer71e814a2014-12-13 23:40:35 +0100362 byte[] binOut = PrepareTestData(true);
363 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
364 try
365 {
366 byte[] binIn = client.testBinary(binOut);
367 Console.WriteLine(" = " + BytesToHex(binIn));
368 if (binIn.Length != binOut.Length)
Jens Geyer178b8132015-09-30 23:16:45 +0200369 {
370 Console.WriteLine("*** FAILED ***");
371 returnCode |= ErrorBaseTypes;
372 }
Jens Geyer71e814a2014-12-13 23:40:35 +0100373 for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
374 if (binIn[ofs] != binOut[ofs])
Jens Geyer178b8132015-09-30 23:16:45 +0200375 {
376 Console.WriteLine("*** FAILED ***");
377 returnCode |= ErrorBaseTypes;
378 }
Jens Geyer71e814a2014-12-13 23:40:35 +0100379 }
Jens Geyer178b8132015-09-30 23:16:45 +0200380 catch (Thrift.TApplicationException ex)
Jens Geyer71e814a2014-12-13 23:40:35 +0100381 {
Jens Geyer178b8132015-09-30 23:16:45 +0200382 Console.WriteLine("*** FAILED ***");
383 returnCode |= ErrorBaseTypes;
384 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
Jens Geyer71e814a2014-12-13 23:40:35 +0100385 }
386
Jens Geyer80aa53e2015-02-18 22:04:09 +0100387 // binary equals? only with hashcode option enabled ...
Jens Geyer178b8132015-09-30 23:16:45 +0200388 Console.WriteLine("Test CrazyNesting");
389 if( typeof(CrazyNesting).GetMethod("Equals").DeclaringType == typeof(CrazyNesting))
Jens Geyer80aa53e2015-02-18 22:04:09 +0100390 {
391 CrazyNesting one = new CrazyNesting();
392 CrazyNesting two = new CrazyNesting();
393 one.String_field = "crazy";
394 two.String_field = "crazy";
395 one.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
396 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
397 if (!one.Equals(two))
Jens Geyer178b8132015-09-30 23:16:45 +0200398 {
399 Console.WriteLine("*** FAILED ***");
400 returnCode |= ErrorContainers;
Jens Geyer80aa53e2015-02-18 22:04:09 +0100401 throw new Exception("CrazyNesting.Equals failed");
Jens Geyer178b8132015-09-30 23:16:45 +0200402 }
Jens Geyer80aa53e2015-02-18 22:04:09 +0100403 }
404
Jens Geyer178b8132015-09-30 23:16:45 +0200405 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200406 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
407 Xtruct o = new Xtruct();
408 o.String_thing = "Zero";
409 o.Byte_thing = (sbyte)1;
410 o.I32_thing = -3;
411 o.I64_thing = -5;
412 Xtruct i = client.testStruct(o);
413 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000414
Jens Geyer178b8132015-09-30 23:16:45 +0200415 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200416 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
417 Xtruct2 o2 = new Xtruct2();
418 o2.Byte_thing = (sbyte)1;
419 o2.Struct_thing = o;
420 o2.I32_thing = 5;
421 Xtruct2 i2 = client.testNest(o2);
422 i = i2.Struct_thing;
423 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 +0000424
Jens Geyerd5436f52014-10-03 19:50:38 +0200425 Dictionary<int, int> mapout = new Dictionary<int, int>();
426 for (int j = 0; j < 5; j++)
427 {
428 mapout[j] = j - 10;
429 }
430 Console.Write("testMap({");
431 bool first = true;
432 foreach (int key in mapout.Keys)
433 {
434 if (first)
435 {
436 first = false;
437 }
438 else
439 {
440 Console.Write(", ");
441 }
442 Console.Write(key + " => " + mapout[key]);
443 }
444 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000445
Jens Geyerd5436f52014-10-03 19:50:38 +0200446 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000447
Jens Geyerd5436f52014-10-03 19:50:38 +0200448 Console.Write(" = {");
449 first = true;
450 foreach (int key in mapin.Keys)
451 {
452 if (first)
453 {
454 first = false;
455 }
456 else
457 {
458 Console.Write(", ");
459 }
460 Console.Write(key + " => " + mapin[key]);
461 }
462 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000463
Jens Geyer178b8132015-09-30 23:16:45 +0200464 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200465 List<int> listout = new List<int>();
466 for (int j = -2; j < 3; j++)
467 {
468 listout.Add(j);
469 }
470 Console.Write("testList({");
471 first = true;
472 foreach (int j in listout)
473 {
474 if (first)
475 {
476 first = false;
477 }
478 else
479 {
480 Console.Write(", ");
481 }
482 Console.Write(j);
483 }
484 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000485
Jens Geyerd5436f52014-10-03 19:50:38 +0200486 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000487
Jens Geyerd5436f52014-10-03 19:50:38 +0200488 Console.Write(" = {");
489 first = true;
490 foreach (int j in listin)
491 {
492 if (first)
493 {
494 first = false;
495 }
496 else
497 {
498 Console.Write(", ");
499 }
500 Console.Write(j);
501 }
502 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000503
Jens Geyerd5436f52014-10-03 19:50:38 +0200504 //set
Jens Geyer178b8132015-09-30 23:16:45 +0200505 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200506 THashSet<int> setout = new THashSet<int>();
507 for (int j = -2; j < 3; j++)
508 {
509 setout.Add(j);
510 }
511 Console.Write("testSet({");
512 first = true;
513 foreach (int j in setout)
514 {
515 if (first)
516 {
517 first = false;
518 }
519 else
520 {
521 Console.Write(", ");
522 }
523 Console.Write(j);
524 }
525 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000526
Jens Geyerd5436f52014-10-03 19:50:38 +0200527 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000528
Jens Geyerd5436f52014-10-03 19:50:38 +0200529 Console.Write(" = {");
530 first = true;
531 foreach (int j in setin)
532 {
533 if (first)
534 {
535 first = false;
536 }
537 else
538 {
539 Console.Write(", ");
540 }
541 Console.Write(j);
542 }
543 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000544
545
Jens Geyerd5436f52014-10-03 19:50:38 +0200546 Console.Write("testEnum(ONE)");
547 Numberz ret = client.testEnum(Numberz.ONE);
548 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200549 if (Numberz.ONE != ret)
550 {
551 Console.WriteLine("*** FAILED ***");
552 returnCode |= ErrorStructs;
553 }
David Reiss63191332009-01-06 19:49:22 +0000554
Jens Geyerd5436f52014-10-03 19:50:38 +0200555 Console.Write("testEnum(TWO)");
556 ret = client.testEnum(Numberz.TWO);
557 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200558 if (Numberz.TWO != ret)
559 {
560 Console.WriteLine("*** FAILED ***");
561 returnCode |= ErrorStructs;
562 }
David Reiss63191332009-01-06 19:49:22 +0000563
Jens Geyerd5436f52014-10-03 19:50:38 +0200564 Console.Write("testEnum(THREE)");
565 ret = client.testEnum(Numberz.THREE);
566 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200567 if (Numberz.THREE != ret)
568 {
569 Console.WriteLine("*** FAILED ***");
570 returnCode |= ErrorStructs;
571 }
David Reiss63191332009-01-06 19:49:22 +0000572
Jens Geyerd5436f52014-10-03 19:50:38 +0200573 Console.Write("testEnum(FIVE)");
574 ret = client.testEnum(Numberz.FIVE);
575 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200576 if (Numberz.FIVE != ret)
577 {
578 Console.WriteLine("*** FAILED ***");
579 returnCode |= ErrorStructs;
580 }
David Reiss63191332009-01-06 19:49:22 +0000581
Jens Geyerd5436f52014-10-03 19:50:38 +0200582 Console.Write("testEnum(EIGHT)");
583 ret = client.testEnum(Numberz.EIGHT);
584 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200585 if (Numberz.EIGHT != ret)
586 {
587 Console.WriteLine("*** FAILED ***");
588 returnCode |= ErrorStructs;
589 }
David Reiss63191332009-01-06 19:49:22 +0000590
Jens Geyerd5436f52014-10-03 19:50:38 +0200591 Console.Write("testTypedef(309858235082523)");
592 long uid = client.testTypedef(309858235082523L);
593 Console.WriteLine(" = " + uid);
Jens Geyer178b8132015-09-30 23:16:45 +0200594 if (309858235082523L != uid)
595 {
596 Console.WriteLine("*** FAILED ***");
597 returnCode |= ErrorStructs;
598 }
David Reiss63191332009-01-06 19:49:22 +0000599
Jens Geyer178b8132015-09-30 23:16:45 +0200600 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200601 Console.Write("testMapMap(1)");
602 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
603 Console.Write(" = {");
604 foreach (int key in mm.Keys)
605 {
606 Console.Write(key + " => {");
607 Dictionary<int, int> m2 = mm[key];
608 foreach (int k2 in m2.Keys)
609 {
610 Console.Write(k2 + " => " + m2[k2] + ", ");
611 }
612 Console.Write("}, ");
613 }
614 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000615
Jens Geyer178b8132015-09-30 23:16:45 +0200616 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200617 Insanity insane = new Insanity();
618 insane.UserMap = new Dictionary<Numberz, long>();
619 insane.UserMap[Numberz.FIVE] = 5000L;
620 Xtruct truck = new Xtruct();
621 truck.String_thing = "Truck";
622 truck.Byte_thing = (sbyte)8;
623 truck.I32_thing = 8;
624 truck.I64_thing = 8;
625 insane.Xtructs = new List<Xtruct>();
626 insane.Xtructs.Add(truck);
627 Console.Write("testInsanity()");
628 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
629 Console.Write(" = {");
630 foreach (long key in whoa.Keys)
631 {
632 Dictionary<Numberz, Insanity> val = whoa[key];
633 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000634
Jens Geyerd5436f52014-10-03 19:50:38 +0200635 foreach (Numberz k2 in val.Keys)
636 {
637 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000638
Jens Geyerd5436f52014-10-03 19:50:38 +0200639 Console.Write(k2 + " => {");
640 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000641
Jens Geyerd5436f52014-10-03 19:50:38 +0200642 Console.Write("{");
643 if (userMap != null)
644 {
645 foreach (Numberz k3 in userMap.Keys)
646 {
647 Console.Write(k3 + " => " + userMap[k3] + ", ");
648 }
649 }
650 else
651 {
652 Console.Write("null");
653 }
654 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000655
Jens Geyerd5436f52014-10-03 19:50:38 +0200656 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000657
Jens Geyerd5436f52014-10-03 19:50:38 +0200658 Console.Write("{");
659 if (xtructs != null)
660 {
661 foreach (Xtruct x in xtructs)
662 {
663 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
664 }
665 }
666 else
667 {
668 Console.Write("null");
669 }
670 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000671
Jens Geyerd5436f52014-10-03 19:50:38 +0200672 Console.Write("}, ");
673 }
674 Console.Write("}, ");
675 }
676 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000677
Jens Geyerd5436f52014-10-03 19:50:38 +0200678 sbyte arg0 = 1;
679 int arg1 = 2;
680 long arg2 = long.MaxValue;
681 Dictionary<short, string> multiDict = new Dictionary<short, string>();
682 multiDict[1] = "one";
683 Numberz arg4 = Numberz.FIVE;
684 long arg5 = 5000000;
685 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
686 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
687 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
688 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000689
Jens Geyer178b8132015-09-30 23:16:45 +0200690 try
691 {
692 Console.WriteLine("testException(\"Xception\")");
693 client.testException("Xception");
694 Console.WriteLine("*** FAILED ***");
695 returnCode |= ErrorExceptions;
696 }
697 catch (Xception ex)
698 {
699 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
700 {
701 Console.WriteLine("*** FAILED ***");
702 returnCode |= ErrorExceptions;
703 }
704 }
705 catch (Exception ex)
706 {
707 Console.WriteLine("*** FAILED ***");
708 returnCode |= ErrorExceptions;
709 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
710 }
711 try
712 {
713 Console.WriteLine("testException(\"TException\")");
714 client.testException("TException");
715 Console.WriteLine("*** FAILED ***");
716 returnCode |= ErrorExceptions;
717 }
718 catch (Thrift.TException)
719 {
720 // OK
721 }
722 catch (Exception ex)
723 {
724 Console.WriteLine("*** FAILED ***");
725 returnCode |= ErrorExceptions;
726 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
727 }
728 try
729 {
730 Console.WriteLine("testException(\"ok\")");
731 client.testException("ok");
732 // OK
733 }
734 catch (Exception ex)
735 {
736 Console.WriteLine("*** FAILED ***");
737 returnCode |= ErrorExceptions;
738 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
739 }
740
741 try
742 {
743 Console.WriteLine("testMultiException(\"Xception\", ...)");
744 client.testMultiException("Xception", "ignore");
745 Console.WriteLine("*** FAILED ***");
746 returnCode |= ErrorExceptions;
747 }
748 catch (Xception ex)
749 {
750 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
751 {
752 Console.WriteLine("*** FAILED ***");
753 returnCode |= ErrorExceptions;
754 }
755 }
756 catch (Exception ex)
757 {
758 Console.WriteLine("*** FAILED ***");
759 returnCode |= ErrorExceptions;
760 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
761 }
762 try
763 {
764 Console.WriteLine("testMultiException(\"Xception2\", ...)");
765 client.testMultiException("Xception2", "ignore");
766 Console.WriteLine("*** FAILED ***");
767 returnCode |= ErrorExceptions;
768 }
769 catch (Xception2 ex)
770 {
771 if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2")
772 {
773 Console.WriteLine("*** FAILED ***");
774 returnCode |= ErrorExceptions;
775 }
776 }
777 catch (Exception ex)
778 {
779 Console.WriteLine("*** FAILED ***");
780 returnCode |= ErrorExceptions;
781 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
782 }
783 try
784 {
785 Console.WriteLine("testMultiException(\"success\", \"OK\")");
786 if ("OK" != client.testMultiException("success", "OK").String_thing)
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
799 Stopwatch sw = new Stopwatch();
800 sw.Start();
Jens Geyerd5436f52014-10-03 19:50:38 +0200801 Console.WriteLine("Test Oneway(1)");
802 client.testOneway(1);
Jens Geyer178b8132015-09-30 23:16:45 +0200803 sw.Stop();
804 if (sw.ElapsedMilliseconds > 1000)
805 {
806 Console.WriteLine("*** FAILED ***");
807 returnCode |= ErrorBaseTypes;
808 }
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000809
Jens Geyerd5436f52014-10-03 19:50:38 +0200810 Console.Write("Test Calltime()");
811 var startt = DateTime.UtcNow;
812 for ( int k=0; k<1000; ++k )
813 client.testVoid();
814 Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" );
Jens Geyer178b8132015-09-30 23:16:45 +0200815 return returnCode;
Jens Geyerd5436f52014-10-03 19:50:38 +0200816 }
817 }
David Reiss63191332009-01-06 19:49:22 +0000818}