T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 4 | # Licensed to the Apache Software Foundation (ASF) under one |
| 5 | # or more contributor license agreements. See the NOTICE file |
| 6 | # distributed with this work for additional information |
| 7 | # regarding copyright ownership. The ASF licenses this file |
| 8 | # to you under the Apache License, Version 2.0 (the |
| 9 | # "License"); you may not use this file except in compliance |
| 10 | # with the License. You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, |
| 15 | # software distributed under the License is distributed on an |
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | # KIND, either express or implied. See the License for the |
| 18 | # specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 22 | require 5.6.0; |
| 23 | use strict; |
| 24 | use warnings; |
| 25 | use Data::Dumper; |
| 26 | use Time::HiRes qw(gettimeofday); |
| 27 | |
| 28 | use lib '../../lib/perl/lib'; |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 29 | use lib 'gen-perl'; |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 30 | |
| 31 | use Thrift; |
| 32 | use Thrift::BinaryProtocol; |
| 33 | use Thrift::Socket; |
| 34 | use Thrift::BufferedTransport; |
| 35 | |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 36 | use ThriftTest::ThriftTest; |
Mark Slee | 27ed6ec | 2007-08-16 01:26:31 +0000 | [diff] [blame] | 37 | use ThriftTest::Types; |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 38 | |
| 39 | $|++; |
| 40 | |
| 41 | my $host = 'localhost'; |
| 42 | my $port = 9090; |
| 43 | |
| 44 | |
| 45 | my $socket = new Thrift::Socket($host, $port); |
| 46 | |
| 47 | my $bufferedSocket = new Thrift::BufferedTransport($socket, 1024, 1024); |
| 48 | my $transport = $bufferedSocket; |
| 49 | my $protocol = new Thrift::BinaryProtocol($transport); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 50 | my $testClient = new ThriftTest::ThriftTestClient($protocol); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 51 | |
| 52 | eval{ |
| 53 | $transport->open(); |
| 54 | }; if($@){ |
| 55 | die(Dumper($@)); |
| 56 | } |
| 57 | my $start = gettimeofday(); |
| 58 | |
| 59 | # |
| 60 | # VOID TEST |
| 61 | # |
| 62 | print("testVoid()"); |
| 63 | $testClient->testVoid(); |
| 64 | print(" = void\n"); |
| 65 | |
| 66 | # |
| 67 | # STRING TEST |
| 68 | # |
| 69 | print("testString(\"Test\")"); |
| 70 | my $s = $testClient->testString("Test"); |
| 71 | print(" = \"$s\"\n"); |
| 72 | |
| 73 | # |
| 74 | # BYTE TEST |
| 75 | # |
| 76 | print("testByte(1)"); |
| 77 | my $u8 = $testClient->testByte(1); |
| 78 | print(" = $u8\n"); |
| 79 | |
| 80 | # |
| 81 | # I32 TEST |
| 82 | # |
| 83 | print("testI32(-1)"); |
| 84 | my $i32 = $testClient->testI32(-1); |
| 85 | print(" = $i32\n"); |
| 86 | |
| 87 | # |
| 88 | #I64 TEST |
| 89 | # |
| 90 | print("testI64(-34359738368)"); |
| 91 | my $i64 = $testClient->testI64(-34359738368); |
| 92 | print(" = $i64\n"); |
| 93 | |
| 94 | # |
| 95 | # DOUBLE TEST |
| 96 | # |
| 97 | print("testDouble(-852.234234234)"); |
| 98 | my $dub = $testClient->testDouble(-852.234234234); |
| 99 | print(" = $dub\n"); |
| 100 | |
| 101 | # |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 102 | # BINARY TEST --- TODO |
| 103 | # |
| 104 | |
| 105 | |
| 106 | # |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 107 | # STRUCT TEST |
| 108 | # |
| 109 | print("testStruct({\"Zero\", 1, -3, -5})"); |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 110 | my $out = new ThriftTest::Xtruct(); |
| 111 | $out->string_thing("Zero"); |
| 112 | $out->byte_thing(1); |
| 113 | $out->i32_thing(-3); |
| 114 | $out->i64_thing(-5); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 115 | my $in = $testClient->testStruct($out); |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 116 | print(" = {\"".$in->string_thing."\", ". |
| 117 | $in->byte_thing.", ". |
| 118 | $in->i32_thing.", ". |
| 119 | $in->i64_thing."}\n"); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 120 | |
| 121 | # |
| 122 | # NESTED STRUCT TEST |
| 123 | # |
| 124 | print("testNest({1, {\"Zero\", 1, -3, -5}, 5}"); |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 125 | my $out2 = new ThriftTest::Xtruct2(); |
| 126 | $out2->byte_thing(1); |
| 127 | $out2->struct_thing($out); |
| 128 | $out2->i32_thing(5); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 129 | my $in2 = $testClient->testNest($out2); |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 130 | $in = $in2->struct_thing; |
| 131 | print(" = {".$in2->byte_thing.", {\"". |
| 132 | $in->string_thing."\", ". |
| 133 | $in->byte_thing.", ". |
| 134 | $in->i32_thing.", ". |
| 135 | $in->i64_thing."}, ". |
| 136 | $in2->i32_thing."}\n"); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 137 | |
| 138 | # |
| 139 | # MAP TEST |
| 140 | # |
| 141 | my $mapout = {}; |
| 142 | for (my $i = 0; $i < 5; ++$i) { |
| 143 | $mapout->{$i} = $i-10; |
| 144 | } |
| 145 | print("testMap({"); |
| 146 | my $first = 1; |
| 147 | while( my($key,$val) = each %$mapout) { |
| 148 | if ($first) { |
| 149 | $first = 0; |
| 150 | } else { |
| 151 | print(", "); |
| 152 | } |
| 153 | print("$key => $val"); |
| 154 | } |
| 155 | print("})"); |
| 156 | |
| 157 | |
| 158 | my $mapin = $testClient->testMap($mapout); |
| 159 | print(" = {"); |
| 160 | |
| 161 | $first = 1; |
| 162 | while( my($key,$val) = each %$mapin){ |
| 163 | if ($first) { |
| 164 | $first = 0; |
| 165 | } else { |
| 166 | print(", "); |
| 167 | } |
| 168 | print("$key => $val"); |
| 169 | } |
| 170 | print("}\n"); |
| 171 | |
| 172 | # |
| 173 | # SET TEST |
| 174 | # |
| 175 | my $setout = []; |
| 176 | for (my $i = -2; $i < 3; ++$i) { |
| 177 | push(@$setout, $i); |
| 178 | } |
| 179 | |
| 180 | print("testSet({".join(",",@$setout)."})"); |
| 181 | |
| 182 | my $setin = $testClient->testSet($setout); |
| 183 | |
| 184 | print(" = {".join(",",@$setout)."}\n"); |
| 185 | |
| 186 | # |
| 187 | # LIST TEST |
| 188 | # |
| 189 | my $listout = []; |
| 190 | for (my $i = -2; $i < 3; ++$i) { |
| 191 | push(@$listout, $i); |
| 192 | } |
| 193 | |
| 194 | print("testList({".join(",",@$listout)."})"); |
| 195 | |
| 196 | my $listin = $testClient->testList($listout); |
| 197 | |
| 198 | print(" = {".join(",",@$listin)."}\n"); |
| 199 | |
| 200 | # |
| 201 | # ENUM TEST |
| 202 | # |
| 203 | print("testEnum(ONE)"); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 204 | my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 205 | print(" = $ret\n"); |
| 206 | |
| 207 | print("testEnum(TWO)"); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 208 | $ret = $testClient->testEnum(ThriftTest::Numberz::TWO); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 209 | print(" = $ret\n"); |
| 210 | |
| 211 | print("testEnum(THREE)"); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 212 | $ret = $testClient->testEnum(ThriftTest::Numberz::THREE); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 213 | print(" = $ret\n"); |
| 214 | |
| 215 | print("testEnum(FIVE)"); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 216 | $ret = $testClient->testEnum(ThriftTest::Numberz::FIVE); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 217 | print(" = $ret\n"); |
| 218 | |
| 219 | print("testEnum(EIGHT)"); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 220 | $ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 221 | print(" = $ret\n"); |
| 222 | |
| 223 | # |
| 224 | # TYPEDEF TEST |
| 225 | # |
| 226 | print("testTypedef(309858235082523)"); |
| 227 | my $uid = $testClient->testTypedef(309858235082523); |
| 228 | print(" = $uid\n"); |
| 229 | |
| 230 | # |
| 231 | # NESTED MAP TEST |
| 232 | # |
| 233 | print("testMapMap(1)"); |
| 234 | my $mm = $testClient->testMapMap(1); |
| 235 | print(" = {"); |
| 236 | while( my ($key,$val) = each %$mm) { |
| 237 | print("$key => {"); |
| 238 | while( my($k2,$v2) = each %$val) { |
| 239 | print("$k2 => $v2, "); |
| 240 | } |
| 241 | print("}, "); |
| 242 | } |
| 243 | print("}\n"); |
| 244 | |
| 245 | # |
| 246 | # INSANITY TEST |
| 247 | # |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 248 | my $insane = new ThriftTest::Insanity(); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 249 | $insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000; |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 250 | my $truck = new ThriftTest::Xtruct(); |
| 251 | $truck->string_thing("Truck"); |
| 252 | $truck->byte_thing(8); |
| 253 | $truck->i32_thing(8); |
| 254 | $truck->i64_thing(8); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 255 | push(@{$insane->{xtructs}}, $truck); |
| 256 | |
| 257 | print("testInsanity()"); |
| 258 | my $whoa = $testClient->testInsanity($insane); |
| 259 | print(" = {"); |
| 260 | while( my ($key,$val) = each %$whoa) { |
| 261 | print("$key => {"); |
| 262 | while( my($k2,$v2) = each %$val) { |
| 263 | print("$k2 => {"); |
| 264 | my $userMap = $v2->{userMap}; |
| 265 | print("{"); |
| 266 | if (ref($userMap) eq "HASH") { |
| 267 | while( my($k3,$v3) = each %$userMap) { |
| 268 | print("$k3 => $v3, "); |
| 269 | } |
| 270 | } |
| 271 | print("}, "); |
| 272 | |
| 273 | my $xtructs = $v2->{xtructs}; |
| 274 | print("{"); |
| 275 | if (ref($xtructs) eq "ARRAY") { |
| 276 | foreach my $x (@$xtructs) { |
| 277 | print("{\"".$x->{string_thing}."\", ". |
| 278 | $x->{byte_thing}.", ".$x->{i32_thing}.", ".$x->{i64_thing}."}, "); |
| 279 | } |
| 280 | } |
| 281 | print("}"); |
| 282 | |
| 283 | print("}, "); |
| 284 | } |
| 285 | print("}, "); |
| 286 | } |
| 287 | print("}\n"); |
| 288 | |
| 289 | # |
| 290 | # EXCEPTION TEST |
| 291 | # |
| 292 | print("testException('Xception')"); |
| 293 | eval { |
| 294 | $testClient->testException('Xception'); |
| 295 | print(" void\nFAILURE\n"); |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 296 | }; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) { |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 297 | print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n"); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | # |
| 302 | # Normal tests done. |
| 303 | # |
| 304 | my $stop = gettimeofday(); |
| 305 | my $elp = sprintf("%d",1000*($stop - $start), 0); |
| 306 | print("Total time: $elp ms\n"); |
| 307 | |
| 308 | # |
| 309 | # Extraneous "I don't trust PHP to pack/unpack integer" tests |
| 310 | # |
| 311 | |
| 312 | # Max I32 |
| 313 | my $num = 2**30 + 2**30 - 1; |
| 314 | my $num2 = $testClient->testI32($num); |
| 315 | if ($num != $num2) { |
| 316 | print "Missed max32 $num = $num2\n"; |
| 317 | } |
| 318 | |
| 319 | # Min I32 |
| 320 | $num = 0 - 2**31; |
| 321 | $num2 = $testClient->testI32($num); |
| 322 | if ($num != $num2) { |
| 323 | print "Missed min32 $num = $num2\n"; |
| 324 | } |
| 325 | |
| 326 | # Max Number I can get out of my perl |
| 327 | $num = 2**40; |
| 328 | $num2 = $testClient->testI64($num); |
| 329 | if ($num != $num2) { |
| 330 | print "Missed max64 $num = $num2\n"; |
| 331 | } |
| 332 | |
| 333 | # Max Number I can get out of my perl |
| 334 | $num = 0 - 2**40; |
| 335 | $num2 = $testClient->testI64($num); |
| 336 | if ($num != $num2) { |
| 337 | print "Missed min64 $num = $num2\n"; |
| 338 | } |
| 339 | |
| 340 | $transport->close(); |
| 341 | |
| 342 | |
| 343 | |