blob: a0ceb15af818db26103b56a3e2571260ce289fdf [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
Jens Geyerd5436f52014-10-03 19:50:38 +020035 public static void Execute(string[] args)
36 {
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;
David Reiss63191332009-01-06 19:49:22 +000044
Jens Geyerd5436f52014-10-03 19:50:38 +020045 try
46 {
47 for (int i = 0; i < args.Length; i++)
48 {
49 if (args[i] == "-u")
50 {
51 url = args[++i];
52 }
53 else if (args[i] == "-n")
54 {
55 numIterations = Convert.ToInt32(args[++i]);
56 }
57 else if (args[i] == "-pipe") // -pipe <name>
58 {
59 pipe = args[++i];
60 Console.WriteLine("Using named pipes transport");
61 }
62 else if (args[i].Contains("--host="))
63 {
64 host = args[i].Substring(args[i].IndexOf("=") + 1);
65 }
66 else if (args[i].Contains("--port="))
67 {
68 port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
69 }
70 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
71 {
72 buffered = true;
73 Console.WriteLine("Using buffered sockets");
74 }
75 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
76 {
77 framed = true;
78 Console.WriteLine("Using framed transport");
79 }
80 else if (args[i] == "-t")
81 {
82 numThreads = Convert.ToInt32(args[++i]);
83 }
84 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
85 {
86 protocol = "compact";
87 Console.WriteLine("Using compact protocol");
88 }
89 else if (args[i] == "--json" || args[i] == "--protocol=json")
90 {
91 protocol = "json";
92 Console.WriteLine("Using JSON protocol");
93 }
94 else if (args[i] == "--ssl")
95 {
96 encrypted = true;
97 Console.WriteLine("Using encrypted transport");
98 }
99 }
100 }
101 catch (Exception e)
102 {
103 Console.WriteLine(e.StackTrace);
104 }
David Reiss63191332009-01-06 19:49:22 +0000105
Jens Geyerd5436f52014-10-03 19:50:38 +0200106 //issue tests on separate threads simultaneously
107 Thread[] threads = new Thread[numThreads];
108 DateTime start = DateTime.Now;
109 for (int test = 0; test < numThreads; test++)
110 {
111 Thread t = new Thread(new ParameterizedThreadStart(ClientThread));
112 threads[test] = t;
113 if (url == null)
114 {
115 // endpoint transport
116 TTransport trans = null;
117 if (pipe != null)
118 trans = new TNamedPipeClientTransport(pipe);
119 else
120 {
121 if (encrypted)
122 trans = new TTLSSocket(host, port, "../../../../../keys/client.pem");
123 else
124 trans = new TSocket(host, port);
125 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200126
Jens Geyerd5436f52014-10-03 19:50:38 +0200127 // layered transport
128 if (buffered)
129 trans = new TBufferedTransport(trans as TStreamTransport);
130 if (framed)
131 trans = new TFramedTransport(trans);
Jens Geyerc1d79432014-04-22 22:52:43 +0200132
Jens Geyerd5436f52014-10-03 19:50:38 +0200133 //ensure proper open/close of transport
134 trans.Open();
135 trans.Close();
136 t.Start(trans);
137 }
138 else
139 {
140 THttpClient http = new THttpClient(new Uri(url));
141 t.Start(http);
142 }
143 }
David Reiss63191332009-01-06 19:49:22 +0000144
Jens Geyerd5436f52014-10-03 19:50:38 +0200145 for (int test = 0; test < numThreads; test++)
146 {
147 threads[test].Join();
148 }
149 Console.Write("Total time: " + (DateTime.Now - start));
150 }
151 catch (Exception outerEx)
152 {
153 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
154 }
David Reiss63191332009-01-06 19:49:22 +0000155
Jens Geyerd5436f52014-10-03 19:50:38 +0200156 Console.WriteLine();
157 Console.WriteLine();
158 }
David Reiss63191332009-01-06 19:49:22 +0000159
Jens Geyerd5436f52014-10-03 19:50:38 +0200160 public static void ClientThread(object obj)
161 {
162 TTransport transport = (TTransport)obj;
163 for (int i = 0; i < numIterations; i++)
164 {
165 ClientTest(transport);
166 }
167 transport.Close();
168 }
David Reiss63191332009-01-06 19:49:22 +0000169
Jens Geyer71e814a2014-12-13 23:40:35 +0100170 public static string BytesToHex(byte[] data) {
171 return BitConverter.ToString(data).Replace("-", string.Empty);
172 }
173
174 public static byte[] PrepareTestData(bool randomDist)
175 {
176 byte[] retval = new byte[0x100];
177 int initLen = Math.Min(0x100,retval.Length);
178
179 // linear distribution, unless random is requested
180 if (!randomDist) {
181 for (var i = 0; i < initLen; ++i) {
182 retval[i] = (byte)i;
183 }
184 return retval;
185 }
186
187 // random distribution
188 for (var i = 0; i < initLen; ++i) {
189 retval[i] = (byte)0;
190 }
191 var rnd = new Random();
192 for (var i = 1; i < initLen; ++i) {
193 while( true) {
194 int nextPos = rnd.Next() % initLen;
195 if (retval[nextPos] == 0) {
196 retval[nextPos] = (byte)i;
197 break;
198 }
199 }
200 }
201 return retval;
202 }
203
Jens Geyerd5436f52014-10-03 19:50:38 +0200204 public static void ClientTest(TTransport transport)
205 {
206 TProtocol proto;
207 if (protocol == "compact")
208 proto = new TCompactProtocol(transport);
209 else if (protocol == "json")
210 proto = new TJSONProtocol(transport);
211 else
212 proto = new TBinaryProtocol(transport);
David Reiss63191332009-01-06 19:49:22 +0000213
Jens Geyerd5436f52014-10-03 19:50:38 +0200214 ThriftTest.Client client = new ThriftTest.Client(proto);
215 try
216 {
217 if (!transport.IsOpen)
218 {
219 transport.Open();
220 }
221 }
222 catch (TTransportException ttx)
223 {
224 Console.WriteLine("Connect failed: " + ttx.Message);
225 return;
226 }
David Reiss63191332009-01-06 19:49:22 +0000227
Jens Geyerd5436f52014-10-03 19:50:38 +0200228 long start = DateTime.Now.ToFileTime();
David Reiss63191332009-01-06 19:49:22 +0000229
Jens Geyerd5436f52014-10-03 19:50:38 +0200230 Console.Write("testVoid()");
231 client.testVoid();
232 Console.WriteLine(" = void");
David Reiss63191332009-01-06 19:49:22 +0000233
Jens Geyerd5436f52014-10-03 19:50:38 +0200234 Console.Write("testString(\"Test\")");
235 string s = client.testString("Test");
236 Console.WriteLine(" = \"" + s + "\"");
David Reiss63191332009-01-06 19:49:22 +0000237
Jens Geyerd5436f52014-10-03 19:50:38 +0200238 Console.Write("testByte(1)");
239 sbyte i8 = client.testByte((sbyte)1);
240 Console.WriteLine(" = " + i8);
David Reiss63191332009-01-06 19:49:22 +0000241
Jens Geyerd5436f52014-10-03 19:50:38 +0200242 Console.Write("testI32(-1)");
243 int i32 = client.testI32(-1);
244 Console.WriteLine(" = " + i32);
David Reiss63191332009-01-06 19:49:22 +0000245
Jens Geyerd5436f52014-10-03 19:50:38 +0200246 Console.Write("testI64(-34359738368)");
247 long i64 = client.testI64(-34359738368);
248 Console.WriteLine(" = " + i64);
David Reiss63191332009-01-06 19:49:22 +0000249
Jens Geyerd5436f52014-10-03 19:50:38 +0200250 Console.Write("testDouble(5.325098235)");
251 double dub = client.testDouble(5.325098235);
252 Console.WriteLine(" = " + dub);
David Reiss63191332009-01-06 19:49:22 +0000253
Jens Geyer71e814a2014-12-13 23:40:35 +0100254 byte[] binOut = PrepareTestData(true);
255 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
256 try
257 {
258 byte[] binIn = client.testBinary(binOut);
259 Console.WriteLine(" = " + BytesToHex(binIn));
260 if (binIn.Length != binOut.Length)
261 throw new Exception("testBinary: length mismatch");
262 for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
263 if (binIn[ofs] != binOut[ofs])
264 throw new Exception("testBinary: content mismatch at offset " + ofs.ToString());
265 }
266 catch (Thrift.TApplicationException e)
267 {
268 Console.Write("testBinary(" + BytesToHex(binOut) + "): "+e.Message);
269 }
270
Jens Geyer80aa53e2015-02-18 22:04:09 +0100271 // binary equals? only with hashcode option enabled ...
272 if( typeof(CrazyNesting).GetMethod("Equals").DeclaringType == typeof(CrazyNesting))
273 {
274 CrazyNesting one = new CrazyNesting();
275 CrazyNesting two = new CrazyNesting();
276 one.String_field = "crazy";
277 two.String_field = "crazy";
278 one.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
279 two.Binary_field = new byte[10] { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF };
280 if (!one.Equals(two))
281 throw new Exception("CrazyNesting.Equals failed");
282 }
283
Jens Geyerd5436f52014-10-03 19:50:38 +0200284 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
285 Xtruct o = new Xtruct();
286 o.String_thing = "Zero";
287 o.Byte_thing = (sbyte)1;
288 o.I32_thing = -3;
289 o.I64_thing = -5;
290 Xtruct i = client.testStruct(o);
291 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000292
Jens Geyerd5436f52014-10-03 19:50:38 +0200293 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
294 Xtruct2 o2 = new Xtruct2();
295 o2.Byte_thing = (sbyte)1;
296 o2.Struct_thing = o;
297 o2.I32_thing = 5;
298 Xtruct2 i2 = client.testNest(o2);
299 i = i2.Struct_thing;
300 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 +0000301
Jens Geyerd5436f52014-10-03 19:50:38 +0200302 Dictionary<int, int> mapout = new Dictionary<int, int>();
303 for (int j = 0; j < 5; j++)
304 {
305 mapout[j] = j - 10;
306 }
307 Console.Write("testMap({");
308 bool first = true;
309 foreach (int key in mapout.Keys)
310 {
311 if (first)
312 {
313 first = false;
314 }
315 else
316 {
317 Console.Write(", ");
318 }
319 Console.Write(key + " => " + mapout[key]);
320 }
321 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000322
Jens Geyerd5436f52014-10-03 19:50:38 +0200323 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000324
Jens Geyerd5436f52014-10-03 19:50:38 +0200325 Console.Write(" = {");
326 first = true;
327 foreach (int key in mapin.Keys)
328 {
329 if (first)
330 {
331 first = false;
332 }
333 else
334 {
335 Console.Write(", ");
336 }
337 Console.Write(key + " => " + mapin[key]);
338 }
339 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000340
Jens Geyerd5436f52014-10-03 19:50:38 +0200341 List<int> listout = new List<int>();
342 for (int j = -2; j < 3; j++)
343 {
344 listout.Add(j);
345 }
346 Console.Write("testList({");
347 first = true;
348 foreach (int j in listout)
349 {
350 if (first)
351 {
352 first = false;
353 }
354 else
355 {
356 Console.Write(", ");
357 }
358 Console.Write(j);
359 }
360 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000361
Jens Geyerd5436f52014-10-03 19:50:38 +0200362 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000363
Jens Geyerd5436f52014-10-03 19:50:38 +0200364 Console.Write(" = {");
365 first = true;
366 foreach (int j in listin)
367 {
368 if (first)
369 {
370 first = false;
371 }
372 else
373 {
374 Console.Write(", ");
375 }
376 Console.Write(j);
377 }
378 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000379
Jens Geyerd5436f52014-10-03 19:50:38 +0200380 //set
381 THashSet<int> setout = new THashSet<int>();
382 for (int j = -2; j < 3; j++)
383 {
384 setout.Add(j);
385 }
386 Console.Write("testSet({");
387 first = true;
388 foreach (int j in setout)
389 {
390 if (first)
391 {
392 first = false;
393 }
394 else
395 {
396 Console.Write(", ");
397 }
398 Console.Write(j);
399 }
400 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000401
Jens Geyerd5436f52014-10-03 19:50:38 +0200402 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000403
Jens Geyerd5436f52014-10-03 19:50:38 +0200404 Console.Write(" = {");
405 first = true;
406 foreach (int j in setin)
407 {
408 if (first)
409 {
410 first = false;
411 }
412 else
413 {
414 Console.Write(", ");
415 }
416 Console.Write(j);
417 }
418 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000419
420
Jens Geyerd5436f52014-10-03 19:50:38 +0200421 Console.Write("testEnum(ONE)");
422 Numberz ret = client.testEnum(Numberz.ONE);
423 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000424
Jens Geyerd5436f52014-10-03 19:50:38 +0200425 Console.Write("testEnum(TWO)");
426 ret = client.testEnum(Numberz.TWO);
427 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000428
Jens Geyerd5436f52014-10-03 19:50:38 +0200429 Console.Write("testEnum(THREE)");
430 ret = client.testEnum(Numberz.THREE);
431 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000432
Jens Geyerd5436f52014-10-03 19:50:38 +0200433 Console.Write("testEnum(FIVE)");
434 ret = client.testEnum(Numberz.FIVE);
435 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000436
Jens Geyerd5436f52014-10-03 19:50:38 +0200437 Console.Write("testEnum(EIGHT)");
438 ret = client.testEnum(Numberz.EIGHT);
439 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000440
Jens Geyerd5436f52014-10-03 19:50:38 +0200441 Console.Write("testTypedef(309858235082523)");
442 long uid = client.testTypedef(309858235082523L);
443 Console.WriteLine(" = " + uid);
David Reiss63191332009-01-06 19:49:22 +0000444
Jens Geyerd5436f52014-10-03 19:50:38 +0200445 Console.Write("testMapMap(1)");
446 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
447 Console.Write(" = {");
448 foreach (int key in mm.Keys)
449 {
450 Console.Write(key + " => {");
451 Dictionary<int, int> m2 = mm[key];
452 foreach (int k2 in m2.Keys)
453 {
454 Console.Write(k2 + " => " + m2[k2] + ", ");
455 }
456 Console.Write("}, ");
457 }
458 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000459
Jens Geyerd5436f52014-10-03 19:50:38 +0200460 Insanity insane = new Insanity();
461 insane.UserMap = new Dictionary<Numberz, long>();
462 insane.UserMap[Numberz.FIVE] = 5000L;
463 Xtruct truck = new Xtruct();
464 truck.String_thing = "Truck";
465 truck.Byte_thing = (sbyte)8;
466 truck.I32_thing = 8;
467 truck.I64_thing = 8;
468 insane.Xtructs = new List<Xtruct>();
469 insane.Xtructs.Add(truck);
470 Console.Write("testInsanity()");
471 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
472 Console.Write(" = {");
473 foreach (long key in whoa.Keys)
474 {
475 Dictionary<Numberz, Insanity> val = whoa[key];
476 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000477
Jens Geyerd5436f52014-10-03 19:50:38 +0200478 foreach (Numberz k2 in val.Keys)
479 {
480 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000481
Jens Geyerd5436f52014-10-03 19:50:38 +0200482 Console.Write(k2 + " => {");
483 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000484
Jens Geyerd5436f52014-10-03 19:50:38 +0200485 Console.Write("{");
486 if (userMap != null)
487 {
488 foreach (Numberz k3 in userMap.Keys)
489 {
490 Console.Write(k3 + " => " + userMap[k3] + ", ");
491 }
492 }
493 else
494 {
495 Console.Write("null");
496 }
497 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000498
Jens Geyerd5436f52014-10-03 19:50:38 +0200499 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000500
Jens Geyerd5436f52014-10-03 19:50:38 +0200501 Console.Write("{");
502 if (xtructs != null)
503 {
504 foreach (Xtruct x in xtructs)
505 {
506 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
507 }
508 }
509 else
510 {
511 Console.Write("null");
512 }
513 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000514
Jens Geyerd5436f52014-10-03 19:50:38 +0200515 Console.Write("}, ");
516 }
517 Console.Write("}, ");
518 }
519 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000520
Jens Geyerd5436f52014-10-03 19:50:38 +0200521 sbyte arg0 = 1;
522 int arg1 = 2;
523 long arg2 = long.MaxValue;
524 Dictionary<short, string> multiDict = new Dictionary<short, string>();
525 multiDict[1] = "one";
526 Numberz arg4 = Numberz.FIVE;
527 long arg5 = 5000000;
528 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
529 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
530 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
531 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000532
Jens Geyerd5436f52014-10-03 19:50:38 +0200533 Console.WriteLine("Test Oneway(1)");
534 client.testOneway(1);
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000535
Jens Geyerd5436f52014-10-03 19:50:38 +0200536 Console.Write("Test Calltime()");
537 var startt = DateTime.UtcNow;
538 for ( int k=0; k<1000; ++k )
539 client.testVoid();
540 Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" );
541 }
542 }
David Reiss63191332009-01-06 19:49:22 +0000543}