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