blob: cb33473702c3f6f9a722fab534d0cfd69a80ae93 [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 Geyerd5436f52014-10-03 19:50:38 +0200170 public static void ClientTest(TTransport transport)
171 {
172 TProtocol proto;
173 if (protocol == "compact")
174 proto = new TCompactProtocol(transport);
175 else if (protocol == "json")
176 proto = new TJSONProtocol(transport);
177 else
178 proto = new TBinaryProtocol(transport);
David Reiss63191332009-01-06 19:49:22 +0000179
Jens Geyerd5436f52014-10-03 19:50:38 +0200180 ThriftTest.Client client = new ThriftTest.Client(proto);
181 try
182 {
183 if (!transport.IsOpen)
184 {
185 transport.Open();
186 }
187 }
188 catch (TTransportException ttx)
189 {
190 Console.WriteLine("Connect failed: " + ttx.Message);
191 return;
192 }
David Reiss63191332009-01-06 19:49:22 +0000193
Jens Geyerd5436f52014-10-03 19:50:38 +0200194 long start = DateTime.Now.ToFileTime();
David Reiss63191332009-01-06 19:49:22 +0000195
Jens Geyerd5436f52014-10-03 19:50:38 +0200196 Console.Write("testVoid()");
197 client.testVoid();
198 Console.WriteLine(" = void");
David Reiss63191332009-01-06 19:49:22 +0000199
Jens Geyerd5436f52014-10-03 19:50:38 +0200200 Console.Write("testString(\"Test\")");
201 string s = client.testString("Test");
202 Console.WriteLine(" = \"" + s + "\"");
David Reiss63191332009-01-06 19:49:22 +0000203
Jens Geyerd5436f52014-10-03 19:50:38 +0200204 Console.Write("testByte(1)");
205 sbyte i8 = client.testByte((sbyte)1);
206 Console.WriteLine(" = " + i8);
David Reiss63191332009-01-06 19:49:22 +0000207
Jens Geyerd5436f52014-10-03 19:50:38 +0200208 Console.Write("testI32(-1)");
209 int i32 = client.testI32(-1);
210 Console.WriteLine(" = " + i32);
David Reiss63191332009-01-06 19:49:22 +0000211
Jens Geyerd5436f52014-10-03 19:50:38 +0200212 Console.Write("testI64(-34359738368)");
213 long i64 = client.testI64(-34359738368);
214 Console.WriteLine(" = " + i64);
David Reiss63191332009-01-06 19:49:22 +0000215
Jens Geyerd5436f52014-10-03 19:50:38 +0200216 Console.Write("testDouble(5.325098235)");
217 double dub = client.testDouble(5.325098235);
218 Console.WriteLine(" = " + dub);
David Reiss63191332009-01-06 19:49:22 +0000219
Jens Geyerd5436f52014-10-03 19:50:38 +0200220 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
221 Xtruct o = new Xtruct();
222 o.String_thing = "Zero";
223 o.Byte_thing = (sbyte)1;
224 o.I32_thing = -3;
225 o.I64_thing = -5;
226 Xtruct i = client.testStruct(o);
227 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000228
Jens Geyerd5436f52014-10-03 19:50:38 +0200229 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
230 Xtruct2 o2 = new Xtruct2();
231 o2.Byte_thing = (sbyte)1;
232 o2.Struct_thing = o;
233 o2.I32_thing = 5;
234 Xtruct2 i2 = client.testNest(o2);
235 i = i2.Struct_thing;
236 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 +0000237
Jens Geyerd5436f52014-10-03 19:50:38 +0200238 Dictionary<int, int> mapout = new Dictionary<int, int>();
239 for (int j = 0; j < 5; j++)
240 {
241 mapout[j] = j - 10;
242 }
243 Console.Write("testMap({");
244 bool first = true;
245 foreach (int key in mapout.Keys)
246 {
247 if (first)
248 {
249 first = false;
250 }
251 else
252 {
253 Console.Write(", ");
254 }
255 Console.Write(key + " => " + mapout[key]);
256 }
257 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000258
Jens Geyerd5436f52014-10-03 19:50:38 +0200259 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000260
Jens Geyerd5436f52014-10-03 19:50:38 +0200261 Console.Write(" = {");
262 first = true;
263 foreach (int key in mapin.Keys)
264 {
265 if (first)
266 {
267 first = false;
268 }
269 else
270 {
271 Console.Write(", ");
272 }
273 Console.Write(key + " => " + mapin[key]);
274 }
275 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000276
Jens Geyerd5436f52014-10-03 19:50:38 +0200277 List<int> listout = new List<int>();
278 for (int j = -2; j < 3; j++)
279 {
280 listout.Add(j);
281 }
282 Console.Write("testList({");
283 first = true;
284 foreach (int j in listout)
285 {
286 if (first)
287 {
288 first = false;
289 }
290 else
291 {
292 Console.Write(", ");
293 }
294 Console.Write(j);
295 }
296 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000297
Jens Geyerd5436f52014-10-03 19:50:38 +0200298 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000299
Jens Geyerd5436f52014-10-03 19:50:38 +0200300 Console.Write(" = {");
301 first = true;
302 foreach (int j in listin)
303 {
304 if (first)
305 {
306 first = false;
307 }
308 else
309 {
310 Console.Write(", ");
311 }
312 Console.Write(j);
313 }
314 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000315
Jens Geyerd5436f52014-10-03 19:50:38 +0200316 //set
317 THashSet<int> setout = new THashSet<int>();
318 for (int j = -2; j < 3; j++)
319 {
320 setout.Add(j);
321 }
322 Console.Write("testSet({");
323 first = true;
324 foreach (int j in setout)
325 {
326 if (first)
327 {
328 first = false;
329 }
330 else
331 {
332 Console.Write(", ");
333 }
334 Console.Write(j);
335 }
336 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000337
Jens Geyerd5436f52014-10-03 19:50:38 +0200338 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000339
Jens Geyerd5436f52014-10-03 19:50:38 +0200340 Console.Write(" = {");
341 first = true;
342 foreach (int j in setin)
343 {
344 if (first)
345 {
346 first = false;
347 }
348 else
349 {
350 Console.Write(", ");
351 }
352 Console.Write(j);
353 }
354 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000355
356
Jens Geyerd5436f52014-10-03 19:50:38 +0200357 Console.Write("testEnum(ONE)");
358 Numberz ret = client.testEnum(Numberz.ONE);
359 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000360
Jens Geyerd5436f52014-10-03 19:50:38 +0200361 Console.Write("testEnum(TWO)");
362 ret = client.testEnum(Numberz.TWO);
363 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000364
Jens Geyerd5436f52014-10-03 19:50:38 +0200365 Console.Write("testEnum(THREE)");
366 ret = client.testEnum(Numberz.THREE);
367 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000368
Jens Geyerd5436f52014-10-03 19:50:38 +0200369 Console.Write("testEnum(FIVE)");
370 ret = client.testEnum(Numberz.FIVE);
371 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000372
Jens Geyerd5436f52014-10-03 19:50:38 +0200373 Console.Write("testEnum(EIGHT)");
374 ret = client.testEnum(Numberz.EIGHT);
375 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000376
Jens Geyerd5436f52014-10-03 19:50:38 +0200377 Console.Write("testTypedef(309858235082523)");
378 long uid = client.testTypedef(309858235082523L);
379 Console.WriteLine(" = " + uid);
David Reiss63191332009-01-06 19:49:22 +0000380
Jens Geyerd5436f52014-10-03 19:50:38 +0200381 Console.Write("testMapMap(1)");
382 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
383 Console.Write(" = {");
384 foreach (int key in mm.Keys)
385 {
386 Console.Write(key + " => {");
387 Dictionary<int, int> m2 = mm[key];
388 foreach (int k2 in m2.Keys)
389 {
390 Console.Write(k2 + " => " + m2[k2] + ", ");
391 }
392 Console.Write("}, ");
393 }
394 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000395
Jens Geyerd5436f52014-10-03 19:50:38 +0200396 Insanity insane = new Insanity();
397 insane.UserMap = new Dictionary<Numberz, long>();
398 insane.UserMap[Numberz.FIVE] = 5000L;
399 Xtruct truck = new Xtruct();
400 truck.String_thing = "Truck";
401 truck.Byte_thing = (sbyte)8;
402 truck.I32_thing = 8;
403 truck.I64_thing = 8;
404 insane.Xtructs = new List<Xtruct>();
405 insane.Xtructs.Add(truck);
406 Console.Write("testInsanity()");
407 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
408 Console.Write(" = {");
409 foreach (long key in whoa.Keys)
410 {
411 Dictionary<Numberz, Insanity> val = whoa[key];
412 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000413
Jens Geyerd5436f52014-10-03 19:50:38 +0200414 foreach (Numberz k2 in val.Keys)
415 {
416 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000417
Jens Geyerd5436f52014-10-03 19:50:38 +0200418 Console.Write(k2 + " => {");
419 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000420
Jens Geyerd5436f52014-10-03 19:50:38 +0200421 Console.Write("{");
422 if (userMap != null)
423 {
424 foreach (Numberz k3 in userMap.Keys)
425 {
426 Console.Write(k3 + " => " + userMap[k3] + ", ");
427 }
428 }
429 else
430 {
431 Console.Write("null");
432 }
433 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000434
Jens Geyerd5436f52014-10-03 19:50:38 +0200435 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000436
Jens Geyerd5436f52014-10-03 19:50:38 +0200437 Console.Write("{");
438 if (xtructs != null)
439 {
440 foreach (Xtruct x in xtructs)
441 {
442 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
443 }
444 }
445 else
446 {
447 Console.Write("null");
448 }
449 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000450
Jens Geyerd5436f52014-10-03 19:50:38 +0200451 Console.Write("}, ");
452 }
453 Console.Write("}, ");
454 }
455 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000456
Jens Geyerd5436f52014-10-03 19:50:38 +0200457 sbyte arg0 = 1;
458 int arg1 = 2;
459 long arg2 = long.MaxValue;
460 Dictionary<short, string> multiDict = new Dictionary<short, string>();
461 multiDict[1] = "one";
462 Numberz arg4 = Numberz.FIVE;
463 long arg5 = 5000000;
464 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
465 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
466 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
467 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000468
Jens Geyerd5436f52014-10-03 19:50:38 +0200469 Console.WriteLine("Test Oneway(1)");
470 client.testOneway(1);
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000471
Jens Geyerd5436f52014-10-03 19:50:38 +0200472 Console.Write("Test Calltime()");
473 var startt = DateTime.UtcNow;
474 for ( int k=0; k<1000; ++k )
475 client.testVoid();
476 Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" );
477 }
478 }
David Reiss63191332009-01-06 19:49:22 +0000479}