blob: f0297d02a02f941f20e8e84a2faec58855942f67 [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 }
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900361 Console.Write("testDouble(-0.000341012439638598279)");
362 dub = client.testDouble(-0.000341012439638598279);
363 Console.WriteLine(" = " + dub);
364 if (-0.000341012439638598279 != dub)
365 {
366 Console.WriteLine("*** FAILED ***");
367 returnCode |= ErrorBaseTypes;
368 }
David Reiss63191332009-01-06 19:49:22 +0000369
Jens Geyer71e814a2014-12-13 23:40:35 +0100370 byte[] binOut = PrepareTestData(true);
371 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
372 try
373 {
374 byte[] binIn = client.testBinary(binOut);
375 Console.WriteLine(" = " + BytesToHex(binIn));
376 if (binIn.Length != binOut.Length)
Jens Geyer178b8132015-09-30 23:16:45 +0200377 {
378 Console.WriteLine("*** FAILED ***");
379 returnCode |= ErrorBaseTypes;
380 }
Jens Geyer71e814a2014-12-13 23:40:35 +0100381 for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
382 if (binIn[ofs] != binOut[ofs])
Jens Geyer178b8132015-09-30 23:16:45 +0200383 {
384 Console.WriteLine("*** FAILED ***");
385 returnCode |= ErrorBaseTypes;
386 }
Jens Geyer71e814a2014-12-13 23:40:35 +0100387 }
Jens Geyer178b8132015-09-30 23:16:45 +0200388 catch (Thrift.TApplicationException ex)
Jens Geyer71e814a2014-12-13 23:40:35 +0100389 {
Jens Geyer178b8132015-09-30 23:16:45 +0200390 Console.WriteLine("*** FAILED ***");
391 returnCode |= ErrorBaseTypes;
392 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
Jens Geyer71e814a2014-12-13 23:40:35 +0100393 }
394
Jens Geyer80aa53e2015-02-18 22:04:09 +0100395 // binary equals? only with hashcode option enabled ...
Jens Geyer178b8132015-09-30 23:16:45 +0200396 Console.WriteLine("Test CrazyNesting");
397 if( typeof(CrazyNesting).GetMethod("Equals").DeclaringType == typeof(CrazyNesting))
Jens Geyer80aa53e2015-02-18 22:04:09 +0100398 {
399 CrazyNesting one = new CrazyNesting();
400 CrazyNesting two = new CrazyNesting();
401 one.String_field = "crazy";
402 two.String_field = "crazy";
403 one.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
404 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
405 if (!one.Equals(two))
Jens Geyer178b8132015-09-30 23:16:45 +0200406 {
407 Console.WriteLine("*** FAILED ***");
408 returnCode |= ErrorContainers;
Jens Geyer80aa53e2015-02-18 22:04:09 +0100409 throw new Exception("CrazyNesting.Equals failed");
Jens Geyer178b8132015-09-30 23:16:45 +0200410 }
Jens Geyer80aa53e2015-02-18 22:04:09 +0100411 }
412
Jens Geyer178b8132015-09-30 23:16:45 +0200413 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200414 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
415 Xtruct o = new Xtruct();
416 o.String_thing = "Zero";
417 o.Byte_thing = (sbyte)1;
418 o.I32_thing = -3;
419 o.I64_thing = -5;
420 Xtruct i = client.testStruct(o);
421 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000422
Jens Geyer178b8132015-09-30 23:16:45 +0200423 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200424 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
425 Xtruct2 o2 = new Xtruct2();
426 o2.Byte_thing = (sbyte)1;
427 o2.Struct_thing = o;
428 o2.I32_thing = 5;
429 Xtruct2 i2 = client.testNest(o2);
430 i = i2.Struct_thing;
431 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 +0000432
Jens Geyerd5436f52014-10-03 19:50:38 +0200433 Dictionary<int, int> mapout = new Dictionary<int, int>();
434 for (int j = 0; j < 5; j++)
435 {
436 mapout[j] = j - 10;
437 }
438 Console.Write("testMap({");
439 bool first = true;
440 foreach (int key in mapout.Keys)
441 {
442 if (first)
443 {
444 first = false;
445 }
446 else
447 {
448 Console.Write(", ");
449 }
450 Console.Write(key + " => " + mapout[key]);
451 }
452 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000453
Jens Geyerd5436f52014-10-03 19:50:38 +0200454 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000455
Jens Geyerd5436f52014-10-03 19:50:38 +0200456 Console.Write(" = {");
457 first = true;
458 foreach (int key in mapin.Keys)
459 {
460 if (first)
461 {
462 first = false;
463 }
464 else
465 {
466 Console.Write(", ");
467 }
468 Console.Write(key + " => " + mapin[key]);
469 }
470 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000471
Jens Geyer178b8132015-09-30 23:16:45 +0200472 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200473 List<int> listout = new List<int>();
474 for (int j = -2; j < 3; j++)
475 {
476 listout.Add(j);
477 }
478 Console.Write("testList({");
479 first = true;
480 foreach (int j in listout)
481 {
482 if (first)
483 {
484 first = false;
485 }
486 else
487 {
488 Console.Write(", ");
489 }
490 Console.Write(j);
491 }
492 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000493
Jens Geyerd5436f52014-10-03 19:50:38 +0200494 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000495
Jens Geyerd5436f52014-10-03 19:50:38 +0200496 Console.Write(" = {");
497 first = true;
498 foreach (int j in listin)
499 {
500 if (first)
501 {
502 first = false;
503 }
504 else
505 {
506 Console.Write(", ");
507 }
508 Console.Write(j);
509 }
510 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000511
Jens Geyerd5436f52014-10-03 19:50:38 +0200512 //set
Jens Geyer178b8132015-09-30 23:16:45 +0200513 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200514 THashSet<int> setout = new THashSet<int>();
515 for (int j = -2; j < 3; j++)
516 {
517 setout.Add(j);
518 }
519 Console.Write("testSet({");
520 first = true;
521 foreach (int j in setout)
522 {
523 if (first)
524 {
525 first = false;
526 }
527 else
528 {
529 Console.Write(", ");
530 }
531 Console.Write(j);
532 }
533 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000534
Jens Geyerd5436f52014-10-03 19:50:38 +0200535 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000536
Jens Geyerd5436f52014-10-03 19:50:38 +0200537 Console.Write(" = {");
538 first = true;
539 foreach (int j in setin)
540 {
541 if (first)
542 {
543 first = false;
544 }
545 else
546 {
547 Console.Write(", ");
548 }
549 Console.Write(j);
550 }
551 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000552
553
Jens Geyerd5436f52014-10-03 19:50:38 +0200554 Console.Write("testEnum(ONE)");
555 Numberz ret = client.testEnum(Numberz.ONE);
556 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200557 if (Numberz.ONE != ret)
558 {
559 Console.WriteLine("*** FAILED ***");
560 returnCode |= ErrorStructs;
561 }
David Reiss63191332009-01-06 19:49:22 +0000562
Jens Geyerd5436f52014-10-03 19:50:38 +0200563 Console.Write("testEnum(TWO)");
564 ret = client.testEnum(Numberz.TWO);
565 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200566 if (Numberz.TWO != ret)
567 {
568 Console.WriteLine("*** FAILED ***");
569 returnCode |= ErrorStructs;
570 }
David Reiss63191332009-01-06 19:49:22 +0000571
Jens Geyerd5436f52014-10-03 19:50:38 +0200572 Console.Write("testEnum(THREE)");
573 ret = client.testEnum(Numberz.THREE);
574 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200575 if (Numberz.THREE != ret)
576 {
577 Console.WriteLine("*** FAILED ***");
578 returnCode |= ErrorStructs;
579 }
David Reiss63191332009-01-06 19:49:22 +0000580
Jens Geyerd5436f52014-10-03 19:50:38 +0200581 Console.Write("testEnum(FIVE)");
582 ret = client.testEnum(Numberz.FIVE);
583 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200584 if (Numberz.FIVE != ret)
585 {
586 Console.WriteLine("*** FAILED ***");
587 returnCode |= ErrorStructs;
588 }
David Reiss63191332009-01-06 19:49:22 +0000589
Jens Geyerd5436f52014-10-03 19:50:38 +0200590 Console.Write("testEnum(EIGHT)");
591 ret = client.testEnum(Numberz.EIGHT);
592 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200593 if (Numberz.EIGHT != ret)
594 {
595 Console.WriteLine("*** FAILED ***");
596 returnCode |= ErrorStructs;
597 }
David Reiss63191332009-01-06 19:49:22 +0000598
Jens Geyerd5436f52014-10-03 19:50:38 +0200599 Console.Write("testTypedef(309858235082523)");
600 long uid = client.testTypedef(309858235082523L);
601 Console.WriteLine(" = " + uid);
Jens Geyer178b8132015-09-30 23:16:45 +0200602 if (309858235082523L != uid)
603 {
604 Console.WriteLine("*** FAILED ***");
605 returnCode |= ErrorStructs;
606 }
David Reiss63191332009-01-06 19:49:22 +0000607
Jens Geyer178b8132015-09-30 23:16:45 +0200608 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200609 Console.Write("testMapMap(1)");
610 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
611 Console.Write(" = {");
612 foreach (int key in mm.Keys)
613 {
614 Console.Write(key + " => {");
615 Dictionary<int, int> m2 = mm[key];
616 foreach (int k2 in m2.Keys)
617 {
618 Console.Write(k2 + " => " + m2[k2] + ", ");
619 }
620 Console.Write("}, ");
621 }
622 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000623
Jens Geyer178b8132015-09-30 23:16:45 +0200624 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200625 Insanity insane = new Insanity();
626 insane.UserMap = new Dictionary<Numberz, long>();
627 insane.UserMap[Numberz.FIVE] = 5000L;
628 Xtruct truck = new Xtruct();
629 truck.String_thing = "Truck";
630 truck.Byte_thing = (sbyte)8;
631 truck.I32_thing = 8;
632 truck.I64_thing = 8;
633 insane.Xtructs = new List<Xtruct>();
634 insane.Xtructs.Add(truck);
635 Console.Write("testInsanity()");
636 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
637 Console.Write(" = {");
638 foreach (long key in whoa.Keys)
639 {
640 Dictionary<Numberz, Insanity> val = whoa[key];
641 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000642
Jens Geyerd5436f52014-10-03 19:50:38 +0200643 foreach (Numberz k2 in val.Keys)
644 {
645 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000646
Jens Geyerd5436f52014-10-03 19:50:38 +0200647 Console.Write(k2 + " => {");
648 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000649
Jens Geyerd5436f52014-10-03 19:50:38 +0200650 Console.Write("{");
651 if (userMap != null)
652 {
653 foreach (Numberz k3 in userMap.Keys)
654 {
655 Console.Write(k3 + " => " + userMap[k3] + ", ");
656 }
657 }
658 else
659 {
660 Console.Write("null");
661 }
662 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000663
Jens Geyerd5436f52014-10-03 19:50:38 +0200664 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000665
Jens Geyerd5436f52014-10-03 19:50:38 +0200666 Console.Write("{");
667 if (xtructs != null)
668 {
669 foreach (Xtruct x in xtructs)
670 {
671 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
672 }
673 }
674 else
675 {
676 Console.Write("null");
677 }
678 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000679
Jens Geyerd5436f52014-10-03 19:50:38 +0200680 Console.Write("}, ");
681 }
682 Console.Write("}, ");
683 }
684 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000685
Jens Geyerd5436f52014-10-03 19:50:38 +0200686 sbyte arg0 = 1;
687 int arg1 = 2;
688 long arg2 = long.MaxValue;
689 Dictionary<short, string> multiDict = new Dictionary<short, string>();
690 multiDict[1] = "one";
691 Numberz arg4 = Numberz.FIVE;
692 long arg5 = 5000000;
693 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
694 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
695 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
696 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000697
Jens Geyer178b8132015-09-30 23:16:45 +0200698 try
699 {
700 Console.WriteLine("testException(\"Xception\")");
701 client.testException("Xception");
702 Console.WriteLine("*** FAILED ***");
703 returnCode |= ErrorExceptions;
704 }
705 catch (Xception ex)
706 {
707 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
708 {
709 Console.WriteLine("*** FAILED ***");
710 returnCode |= ErrorExceptions;
711 }
712 }
713 catch (Exception ex)
714 {
715 Console.WriteLine("*** FAILED ***");
716 returnCode |= ErrorExceptions;
717 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
718 }
719 try
720 {
721 Console.WriteLine("testException(\"TException\")");
722 client.testException("TException");
723 Console.WriteLine("*** FAILED ***");
724 returnCode |= ErrorExceptions;
725 }
726 catch (Thrift.TException)
727 {
728 // OK
729 }
730 catch (Exception ex)
731 {
732 Console.WriteLine("*** FAILED ***");
733 returnCode |= ErrorExceptions;
734 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
735 }
736 try
737 {
738 Console.WriteLine("testException(\"ok\")");
739 client.testException("ok");
740 // OK
741 }
742 catch (Exception ex)
743 {
744 Console.WriteLine("*** FAILED ***");
745 returnCode |= ErrorExceptions;
746 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
747 }
748
749 try
750 {
751 Console.WriteLine("testMultiException(\"Xception\", ...)");
752 client.testMultiException("Xception", "ignore");
753 Console.WriteLine("*** FAILED ***");
754 returnCode |= ErrorExceptions;
755 }
756 catch (Xception ex)
757 {
758 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
759 {
760 Console.WriteLine("*** FAILED ***");
761 returnCode |= ErrorExceptions;
762 }
763 }
764 catch (Exception ex)
765 {
766 Console.WriteLine("*** FAILED ***");
767 returnCode |= ErrorExceptions;
768 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
769 }
770 try
771 {
772 Console.WriteLine("testMultiException(\"Xception2\", ...)");
773 client.testMultiException("Xception2", "ignore");
774 Console.WriteLine("*** FAILED ***");
775 returnCode |= ErrorExceptions;
776 }
777 catch (Xception2 ex)
778 {
779 if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2")
780 {
781 Console.WriteLine("*** FAILED ***");
782 returnCode |= ErrorExceptions;
783 }
784 }
785 catch (Exception ex)
786 {
787 Console.WriteLine("*** FAILED ***");
788 returnCode |= ErrorExceptions;
789 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
790 }
791 try
792 {
793 Console.WriteLine("testMultiException(\"success\", \"OK\")");
794 if ("OK" != client.testMultiException("success", "OK").String_thing)
795 {
796 Console.WriteLine("*** FAILED ***");
797 returnCode |= ErrorExceptions;
798 }
799 }
800 catch (Exception ex)
801 {
802 Console.WriteLine("*** FAILED ***");
803 returnCode |= ErrorExceptions;
804 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
805 }
806
807 Stopwatch sw = new Stopwatch();
808 sw.Start();
Jens Geyerd5436f52014-10-03 19:50:38 +0200809 Console.WriteLine("Test Oneway(1)");
810 client.testOneway(1);
Jens Geyer178b8132015-09-30 23:16:45 +0200811 sw.Stop();
812 if (sw.ElapsedMilliseconds > 1000)
813 {
814 Console.WriteLine("*** FAILED ***");
815 returnCode |= ErrorBaseTypes;
816 }
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000817
Jens Geyerd5436f52014-10-03 19:50:38 +0200818 Console.Write("Test Calltime()");
819 var startt = DateTime.UtcNow;
820 for ( int k=0; k<1000; ++k )
821 client.testVoid();
822 Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" );
Jens Geyer178b8132015-09-30 23:16:45 +0200823 return returnCode;
Jens Geyerd5436f52014-10-03 19:50:38 +0200824 }
825 }
David Reiss63191332009-01-06 19:49:22 +0000826}