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