blob: 68949ac3fa92808563f56837becb91cb9d2281d7 [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
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900245 Console.Write("testBool(true)");
246 bool t = client.testBool((bool)true);
247 Console.WriteLine(" = " + t);
248 Console.Write("testBool(false)");
249 bool f = client.testBool((bool)false);
250 Console.WriteLine(" = " + f);
251
Jens Geyerd5436f52014-10-03 19:50:38 +0200252 Console.Write("testByte(1)");
253 sbyte i8 = client.testByte((sbyte)1);
254 Console.WriteLine(" = " + i8);
David Reiss63191332009-01-06 19:49:22 +0000255
Jens Geyerd5436f52014-10-03 19:50:38 +0200256 Console.Write("testI32(-1)");
257 int i32 = client.testI32(-1);
258 Console.WriteLine(" = " + i32);
David Reiss63191332009-01-06 19:49:22 +0000259
Jens Geyerd5436f52014-10-03 19:50:38 +0200260 Console.Write("testI64(-34359738368)");
261 long i64 = client.testI64(-34359738368);
262 Console.WriteLine(" = " + i64);
David Reiss63191332009-01-06 19:49:22 +0000263
Jens Geyerd5436f52014-10-03 19:50:38 +0200264 Console.Write("testDouble(5.325098235)");
265 double dub = client.testDouble(5.325098235);
266 Console.WriteLine(" = " + dub);
David Reiss63191332009-01-06 19:49:22 +0000267
Jens Geyer71e814a2014-12-13 23:40:35 +0100268 byte[] binOut = PrepareTestData(true);
269 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
270 try
271 {
272 byte[] binIn = client.testBinary(binOut);
273 Console.WriteLine(" = " + BytesToHex(binIn));
274 if (binIn.Length != binOut.Length)
275 throw new Exception("testBinary: length mismatch");
276 for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
277 if (binIn[ofs] != binOut[ofs])
278 throw new Exception("testBinary: content mismatch at offset " + ofs.ToString());
279 }
Jens Geyer95717c92015-04-23 22:48:13 +0200280 catch (Thrift.TApplicationException e)
Jens Geyer71e814a2014-12-13 23:40:35 +0100281 {
282 Console.Write("testBinary(" + BytesToHex(binOut) + "): "+e.Message);
283 }
284
Jens Geyer80aa53e2015-02-18 22:04:09 +0100285 // binary equals? only with hashcode option enabled ...
Jens Geyer95717c92015-04-23 22:48:13 +0200286 if( typeof(CrazyNesting).GetMethod("Equals").DeclaringType == typeof(CrazyNesting))
Jens Geyer80aa53e2015-02-18 22:04:09 +0100287 {
288 CrazyNesting one = new CrazyNesting();
289 CrazyNesting two = new CrazyNesting();
290 one.String_field = "crazy";
291 two.String_field = "crazy";
292 one.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
293 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
294 if (!one.Equals(two))
295 throw new Exception("CrazyNesting.Equals failed");
296 }
297
Jens Geyerd5436f52014-10-03 19:50:38 +0200298 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
299 Xtruct o = new Xtruct();
300 o.String_thing = "Zero";
301 o.Byte_thing = (sbyte)1;
302 o.I32_thing = -3;
303 o.I64_thing = -5;
304 Xtruct i = client.testStruct(o);
305 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000306
Jens Geyerd5436f52014-10-03 19:50:38 +0200307 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
308 Xtruct2 o2 = new Xtruct2();
309 o2.Byte_thing = (sbyte)1;
310 o2.Struct_thing = o;
311 o2.I32_thing = 5;
312 Xtruct2 i2 = client.testNest(o2);
313 i = i2.Struct_thing;
314 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 +0000315
Jens Geyerd5436f52014-10-03 19:50:38 +0200316 Dictionary<int, int> mapout = new Dictionary<int, int>();
317 for (int j = 0; j < 5; j++)
318 {
319 mapout[j] = j - 10;
320 }
321 Console.Write("testMap({");
322 bool first = true;
323 foreach (int key in mapout.Keys)
324 {
325 if (first)
326 {
327 first = false;
328 }
329 else
330 {
331 Console.Write(", ");
332 }
333 Console.Write(key + " => " + mapout[key]);
334 }
335 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000336
Jens Geyerd5436f52014-10-03 19:50:38 +0200337 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000338
Jens Geyerd5436f52014-10-03 19:50:38 +0200339 Console.Write(" = {");
340 first = true;
341 foreach (int key in mapin.Keys)
342 {
343 if (first)
344 {
345 first = false;
346 }
347 else
348 {
349 Console.Write(", ");
350 }
351 Console.Write(key + " => " + mapin[key]);
352 }
353 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000354
Jens Geyerd5436f52014-10-03 19:50:38 +0200355 List<int> listout = new List<int>();
356 for (int j = -2; j < 3; j++)
357 {
358 listout.Add(j);
359 }
360 Console.Write("testList({");
361 first = true;
362 foreach (int j in listout)
363 {
364 if (first)
365 {
366 first = false;
367 }
368 else
369 {
370 Console.Write(", ");
371 }
372 Console.Write(j);
373 }
374 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000375
Jens Geyerd5436f52014-10-03 19:50:38 +0200376 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000377
Jens Geyerd5436f52014-10-03 19:50:38 +0200378 Console.Write(" = {");
379 first = true;
380 foreach (int j in listin)
381 {
382 if (first)
383 {
384 first = false;
385 }
386 else
387 {
388 Console.Write(", ");
389 }
390 Console.Write(j);
391 }
392 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000393
Jens Geyerd5436f52014-10-03 19:50:38 +0200394 //set
395 THashSet<int> setout = new THashSet<int>();
396 for (int j = -2; j < 3; j++)
397 {
398 setout.Add(j);
399 }
400 Console.Write("testSet({");
401 first = true;
402 foreach (int j in setout)
403 {
404 if (first)
405 {
406 first = false;
407 }
408 else
409 {
410 Console.Write(", ");
411 }
412 Console.Write(j);
413 }
414 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000415
Jens Geyerd5436f52014-10-03 19:50:38 +0200416 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000417
Jens Geyerd5436f52014-10-03 19:50:38 +0200418 Console.Write(" = {");
419 first = true;
420 foreach (int j in setin)
421 {
422 if (first)
423 {
424 first = false;
425 }
426 else
427 {
428 Console.Write(", ");
429 }
430 Console.Write(j);
431 }
432 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000433
434
Jens Geyerd5436f52014-10-03 19:50:38 +0200435 Console.Write("testEnum(ONE)");
436 Numberz ret = client.testEnum(Numberz.ONE);
437 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000438
Jens Geyerd5436f52014-10-03 19:50:38 +0200439 Console.Write("testEnum(TWO)");
440 ret = client.testEnum(Numberz.TWO);
441 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000442
Jens Geyerd5436f52014-10-03 19:50:38 +0200443 Console.Write("testEnum(THREE)");
444 ret = client.testEnum(Numberz.THREE);
445 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000446
Jens Geyerd5436f52014-10-03 19:50:38 +0200447 Console.Write("testEnum(FIVE)");
448 ret = client.testEnum(Numberz.FIVE);
449 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000450
Jens Geyerd5436f52014-10-03 19:50:38 +0200451 Console.Write("testEnum(EIGHT)");
452 ret = client.testEnum(Numberz.EIGHT);
453 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000454
Jens Geyerd5436f52014-10-03 19:50:38 +0200455 Console.Write("testTypedef(309858235082523)");
456 long uid = client.testTypedef(309858235082523L);
457 Console.WriteLine(" = " + uid);
David Reiss63191332009-01-06 19:49:22 +0000458
Jens Geyerd5436f52014-10-03 19:50:38 +0200459 Console.Write("testMapMap(1)");
460 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
461 Console.Write(" = {");
462 foreach (int key in mm.Keys)
463 {
464 Console.Write(key + " => {");
465 Dictionary<int, int> m2 = mm[key];
466 foreach (int k2 in m2.Keys)
467 {
468 Console.Write(k2 + " => " + m2[k2] + ", ");
469 }
470 Console.Write("}, ");
471 }
472 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000473
Jens Geyerd5436f52014-10-03 19:50:38 +0200474 Insanity insane = new Insanity();
475 insane.UserMap = new Dictionary<Numberz, long>();
476 insane.UserMap[Numberz.FIVE] = 5000L;
477 Xtruct truck = new Xtruct();
478 truck.String_thing = "Truck";
479 truck.Byte_thing = (sbyte)8;
480 truck.I32_thing = 8;
481 truck.I64_thing = 8;
482 insane.Xtructs = new List<Xtruct>();
483 insane.Xtructs.Add(truck);
484 Console.Write("testInsanity()");
485 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
486 Console.Write(" = {");
487 foreach (long key in whoa.Keys)
488 {
489 Dictionary<Numberz, Insanity> val = whoa[key];
490 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000491
Jens Geyerd5436f52014-10-03 19:50:38 +0200492 foreach (Numberz k2 in val.Keys)
493 {
494 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000495
Jens Geyerd5436f52014-10-03 19:50:38 +0200496 Console.Write(k2 + " => {");
497 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000498
Jens Geyerd5436f52014-10-03 19:50:38 +0200499 Console.Write("{");
500 if (userMap != null)
501 {
502 foreach (Numberz k3 in userMap.Keys)
503 {
504 Console.Write(k3 + " => " + userMap[k3] + ", ");
505 }
506 }
507 else
508 {
509 Console.Write("null");
510 }
511 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000512
Jens Geyerd5436f52014-10-03 19:50:38 +0200513 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000514
Jens Geyerd5436f52014-10-03 19:50:38 +0200515 Console.Write("{");
516 if (xtructs != null)
517 {
518 foreach (Xtruct x in xtructs)
519 {
520 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
521 }
522 }
523 else
524 {
525 Console.Write("null");
526 }
527 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000528
Jens Geyerd5436f52014-10-03 19:50:38 +0200529 Console.Write("}, ");
530 }
531 Console.Write("}, ");
532 }
533 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000534
Jens Geyerd5436f52014-10-03 19:50:38 +0200535 sbyte arg0 = 1;
536 int arg1 = 2;
537 long arg2 = long.MaxValue;
538 Dictionary<short, string> multiDict = new Dictionary<short, string>();
539 multiDict[1] = "one";
540 Numberz arg4 = Numberz.FIVE;
541 long arg5 = 5000000;
542 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
543 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
544 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
545 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000546
Jens Geyerd5436f52014-10-03 19:50:38 +0200547 Console.WriteLine("Test Oneway(1)");
548 client.testOneway(1);
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000549
Jens Geyerd5436f52014-10-03 19:50:38 +0200550 Console.Write("Test Calltime()");
551 var startt = DateTime.UtcNow;
552 for ( int k=0; k<1000; ++k )
553 client.testVoid();
554 Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" );
555 }
556 }
David Reiss63191332009-01-06 19:49:22 +0000557}