blob: bcdb00e5efdb461d8b44e36f553960b55b6ef989 [file] [log] [blame]
David Reiss63191332009-01-06 19:49:22 +00001using System;
2using System.Collections.Generic;
3using System.Text;
4
5using Thrift.Protocol;
6using Thrift.Transport;
7using Thrift.Test;
8using System.Threading;
9
10namespace Test
11{
12 public class TestClient
13 {
14 private static int numIterations = 1;
15
16 public static void Execute(string[] args)
17 {
18 try
19 {
20 string host = "localhost";
21 int port = 9090;
22 string url = null;
23 int numThreads = 1;
24 bool buffered = false;
25
26 try
27 {
28 for (int i = 0; i < args.Length; i++)
29 {
30 if (args[i] == "-h")
31 {
32 string[] hostport = args[++i].Split(':');
33 host = hostport[0];
34 if (hostport.Length > 1)
35 {
36 port = Convert.ToInt32(hostport[1]);
37 }
38 }
39 else if (args[i] == "-u")
40 {
41 url = args[++i];
42 }
43 else if (args[i] == "-n")
44 {
45 numIterations = Convert.ToInt32(args[++i]);
46 }
47 else if (args[i] == "-b" || args[i] == "-buffered")
48 {
49 buffered = true;
50 Console.WriteLine("Using buffered sockets");
51 }
52 else if (args[i] == "-t")
53 {
54 numThreads = Convert.ToInt32(args[++i]);
55 }
56 }
57 }
58 catch (Exception e)
59 {
60 Console.WriteLine(e.StackTrace);
61 }
62
63
64
65 //issue tests on separate threads simultaneously
66 Thread[] threads = new Thread[numThreads];
67 DateTime start = DateTime.Now;
68 for (int test = 0; test < numThreads; test++)
69 {
70 Thread t = new Thread(new ParameterizedThreadStart(ClientThread));
71 threads[test] = t;
72 TSocket socket = new TSocket(host, port);
73 if (buffered)
74 {
75 TBufferedTransport buffer = new TBufferedTransport(socket);
76 t.Start(buffer);
77 }
78 else
79 {
80 t.Start(socket);
81 }
82 }
83
84 for (int test = 0; test < numThreads; test++)
85 {
86 threads[test].Join();
87 }
88 Console.Write("Total time: " + (DateTime.Now - start));
89 }
90 catch (Exception outerEx)
91 {
92 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
93 }
94
95 Console.WriteLine();
96 Console.WriteLine();
97 }
98
99 public static void ClientThread(object obj)
100 {
101 TTransport transport = (TTransport)obj;
102 for (int i = 0; i < numIterations; i++)
103 {
104 ClientTest(transport);
105 }
106 transport.Close();
107 }
108
109 public static void ClientTest(TTransport transport)
110 {
111 TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport);
112
113 ThriftTest.Client client = new ThriftTest.Client(binaryProtocol);
114 try
115 {
116 if (!transport.IsOpen)
117 {
118 transport.Open();
119 }
120 }
121 catch (TTransportException ttx)
122 {
123 Console.WriteLine("Connect failed: " + ttx.Message);
124 return;
125 }
126
127 long start = DateTime.Now.ToFileTime();
128
129 Console.Write("testVoid()");
130 client.testVoid();
131 Console.WriteLine(" = void");
132
133 Console.Write("testString(\"Test\")");
134 string s = client.testString("Test");
135 Console.WriteLine(" = \"" + s + "\"");
136
137 Console.Write("testByte(1)");
138 byte i8 = client.testByte((byte)1);
139 Console.WriteLine(" = " + i8);
140
141 Console.Write("testI32(-1)");
142 int i32 = client.testI32(-1);
143 Console.WriteLine(" = " + i32);
144
145 Console.Write("testI64(-34359738368)");
146 long i64 = client.testI64(-34359738368);
147 Console.WriteLine(" = " + i64);
148
149 Console.Write("testDouble(5.325098235)");
150 double dub = client.testDouble(5.325098235);
151 Console.WriteLine(" = " + dub);
152
153 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
154 Xtruct o = new Xtruct();
155 o.String_thing = "Zero";
156 o.Byte_thing = (byte)1;
157 o.I32_thing = -3;
158 o.I64_thing = -5;
159 Xtruct i = client.testStruct(o);
160 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
161
162 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
163 Xtruct2 o2 = new Xtruct2();
164 o2.Byte_thing = (byte)1;
165 o2.Struct_thing = o;
166 o2.I32_thing = 5;
167 Xtruct2 i2 = client.testNest(o2);
168 i = i2.Struct_thing;
169 Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");
170
171 Dictionary<int, int> mapout = new Dictionary<int, int>();
172 for (int j = 0; j < 5; j++)
173 {
174 mapout[j] = j - 10;
175 }
176 Console.Write("testMap({");
177 bool first = true;
178 foreach (int key in mapout.Keys)
179 {
180 if (first)
181 {
182 first = false;
183 }
184 else
185 {
186 Console.Write(", ");
187 }
188 Console.Write(key + " => " + mapout[key]);
189 }
190 Console.Write("})");
191
192 Dictionary<int, int> mapin = client.testMap(mapout);
193
194 Console.Write(" = {");
195 first = true;
196 foreach (int key in mapin.Keys)
197 {
198 if (first)
199 {
200 first = false;
201 }
202 else
203 {
204 Console.Write(", ");
205 }
206 Console.Write(key + " => " + mapin[key]);
207 }
208 Console.WriteLine("}");
209
210 List<int> listout = new List<int>();
211 for (int j = -2; j < 3; j++)
212 {
213 listout.Add(j);
214 }
215 Console.Write("testList({");
216 first = true;
217 foreach (int j in listout)
218 {
219 if (first)
220 {
221 first = false;
222 }
223 else
224 {
225 Console.Write(", ");
226 }
227 Console.Write(j);
228 }
229 Console.Write("})");
230
231 List<int> listin = client.testList(listout);
232
233 Console.Write(" = {");
234 first = true;
235 foreach (int j in listin)
236 {
237 if (first)
238 {
239 first = false;
240 }
241 else
242 {
243 Console.Write(", ");
244 }
245 Console.Write(j);
246 }
247 Console.WriteLine("}");
248
249 //set
250 HashSet<int> setout = new HashSet<int>();
251 for (int j = -2; j < 3; j++)
252 {
253 setout.Add(j);
254 }
255 Console.Write("testSet({");
256 first = true;
257 foreach (int j in setout)
258 {
259 if (first)
260 {
261 first = false;
262 }
263 else
264 {
265 Console.Write(", ");
266 }
267 Console.Write(j);
268 }
269 Console.Write("})");
270
271 HashSet<int> setin = client.testSet(setout);
272
273 Console.Write(" = {");
274 first = true;
275 foreach (int j in setin)
276 {
277 if (first)
278 {
279 first = false;
280 }
281 else
282 {
283 Console.Write(", ");
284 }
285 Console.Write(j);
286 }
287 Console.WriteLine("}");
288
289
290 Console.Write("testEnum(ONE)");
291 Numberz ret = client.testEnum(Numberz.ONE);
292 Console.WriteLine(" = " + ret);
293
294 Console.Write("testEnum(TWO)");
295 ret = client.testEnum(Numberz.TWO);
296 Console.WriteLine(" = " + ret);
297
298 Console.Write("testEnum(THREE)");
299 ret = client.testEnum(Numberz.THREE);
300 Console.WriteLine(" = " + ret);
301
302 Console.Write("testEnum(FIVE)");
303 ret = client.testEnum(Numberz.FIVE);
304 Console.WriteLine(" = " + ret);
305
306 Console.Write("testEnum(EIGHT)");
307 ret = client.testEnum(Numberz.EIGHT);
308 Console.WriteLine(" = " + ret);
309
310 Console.Write("testTypedef(309858235082523)");
311 long uid = client.testTypedef(309858235082523L);
312 Console.WriteLine(" = " + uid);
313
314 Console.Write("testMapMap(1)");
315 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
316 Console.Write(" = {");
317 foreach (int key in mm.Keys)
318 {
319 Console.Write(key + " => {");
320 Dictionary<int, int> m2 = mm[key];
321 foreach (int k2 in m2.Keys)
322 {
323 Console.Write(k2 + " => " + m2[k2] + ", ");
324 }
325 Console.Write("}, ");
326 }
327 Console.WriteLine("}");
328
329 Insanity insane = new Insanity();
330 insane.UserMap = new Dictionary<Numberz, long>();
331 insane.UserMap[Numberz.FIVE] = 5000L;
332 Xtruct truck = new Xtruct();
333 truck.String_thing = "Truck";
334 truck.Byte_thing = (byte)8;
335 truck.I32_thing = 8;
336 truck.I64_thing = 8;
337 insane.Xtructs = new List<Xtruct>();
338 insane.Xtructs.Add(truck);
339 Console.Write("testInsanity()");
340 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
341 Console.Write(" = {");
342 foreach (long key in whoa.Keys)
343 {
344 Dictionary<Numberz, Insanity> val = whoa[key];
345 Console.Write(key + " => {");
346
347 foreach (Numberz k2 in val.Keys)
348 {
349 Insanity v2 = val[k2];
350
351 Console.Write(k2 + " => {");
352 Dictionary<Numberz, long> userMap = v2.UserMap;
353
354 Console.Write("{");
355 if (userMap != null)
356 {
357 foreach (Numberz k3 in userMap.Keys)
358 {
359 Console.Write(k3 + " => " + userMap[k3] + ", ");
360 }
361 }
362 else
363 {
364 Console.Write("null");
365 }
366 Console.Write("}, ");
367
368 List<Xtruct> xtructs = v2.Xtructs;
369
370 Console.Write("{");
371 if (xtructs != null)
372 {
373 foreach (Xtruct x in xtructs)
374 {
375 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
376 }
377 }
378 else
379 {
380 Console.Write("null");
381 }
382 Console.Write("}");
383
384 Console.Write("}, ");
385 }
386 Console.Write("}, ");
387 }
388 Console.WriteLine("}");
389
390
391 byte arg0 = 1;
392 int arg1 = 2;
393 long arg2 = long.MaxValue;
394 Dictionary<short, string> multiDict = new Dictionary<short, string>();
395 multiDict[1] = "one";
396 Numberz arg4 = Numberz.FIVE;
397 long arg5 = 5000000;
398 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
399 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
400 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
401 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
402
403 Console.WriteLine("Test Async(1)");
404 client.testAsync(1);
405 }
406 }
407}