blob: ba2d4d07e2d78d7782f20447b4473c8350a33626 [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{
30 public class TestClient
31 {
32 private static int numIterations = 1;
33
34 public static void Execute(string[] args)
35 {
36 try
37 {
38 string host = "localhost";
39 int port = 9090;
Jens Geyerfd62df72014-03-20 00:52:18 +020040 string url = null, pipe = null;
David Reiss63191332009-01-06 19:49:22 +000041 int numThreads = 1;
T Jake Luciani7070aaa2011-01-27 02:51:51 +000042 bool buffered = false, framed = false;
David Reiss63191332009-01-06 19:49:22 +000043
44 try
45 {
46 for (int i = 0; i < args.Length; i++)
47 {
48 if (args[i] == "-h")
49 {
50 string[] hostport = args[++i].Split(':');
51 host = hostport[0];
52 if (hostport.Length > 1)
53 {
54 port = Convert.ToInt32(hostport[1]);
55 }
56 }
57 else if (args[i] == "-u")
58 {
59 url = args[++i];
60 }
61 else if (args[i] == "-n")
62 {
63 numIterations = Convert.ToInt32(args[++i]);
64 }
65 else if (args[i] == "-b" || args[i] == "-buffered")
66 {
67 buffered = true;
68 Console.WriteLine("Using buffered sockets");
69 }
T Jake Luciani7070aaa2011-01-27 02:51:51 +000070 else if (args[i] == "-f" || args[i] == "-framed")
71 {
72 framed = true;
73 Console.WriteLine("Using framed transport");
74 }
Jens Geyerfd62df72014-03-20 00:52:18 +020075 else if (args[i] == "-pipe") // -pipe <name>
76 {
77 pipe = args[++i];
78 Console.WriteLine("Using named pipes transport");
79 }
David Reiss63191332009-01-06 19:49:22 +000080 else if (args[i] == "-t")
81 {
82 numThreads = Convert.ToInt32(args[++i]);
83 }
84 }
85 }
86 catch (Exception e)
87 {
88 Console.WriteLine(e.StackTrace);
89 }
90
91
92
93 //issue tests on separate threads simultaneously
94 Thread[] threads = new Thread[numThreads];
95 DateTime start = DateTime.Now;
96 for (int test = 0; test < numThreads; test++)
97 {
98 Thread t = new Thread(new ParameterizedThreadStart(ClientThread));
99 threads[test] = t;
Bryan Duxbury62359472010-06-24 20:34:34 +0000100 if (url == null)
David Reiss63191332009-01-06 19:49:22 +0000101 {
Jens Geyerfd62df72014-03-20 00:52:18 +0200102 // endpoint transport
103 TTransport trans = null;
104 if( pipe != null)
105 trans = new TNamedPipeClientTransport(pipe);
106 else
107 trans = new TSocket(host, port);
108
109 // layered transport
Bryan Duxbury62359472010-06-24 20:34:34 +0000110 if (buffered)
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000111 trans = new TBufferedTransport(trans as TStreamTransport);
112 if (framed)
113 trans = new TFramedTransport(trans);
Jens Geyerf6acf442014-01-02 22:58:43 +0100114
115 //ensure proper open/close of transport
116 trans.Open();
117 trans.Close();
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000118 t.Start(trans);
David Reiss63191332009-01-06 19:49:22 +0000119 }
120 else
121 {
Bryan Duxbury62359472010-06-24 20:34:34 +0000122 THttpClient http = new THttpClient(new Uri(url));
123 t.Start(http);
David Reiss63191332009-01-06 19:49:22 +0000124 }
125 }
126
127 for (int test = 0; test < numThreads; test++)
128 {
129 threads[test].Join();
130 }
131 Console.Write("Total time: " + (DateTime.Now - start));
132 }
133 catch (Exception outerEx)
134 {
135 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
136 }
137
138 Console.WriteLine();
139 Console.WriteLine();
140 }
141
142 public static void ClientThread(object obj)
143 {
144 TTransport transport = (TTransport)obj;
145 for (int i = 0; i < numIterations; i++)
146 {
147 ClientTest(transport);
148 }
149 transport.Close();
150 }
151
152 public static void ClientTest(TTransport transport)
153 {
154 TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport);
155
156 ThriftTest.Client client = new ThriftTest.Client(binaryProtocol);
157 try
158 {
159 if (!transport.IsOpen)
160 {
161 transport.Open();
162 }
163 }
164 catch (TTransportException ttx)
165 {
166 Console.WriteLine("Connect failed: " + ttx.Message);
167 return;
168 }
169
170 long start = DateTime.Now.ToFileTime();
171
172 Console.Write("testVoid()");
173 client.testVoid();
174 Console.WriteLine(" = void");
175
176 Console.Write("testString(\"Test\")");
177 string s = client.testString("Test");
178 Console.WriteLine(" = \"" + s + "\"");
179
180 Console.Write("testByte(1)");
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200181 sbyte i8 = client.testByte((sbyte)1);
David Reiss63191332009-01-06 19:49:22 +0000182 Console.WriteLine(" = " + i8);
183
184 Console.Write("testI32(-1)");
185 int i32 = client.testI32(-1);
186 Console.WriteLine(" = " + i32);
187
188 Console.Write("testI64(-34359738368)");
189 long i64 = client.testI64(-34359738368);
190 Console.WriteLine(" = " + i64);
191
192 Console.Write("testDouble(5.325098235)");
193 double dub = client.testDouble(5.325098235);
194 Console.WriteLine(" = " + dub);
195
196 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
197 Xtruct o = new Xtruct();
198 o.String_thing = "Zero";
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200199 o.Byte_thing = (sbyte)1;
David Reiss63191332009-01-06 19:49:22 +0000200 o.I32_thing = -3;
201 o.I64_thing = -5;
202 Xtruct i = client.testStruct(o);
203 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
204
205 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
206 Xtruct2 o2 = new Xtruct2();
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200207 o2.Byte_thing = (sbyte)1;
David Reiss63191332009-01-06 19:49:22 +0000208 o2.Struct_thing = o;
209 o2.I32_thing = 5;
210 Xtruct2 i2 = client.testNest(o2);
211 i = i2.Struct_thing;
212 Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");
213
214 Dictionary<int, int> mapout = new Dictionary<int, int>();
215 for (int j = 0; j < 5; j++)
216 {
217 mapout[j] = j - 10;
218 }
219 Console.Write("testMap({");
220 bool first = true;
221 foreach (int key in mapout.Keys)
222 {
223 if (first)
224 {
225 first = false;
226 }
227 else
228 {
229 Console.Write(", ");
230 }
231 Console.Write(key + " => " + mapout[key]);
232 }
233 Console.Write("})");
234
235 Dictionary<int, int> mapin = client.testMap(mapout);
236
237 Console.Write(" = {");
238 first = true;
239 foreach (int key in mapin.Keys)
240 {
241 if (first)
242 {
243 first = false;
244 }
245 else
246 {
247 Console.Write(", ");
248 }
249 Console.Write(key + " => " + mapin[key]);
250 }
251 Console.WriteLine("}");
252
253 List<int> listout = new List<int>();
254 for (int j = -2; j < 3; j++)
255 {
256 listout.Add(j);
257 }
258 Console.Write("testList({");
259 first = true;
260 foreach (int j in listout)
261 {
262 if (first)
263 {
264 first = false;
265 }
266 else
267 {
268 Console.Write(", ");
269 }
270 Console.Write(j);
271 }
272 Console.Write("})");
273
274 List<int> listin = client.testList(listout);
275
276 Console.Write(" = {");
277 first = true;
278 foreach (int j in listin)
279 {
280 if (first)
281 {
282 first = false;
283 }
284 else
285 {
286 Console.Write(", ");
287 }
288 Console.Write(j);
289 }
290 Console.WriteLine("}");
291
292 //set
David Reissd831a212009-02-13 03:09:52 +0000293 THashSet<int> setout = new THashSet<int>();
David Reiss63191332009-01-06 19:49:22 +0000294 for (int j = -2; j < 3; j++)
295 {
296 setout.Add(j);
297 }
298 Console.Write("testSet({");
299 first = true;
300 foreach (int j in setout)
301 {
302 if (first)
303 {
304 first = false;
305 }
306 else
307 {
308 Console.Write(", ");
309 }
310 Console.Write(j);
311 }
312 Console.Write("})");
313
David Reissd831a212009-02-13 03:09:52 +0000314 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000315
316 Console.Write(" = {");
317 first = true;
318 foreach (int j in setin)
319 {
320 if (first)
321 {
322 first = false;
323 }
324 else
325 {
326 Console.Write(", ");
327 }
328 Console.Write(j);
329 }
330 Console.WriteLine("}");
331
332
333 Console.Write("testEnum(ONE)");
334 Numberz ret = client.testEnum(Numberz.ONE);
335 Console.WriteLine(" = " + ret);
336
337 Console.Write("testEnum(TWO)");
338 ret = client.testEnum(Numberz.TWO);
339 Console.WriteLine(" = " + ret);
340
341 Console.Write("testEnum(THREE)");
342 ret = client.testEnum(Numberz.THREE);
343 Console.WriteLine(" = " + ret);
344
345 Console.Write("testEnum(FIVE)");
346 ret = client.testEnum(Numberz.FIVE);
347 Console.WriteLine(" = " + ret);
348
349 Console.Write("testEnum(EIGHT)");
350 ret = client.testEnum(Numberz.EIGHT);
351 Console.WriteLine(" = " + ret);
352
353 Console.Write("testTypedef(309858235082523)");
354 long uid = client.testTypedef(309858235082523L);
355 Console.WriteLine(" = " + uid);
356
357 Console.Write("testMapMap(1)");
358 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
359 Console.Write(" = {");
360 foreach (int key in mm.Keys)
361 {
362 Console.Write(key + " => {");
363 Dictionary<int, int> m2 = mm[key];
364 foreach (int k2 in m2.Keys)
365 {
366 Console.Write(k2 + " => " + m2[k2] + ", ");
367 }
368 Console.Write("}, ");
369 }
370 Console.WriteLine("}");
371
372 Insanity insane = new Insanity();
373 insane.UserMap = new Dictionary<Numberz, long>();
374 insane.UserMap[Numberz.FIVE] = 5000L;
375 Xtruct truck = new Xtruct();
376 truck.String_thing = "Truck";
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200377 truck.Byte_thing = (sbyte)8;
David Reiss63191332009-01-06 19:49:22 +0000378 truck.I32_thing = 8;
379 truck.I64_thing = 8;
380 insane.Xtructs = new List<Xtruct>();
381 insane.Xtructs.Add(truck);
382 Console.Write("testInsanity()");
383 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
384 Console.Write(" = {");
385 foreach (long key in whoa.Keys)
386 {
387 Dictionary<Numberz, Insanity> val = whoa[key];
388 Console.Write(key + " => {");
389
390 foreach (Numberz k2 in val.Keys)
391 {
392 Insanity v2 = val[k2];
393
394 Console.Write(k2 + " => {");
395 Dictionary<Numberz, long> userMap = v2.UserMap;
396
397 Console.Write("{");
398 if (userMap != null)
399 {
400 foreach (Numberz k3 in userMap.Keys)
401 {
402 Console.Write(k3 + " => " + userMap[k3] + ", ");
403 }
404 }
405 else
406 {
407 Console.Write("null");
408 }
409 Console.Write("}, ");
410
411 List<Xtruct> xtructs = v2.Xtructs;
412
413 Console.Write("{");
414 if (xtructs != null)
415 {
416 foreach (Xtruct x in xtructs)
417 {
418 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
419 }
420 }
421 else
422 {
423 Console.Write("null");
424 }
425 Console.Write("}");
426
427 Console.Write("}, ");
428 }
429 Console.Write("}, ");
430 }
431 Console.WriteLine("}");
432
433
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200434 sbyte arg0 = 1;
David Reiss63191332009-01-06 19:49:22 +0000435 int arg1 = 2;
436 long arg2 = long.MaxValue;
437 Dictionary<short, string> multiDict = new Dictionary<short, string>();
438 multiDict[1] = "one";
439 Numberz arg4 = Numberz.FIVE;
440 long arg5 = 5000000;
441 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
442 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
443 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
444 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
445
David Reiss6ce401d2009-03-24 20:01:58 +0000446 Console.WriteLine("Test Oneway(1)");
447 client.testOneway(1);
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000448
449 Console.Write("Test Calltime()");
450 var startt = DateTime.UtcNow;
451 for ( int k=0; k<1000; ++k )
452 client.testVoid();
453 Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" );
David Reiss63191332009-01-06 19:49:22 +0000454 }
455 }
456}