blob: 17e59787acd2a129126b072de4423728854b3b45 [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 {
James E. King, III58402ff2017-11-17 14:41:46 -050036 public class TestParams
Jens Geyer178b8132015-09-30 23:16:45 +020037 {
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;
James E. King, III58402ff2017-11-17 14:41:46 -050047 public bool multiplexed = false;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000048 protected bool _isFirstTransport = true;
49
David Reiss63191332009-01-06 19:49:22 +000050
Jens Geyer178b8132015-09-30 23:16:45 +020051 public TTransport CreateTransport()
52 {
53 if (url == null)
54 {
55 // endpoint transport
56 TTransport trans = null;
57 if (pipe != null)
58 trans = new TNamedPipeClientTransport(pipe);
59 else
60 {
61 if (encrypted)
62 {
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +090063 string certPath = "../keys/client.p12";
Jens Geyer178b8132015-09-30 23:16:45 +020064 X509Certificate cert = new X509Certificate2(certPath, "thrift");
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +090065 trans = new TTLSSocket(host, port, 0, cert, (o, c, chain, errors) => true, null, SslProtocols.Tls);
Jens Geyer178b8132015-09-30 23:16:45 +020066 }
67 else
68 {
69 trans = new TSocket(host, port);
70 }
71 }
72
73 // layered transport
74 if (buffered)
75 trans = new TBufferedTransport(trans);
76 if (framed)
77 trans = new TFramedTransport(trans);
78
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000079 if (_isFirstTransport)
80 {
81 //ensure proper open/close of transport
82 trans.Open();
83 trans.Close();
84 _isFirstTransport = false;
85 }
Jens Geyer178b8132015-09-30 23:16:45 +020086 return trans;
87 }
88 else
89 {
90 return new THttpClient(new Uri(url));
91 }
92 }
93
94 public TProtocol CreateProtocol(TTransport transport)
95 {
96 if (protocol == "compact")
97 return new TCompactProtocol(transport);
98 else if (protocol == "json")
99 return new TJSONProtocol(transport);
100 else
101 return new TBinaryProtocol(transport);
102 }
103 };
104
105 private const int ErrorBaseTypes = 1;
106 private const int ErrorStructs = 2;
107 private const int ErrorContainers = 4;
108 private const int ErrorExceptions = 8;
James E. King, III58402ff2017-11-17 14:41:46 -0500109 private const int ErrorProtocol = 16;
Jens Geyer178b8132015-09-30 23:16:45 +0200110 private const int ErrorUnknown = 64;
111
112 private class ClientTest
113 {
James E. King, III58402ff2017-11-17 14:41:46 -0500114 private readonly TestParams param;
Jens Geyer178b8132015-09-30 23:16:45 +0200115 private readonly TTransport transport;
James E. King, III58402ff2017-11-17 14:41:46 -0500116 private readonly SecondService.Client second;
Jens Geyer178b8132015-09-30 23:16:45 +0200117 private readonly ThriftTest.Client client;
118 private readonly int numIterations;
119 private bool done;
120
121 public int ReturnCode { get; set; }
122
James E. King, III58402ff2017-11-17 14:41:46 -0500123 public ClientTest(TestParams paramin)
Jens Geyer178b8132015-09-30 23:16:45 +0200124 {
James E. King, III58402ff2017-11-17 14:41:46 -0500125 param = paramin;
Jens Geyer178b8132015-09-30 23:16:45 +0200126 transport = param.CreateTransport();
James E. King, III58402ff2017-11-17 14:41:46 -0500127 TProtocol protocol = param.CreateProtocol(transport);
128 if (param.multiplexed)
129 {
130 second = new SecondService.Client(new TMultiplexedProtocol(protocol, "SecondService"));
131 }
132 client = new ThriftTest.Client(protocol);
Jens Geyer178b8132015-09-30 23:16:45 +0200133 numIterations = param.numIterations;
134 }
135 public void Execute()
136 {
137 if (done)
138 {
139 Console.WriteLine("Execute called more than once");
140 throw new InvalidOperationException();
141 }
142
143 for (int i = 0; i < numIterations; i++)
144 {
145 try
146 {
147 if (!transport.IsOpen)
148 transport.Open();
149 }
150 catch (TTransportException ex)
151 {
152 Console.WriteLine("*** FAILED ***");
153 Console.WriteLine("Connect failed: " + ex.Message);
154 ReturnCode |= ErrorUnknown;
155 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
156 continue;
157 }
158
159 try
160 {
James E. King, III58402ff2017-11-17 14:41:46 -0500161 ReturnCode |= ExecuteClientTest(client, second, param);
Jens Geyer178b8132015-09-30 23:16:45 +0200162 }
163 catch (Exception ex)
164 {
165 Console.WriteLine("*** FAILED ***");
166 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
167 ReturnCode |= ErrorUnknown;
168 }
169 }
170 try
171 {
172 transport.Close();
173 }
174 catch(Exception ex)
175 {
176 Console.WriteLine("Error while closing transport");
177 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
178 }
179 done = true;
180 }
181 }
182
183 public static int Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +0200184 {
185 try
186 {
Jens Geyer178b8132015-09-30 23:16:45 +0200187 TestParams param = new TestParams();
Jens Geyerd5436f52014-10-03 19:50:38 +0200188 int numThreads = 1;
Jens Geyerd5436f52014-10-03 19:50:38 +0200189 try
190 {
191 for (int i = 0; i < args.Length; i++)
192 {
193 if (args[i] == "-u")
194 {
Jens Geyer178b8132015-09-30 23:16:45 +0200195 param.url = args[++i];
Jens Geyerd5436f52014-10-03 19:50:38 +0200196 }
197 else if (args[i] == "-n")
198 {
Jens Geyer178b8132015-09-30 23:16:45 +0200199 param.numIterations = Convert.ToInt32(args[++i]);
Jens Geyerd5436f52014-10-03 19:50:38 +0200200 }
201 else if (args[i] == "-pipe") // -pipe <name>
202 {
Jens Geyer178b8132015-09-30 23:16:45 +0200203 param.pipe = args[++i];
Jens Geyerd5436f52014-10-03 19:50:38 +0200204 Console.WriteLine("Using named pipes transport");
205 }
206 else if (args[i].Contains("--host="))
207 {
Jens Geyer178b8132015-09-30 23:16:45 +0200208 param.host = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200209 }
210 else if (args[i].Contains("--port="))
211 {
Jens Geyer178b8132015-09-30 23:16:45 +0200212 param.port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
Jens Geyerd5436f52014-10-03 19:50:38 +0200213 }
214 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
215 {
Jens Geyer178b8132015-09-30 23:16:45 +0200216 param.buffered = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200217 Console.WriteLine("Using buffered sockets");
218 }
219 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
220 {
Jens Geyer178b8132015-09-30 23:16:45 +0200221 param.framed = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200222 Console.WriteLine("Using framed transport");
223 }
224 else if (args[i] == "-t")
225 {
226 numThreads = Convert.ToInt32(args[++i]);
227 }
James E. King, III58402ff2017-11-17 14:41:46 -0500228 else if (args[i] == "--compact" || args[i] == "--protocol=compact" || args[i] == "--protocol=multic")
Jens Geyerd5436f52014-10-03 19:50:38 +0200229 {
Jens Geyer178b8132015-09-30 23:16:45 +0200230 param.protocol = "compact";
Jens Geyerd5436f52014-10-03 19:50:38 +0200231 Console.WriteLine("Using compact protocol");
232 }
James E. King, III58402ff2017-11-17 14:41:46 -0500233 else if (args[i] == "--json" || args[i] == "--protocol=json" || args[i] == "--protocol=multij")
Jens Geyerd5436f52014-10-03 19:50:38 +0200234 {
Jens Geyer178b8132015-09-30 23:16:45 +0200235 param.protocol = "json";
Jens Geyerd5436f52014-10-03 19:50:38 +0200236 Console.WriteLine("Using JSON protocol");
237 }
238 else if (args[i] == "--ssl")
239 {
Jens Geyer178b8132015-09-30 23:16:45 +0200240 param.encrypted = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200241 Console.WriteLine("Using encrypted transport");
242 }
James E. King, III58402ff2017-11-17 14:41:46 -0500243
244 if (args[i].StartsWith("--protocol=multi"))
245 {
246 param.multiplexed = true;
247 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200248 }
249 }
Jens Geyer178b8132015-09-30 23:16:45 +0200250 catch (Exception ex)
Jens Geyerd5436f52014-10-03 19:50:38 +0200251 {
Jens Geyer178b8132015-09-30 23:16:45 +0200252 Console.WriteLine("*** FAILED ***");
253 Console.WriteLine("Error while parsing arguments");
254 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
255 return ErrorUnknown;
Jens Geyerd5436f52014-10-03 19:50:38 +0200256 }
David Reiss63191332009-01-06 19:49:22 +0000257
Jens Geyer178b8132015-09-30 23:16:45 +0200258 var tests = Enumerable.Range(0, numThreads).Select(_ => new ClientTest(param)).ToArray();
Jens Geyerd5436f52014-10-03 19:50:38 +0200259 //issue tests on separate threads simultaneously
Jens Geyer178b8132015-09-30 23:16:45 +0200260 var threads = tests.Select(test => new Thread(test.Execute)).ToArray();
Jens Geyerd5436f52014-10-03 19:50:38 +0200261 DateTime start = DateTime.Now;
Jens Geyer178b8132015-09-30 23:16:45 +0200262 foreach (var t in threads)
263 t.Start();
264 foreach (var t in threads)
265 t.Join();
266 Console.WriteLine("Total time: " + (DateTime.Now - start));
267 Console.WriteLine();
268 return tests.Select(t => t.ReturnCode).Aggregate((r1, r2) => r1 | r2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200269 }
270 catch (Exception outerEx)
271 {
Jens Geyer178b8132015-09-30 23:16:45 +0200272 Console.WriteLine("*** FAILED ***");
273 Console.WriteLine("Unexpected error");
Jens Geyerd5436f52014-10-03 19:50:38 +0200274 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
Jens Geyer178b8132015-09-30 23:16:45 +0200275 return ErrorUnknown;
Jens Geyerd5436f52014-10-03 19:50:38 +0200276 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200277 }
David Reiss63191332009-01-06 19:49:22 +0000278
Jens Geyer71e814a2014-12-13 23:40:35 +0100279 public static string BytesToHex(byte[] data) {
280 return BitConverter.ToString(data).Replace("-", string.Empty);
281 }
282
Jens Geyerd4df9172017-10-25 22:30:23 +0200283 public static byte[] PrepareTestData(bool randomDist, bool huge)
Jens Geyer71e814a2014-12-13 23:40:35 +0100284 {
Jens Geyerd4df9172017-10-25 22:30:23 +0200285 // huge = true tests for THRIFT-4372
286 byte[] retval = new byte[huge ? 0x12345 : 0x100];
287 int initLen = retval.Length;
Jens Geyer71e814a2014-12-13 23:40:35 +0100288
289 // linear distribution, unless random is requested
290 if (!randomDist) {
Jens Geyer178b8132015-09-30 23:16:45 +0200291 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100292 retval[i] = (byte)i;
293 }
294 return retval;
295 }
296
297 // random distribution
Jens Geyer178b8132015-09-30 23:16:45 +0200298 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100299 retval[i] = (byte)0;
300 }
301 var rnd = new Random();
302 for (var i = 1; i < initLen; ++i) {
303 while( true) {
304 int nextPos = rnd.Next() % initLen;
305 if (retval[nextPos] == 0) {
306 retval[nextPos] = (byte)i;
307 break;
308 }
309 }
310 }
311 return retval;
312 }
313
James E. King, III58402ff2017-11-17 14:41:46 -0500314 public static int ExecuteClientTest(ThriftTest.Client client, SecondService.Client second, TestParams param)
Jens Geyerd5436f52014-10-03 19:50:38 +0200315 {
Jens Geyer178b8132015-09-30 23:16:45 +0200316 int returnCode = 0;
David Reiss63191332009-01-06 19:49:22 +0000317
Jens Geyerd5436f52014-10-03 19:50:38 +0200318 Console.Write("testVoid()");
319 client.testVoid();
320 Console.WriteLine(" = void");
David Reiss63191332009-01-06 19:49:22 +0000321
Jens Geyerd5436f52014-10-03 19:50:38 +0200322 Console.Write("testString(\"Test\")");
323 string s = client.testString("Test");
324 Console.WriteLine(" = \"" + s + "\"");
Jens Geyer178b8132015-09-30 23:16:45 +0200325 if ("Test" != s)
326 {
327 Console.WriteLine("*** FAILED ***");
328 returnCode |= ErrorBaseTypes;
329 }
David Reiss63191332009-01-06 19:49:22 +0000330
James E. King, III58402ff2017-11-17 14:41:46 -0500331 if (param.multiplexed)
332 {
333 Console.WriteLine("secondTestString(\"Test2\")");
334 s = second.secondtestString("Test2");
335 Console.WriteLine(" = \"" + s + "\"");
336 if ("testString(\"Test2\")" != s)
337 {
338 Console.WriteLine("*** FAILED ***");
339 returnCode |= ErrorProtocol;
340 }
341 }
342
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900343 Console.Write("testBool(true)");
344 bool t = client.testBool((bool)true);
345 Console.WriteLine(" = " + t);
Jens Geyer178b8132015-09-30 23:16:45 +0200346 if (!t)
347 {
348 Console.WriteLine("*** FAILED ***");
349 returnCode |= ErrorBaseTypes;
350 }
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900351 Console.Write("testBool(false)");
352 bool f = client.testBool((bool)false);
353 Console.WriteLine(" = " + f);
Jens Geyer178b8132015-09-30 23:16:45 +0200354 if (f)
355 {
356 Console.WriteLine("*** FAILED ***");
357 returnCode |= ErrorBaseTypes;
358 }
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900359
Jens Geyerd5436f52014-10-03 19:50:38 +0200360 Console.Write("testByte(1)");
361 sbyte i8 = client.testByte((sbyte)1);
362 Console.WriteLine(" = " + i8);
Jens Geyer178b8132015-09-30 23:16:45 +0200363 if (1 != i8)
364 {
365 Console.WriteLine("*** FAILED ***");
366 returnCode |= ErrorBaseTypes;
367 }
David Reiss63191332009-01-06 19:49:22 +0000368
Jens Geyerd5436f52014-10-03 19:50:38 +0200369 Console.Write("testI32(-1)");
370 int i32 = client.testI32(-1);
371 Console.WriteLine(" = " + i32);
Jens Geyer178b8132015-09-30 23:16:45 +0200372 if (-1 != i32)
373 {
374 Console.WriteLine("*** FAILED ***");
375 returnCode |= ErrorBaseTypes;
376 }
David Reiss63191332009-01-06 19:49:22 +0000377
Jens Geyerd5436f52014-10-03 19:50:38 +0200378 Console.Write("testI64(-34359738368)");
379 long i64 = client.testI64(-34359738368);
380 Console.WriteLine(" = " + i64);
Jens Geyer178b8132015-09-30 23:16:45 +0200381 if (-34359738368 != i64)
382 {
383 Console.WriteLine("*** FAILED ***");
384 returnCode |= ErrorBaseTypes;
385 }
David Reiss63191332009-01-06 19:49:22 +0000386
Jens Geyer178b8132015-09-30 23:16:45 +0200387 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200388 Console.Write("testDouble(5.325098235)");
389 double dub = client.testDouble(5.325098235);
390 Console.WriteLine(" = " + dub);
Jens Geyer178b8132015-09-30 23:16:45 +0200391 if (5.325098235 != dub)
392 {
393 Console.WriteLine("*** FAILED ***");
394 returnCode |= ErrorBaseTypes;
395 }
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900396 Console.Write("testDouble(-0.000341012439638598279)");
397 dub = client.testDouble(-0.000341012439638598279);
398 Console.WriteLine(" = " + dub);
399 if (-0.000341012439638598279 != dub)
400 {
401 Console.WriteLine("*** FAILED ***");
402 returnCode |= ErrorBaseTypes;
403 }
David Reiss63191332009-01-06 19:49:22 +0000404
Jens Geyerd4df9172017-10-25 22:30:23 +0200405 for (i32 = 0; i32 < 2; ++i32)
Jens Geyer71e814a2014-12-13 23:40:35 +0100406 {
Jens Geyerd4df9172017-10-25 22:30:23 +0200407 var huge = (i32 > 0);
408 byte[] binOut = PrepareTestData(false,huge);
409 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
410 try
Jens Geyer178b8132015-09-30 23:16:45 +0200411 {
Jens Geyerd4df9172017-10-25 22:30:23 +0200412 byte[] binIn = client.testBinary(binOut);
413 Console.WriteLine(" = " + BytesToHex(binIn));
414 if (binIn.Length != binOut.Length)
Jens Geyer178b8132015-09-30 23:16:45 +0200415 {
416 Console.WriteLine("*** FAILED ***");
417 returnCode |= ErrorBaseTypes;
418 }
Jens Geyerd4df9172017-10-25 22:30:23 +0200419 for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
420 if (binIn[ofs] != binOut[ofs])
421 {
422 Console.WriteLine("*** FAILED ***");
423 returnCode |= ErrorBaseTypes;
424 }
425 }
426 catch (Thrift.TApplicationException ex)
427 {
428 Console.WriteLine("*** FAILED ***");
429 returnCode |= ErrorBaseTypes;
430 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
431 }
Jens Geyer71e814a2014-12-13 23:40:35 +0100432 }
433
Jens Geyer80aa53e2015-02-18 22:04:09 +0100434 // binary equals? only with hashcode option enabled ...
Jens Geyer178b8132015-09-30 23:16:45 +0200435 Console.WriteLine("Test CrazyNesting");
436 if( typeof(CrazyNesting).GetMethod("Equals").DeclaringType == typeof(CrazyNesting))
Jens Geyer80aa53e2015-02-18 22:04:09 +0100437 {
438 CrazyNesting one = new CrazyNesting();
439 CrazyNesting two = new CrazyNesting();
440 one.String_field = "crazy";
441 two.String_field = "crazy";
442 one.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
443 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
444 if (!one.Equals(two))
Jens Geyer178b8132015-09-30 23:16:45 +0200445 {
446 Console.WriteLine("*** FAILED ***");
447 returnCode |= ErrorContainers;
Jens Geyer80aa53e2015-02-18 22:04:09 +0100448 throw new Exception("CrazyNesting.Equals failed");
Jens Geyer178b8132015-09-30 23:16:45 +0200449 }
Jens Geyer80aa53e2015-02-18 22:04:09 +0100450 }
451
Jens Geyer178b8132015-09-30 23:16:45 +0200452 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200453 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
454 Xtruct o = new Xtruct();
455 o.String_thing = "Zero";
456 o.Byte_thing = (sbyte)1;
457 o.I32_thing = -3;
458 o.I64_thing = -5;
459 Xtruct i = client.testStruct(o);
460 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000461
Jens Geyer178b8132015-09-30 23:16:45 +0200462 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200463 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
464 Xtruct2 o2 = new Xtruct2();
465 o2.Byte_thing = (sbyte)1;
466 o2.Struct_thing = o;
467 o2.I32_thing = 5;
468 Xtruct2 i2 = client.testNest(o2);
469 i = i2.Struct_thing;
470 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 +0000471
Jens Geyerd5436f52014-10-03 19:50:38 +0200472 Dictionary<int, int> mapout = new Dictionary<int, int>();
473 for (int j = 0; j < 5; j++)
474 {
475 mapout[j] = j - 10;
476 }
477 Console.Write("testMap({");
478 bool first = true;
479 foreach (int key in mapout.Keys)
480 {
481 if (first)
482 {
483 first = false;
484 }
485 else
486 {
487 Console.Write(", ");
488 }
489 Console.Write(key + " => " + mapout[key]);
490 }
491 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000492
Jens Geyerd5436f52014-10-03 19:50:38 +0200493 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000494
Jens Geyerd5436f52014-10-03 19:50:38 +0200495 Console.Write(" = {");
496 first = true;
497 foreach (int key in mapin.Keys)
498 {
499 if (first)
500 {
501 first = false;
502 }
503 else
504 {
505 Console.Write(", ");
506 }
507 Console.Write(key + " => " + mapin[key]);
508 }
509 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000510
Jens Geyer178b8132015-09-30 23:16:45 +0200511 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200512 List<int> listout = new List<int>();
513 for (int j = -2; j < 3; j++)
514 {
515 listout.Add(j);
516 }
517 Console.Write("testList({");
518 first = true;
519 foreach (int j in listout)
520 {
521 if (first)
522 {
523 first = false;
524 }
525 else
526 {
527 Console.Write(", ");
528 }
529 Console.Write(j);
530 }
531 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000532
Jens Geyerd5436f52014-10-03 19:50:38 +0200533 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000534
Jens Geyerd5436f52014-10-03 19:50:38 +0200535 Console.Write(" = {");
536 first = true;
537 foreach (int j in listin)
538 {
539 if (first)
540 {
541 first = false;
542 }
543 else
544 {
545 Console.Write(", ");
546 }
547 Console.Write(j);
548 }
549 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000550
Jens Geyerd5436f52014-10-03 19:50:38 +0200551 //set
Jens Geyer178b8132015-09-30 23:16:45 +0200552 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200553 THashSet<int> setout = new THashSet<int>();
554 for (int j = -2; j < 3; j++)
555 {
556 setout.Add(j);
557 }
558 Console.Write("testSet({");
559 first = true;
560 foreach (int j in setout)
561 {
562 if (first)
563 {
564 first = false;
565 }
566 else
567 {
568 Console.Write(", ");
569 }
570 Console.Write(j);
571 }
572 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000573
Jens Geyerd5436f52014-10-03 19:50:38 +0200574 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000575
Jens Geyerd5436f52014-10-03 19:50:38 +0200576 Console.Write(" = {");
577 first = true;
578 foreach (int j in setin)
579 {
580 if (first)
581 {
582 first = false;
583 }
584 else
585 {
586 Console.Write(", ");
587 }
588 Console.Write(j);
589 }
590 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000591
592
Jens Geyerd5436f52014-10-03 19:50:38 +0200593 Console.Write("testEnum(ONE)");
594 Numberz ret = client.testEnum(Numberz.ONE);
595 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200596 if (Numberz.ONE != 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(TWO)");
603 ret = client.testEnum(Numberz.TWO);
604 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200605 if (Numberz.TWO != 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("testEnum(THREE)");
612 ret = client.testEnum(Numberz.THREE);
613 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200614 if (Numberz.THREE != ret)
615 {
616 Console.WriteLine("*** FAILED ***");
617 returnCode |= ErrorStructs;
618 }
David Reiss63191332009-01-06 19:49:22 +0000619
Jens Geyerd5436f52014-10-03 19:50:38 +0200620 Console.Write("testEnum(FIVE)");
621 ret = client.testEnum(Numberz.FIVE);
622 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200623 if (Numberz.FIVE != ret)
624 {
625 Console.WriteLine("*** FAILED ***");
626 returnCode |= ErrorStructs;
627 }
David Reiss63191332009-01-06 19:49:22 +0000628
Jens Geyerd5436f52014-10-03 19:50:38 +0200629 Console.Write("testEnum(EIGHT)");
630 ret = client.testEnum(Numberz.EIGHT);
631 Console.WriteLine(" = " + ret);
Jens Geyer178b8132015-09-30 23:16:45 +0200632 if (Numberz.EIGHT != ret)
633 {
634 Console.WriteLine("*** FAILED ***");
635 returnCode |= ErrorStructs;
636 }
David Reiss63191332009-01-06 19:49:22 +0000637
Jens Geyerd5436f52014-10-03 19:50:38 +0200638 Console.Write("testTypedef(309858235082523)");
639 long uid = client.testTypedef(309858235082523L);
640 Console.WriteLine(" = " + uid);
Jens Geyer178b8132015-09-30 23:16:45 +0200641 if (309858235082523L != uid)
642 {
643 Console.WriteLine("*** FAILED ***");
644 returnCode |= ErrorStructs;
645 }
David Reiss63191332009-01-06 19:49:22 +0000646
Jens Geyer178b8132015-09-30 23:16:45 +0200647 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200648 Console.Write("testMapMap(1)");
649 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
650 Console.Write(" = {");
651 foreach (int key in mm.Keys)
652 {
653 Console.Write(key + " => {");
654 Dictionary<int, int> m2 = mm[key];
655 foreach (int k2 in m2.Keys)
656 {
657 Console.Write(k2 + " => " + m2[k2] + ", ");
658 }
659 Console.Write("}, ");
660 }
661 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000662
Jens Geyer178b8132015-09-30 23:16:45 +0200663 // TODO: Validate received message
Jens Geyerd5436f52014-10-03 19:50:38 +0200664 Insanity insane = new Insanity();
665 insane.UserMap = new Dictionary<Numberz, long>();
666 insane.UserMap[Numberz.FIVE] = 5000L;
667 Xtruct truck = new Xtruct();
668 truck.String_thing = "Truck";
669 truck.Byte_thing = (sbyte)8;
670 truck.I32_thing = 8;
671 truck.I64_thing = 8;
672 insane.Xtructs = new List<Xtruct>();
673 insane.Xtructs.Add(truck);
674 Console.Write("testInsanity()");
675 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
676 Console.Write(" = {");
677 foreach (long key in whoa.Keys)
678 {
679 Dictionary<Numberz, Insanity> val = whoa[key];
680 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000681
Jens Geyerd5436f52014-10-03 19:50:38 +0200682 foreach (Numberz k2 in val.Keys)
683 {
684 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000685
Jens Geyerd5436f52014-10-03 19:50:38 +0200686 Console.Write(k2 + " => {");
687 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000688
Jens Geyerd5436f52014-10-03 19:50:38 +0200689 Console.Write("{");
690 if (userMap != null)
691 {
692 foreach (Numberz k3 in userMap.Keys)
693 {
694 Console.Write(k3 + " => " + userMap[k3] + ", ");
695 }
696 }
697 else
698 {
699 Console.Write("null");
700 }
701 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000702
Jens Geyerd5436f52014-10-03 19:50:38 +0200703 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000704
Jens Geyerd5436f52014-10-03 19:50:38 +0200705 Console.Write("{");
706 if (xtructs != null)
707 {
708 foreach (Xtruct x in xtructs)
709 {
710 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
711 }
712 }
713 else
714 {
715 Console.Write("null");
716 }
717 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000718
Jens Geyerd5436f52014-10-03 19:50:38 +0200719 Console.Write("}, ");
720 }
721 Console.Write("}, ");
722 }
723 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000724
Jens Geyerd5436f52014-10-03 19:50:38 +0200725 sbyte arg0 = 1;
726 int arg1 = 2;
727 long arg2 = long.MaxValue;
728 Dictionary<short, string> multiDict = new Dictionary<short, string>();
729 multiDict[1] = "one";
730 Numberz arg4 = Numberz.FIVE;
731 long arg5 = 5000000;
732 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
733 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
734 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
735 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000736
Jens Geyer178b8132015-09-30 23:16:45 +0200737 try
738 {
739 Console.WriteLine("testException(\"Xception\")");
740 client.testException("Xception");
741 Console.WriteLine("*** FAILED ***");
742 returnCode |= ErrorExceptions;
743 }
744 catch (Xception ex)
745 {
746 if (ex.ErrorCode != 1001 || ex.Message != "Xception")
747 {
748 Console.WriteLine("*** FAILED ***");
749 returnCode |= ErrorExceptions;
750 }
751 }
752 catch (Exception ex)
753 {
754 Console.WriteLine("*** FAILED ***");
755 returnCode |= ErrorExceptions;
756 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
757 }
758 try
759 {
760 Console.WriteLine("testException(\"TException\")");
761 client.testException("TException");
762 Console.WriteLine("*** FAILED ***");
763 returnCode |= ErrorExceptions;
764 }
765 catch (Thrift.TException)
766 {
767 // OK
768 }
769 catch (Exception ex)
770 {
771 Console.WriteLine("*** FAILED ***");
772 returnCode |= ErrorExceptions;
773 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
774 }
775 try
776 {
777 Console.WriteLine("testException(\"ok\")");
778 client.testException("ok");
779 // OK
780 }
781 catch (Exception ex)
782 {
783 Console.WriteLine("*** FAILED ***");
784 returnCode |= ErrorExceptions;
785 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
786 }
787
788 try
789 {
790 Console.WriteLine("testMultiException(\"Xception\", ...)");
791 client.testMultiException("Xception", "ignore");
792 Console.WriteLine("*** FAILED ***");
793 returnCode |= ErrorExceptions;
794 }
795 catch (Xception ex)
796 {
797 if (ex.ErrorCode != 1001 || ex.Message != "This is an Xception")
798 {
799 Console.WriteLine("*** FAILED ***");
800 returnCode |= ErrorExceptions;
801 }
802 }
803 catch (Exception ex)
804 {
805 Console.WriteLine("*** FAILED ***");
806 returnCode |= ErrorExceptions;
807 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
808 }
809 try
810 {
811 Console.WriteLine("testMultiException(\"Xception2\", ...)");
812 client.testMultiException("Xception2", "ignore");
813 Console.WriteLine("*** FAILED ***");
814 returnCode |= ErrorExceptions;
815 }
816 catch (Xception2 ex)
817 {
818 if (ex.ErrorCode != 2002 || ex.Struct_thing.String_thing != "This is an Xception2")
819 {
820 Console.WriteLine("*** FAILED ***");
821 returnCode |= ErrorExceptions;
822 }
823 }
824 catch (Exception ex)
825 {
826 Console.WriteLine("*** FAILED ***");
827 returnCode |= ErrorExceptions;
828 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
829 }
830 try
831 {
832 Console.WriteLine("testMultiException(\"success\", \"OK\")");
833 if ("OK" != client.testMultiException("success", "OK").String_thing)
834 {
835 Console.WriteLine("*** FAILED ***");
836 returnCode |= ErrorExceptions;
837 }
838 }
839 catch (Exception ex)
840 {
841 Console.WriteLine("*** FAILED ***");
842 returnCode |= ErrorExceptions;
843 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
844 }
845
846 Stopwatch sw = new Stopwatch();
847 sw.Start();
Jens Geyerd5436f52014-10-03 19:50:38 +0200848 Console.WriteLine("Test Oneway(1)");
849 client.testOneway(1);
Jens Geyer178b8132015-09-30 23:16:45 +0200850 sw.Stop();
851 if (sw.ElapsedMilliseconds > 1000)
852 {
853 Console.WriteLine("*** FAILED ***");
854 returnCode |= ErrorBaseTypes;
855 }
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000856
Jens Geyerd5436f52014-10-03 19:50:38 +0200857 Console.Write("Test Calltime()");
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +0900858 var times = 50;
859 sw.Reset();
860 sw.Start();
861 for (int k = 0; k < times; ++k)
Jens Geyerd5436f52014-10-03 19:50:38 +0200862 client.testVoid();
Nobuaki Sukegawa88c5ee72016-09-04 18:49:18 +0900863 sw.Stop();
864 Console.WriteLine(" = {0} ms a testVoid() call", sw.ElapsedMilliseconds / times);
Jens Geyer178b8132015-09-30 23:16:45 +0200865 return returnCode;
Jens Geyerd5436f52014-10-03 19:50:38 +0200866 }
867 }
David Reiss63191332009-01-06 19:49:22 +0000868}