blob: fad1057d62d36b2706ebc5c64039a2241372f5f8 [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
Jens Geyerd4df9172017-10-25 22:30:23 +0200268 public static byte[] PrepareTestData(bool randomDist, bool huge)
Jens Geyer71e814a2014-12-13 23:40:35 +0100269 {
Jens Geyerd4df9172017-10-25 22:30:23 +0200270 // huge = true tests for THRIFT-4372
271 byte[] retval = new byte[huge ? 0x12345 : 0x100];
272 int initLen = retval.Length;
Jens Geyer71e814a2014-12-13 23:40:35 +0100273
274 // linear distribution, unless random is requested
275 if (!randomDist) {
Jens Geyer178b8132015-09-30 23:16:45 +0200276 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100277 retval[i] = (byte)i;
278 }
279 return retval;
280 }
281
282 // random distribution
Jens Geyer178b8132015-09-30 23:16:45 +0200283 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100284 retval[i] = (byte)0;
285 }
286 var rnd = new Random();
287 for (var i = 1; i < initLen; ++i) {
288 while( true) {
289 int nextPos = rnd.Next() % initLen;
290 if (retval[nextPos] == 0) {
291 retval[nextPos] = (byte)i;
292 break;
293 }
294 }
295 }
296 return retval;
297 }
298
Jens Geyer178b8132015-09-30 23:16:45 +0200299 public static int ExecuteClientTest(ThriftTest.Client client)
Jens Geyerd5436f52014-10-03 19:50:38 +0200300 {
Jens Geyer178b8132015-09-30 23:16:45 +0200301 int returnCode = 0;
David Reiss63191332009-01-06 19:49:22 +0000302
Jens Geyerd5436f52014-10-03 19:50:38 +0200303 Console.Write("testVoid()");
304 client.testVoid();
305 Console.WriteLine(" = void");
David Reiss63191332009-01-06 19:49:22 +0000306
Jens Geyerd5436f52014-10-03 19:50:38 +0200307 Console.Write("testString(\"Test\")");
308 string s = client.testString("Test");
309 Console.WriteLine(" = \"" + s + "\"");
Jens Geyer178b8132015-09-30 23:16:45 +0200310 if ("Test" != s)
311 {
312 Console.WriteLine("*** FAILED ***");
313 returnCode |= ErrorBaseTypes;
314 }
David Reiss63191332009-01-06 19:49:22 +0000315
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900316 Console.Write("testBool(true)");
317 bool t = client.testBool((bool)true);
318 Console.WriteLine(" = " + t);
Jens Geyer178b8132015-09-30 23:16:45 +0200319 if (!t)
320 {
321 Console.WriteLine("*** FAILED ***");
322 returnCode |= ErrorBaseTypes;
323 }
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900324 Console.Write("testBool(false)");
325 bool f = client.testBool((bool)false);
326 Console.WriteLine(" = " + f);
Jens Geyer178b8132015-09-30 23:16:45 +0200327 if (f)
328 {
329 Console.WriteLine("*** FAILED ***");
330 returnCode |= ErrorBaseTypes;
331 }
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900332
Jens Geyerd5436f52014-10-03 19:50:38 +0200333 Console.Write("testByte(1)");
334 sbyte i8 = client.testByte((sbyte)1);
335 Console.WriteLine(" = " + i8);
Jens Geyer178b8132015-09-30 23:16:45 +0200336 if (1 != i8)
337 {
338 Console.WriteLine("*** FAILED ***");
339 returnCode |= ErrorBaseTypes;
340 }
David Reiss63191332009-01-06 19:49:22 +0000341
Jens Geyerd5436f52014-10-03 19:50:38 +0200342 Console.Write("testI32(-1)");
343 int i32 = client.testI32(-1);
344 Console.WriteLine(" = " + i32);
Jens Geyer178b8132015-09-30 23:16:45 +0200345 if (-1 != i32)
346 {
347 Console.WriteLine("*** FAILED ***");
348 returnCode |= ErrorBaseTypes;
349 }
David Reiss63191332009-01-06 19:49:22 +0000350
Jens Geyerd5436f52014-10-03 19:50:38 +0200351 Console.Write("testI64(-34359738368)");
352 long i64 = client.testI64(-34359738368);
353 Console.WriteLine(" = " + i64);
Jens Geyer178b8132015-09-30 23:16:45 +0200354 if (-34359738368 != i64)
355 {
356 Console.WriteLine("*** FAILED ***");
357 returnCode |= ErrorBaseTypes;
358 }
David Reiss63191332009-01-06 19:49:22 +0000359
Jens Geyer178b8132015-09-30 23:16:45 +0200360 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200361 Console.Write("testDouble(5.325098235)");
362 double dub = client.testDouble(5.325098235);
363 Console.WriteLine(" = " + dub);
Jens Geyer178b8132015-09-30 23:16:45 +0200364 if (5.325098235 != dub)
365 {
366 Console.WriteLine("*** FAILED ***");
367 returnCode |= ErrorBaseTypes;
368 }
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900369 Console.Write("testDouble(-0.000341012439638598279)");
370 dub = client.testDouble(-0.000341012439638598279);
371 Console.WriteLine(" = " + dub);
372 if (-0.000341012439638598279 != dub)
373 {
374 Console.WriteLine("*** FAILED ***");
375 returnCode |= ErrorBaseTypes;
376 }
David Reiss63191332009-01-06 19:49:22 +0000377
Jens Geyerd4df9172017-10-25 22:30:23 +0200378 for (i32 = 0; i32 < 2; ++i32)
Jens Geyer71e814a2014-12-13 23:40:35 +0100379 {
Jens Geyerd4df9172017-10-25 22:30:23 +0200380 var huge = (i32 > 0);
381 byte[] binOut = PrepareTestData(false,huge);
382 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
383 try
Jens Geyer178b8132015-09-30 23:16:45 +0200384 {
Jens Geyerd4df9172017-10-25 22:30:23 +0200385 byte[] binIn = client.testBinary(binOut);
386 Console.WriteLine(" = " + BytesToHex(binIn));
387 if (binIn.Length != binOut.Length)
Jens Geyer178b8132015-09-30 23:16:45 +0200388 {
389 Console.WriteLine("*** FAILED ***");
390 returnCode |= ErrorBaseTypes;
391 }
Jens Geyerd4df9172017-10-25 22:30:23 +0200392 for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
393 if (binIn[ofs] != binOut[ofs])
394 {
395 Console.WriteLine("*** FAILED ***");
396 returnCode |= ErrorBaseTypes;
397 }
398 }
399 catch (Thrift.TApplicationException ex)
400 {
401 Console.WriteLine("*** FAILED ***");
402 returnCode |= ErrorBaseTypes;
403 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
404 }
Jens Geyer71e814a2014-12-13 23:40:35 +0100405 }
406
Jens Geyer80aa53e2015-02-18 22:04:09 +0100407 // binary equals? only with hashcode option enabled ...
Jens Geyer178b8132015-09-30 23:16:45 +0200408 Console.WriteLine("Test CrazyNesting");
409 if( typeof(CrazyNesting).GetMethod("Equals").DeclaringType == typeof(CrazyNesting))
Jens Geyer80aa53e2015-02-18 22:04:09 +0100410 {
411 CrazyNesting one = new CrazyNesting();
412 CrazyNesting two = new CrazyNesting();
413 one.String_field = "crazy";
414 two.String_field = "crazy";
415 one.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
416 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
417 if (!one.Equals(two))
Jens Geyer178b8132015-09-30 23:16:45 +0200418 {
419 Console.WriteLine("*** FAILED ***");
420 returnCode |= ErrorContainers;
Jens Geyer80aa53e2015-02-18 22:04:09 +0100421 throw new Exception("CrazyNesting.Equals failed");
Jens Geyer178b8132015-09-30 23:16:45 +0200422 }
Jens Geyer80aa53e2015-02-18 22:04:09 +0100423 }
424
Jens Geyer178b8132015-09-30 23:16:45 +0200425 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200426 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
427 Xtruct o = new Xtruct();
428 o.String_thing = "Zero";
429 o.Byte_thing = (sbyte)1;
430 o.I32_thing = -3;
431 o.I64_thing = -5;
432 Xtruct i = client.testStruct(o);
433 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000434
Jens Geyer178b8132015-09-30 23:16:45 +0200435 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200436 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
437 Xtruct2 o2 = new Xtruct2();
438 o2.Byte_thing = (sbyte)1;
439 o2.Struct_thing = o;
440 o2.I32_thing = 5;
441 Xtruct2 i2 = client.testNest(o2);
442 i = i2.Struct_thing;
443 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 +0000444
Jens Geyerd5436f52014-10-03 19:50:38 +0200445 Dictionary<int, int> mapout = new Dictionary<int, int>();
446 for (int j = 0; j < 5; j++)
447 {
448 mapout[j] = j - 10;
449 }
450 Console.Write("testMap({");
451 bool first = true;
452 foreach (int key in mapout.Keys)
453 {
454 if (first)
455 {
456 first = false;
457 }
458 else
459 {
460 Console.Write(", ");
461 }
462 Console.Write(key + " => " + mapout[key]);
463 }
464 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000465
Jens Geyerd5436f52014-10-03 19:50:38 +0200466 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000467
Jens Geyerd5436f52014-10-03 19:50:38 +0200468 Console.Write(" = {");
469 first = true;
470 foreach (int key in mapin.Keys)
471 {
472 if (first)
473 {
474 first = false;
475 }
476 else
477 {
478 Console.Write(", ");
479 }
480 Console.Write(key + " => " + mapin[key]);
481 }
482 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000483
Jens Geyer178b8132015-09-30 23:16:45 +0200484 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200485 List<int> listout = new List<int>();
486 for (int j = -2; j < 3; j++)
487 {
488 listout.Add(j);
489 }
490 Console.Write("testList({");
491 first = true;
492 foreach (int j in listout)
493 {
494 if (first)
495 {
496 first = false;
497 }
498 else
499 {
500 Console.Write(", ");
501 }
502 Console.Write(j);
503 }
504 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000505
Jens Geyerd5436f52014-10-03 19:50:38 +0200506 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000507
Jens Geyerd5436f52014-10-03 19:50:38 +0200508 Console.Write(" = {");
509 first = true;
510 foreach (int j in listin)
511 {
512 if (first)
513 {
514 first = false;
515 }
516 else
517 {
518 Console.Write(", ");
519 }
520 Console.Write(j);
521 }
522 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000523
Jens Geyerd5436f52014-10-03 19:50:38 +0200524 //set
Jens Geyer178b8132015-09-30 23:16:45 +0200525 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200526 THashSet<int> setout = new THashSet<int>();
527 for (int j = -2; j < 3; j++)
528 {
529 setout.Add(j);
530 }
531 Console.Write("testSet({");
532 first = true;
533 foreach (int j in setout)
534 {
535 if (first)
536 {
537 first = false;
538 }
539 else
540 {
541 Console.Write(", ");
542 }
543 Console.Write(j);
544 }
545 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000546
Jens Geyerd5436f52014-10-03 19:50:38 +0200547 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000548
Jens Geyerd5436f52014-10-03 19:50:38 +0200549 Console.Write(" = {");
550 first = true;
551 foreach (int j in setin)
552 {
553 if (first)
554 {
555 first = false;
556 }
557 else
558 {
559 Console.Write(", ");
560 }
561 Console.Write(j);
562 }
563 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000564
565
Jens Geyerd5436f52014-10-03 19:50:38 +0200566 Console.Write("testEnum(ONE)");
567 Numberz ret = client.testEnum(Numberz.ONE);
568 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200569 if (Numberz.ONE != ret)
570 {
571 Console.WriteLine("*** FAILED ***");
572 returnCode |= ErrorStructs;
573 }
David Reiss63191332009-01-06 19:49:22 +0000574
Jens Geyerd5436f52014-10-03 19:50:38 +0200575 Console.Write("testEnum(TWO)");
576 ret = client.testEnum(Numberz.TWO);
577 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200578 if (Numberz.TWO != ret)
579 {
580 Console.WriteLine("*** FAILED ***");
581 returnCode |= ErrorStructs;
582 }
David Reiss63191332009-01-06 19:49:22 +0000583
Jens Geyerd5436f52014-10-03 19:50:38 +0200584 Console.Write("testEnum(THREE)");
585 ret = client.testEnum(Numberz.THREE);
586 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200587 if (Numberz.THREE != ret)
588 {
589 Console.WriteLine("*** FAILED ***");
590 returnCode |= ErrorStructs;
591 }
David Reiss63191332009-01-06 19:49:22 +0000592
Jens Geyerd5436f52014-10-03 19:50:38 +0200593 Console.Write("testEnum(FIVE)");
594 ret = client.testEnum(Numberz.FIVE);
595 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200596 if (Numberz.FIVE != ret)
597 {
598 Console.WriteLine("*** FAILED ***");
599 returnCode |= ErrorStructs;
600 }
David Reiss63191332009-01-06 19:49:22 +0000601
Jens Geyerd5436f52014-10-03 19:50:38 +0200602 Console.Write("testEnum(EIGHT)");
603 ret = client.testEnum(Numberz.EIGHT);
604 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200605 if (Numberz.EIGHT != ret)
606 {
607 Console.WriteLine("*** FAILED ***");
608 returnCode |= ErrorStructs;
609 }
David Reiss63191332009-01-06 19:49:22 +0000610
Jens Geyerd5436f52014-10-03 19:50:38 +0200611 Console.Write("testTypedef(309858235082523)");
612 long uid = client.testTypedef(309858235082523L);
613 Console.WriteLine(" = " + uid);
Jens Geyer178b8132015-09-30 23:16:45 +0200614 if (309858235082523L != uid)
615 {
616 Console.WriteLine("*** FAILED ***");
617 returnCode |= ErrorStructs;
618 }
David Reiss63191332009-01-06 19:49:22 +0000619
Jens Geyer178b8132015-09-30 23:16:45 +0200620 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200621 Console.Write("testMapMap(1)");
622 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
623 Console.Write(" = {");
624 foreach (int key in mm.Keys)
625 {
626 Console.Write(key + " => {");
627 Dictionary<int, int> m2 = mm[key];
628 foreach (int k2 in m2.Keys)
629 {
630 Console.Write(k2 + " => " + m2[k2] + ", ");
631 }
632 Console.Write("}, ");
633 }
634 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000635
Jens Geyer178b8132015-09-30 23:16:45 +0200636 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200637 Insanity insane = new Insanity();
638 insane.UserMap = new Dictionary<Numberz, long>();
639 insane.UserMap[Numberz.FIVE] = 5000L;
640 Xtruct truck = new Xtruct();
641 truck.String_thing = "Truck";
642 truck.Byte_thing = (sbyte)8;
643 truck.I32_thing = 8;
644 truck.I64_thing = 8;
645 insane.Xtructs = new List<Xtruct>();
646 insane.Xtructs.Add(truck);
647 Console.Write("testInsanity()");
648 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
649 Console.Write(" = {");
650 foreach (long key in whoa.Keys)
651 {
652 Dictionary<Numberz, Insanity> val = whoa[key];
653 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000654
Jens Geyerd5436f52014-10-03 19:50:38 +0200655 foreach (Numberz k2 in val.Keys)
656 {
657 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000658
Jens Geyerd5436f52014-10-03 19:50:38 +0200659 Console.Write(k2 + " => {");
660 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000661
Jens Geyerd5436f52014-10-03 19:50:38 +0200662 Console.Write("{");
663 if (userMap != null)
664 {
665 foreach (Numberz k3 in userMap.Keys)
666 {
667 Console.Write(k3 + " => " + userMap[k3] + ", ");
668 }
669 }
670 else
671 {
672 Console.Write("null");
673 }
674 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000675
Jens Geyerd5436f52014-10-03 19:50:38 +0200676 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000677
Jens Geyerd5436f52014-10-03 19:50:38 +0200678 Console.Write("{");
679 if (xtructs != null)
680 {
681 foreach (Xtruct x in xtructs)
682 {
683 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
684 }
685 }
686 else
687 {
688 Console.Write("null");
689 }
690 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000691
Jens Geyerd5436f52014-10-03 19:50:38 +0200692 Console.Write("}, ");
693 }
694 Console.Write("}, ");
695 }
696 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000697
Jens Geyerd5436f52014-10-03 19:50:38 +0200698 sbyte arg0 = 1;
699 int arg1 = 2;
700 long arg2 = long.MaxValue;
701 Dictionary<short, string> multiDict = new Dictionary<short, string>();
702 multiDict[1] = "one";
703 Numberz arg4 = Numberz.FIVE;
704 long arg5 = 5000000;
705 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
706 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
707 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
708 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000709
Jens Geyer178b8132015-09-30 23:16:45 +0200710 try
711 {
712 Console.WriteLine("testException(\"Xception\")");
713 client.testException("Xception");
714 Console.WriteLine("*** FAILED ***");
715 returnCode |= ErrorExceptions;
716 }
717 catch (Xception ex)
718 {
719 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
720 {
721 Console.WriteLine("*** FAILED ***");
722 returnCode |= ErrorExceptions;
723 }
724 }
725 catch (Exception ex)
726 {
727 Console.WriteLine("*** FAILED ***");
728 returnCode |= ErrorExceptions;
729 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
730 }
731 try
732 {
733 Console.WriteLine("testException(\"TException\")");
734 client.testException("TException");
735 Console.WriteLine("*** FAILED ***");
736 returnCode |= ErrorExceptions;
737 }
738 catch (Thrift.TException)
739 {
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 try
749 {
750 Console.WriteLine("testException(\"ok\")");
751 client.testException("ok");
752 // OK
753 }
754 catch (Exception ex)
755 {
756 Console.WriteLine("*** FAILED ***");
757 returnCode |= ErrorExceptions;
758 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
759 }
760
761 try
762 {
763 Console.WriteLine("testMultiException(\"Xception\", ...)");
764 client.testMultiException("Xception", "ignore");
765 Console.WriteLine("*** FAILED ***");
766 returnCode |= ErrorExceptions;
767 }
768 catch (Xception ex)
769 {
770 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
771 {
772 Console.WriteLine("*** FAILED ***");
773 returnCode |= ErrorExceptions;
774 }
775 }
776 catch (Exception ex)
777 {
778 Console.WriteLine("*** FAILED ***");
779 returnCode |= ErrorExceptions;
780 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
781 }
782 try
783 {
784 Console.WriteLine("testMultiException(\"Xception2\", ...)");
785 client.testMultiException("Xception2", "ignore");
786 Console.WriteLine("*** FAILED ***");
787 returnCode |= ErrorExceptions;
788 }
789 catch (Xception2 ex)
790 {
791 if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2")
792 {
793 Console.WriteLine("*** FAILED ***");
794 returnCode |= ErrorExceptions;
795 }
796 }
797 catch (Exception ex)
798 {
799 Console.WriteLine("*** FAILED ***");
800 returnCode |= ErrorExceptions;
801 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
802 }
803 try
804 {
805 Console.WriteLine("testMultiException(\"success\", \"OK\")");
806 if ("OK" != client.testMultiException("success", "OK").String_thing)
807 {
808 Console.WriteLine("*** FAILED ***");
809 returnCode |= ErrorExceptions;
810 }
811 }
812 catch (Exception ex)
813 {
814 Console.WriteLine("*** FAILED ***");
815 returnCode |= ErrorExceptions;
816 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
817 }
818
819 Stopwatch sw = new Stopwatch();
820 sw.Start();
Jens Geyerd5436f52014-10-03 19:50:38 +0200821 Console.WriteLine("Test Oneway(1)");
822 client.testOneway(1);
Jens Geyer178b8132015-09-30 23:16:45 +0200823 sw.Stop();
824 if (sw.ElapsedMilliseconds > 1000)
825 {
826 Console.WriteLine("*** FAILED ***");
827 returnCode |= ErrorBaseTypes;
828 }
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000829
Jens Geyerd5436f52014-10-03 19:50:38 +0200830 Console.Write("Test Calltime()");
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +0900831 var times = 50;
832 sw.Reset();
833 sw.Start();
834 for (int k = 0; k < times; ++k)
Jens Geyerd5436f52014-10-03 19:50:38 +0200835 client.testVoid();
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +0900836 sw.Stop();
837 Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times);
Jens Geyer178b8132015-09-30 23:16:45 +0200838 return returnCode;
Jens Geyerd5436f52014-10-03 19:50:38 +0200839 }
840 }
David Reiss63191332009-01-06 19:49:22 +0000841}