blob: 4c42ef7f1dab576b55defd57110522e22a1b87a6 [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;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000046 protected bool _isFirstTransport = true;
47
David Reiss63191332009-01-06 19:49:22 +000048
Jens Geyer178b8132015-09-30 23:16:45 +020049 public TTransport CreateTransport()
50 {
51 if (url == null)
52 {
53 // endpoint transport
54 TTransport trans = null;
55 if (pipe != null)
56 trans = new TNamedPipeClientTransport(pipe);
57 else
58 {
59 if (encrypted)
60 {
61 string certPath = "../../../../test/keys/client.p12";
62 X509Certificate cert = new X509Certificate2(certPath, "thrift");
63 trans = new TTLSSocket(host, port, cert, (o, c, chain, errors) => true);
64 }
65 else
66 {
67 trans = new TSocket(host, port);
68 }
69 }
70
71 // layered transport
72 if (buffered)
73 trans = new TBufferedTransport(trans);
74 if (framed)
75 trans = new TFramedTransport(trans);
76
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000077 if (_isFirstTransport)
78 {
79 //ensure proper open/close of transport
80 trans.Open();
81 trans.Close();
82 _isFirstTransport = false;
83 }
Jens Geyer178b8132015-09-30 23:16:45 +020084 return trans;
85 }
86 else
87 {
88 return new THttpClient(new Uri(url));
89 }
90 }
91
92 public TProtocol CreateProtocol(TTransport transport)
93 {
94 if (protocol == "compact")
95 return new TCompactProtocol(transport);
96 else if (protocol == "json")
97 return new TJSONProtocol(transport);
98 else
99 return new TBinaryProtocol(transport);
100 }
101 };
102
103 private const int ErrorBaseTypes = 1;
104 private const int ErrorStructs = 2;
105 private const int ErrorContainers = 4;
106 private const int ErrorExceptions = 8;
107 private const int ErrorUnknown = 64;
108
109 private class ClientTest
110 {
111 private readonly TTransport transport;
112 private readonly ThriftTest.Client client;
113 private readonly int numIterations;
114 private bool done;
115
116 public int ReturnCode { get; set; }
117
118 public ClientTest(TestParams param)
119 {
120 transport = param.CreateTransport();
121 client = new ThriftTest.Client(param.CreateProtocol(transport));
122 numIterations = param.numIterations;
123 }
124 public void Execute()
125 {
126 if (done)
127 {
128 Console.WriteLine("Execute called more than once");
129 throw new InvalidOperationException();
130 }
131
132 for (int i = 0; i < numIterations; i++)
133 {
134 try
135 {
136 if (!transport.IsOpen)
137 transport.Open();
138 }
139 catch (TTransportException ex)
140 {
141 Console.WriteLine("*** FAILED ***");
142 Console.WriteLine("Connect failed: " + ex.Message);
143 ReturnCode |= ErrorUnknown;
144 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
145 continue;
146 }
147
148 try
149 {
150 ReturnCode |= ExecuteClientTest(client);
151 }
152 catch (Exception ex)
153 {
154 Console.WriteLine("*** FAILED ***");
155 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
156 ReturnCode |= ErrorUnknown;
157 }
158 }
159 try
160 {
161 transport.Close();
162 }
163 catch(Exception ex)
164 {
165 Console.WriteLine("Error while closing transport");
166 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
167 }
168 done = true;
169 }
170 }
171
172 public static int Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +0200173 {
174 try
175 {
Jens Geyer178b8132015-09-30 23:16:45 +0200176 TestParams param = new TestParams();
Jens Geyerd5436f52014-10-03 19:50:38 +0200177 int numThreads = 1;
Jens Geyerd5436f52014-10-03 19:50:38 +0200178 try
179 {
180 for (int i = 0; i < args.Length; i++)
181 {
182 if (args[i] == "-u")
183 {
Jens Geyer178b8132015-09-30 23:16:45 +0200184 param.url = args[++i];
Jens Geyerd5436f52014-10-03 19:50:38 +0200185 }
186 else if (args[i] == "-n")
187 {
Jens Geyer178b8132015-09-30 23:16:45 +0200188 param.numIterations = Convert.ToInt32(args[++i]);
Jens Geyerd5436f52014-10-03 19:50:38 +0200189 }
190 else if (args[i] == "-pipe") // -pipe <name>
191 {
Jens Geyer178b8132015-09-30 23:16:45 +0200192 param.pipe = args[++i];
Jens Geyerd5436f52014-10-03 19:50:38 +0200193 Console.WriteLine("Using named pipes transport");
194 }
195 else if (args[i].Contains("--host="))
196 {
Jens Geyer178b8132015-09-30 23:16:45 +0200197 param.host = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200198 }
199 else if (args[i].Contains("--port="))
200 {
Jens Geyer178b8132015-09-30 23:16:45 +0200201 param.port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
Jens Geyerd5436f52014-10-03 19:50:38 +0200202 }
203 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
204 {
Jens Geyer178b8132015-09-30 23:16:45 +0200205 param.buffered = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200206 Console.WriteLine("Using buffered sockets");
207 }
208 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
209 {
Jens Geyer178b8132015-09-30 23:16:45 +0200210 param.framed = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200211 Console.WriteLine("Using framed transport");
212 }
213 else if (args[i] == "-t")
214 {
215 numThreads = Convert.ToInt32(args[++i]);
216 }
217 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
218 {
Jens Geyer178b8132015-09-30 23:16:45 +0200219 param.protocol = "compact";
Jens Geyerd5436f52014-10-03 19:50:38 +0200220 Console.WriteLine("Using compact protocol");
221 }
222 else if (args[i] == "--json" || args[i] == "--protocol=json")
223 {
Jens Geyer178b8132015-09-30 23:16:45 +0200224 param.protocol = "json";
Jens Geyerd5436f52014-10-03 19:50:38 +0200225 Console.WriteLine("Using JSON protocol");
226 }
227 else if (args[i] == "--ssl")
228 {
Jens Geyer178b8132015-09-30 23:16:45 +0200229 param.encrypted = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200230 Console.WriteLine("Using encrypted transport");
231 }
232 }
233 }
Jens Geyer178b8132015-09-30 23:16:45 +0200234 catch (Exception ex)
Jens Geyerd5436f52014-10-03 19:50:38 +0200235 {
Jens Geyer178b8132015-09-30 23:16:45 +0200236 Console.WriteLine("*** FAILED ***");
237 Console.WriteLine("Error while parsing arguments");
238 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
239 return ErrorUnknown;
Jens Geyerd5436f52014-10-03 19:50:38 +0200240 }
David Reiss63191332009-01-06 19:49:22 +0000241
Jens Geyer178b8132015-09-30 23:16:45 +0200242 var tests = Enumerable.Range(0, numThreads).Select(_ => new ClientTest(param)).ToArray();
Jens Geyerd5436f52014-10-03 19:50:38 +0200243 //issue tests on separate threads simultaneously
Jens Geyer178b8132015-09-30 23:16:45 +0200244 var threads = tests.Select(test => new Thread(test.Execute)).ToArray();
Jens Geyerd5436f52014-10-03 19:50:38 +0200245 DateTime start = DateTime.Now;
Jens Geyer178b8132015-09-30 23:16:45 +0200246 foreach (var t in threads)
247 t.Start();
248 foreach (var t in threads)
249 t.Join();
250 Console.WriteLine("Total time: " + (DateTime.Now - start));
251 Console.WriteLine();
252 return tests.Select(t => t.ReturnCode).Aggregate((r1, r2) => r1 | r2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200253 }
254 catch (Exception outerEx)
255 {
Jens Geyer178b8132015-09-30 23:16:45 +0200256 Console.WriteLine("*** FAILED ***");
257 Console.WriteLine("Unexpected error");
Jens Geyerd5436f52014-10-03 19:50:38 +0200258 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
Jens Geyer178b8132015-09-30 23:16:45 +0200259 return ErrorUnknown;
Jens Geyerd5436f52014-10-03 19:50:38 +0200260 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200261 }
David Reiss63191332009-01-06 19:49:22 +0000262
Jens Geyer71e814a2014-12-13 23:40:35 +0100263 public static string BytesToHex(byte[] data) {
264 return BitConverter.ToString(data).Replace("-", string.Empty);
265 }
266
267 public static byte[] PrepareTestData(bool randomDist)
268 {
269 byte[] retval = new byte[0x100];
270 int initLen = Math.Min(0x100,retval.Length);
271
272 // linear distribution, unless random is requested
273 if (!randomDist) {
Jens Geyer178b8132015-09-30 23:16:45 +0200274 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100275 retval[i] = (byte)i;
276 }
277 return retval;
278 }
279
280 // random distribution
Jens Geyer178b8132015-09-30 23:16:45 +0200281 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100282 retval[i] = (byte)0;
283 }
284 var rnd = new Random();
285 for (var i = 1; i < initLen; ++i) {
286 while( true) {
287 int nextPos = rnd.Next() % initLen;
288 if (retval[nextPos] == 0) {
289 retval[nextPos] = (byte)i;
290 break;
291 }
292 }
293 }
294 return retval;
295 }
296
Jens Geyer178b8132015-09-30 23:16:45 +0200297 public static int ExecuteClientTest(ThriftTest.Client client)
Jens Geyerd5436f52014-10-03 19:50:38 +0200298 {
Jens Geyer178b8132015-09-30 23:16:45 +0200299 int returnCode = 0;
David Reiss63191332009-01-06 19:49:22 +0000300
Jens Geyerd5436f52014-10-03 19:50:38 +0200301 Console.Write("testVoid()");
302 client.testVoid();
303 Console.WriteLine(" = void");
David Reiss63191332009-01-06 19:49:22 +0000304
Jens Geyerd5436f52014-10-03 19:50:38 +0200305 Console.Write("testString(\"Test\")");
306 string s = client.testString("Test");
307 Console.WriteLine(" = \"" + s + "\"");
Jens Geyer178b8132015-09-30 23:16:45 +0200308 if ("Test" != s)
309 {
310 Console.WriteLine("*** FAILED ***");
311 returnCode |= ErrorBaseTypes;
312 }
David Reiss63191332009-01-06 19:49:22 +0000313
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900314 Console.Write("testBool(true)");
315 bool t = client.testBool((bool)true);
316 Console.WriteLine(" = " + t);
Jens Geyer178b8132015-09-30 23:16:45 +0200317 if (!t)
318 {
319 Console.WriteLine("*** FAILED ***");
320 returnCode |= ErrorBaseTypes;
321 }
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900322 Console.Write("testBool(false)");
323 bool f = client.testBool((bool)false);
324 Console.WriteLine(" = " + f);
Jens Geyer178b8132015-09-30 23:16:45 +0200325 if (f)
326 {
327 Console.WriteLine("*** FAILED ***");
328 returnCode |= ErrorBaseTypes;
329 }
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900330
Jens Geyerd5436f52014-10-03 19:50:38 +0200331 Console.Write("testByte(1)");
332 sbyte i8 = client.testByte((sbyte)1);
333 Console.WriteLine(" = " + i8);
Jens Geyer178b8132015-09-30 23:16:45 +0200334 if (1 != i8)
335 {
336 Console.WriteLine("*** FAILED ***");
337 returnCode |= ErrorBaseTypes;
338 }
David Reiss63191332009-01-06 19:49:22 +0000339
Jens Geyerd5436f52014-10-03 19:50:38 +0200340 Console.Write("testI32(-1)");
341 int i32 = client.testI32(-1);
342 Console.WriteLine(" = " + i32);
Jens Geyer178b8132015-09-30 23:16:45 +0200343 if (-1 != i32)
344 {
345 Console.WriteLine("*** FAILED ***");
346 returnCode |= ErrorBaseTypes;
347 }
David Reiss63191332009-01-06 19:49:22 +0000348
Jens Geyerd5436f52014-10-03 19:50:38 +0200349 Console.Write("testI64(-34359738368)");
350 long i64 = client.testI64(-34359738368);
351 Console.WriteLine(" = " + i64);
Jens Geyer178b8132015-09-30 23:16:45 +0200352 if (-34359738368 != i64)
353 {
354 Console.WriteLine("*** FAILED ***");
355 returnCode |= ErrorBaseTypes;
356 }
David Reiss63191332009-01-06 19:49:22 +0000357
Jens Geyer178b8132015-09-30 23:16:45 +0200358 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200359 Console.Write("testDouble(5.325098235)");
360 double dub = client.testDouble(5.325098235);
361 Console.WriteLine(" = " + dub);
Jens Geyer178b8132015-09-30 23:16:45 +0200362 if (5.325098235 != dub)
363 {
364 Console.WriteLine("*** FAILED ***");
365 returnCode |= ErrorBaseTypes;
366 }
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900367 Console.Write("testDouble(-0.000341012439638598279)");
368 dub = client.testDouble(-0.000341012439638598279);
369 Console.WriteLine(" = " + dub);
370 if (-0.000341012439638598279 != dub)
371 {
372 Console.WriteLine("*** FAILED ***");
373 returnCode |= ErrorBaseTypes;
374 }
David Reiss63191332009-01-06 19:49:22 +0000375
Jens Geyer71e814a2014-12-13 23:40:35 +0100376 byte[] binOut = PrepareTestData(true);
377 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
378 try
379 {
380 byte[] binIn = client.testBinary(binOut);
381 Console.WriteLine(" = " + BytesToHex(binIn));
382 if (binIn.Length != binOut.Length)
Jens Geyer178b8132015-09-30 23:16:45 +0200383 {
384 Console.WriteLine("*** FAILED ***");
385 returnCode |= ErrorBaseTypes;
386 }
Jens Geyer71e814a2014-12-13 23:40:35 +0100387 for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
388 if (binIn[ofs] != binOut[ofs])
Jens Geyer178b8132015-09-30 23:16:45 +0200389 {
390 Console.WriteLine("*** FAILED ***");
391 returnCode |= ErrorBaseTypes;
392 }
Jens Geyer71e814a2014-12-13 23:40:35 +0100393 }
Jens Geyer178b8132015-09-30 23:16:45 +0200394 catch (Thrift.TApplicationException ex)
Jens Geyer71e814a2014-12-13 23:40:35 +0100395 {
Jens Geyer178b8132015-09-30 23:16:45 +0200396 Console.WriteLine("*** FAILED ***");
397 returnCode |= ErrorBaseTypes;
398 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
Jens Geyer71e814a2014-12-13 23:40:35 +0100399 }
400
Jens Geyer80aa53e2015-02-18 22:04:09 +0100401 // binary equals? only with hashcode option enabled ...
Jens Geyer178b8132015-09-30 23:16:45 +0200402 Console.WriteLine("Test CrazyNesting");
403 if( typeof(CrazyNesting).GetMethod("Equals").DeclaringType == typeof(CrazyNesting))
Jens Geyer80aa53e2015-02-18 22:04:09 +0100404 {
405 CrazyNesting one = new CrazyNesting();
406 CrazyNesting two = new CrazyNesting();
407 one.String_field = "crazy";
408 two.String_field = "crazy";
409 one.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
410 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
411 if (!one.Equals(two))
Jens Geyer178b8132015-09-30 23:16:45 +0200412 {
413 Console.WriteLine("*** FAILED ***");
414 returnCode |= ErrorContainers;
Jens Geyer80aa53e2015-02-18 22:04:09 +0100415 throw new Exception("CrazyNesting.Equals failed");
Jens Geyer178b8132015-09-30 23:16:45 +0200416 }
Jens Geyer80aa53e2015-02-18 22:04:09 +0100417 }
418
Jens Geyer178b8132015-09-30 23:16:45 +0200419 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200420 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
421 Xtruct o = new Xtruct();
422 o.String_thing = "Zero";
423 o.Byte_thing = (sbyte)1;
424 o.I32_thing = -3;
425 o.I64_thing = -5;
426 Xtruct i = client.testStruct(o);
427 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000428
Jens Geyer178b8132015-09-30 23:16:45 +0200429 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200430 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
431 Xtruct2 o2 = new Xtruct2();
432 o2.Byte_thing = (sbyte)1;
433 o2.Struct_thing = o;
434 o2.I32_thing = 5;
435 Xtruct2 i2 = client.testNest(o2);
436 i = i2.Struct_thing;
437 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 +0000438
Jens Geyerd5436f52014-10-03 19:50:38 +0200439 Dictionary<int, int> mapout = new Dictionary<int, int>();
440 for (int j = 0; j < 5; j++)
441 {
442 mapout[j] = j - 10;
443 }
444 Console.Write("testMap({");
445 bool first = true;
446 foreach (int key in mapout.Keys)
447 {
448 if (first)
449 {
450 first = false;
451 }
452 else
453 {
454 Console.Write(", ");
455 }
456 Console.Write(key + " => " + mapout[key]);
457 }
458 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000459
Jens Geyerd5436f52014-10-03 19:50:38 +0200460 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000461
Jens Geyerd5436f52014-10-03 19:50:38 +0200462 Console.Write(" = {");
463 first = true;
464 foreach (int key in mapin.Keys)
465 {
466 if (first)
467 {
468 first = false;
469 }
470 else
471 {
472 Console.Write(", ");
473 }
474 Console.Write(key + " => " + mapin[key]);
475 }
476 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000477
Jens Geyer178b8132015-09-30 23:16:45 +0200478 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200479 List<int> listout = new List<int>();
480 for (int j = -2; j < 3; j++)
481 {
482 listout.Add(j);
483 }
484 Console.Write("testList({");
485 first = true;
486 foreach (int j in listout)
487 {
488 if (first)
489 {
490 first = false;
491 }
492 else
493 {
494 Console.Write(", ");
495 }
496 Console.Write(j);
497 }
498 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000499
Jens Geyerd5436f52014-10-03 19:50:38 +0200500 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000501
Jens Geyerd5436f52014-10-03 19:50:38 +0200502 Console.Write(" = {");
503 first = true;
504 foreach (int j in listin)
505 {
506 if (first)
507 {
508 first = false;
509 }
510 else
511 {
512 Console.Write(", ");
513 }
514 Console.Write(j);
515 }
516 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000517
Jens Geyerd5436f52014-10-03 19:50:38 +0200518 //set
Jens Geyer178b8132015-09-30 23:16:45 +0200519 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200520 THashSet<int> setout = new THashSet<int>();
521 for (int j = -2; j < 3; j++)
522 {
523 setout.Add(j);
524 }
525 Console.Write("testSet({");
526 first = true;
527 foreach (int j in setout)
528 {
529 if (first)
530 {
531 first = false;
532 }
533 else
534 {
535 Console.Write(", ");
536 }
537 Console.Write(j);
538 }
539 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000540
Jens Geyerd5436f52014-10-03 19:50:38 +0200541 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000542
Jens Geyerd5436f52014-10-03 19:50:38 +0200543 Console.Write(" = {");
544 first = true;
545 foreach (int j in setin)
546 {
547 if (first)
548 {
549 first = false;
550 }
551 else
552 {
553 Console.Write(", ");
554 }
555 Console.Write(j);
556 }
557 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000558
559
Jens Geyerd5436f52014-10-03 19:50:38 +0200560 Console.Write("testEnum(ONE)");
561 Numberz ret = client.testEnum(Numberz.ONE);
562 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200563 if (Numberz.ONE != ret)
564 {
565 Console.WriteLine("*** FAILED ***");
566 returnCode |= ErrorStructs;
567 }
David Reiss63191332009-01-06 19:49:22 +0000568
Jens Geyerd5436f52014-10-03 19:50:38 +0200569 Console.Write("testEnum(TWO)");
570 ret = client.testEnum(Numberz.TWO);
571 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200572 if (Numberz.TWO != ret)
573 {
574 Console.WriteLine("*** FAILED ***");
575 returnCode |= ErrorStructs;
576 }
David Reiss63191332009-01-06 19:49:22 +0000577
Jens Geyerd5436f52014-10-03 19:50:38 +0200578 Console.Write("testEnum(THREE)");
579 ret = client.testEnum(Numberz.THREE);
580 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200581 if (Numberz.THREE != ret)
582 {
583 Console.WriteLine("*** FAILED ***");
584 returnCode |= ErrorStructs;
585 }
David Reiss63191332009-01-06 19:49:22 +0000586
Jens Geyerd5436f52014-10-03 19:50:38 +0200587 Console.Write("testEnum(FIVE)");
588 ret = client.testEnum(Numberz.FIVE);
589 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200590 if (Numberz.FIVE != ret)
591 {
592 Console.WriteLine("*** FAILED ***");
593 returnCode |= ErrorStructs;
594 }
David Reiss63191332009-01-06 19:49:22 +0000595
Jens Geyerd5436f52014-10-03 19:50:38 +0200596 Console.Write("testEnum(EIGHT)");
597 ret = client.testEnum(Numberz.EIGHT);
598 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200599 if (Numberz.EIGHT != ret)
600 {
601 Console.WriteLine("*** FAILED ***");
602 returnCode |= ErrorStructs;
603 }
David Reiss63191332009-01-06 19:49:22 +0000604
Jens Geyerd5436f52014-10-03 19:50:38 +0200605 Console.Write("testTypedef(309858235082523)");
606 long uid = client.testTypedef(309858235082523L);
607 Console.WriteLine(" = " + uid);
Jens Geyer178b8132015-09-30 23:16:45 +0200608 if (309858235082523L != uid)
609 {
610 Console.WriteLine("*** FAILED ***");
611 returnCode |= ErrorStructs;
612 }
David Reiss63191332009-01-06 19:49:22 +0000613
Jens Geyer178b8132015-09-30 23:16:45 +0200614 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200615 Console.Write("testMapMap(1)");
616 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
617 Console.Write(" = {");
618 foreach (int key in mm.Keys)
619 {
620 Console.Write(key + " => {");
621 Dictionary<int, int> m2 = mm[key];
622 foreach (int k2 in m2.Keys)
623 {
624 Console.Write(k2 + " => " + m2[k2] + ", ");
625 }
626 Console.Write("}, ");
627 }
628 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000629
Jens Geyer178b8132015-09-30 23:16:45 +0200630 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200631 Insanity insane = new Insanity();
632 insane.UserMap = new Dictionary<Numberz, long>();
633 insane.UserMap[Numberz.FIVE] = 5000L;
634 Xtruct truck = new Xtruct();
635 truck.String_thing = "Truck";
636 truck.Byte_thing = (sbyte)8;
637 truck.I32_thing = 8;
638 truck.I64_thing = 8;
639 insane.Xtructs = new List<Xtruct>();
640 insane.Xtructs.Add(truck);
641 Console.Write("testInsanity()");
642 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
643 Console.Write(" = {");
644 foreach (long key in whoa.Keys)
645 {
646 Dictionary<Numberz, Insanity> val = whoa[key];
647 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000648
Jens Geyerd5436f52014-10-03 19:50:38 +0200649 foreach (Numberz k2 in val.Keys)
650 {
651 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000652
Jens Geyerd5436f52014-10-03 19:50:38 +0200653 Console.Write(k2 + " => {");
654 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000655
Jens Geyerd5436f52014-10-03 19:50:38 +0200656 Console.Write("{");
657 if (userMap != null)
658 {
659 foreach (Numberz k3 in userMap.Keys)
660 {
661 Console.Write(k3 + " => " + userMap[k3] + ", ");
662 }
663 }
664 else
665 {
666 Console.Write("null");
667 }
668 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000669
Jens Geyerd5436f52014-10-03 19:50:38 +0200670 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000671
Jens Geyerd5436f52014-10-03 19:50:38 +0200672 Console.Write("{");
673 if (xtructs != null)
674 {
675 foreach (Xtruct x in xtructs)
676 {
677 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
678 }
679 }
680 else
681 {
682 Console.Write("null");
683 }
684 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000685
Jens Geyerd5436f52014-10-03 19:50:38 +0200686 Console.Write("}, ");
687 }
688 Console.Write("}, ");
689 }
690 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000691
Jens Geyerd5436f52014-10-03 19:50:38 +0200692 sbyte arg0 = 1;
693 int arg1 = 2;
694 long arg2 = long.MaxValue;
695 Dictionary<short, string> multiDict = new Dictionary<short, string>();
696 multiDict[1] = "one";
697 Numberz arg4 = Numberz.FIVE;
698 long arg5 = 5000000;
699 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
700 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
701 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
702 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000703
Jens Geyer178b8132015-09-30 23:16:45 +0200704 try
705 {
706 Console.WriteLine("testException(\"Xception\")");
707 client.testException("Xception");
708 Console.WriteLine("*** FAILED ***");
709 returnCode |= ErrorExceptions;
710 }
711 catch (Xception ex)
712 {
713 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
714 {
715 Console.WriteLine("*** FAILED ***");
716 returnCode |= ErrorExceptions;
717 }
718 }
719 catch (Exception ex)
720 {
721 Console.WriteLine("*** FAILED ***");
722 returnCode |= ErrorExceptions;
723 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
724 }
725 try
726 {
727 Console.WriteLine("testException(\"TException\")");
728 client.testException("TException");
729 Console.WriteLine("*** FAILED ***");
730 returnCode |= ErrorExceptions;
731 }
732 catch (Thrift.TException)
733 {
734 // OK
735 }
736 catch (Exception ex)
737 {
738 Console.WriteLine("*** FAILED ***");
739 returnCode |= ErrorExceptions;
740 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
741 }
742 try
743 {
744 Console.WriteLine("testException(\"ok\")");
745 client.testException("ok");
746 // OK
747 }
748 catch (Exception ex)
749 {
750 Console.WriteLine("*** FAILED ***");
751 returnCode |= ErrorExceptions;
752 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
753 }
754
755 try
756 {
757 Console.WriteLine("testMultiException(\"Xception\", ...)");
758 client.testMultiException("Xception", "ignore");
759 Console.WriteLine("*** FAILED ***");
760 returnCode |= ErrorExceptions;
761 }
762 catch (Xception ex)
763 {
764 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
765 {
766 Console.WriteLine("*** FAILED ***");
767 returnCode |= ErrorExceptions;
768 }
769 }
770 catch (Exception ex)
771 {
772 Console.WriteLine("*** FAILED ***");
773 returnCode |= ErrorExceptions;
774 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
775 }
776 try
777 {
778 Console.WriteLine("testMultiException(\"Xception2\", ...)");
779 client.testMultiException("Xception2", "ignore");
780 Console.WriteLine("*** FAILED ***");
781 returnCode |= ErrorExceptions;
782 }
783 catch (Xception2 ex)
784 {
785 if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2")
786 {
787 Console.WriteLine("*** FAILED ***");
788 returnCode |= ErrorExceptions;
789 }
790 }
791 catch (Exception ex)
792 {
793 Console.WriteLine("*** FAILED ***");
794 returnCode |= ErrorExceptions;
795 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
796 }
797 try
798 {
799 Console.WriteLine("testMultiException(\"success\", \"OK\")");
800 if ("OK" != client.testMultiException("success", "OK").String_thing)
801 {
802 Console.WriteLine("*** FAILED ***");
803 returnCode |= ErrorExceptions;
804 }
805 }
806 catch (Exception ex)
807 {
808 Console.WriteLine("*** FAILED ***");
809 returnCode |= ErrorExceptions;
810 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
811 }
812
813 Stopwatch sw = new Stopwatch();
814 sw.Start();
Jens Geyerd5436f52014-10-03 19:50:38 +0200815 Console.WriteLine("Test Oneway(1)");
816 client.testOneway(1);
Jens Geyer178b8132015-09-30 23:16:45 +0200817 sw.Stop();
818 if (sw.ElapsedMilliseconds > 1000)
819 {
820 Console.WriteLine("*** FAILED ***");
821 returnCode |= ErrorBaseTypes;
822 }
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000823
Jens Geyerd5436f52014-10-03 19:50:38 +0200824 Console.Write("Test Calltime()");
825 var startt = DateTime.UtcNow;
826 for ( int k=0; k<1000; ++k )
827 client.testVoid();
828 Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" );
Jens Geyer178b8132015-09-30 23:16:45 +0200829 return returnCode;
Jens Geyerd5436f52014-10-03 19:50:38 +0200830 }
831 }
David Reiss63191332009-01-06 19:49:22 +0000832}