blob: 1d7c75ea8313ba19cc49be030192ce6cc4acdd0b [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;
40 string url = null;
41 int numThreads = 1;
42 bool buffered = false;
43
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 }
70 else if (args[i] == "-t")
71 {
72 numThreads = Convert.ToInt32(args[++i]);
73 }
74 }
75 }
76 catch (Exception e)
77 {
78 Console.WriteLine(e.StackTrace);
79 }
80
81
82
83 //issue tests on separate threads simultaneously
84 Thread[] threads = new Thread[numThreads];
85 DateTime start = DateTime.Now;
86 for (int test = 0; test < numThreads; test++)
87 {
88 Thread t = new Thread(new ParameterizedThreadStart(ClientThread));
89 threads[test] = t;
Bryan Duxbury62359472010-06-24 20:34:34 +000090 if (url == null)
David Reiss63191332009-01-06 19:49:22 +000091 {
Bryan Duxbury62359472010-06-24 20:34:34 +000092 TSocket socket = new TSocket(host, port);
93 if (buffered)
94 {
95 TBufferedTransport buffer = new TBufferedTransport(socket);
96 t.Start(buffer);
97 }
98 else
99 {
100 t.Start(socket);
101 }
David Reiss63191332009-01-06 19:49:22 +0000102 }
103 else
104 {
Bryan Duxbury62359472010-06-24 20:34:34 +0000105 THttpClient http = new THttpClient(new Uri(url));
106 t.Start(http);
David Reiss63191332009-01-06 19:49:22 +0000107 }
108 }
109
110 for (int test = 0; test < numThreads; test++)
111 {
112 threads[test].Join();
113 }
114 Console.Write("Total time: " + (DateTime.Now - start));
115 }
116 catch (Exception outerEx)
117 {
118 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
119 }
120
121 Console.WriteLine();
122 Console.WriteLine();
123 }
124
125 public static void ClientThread(object obj)
126 {
127 TTransport transport = (TTransport)obj;
128 for (int i = 0; i < numIterations; i++)
129 {
130 ClientTest(transport);
131 }
132 transport.Close();
133 }
134
135 public static void ClientTest(TTransport transport)
136 {
137 TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport);
138
139 ThriftTest.Client client = new ThriftTest.Client(binaryProtocol);
140 try
141 {
142 if (!transport.IsOpen)
143 {
144 transport.Open();
145 }
146 }
147 catch (TTransportException ttx)
148 {
149 Console.WriteLine("Connect failed: " + ttx.Message);
150 return;
151 }
152
153 long start = DateTime.Now.ToFileTime();
154
155 Console.Write("testVoid()");
156 client.testVoid();
157 Console.WriteLine(" = void");
158
159 Console.Write("testString(\"Test\")");
160 string s = client.testString("Test");
161 Console.WriteLine(" = \"" + s + "\"");
162
163 Console.Write("testByte(1)");
164 byte i8 = client.testByte((byte)1);
165 Console.WriteLine(" = " + i8);
166
167 Console.Write("testI32(-1)");
168 int i32 = client.testI32(-1);
169 Console.WriteLine(" = " + i32);
170
171 Console.Write("testI64(-34359738368)");
172 long i64 = client.testI64(-34359738368);
173 Console.WriteLine(" = " + i64);
174
175 Console.Write("testDouble(5.325098235)");
176 double dub = client.testDouble(5.325098235);
177 Console.WriteLine(" = " + dub);
178
179 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
180 Xtruct o = new Xtruct();
181 o.String_thing = "Zero";
182 o.Byte_thing = (byte)1;
183 o.I32_thing = -3;
184 o.I64_thing = -5;
185 Xtruct i = client.testStruct(o);
186 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
187
188 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
189 Xtruct2 o2 = new Xtruct2();
190 o2.Byte_thing = (byte)1;
191 o2.Struct_thing = o;
192 o2.I32_thing = 5;
193 Xtruct2 i2 = client.testNest(o2);
194 i = i2.Struct_thing;
195 Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");
196
197 Dictionary<int, int> mapout = new Dictionary<int, int>();
198 for (int j = 0; j < 5; j++)
199 {
200 mapout[j] = j - 10;
201 }
202 Console.Write("testMap({");
203 bool first = true;
204 foreach (int key in mapout.Keys)
205 {
206 if (first)
207 {
208 first = false;
209 }
210 else
211 {
212 Console.Write(", ");
213 }
214 Console.Write(key + " => " + mapout[key]);
215 }
216 Console.Write("})");
217
218 Dictionary<int, int> mapin = client.testMap(mapout);
219
220 Console.Write(" = {");
221 first = true;
222 foreach (int key in mapin.Keys)
223 {
224 if (first)
225 {
226 first = false;
227 }
228 else
229 {
230 Console.Write(", ");
231 }
232 Console.Write(key + " => " + mapin[key]);
233 }
234 Console.WriteLine("}");
235
236 List<int> listout = new List<int>();
237 for (int j = -2; j < 3; j++)
238 {
239 listout.Add(j);
240 }
241 Console.Write("testList({");
242 first = true;
243 foreach (int j in listout)
244 {
245 if (first)
246 {
247 first = false;
248 }
249 else
250 {
251 Console.Write(", ");
252 }
253 Console.Write(j);
254 }
255 Console.Write("})");
256
257 List<int> listin = client.testList(listout);
258
259 Console.Write(" = {");
260 first = true;
261 foreach (int j in listin)
262 {
263 if (first)
264 {
265 first = false;
266 }
267 else
268 {
269 Console.Write(", ");
270 }
271 Console.Write(j);
272 }
273 Console.WriteLine("}");
274
275 //set
David Reissd831a212009-02-13 03:09:52 +0000276 THashSet<int> setout = new THashSet<int>();
David Reiss63191332009-01-06 19:49:22 +0000277 for (int j = -2; j < 3; j++)
278 {
279 setout.Add(j);
280 }
281 Console.Write("testSet({");
282 first = true;
283 foreach (int j in setout)
284 {
285 if (first)
286 {
287 first = false;
288 }
289 else
290 {
291 Console.Write(", ");
292 }
293 Console.Write(j);
294 }
295 Console.Write("})");
296
David Reissd831a212009-02-13 03:09:52 +0000297 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000298
299 Console.Write(" = {");
300 first = true;
301 foreach (int j in setin)
302 {
303 if (first)
304 {
305 first = false;
306 }
307 else
308 {
309 Console.Write(", ");
310 }
311 Console.Write(j);
312 }
313 Console.WriteLine("}");
314
315
316 Console.Write("testEnum(ONE)");
317 Numberz ret = client.testEnum(Numberz.ONE);
318 Console.WriteLine(" = " + ret);
319
320 Console.Write("testEnum(TWO)");
321 ret = client.testEnum(Numberz.TWO);
322 Console.WriteLine(" = " + ret);
323
324 Console.Write("testEnum(THREE)");
325 ret = client.testEnum(Numberz.THREE);
326 Console.WriteLine(" = " + ret);
327
328 Console.Write("testEnum(FIVE)");
329 ret = client.testEnum(Numberz.FIVE);
330 Console.WriteLine(" = " + ret);
331
332 Console.Write("testEnum(EIGHT)");
333 ret = client.testEnum(Numberz.EIGHT);
334 Console.WriteLine(" = " + ret);
335
336 Console.Write("testTypedef(309858235082523)");
337 long uid = client.testTypedef(309858235082523L);
338 Console.WriteLine(" = " + uid);
339
340 Console.Write("testMapMap(1)");
341 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
342 Console.Write(" = {");
343 foreach (int key in mm.Keys)
344 {
345 Console.Write(key + " => {");
346 Dictionary<int, int> m2 = mm[key];
347 foreach (int k2 in m2.Keys)
348 {
349 Console.Write(k2 + " => " + m2[k2] + ", ");
350 }
351 Console.Write("}, ");
352 }
353 Console.WriteLine("}");
354
355 Insanity insane = new Insanity();
356 insane.UserMap = new Dictionary<Numberz, long>();
357 insane.UserMap[Numberz.FIVE] = 5000L;
358 Xtruct truck = new Xtruct();
359 truck.String_thing = "Truck";
360 truck.Byte_thing = (byte)8;
361 truck.I32_thing = 8;
362 truck.I64_thing = 8;
363 insane.Xtructs = new List<Xtruct>();
364 insane.Xtructs.Add(truck);
365 Console.Write("testInsanity()");
366 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
367 Console.Write(" = {");
368 foreach (long key in whoa.Keys)
369 {
370 Dictionary<Numberz, Insanity> val = whoa[key];
371 Console.Write(key + " => {");
372
373 foreach (Numberz k2 in val.Keys)
374 {
375 Insanity v2 = val[k2];
376
377 Console.Write(k2 + " => {");
378 Dictionary<Numberz, long> userMap = v2.UserMap;
379
380 Console.Write("{");
381 if (userMap != null)
382 {
383 foreach (Numberz k3 in userMap.Keys)
384 {
385 Console.Write(k3 + " => " + userMap[k3] + ", ");
386 }
387 }
388 else
389 {
390 Console.Write("null");
391 }
392 Console.Write("}, ");
393
394 List<Xtruct> xtructs = v2.Xtructs;
395
396 Console.Write("{");
397 if (xtructs != null)
398 {
399 foreach (Xtruct x in xtructs)
400 {
401 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
402 }
403 }
404 else
405 {
406 Console.Write("null");
407 }
408 Console.Write("}");
409
410 Console.Write("}, ");
411 }
412 Console.Write("}, ");
413 }
414 Console.WriteLine("}");
415
416
417 byte arg0 = 1;
418 int arg1 = 2;
419 long arg2 = long.MaxValue;
420 Dictionary<short, string> multiDict = new Dictionary<short, string>();
421 multiDict[1] = "one";
422 Numberz arg4 = Numberz.FIVE;
423 long arg5 = 5000000;
424 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
425 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
426 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
427 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
428
David Reiss6ce401d2009-03-24 20:01:58 +0000429 Console.WriteLine("Test Oneway(1)");
430 client.testOneway(1);
David Reiss63191332009-01-06 19:49:22 +0000431 }
432 }
433}