blob: ec0696abc5115c29106eb9b2a81e32bff312c922 [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;
21using System.Collections.Generic;
David Reissd831a212009-02-13 03:09:52 +000022using System.Threading;
23using Thrift.Collections;
David Reiss63191332009-01-06 19:49:22 +000024using Thrift.Protocol;
25using Thrift.Transport;
26using Thrift.Test;
David Reiss63191332009-01-06 19:49:22 +000027
28namespace Test
29{
Jens Geyerd5436f52014-10-03 19:50:38 +020030 public class TestClient
31 {
32 private static int numIterations = 1;
33 private static string protocol = "";
David Reiss63191332009-01-06 19:49:22 +000034
Roger Meier41ad4342015-03-24 22:30:40 +010035 public static bool Execute(string[] args)
Jens Geyerd5436f52014-10-03 19:50:38 +020036 {
37 try
38 {
39 string host = "localhost";
40 int port = 9090;
41 string url = null, pipe = null;
42 int numThreads = 1;
43 bool buffered = false, framed = false, encrypted = false;
Roger Meier41ad4342015-03-24 22:30:40 +010044 string certPath = "../../../../../keys/server.pem";
David Reiss63191332009-01-06 19:49:22 +000045
Jens Geyerd5436f52014-10-03 19:50:38 +020046 try
47 {
48 for (int i = 0; i < args.Length; i++)
49 {
50 if (args[i] == "-u")
51 {
52 url = args[++i];
53 }
54 else if (args[i] == "-n")
55 {
56 numIterations = Convert.ToInt32(args[++i]);
57 }
58 else if (args[i] == "-pipe") // -pipe <name>
59 {
60 pipe = args[++i];
61 Console.WriteLine("Using named pipes transport");
62 }
63 else if (args[i].Contains("--host="))
64 {
65 host = args[i].Substring(args[i].IndexOf("=") + 1);
66 }
67 else if (args[i].Contains("--port="))
68 {
69 port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
70 }
71 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
72 {
73 buffered = true;
74 Console.WriteLine("Using buffered sockets");
75 }
76 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
77 {
78 framed = true;
79 Console.WriteLine("Using framed transport");
80 }
81 else if (args[i] == "-t")
82 {
83 numThreads = Convert.ToInt32(args[++i]);
84 }
85 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
86 {
87 protocol = "compact";
88 Console.WriteLine("Using compact protocol");
89 }
90 else if (args[i] == "--json" || args[i] == "--protocol=json")
91 {
92 protocol = "json";
93 Console.WriteLine("Using JSON protocol");
94 }
95 else if (args[i] == "--ssl")
96 {
97 encrypted = true;
98 Console.WriteLine("Using encrypted transport");
99 }
Roger Meier41ad4342015-03-24 22:30:40 +0100100 else if (args[i].StartsWith("--cert="))
101 {
102 certPath = args[i].Substring("--cert=".Length);
103 }
Jens Geyerd5436f52014-10-03 19:50:38 +0200104 }
105 }
106 catch (Exception e)
107 {
108 Console.WriteLine(e.StackTrace);
109 }
David Reiss63191332009-01-06 19:49:22 +0000110
Jens Geyerd5436f52014-10-03 19:50:38 +0200111 //issue tests on separate threads simultaneously
112 Thread[] threads = new Thread[numThreads];
113 DateTime start = DateTime.Now;
114 for (int test = 0; test < numThreads; test++)
115 {
116 Thread t = new Thread(new ParameterizedThreadStart(ClientThread));
117 threads[test] = t;
118 if (url == null)
119 {
120 // endpoint transport
121 TTransport trans = null;
122 if (pipe != null)
123 trans = new TNamedPipeClientTransport(pipe);
124 else
125 {
126 if (encrypted)
Roger Meier41ad4342015-03-24 22:30:40 +0100127 trans = new TTLSSocket(host, port, certPath);
Jens Geyerd5436f52014-10-03 19:50:38 +0200128 else
129 trans = new TSocket(host, port);
130 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200131
Jens Geyerd5436f52014-10-03 19:50:38 +0200132 // layered transport
133 if (buffered)
134 trans = new TBufferedTransport(trans as TStreamTransport);
135 if (framed)
136 trans = new TFramedTransport(trans);
Jens Geyerc1d79432014-04-22 22:52:43 +0200137
Jens Geyerd5436f52014-10-03 19:50:38 +0200138 //ensure proper open/close of transport
139 trans.Open();
140 trans.Close();
141 t.Start(trans);
142 }
143 else
144 {
145 THttpClient http = new THttpClient(new Uri(url));
146 t.Start(http);
147 }
148 }
David Reiss63191332009-01-06 19:49:22 +0000149
Jens Geyerd5436f52014-10-03 19:50:38 +0200150 for (int test = 0; test < numThreads; test++)
151 {
152 threads[test].Join();
153 }
154 Console.Write("Total time: " + (DateTime.Now - start));
155 }
156 catch (Exception outerEx)
157 {
158 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
Roger Meier41ad4342015-03-24 22:30:40 +0100159 return false;
Jens Geyerd5436f52014-10-03 19:50:38 +0200160 }
David Reiss63191332009-01-06 19:49:22 +0000161
Jens Geyerd5436f52014-10-03 19:50:38 +0200162 Console.WriteLine();
163 Console.WriteLine();
Roger Meier41ad4342015-03-24 22:30:40 +0100164 return true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200165 }
David Reiss63191332009-01-06 19:49:22 +0000166
Jens Geyerd5436f52014-10-03 19:50:38 +0200167 public static void ClientThread(object obj)
168 {
169 TTransport transport = (TTransport)obj;
170 for (int i = 0; i < numIterations; i++)
171 {
172 ClientTest(transport);
173 }
174 transport.Close();
175 }
David Reiss63191332009-01-06 19:49:22 +0000176
Jens Geyer71e814a2014-12-13 23:40:35 +0100177 public static string BytesToHex(byte[] data) {
178 return BitConverter.ToString(data).Replace("-", string.Empty);
179 }
180
181 public static byte[] PrepareTestData(bool randomDist)
182 {
183 byte[] retval = new byte[0x100];
184 int initLen = Math.Min(0x100,retval.Length);
185
186 // linear distribution, unless random is requested
187 if (!randomDist) {
Jens Geyer95717c92015-04-23 22:48:13 +0200188 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100189 retval[i] = (byte)i;
190 }
191 return retval;
192 }
193
194 // random distribution
Jens Geyer95717c92015-04-23 22:48:13 +0200195 for (var i = 0; i < initLen; ++i) {
Jens Geyer71e814a2014-12-13 23:40:35 +0100196 retval[i] = (byte)0;
197 }
198 var rnd = new Random();
199 for (var i = 1; i < initLen; ++i) {
200 while( true) {
201 int nextPos = rnd.Next() % initLen;
202 if (retval[nextPos] == 0) {
203 retval[nextPos] = (byte)i;
204 break;
205 }
206 }
207 }
208 return retval;
209 }
210
Jens Geyerd5436f52014-10-03 19:50:38 +0200211 public static void ClientTest(TTransport transport)
212 {
213 TProtocol proto;
214 if (protocol == "compact")
215 proto = new TCompactProtocol(transport);
216 else if (protocol == "json")
217 proto = new TJSONProtocol(transport);
218 else
219 proto = new TBinaryProtocol(transport);
David Reiss63191332009-01-06 19:49:22 +0000220
Jens Geyerd5436f52014-10-03 19:50:38 +0200221 ThriftTest.Client client = new ThriftTest.Client(proto);
222 try
223 {
224 if (!transport.IsOpen)
225 {
226 transport.Open();
227 }
228 }
229 catch (TTransportException ttx)
230 {
231 Console.WriteLine("Connect failed: " + ttx.Message);
232 return;
233 }
David Reiss63191332009-01-06 19:49:22 +0000234
Jens Geyerd5436f52014-10-03 19:50:38 +0200235 long start = DateTime.Now.ToFileTime();
David Reiss63191332009-01-06 19:49:22 +0000236
Jens Geyerd5436f52014-10-03 19:50:38 +0200237 Console.Write("testVoid()");
238 client.testVoid();
239 Console.WriteLine(" = void");
David Reiss63191332009-01-06 19:49:22 +0000240
Jens Geyerd5436f52014-10-03 19:50:38 +0200241 Console.Write("testString(\"Test\")");
242 string s = client.testString("Test");
243 Console.WriteLine(" = \"" + s + "\"");
David Reiss63191332009-01-06 19:49:22 +0000244
Jens Geyerd5436f52014-10-03 19:50:38 +0200245 Console.Write("testByte(1)");
246 sbyte i8 = client.testByte((sbyte)1);
247 Console.WriteLine(" = " + i8);
David Reiss63191332009-01-06 19:49:22 +0000248
Jens Geyerd5436f52014-10-03 19:50:38 +0200249 Console.Write("testI32(-1)");
250 int i32 = client.testI32(-1);
251 Console.WriteLine(" = " + i32);
David Reiss63191332009-01-06 19:49:22 +0000252
Jens Geyerd5436f52014-10-03 19:50:38 +0200253 Console.Write("testI64(-34359738368)");
254 long i64 = client.testI64(-34359738368);
255 Console.WriteLine(" = " + i64);
David Reiss63191332009-01-06 19:49:22 +0000256
Jens Geyerd5436f52014-10-03 19:50:38 +0200257 Console.Write("testDouble(5.325098235)");
258 double dub = client.testDouble(5.325098235);
259 Console.WriteLine(" = " + dub);
David Reiss63191332009-01-06 19:49:22 +0000260
Jens Geyer71e814a2014-12-13 23:40:35 +0100261 byte[] binOut = PrepareTestData(true);
262 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
263 try
264 {
265 byte[] binIn = client.testBinary(binOut);
266 Console.WriteLine(" = " + BytesToHex(binIn));
267 if (binIn.Length != binOut.Length)
268 throw new Exception("testBinary: length mismatch");
269 for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
270 if (binIn[ofs] != binOut[ofs])
271 throw new Exception("testBinary: content mismatch at offset " + ofs.ToString());
272 }
Jens Geyer95717c92015-04-23 22:48:13 +0200273 catch (Thrift.TApplicationException e)
Jens Geyer71e814a2014-12-13 23:40:35 +0100274 {
275 Console.Write("testBinary(" + BytesToHex(binOut) + "): "+e.Message);
276 }
277
Jens Geyer80aa53e2015-02-18 22:04:09 +0100278 // binary equals? only with hashcode option enabled ...
Jens Geyer95717c92015-04-23 22:48:13 +0200279 if( typeof(CrazyNesting).GetMethod("Equals").DeclaringType == typeof(CrazyNesting))
Jens Geyer80aa53e2015-02-18 22:04:09 +0100280 {
281 CrazyNesting one = new CrazyNesting();
282 CrazyNesting two = new CrazyNesting();
283 one.String_field = "crazy";
284 two.String_field = "crazy";
285 one.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
286 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
287 if (!one.Equals(two))
288 throw new Exception("CrazyNesting.Equals failed");
289 }
290
Jens Geyerd5436f52014-10-03 19:50:38 +0200291 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
292 Xtruct o = new Xtruct();
293 o.String_thing = "Zero";
294 o.Byte_thing = (sbyte)1;
295 o.I32_thing = -3;
296 o.I64_thing = -5;
297 Xtruct i = client.testStruct(o);
298 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000299
Jens Geyerd5436f52014-10-03 19:50:38 +0200300 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
301 Xtruct2 o2 = new Xtruct2();
302 o2.Byte_thing = (sbyte)1;
303 o2.Struct_thing = o;
304 o2.I32_thing = 5;
305 Xtruct2 i2 = client.testNest(o2);
306 i = i2.Struct_thing;
307 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 +0000308
Jens Geyerd5436f52014-10-03 19:50:38 +0200309 Dictionary<int, int> mapout = new Dictionary<int, int>();
310 for (int j = 0; j < 5; j++)
311 {
312 mapout[j] = j - 10;
313 }
314 Console.Write("testMap({");
315 bool first = true;
316 foreach (int key in mapout.Keys)
317 {
318 if (first)
319 {
320 first = false;
321 }
322 else
323 {
324 Console.Write(", ");
325 }
326 Console.Write(key + " => " + mapout[key]);
327 }
328 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000329
Jens Geyerd5436f52014-10-03 19:50:38 +0200330 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000331
Jens Geyerd5436f52014-10-03 19:50:38 +0200332 Console.Write(" = {");
333 first = true;
334 foreach (int key in mapin.Keys)
335 {
336 if (first)
337 {
338 first = false;
339 }
340 else
341 {
342 Console.Write(", ");
343 }
344 Console.Write(key + " => " + mapin[key]);
345 }
346 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000347
Jens Geyerd5436f52014-10-03 19:50:38 +0200348 List<int> listout = new List<int>();
349 for (int j = -2; j < 3; j++)
350 {
351 listout.Add(j);
352 }
353 Console.Write("testList({");
354 first = true;
355 foreach (int j in listout)
356 {
357 if (first)
358 {
359 first = false;
360 }
361 else
362 {
363 Console.Write(", ");
364 }
365 Console.Write(j);
366 }
367 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000368
Jens Geyerd5436f52014-10-03 19:50:38 +0200369 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000370
Jens Geyerd5436f52014-10-03 19:50:38 +0200371 Console.Write(" = {");
372 first = true;
373 foreach (int j in listin)
374 {
375 if (first)
376 {
377 first = false;
378 }
379 else
380 {
381 Console.Write(", ");
382 }
383 Console.Write(j);
384 }
385 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000386
Jens Geyerd5436f52014-10-03 19:50:38 +0200387 //set
388 THashSet<int> setout = new THashSet<int>();
389 for (int j = -2; j < 3; j++)
390 {
391 setout.Add(j);
392 }
393 Console.Write("testSet({");
394 first = true;
395 foreach (int j in setout)
396 {
397 if (first)
398 {
399 first = false;
400 }
401 else
402 {
403 Console.Write(", ");
404 }
405 Console.Write(j);
406 }
407 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000408
Jens Geyerd5436f52014-10-03 19:50:38 +0200409 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000410
Jens Geyerd5436f52014-10-03 19:50:38 +0200411 Console.Write(" = {");
412 first = true;
413 foreach (int j in setin)
414 {
415 if (first)
416 {
417 first = false;
418 }
419 else
420 {
421 Console.Write(", ");
422 }
423 Console.Write(j);
424 }
425 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000426
427
Jens Geyerd5436f52014-10-03 19:50:38 +0200428 Console.Write("testEnum(ONE)");
429 Numberz ret = client.testEnum(Numberz.ONE);
430 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000431
Jens Geyerd5436f52014-10-03 19:50:38 +0200432 Console.Write("testEnum(TWO)");
433 ret = client.testEnum(Numberz.TWO);
434 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000435
Jens Geyerd5436f52014-10-03 19:50:38 +0200436 Console.Write("testEnum(THREE)");
437 ret = client.testEnum(Numberz.THREE);
438 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000439
Jens Geyerd5436f52014-10-03 19:50:38 +0200440 Console.Write("testEnum(FIVE)");
441 ret = client.testEnum(Numberz.FIVE);
442 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000443
Jens Geyerd5436f52014-10-03 19:50:38 +0200444 Console.Write("testEnum(EIGHT)");
445 ret = client.testEnum(Numberz.EIGHT);
446 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000447
Jens Geyerd5436f52014-10-03 19:50:38 +0200448 Console.Write("testTypedef(309858235082523)");
449 long uid = client.testTypedef(309858235082523L);
450 Console.WriteLine(" = " + uid);
David Reiss63191332009-01-06 19:49:22 +0000451
Jens Geyerd5436f52014-10-03 19:50:38 +0200452 Console.Write("testMapMap(1)");
453 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
454 Console.Write(" = {");
455 foreach (int key in mm.Keys)
456 {
457 Console.Write(key + " => {");
458 Dictionary<int, int> m2 = mm[key];
459 foreach (int k2 in m2.Keys)
460 {
461 Console.Write(k2 + " => " + m2[k2] + ", ");
462 }
463 Console.Write("}, ");
464 }
465 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000466
Jens Geyerd5436f52014-10-03 19:50:38 +0200467 Insanity insane = new Insanity();
468 insane.UserMap = new Dictionary<Numberz, long>();
469 insane.UserMap[Numberz.FIVE] = 5000L;
470 Xtruct truck = new Xtruct();
471 truck.String_thing = "Truck";
472 truck.Byte_thing = (sbyte)8;
473 truck.I32_thing = 8;
474 truck.I64_thing = 8;
475 insane.Xtructs = new List<Xtruct>();
476 insane.Xtructs.Add(truck);
477 Console.Write("testInsanity()");
478 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
479 Console.Write(" = {");
480 foreach (long key in whoa.Keys)
481 {
482 Dictionary<Numberz, Insanity> val = whoa[key];
483 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000484
Jens Geyerd5436f52014-10-03 19:50:38 +0200485 foreach (Numberz k2 in val.Keys)
486 {
487 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000488
Jens Geyerd5436f52014-10-03 19:50:38 +0200489 Console.Write(k2 + " => {");
490 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000491
Jens Geyerd5436f52014-10-03 19:50:38 +0200492 Console.Write("{");
493 if (userMap != null)
494 {
495 foreach (Numberz k3 in userMap.Keys)
496 {
497 Console.Write(k3 + " => " + userMap[k3] + ", ");
498 }
499 }
500 else
501 {
502 Console.Write("null");
503 }
504 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000505
Jens Geyerd5436f52014-10-03 19:50:38 +0200506 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000507
Jens Geyerd5436f52014-10-03 19:50:38 +0200508 Console.Write("{");
509 if (xtructs != null)
510 {
511 foreach (Xtruct x in xtructs)
512 {
513 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
514 }
515 }
516 else
517 {
518 Console.Write("null");
519 }
520 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000521
Jens Geyerd5436f52014-10-03 19:50:38 +0200522 Console.Write("}, ");
523 }
524 Console.Write("}, ");
525 }
526 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000527
Jens Geyerd5436f52014-10-03 19:50:38 +0200528 sbyte arg0 = 1;
529 int arg1 = 2;
530 long arg2 = long.MaxValue;
531 Dictionary<short, string> multiDict = new Dictionary<short, string>();
532 multiDict[1] = "one";
533 Numberz arg4 = Numberz.FIVE;
534 long arg5 = 5000000;
535 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
536 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
537 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
538 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000539
Jens Geyerd5436f52014-10-03 19:50:38 +0200540 Console.WriteLine("Test Oneway(1)");
541 client.testOneway(1);
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000542
Jens Geyerd5436f52014-10-03 19:50:38 +0200543 Console.Write("Test Calltime()");
544 var startt = DateTime.UtcNow;
545 for ( int k=0; k<1000; ++k )
546 client.testVoid();
547 Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" );
548 }
549 }
David Reiss63191332009-01-06 19:49:22 +0000550}