blob: af80d46946fac6ab9b80ae1dd41843b1f99f7402 [file] [log] [blame]
T Jake Luciani41687fc2008-12-23 03:45:43 +00001#!/usr/bin/env perl
Mark Slee3e3d7ad2007-05-16 02:35:58 +00002
David Reissea2cba82009-03-30 21:35:00 +00003#
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 Slee3e3d7ad2007-05-16 02:35:58 +000022require 5.6.0;
23use strict;
24use warnings;
25use Data::Dumper;
26use Time::HiRes qw(gettimeofday);
27
28use lib '../../lib/perl/lib';
T Jake Luciani41687fc2008-12-23 03:45:43 +000029use lib 'gen-perl';
Mark Slee3e3d7ad2007-05-16 02:35:58 +000030
31use Thrift;
32use Thrift::BinaryProtocol;
33use Thrift::Socket;
34use Thrift::BufferedTransport;
35
T Jake Luciani41687fc2008-12-23 03:45:43 +000036use ThriftTest::ThriftTest;
Mark Slee27ed6ec2007-08-16 01:26:31 +000037use ThriftTest::Types;
Mark Slee3e3d7ad2007-05-16 02:35:58 +000038
39$|++;
40
41my $host = 'localhost';
42my $port = 9090;
43
44
45my $socket = new Thrift::Socket($host, $port);
46
47my $bufferedSocket = new Thrift::BufferedTransport($socket, 1024, 1024);
48my $transport = $bufferedSocket;
49my $protocol = new Thrift::BinaryProtocol($transport);
T Jake Luciani41687fc2008-12-23 03:45:43 +000050my $testClient = new ThriftTest::ThriftTestClient($protocol);
Mark Slee3e3d7ad2007-05-16 02:35:58 +000051
52eval{
53$transport->open();
54}; if($@){
55 die(Dumper($@));
56}
57my $start = gettimeofday();
58
59#
60# VOID TEST
61#
62print("testVoid()");
63$testClient->testVoid();
64print(" = void\n");
65
66#
67# STRING TEST
68#
69print("testString(\"Test\")");
70my $s = $testClient->testString("Test");
71print(" = \"$s\"\n");
72
73#
74# BYTE TEST
75#
76print("testByte(1)");
77my $u8 = $testClient->testByte(1);
78print(" = $u8\n");
79
80#
81# I32 TEST
82#
83print("testI32(-1)");
84my $i32 = $testClient->testI32(-1);
85print(" = $i32\n");
86
87#
88#I64 TEST
89#
90print("testI64(-34359738368)");
91my $i64 = $testClient->testI64(-34359738368);
92print(" = $i64\n");
93
94#
95# DOUBLE TEST
96#
97print("testDouble(-852.234234234)");
98my $dub = $testClient->testDouble(-852.234234234);
99print(" = $dub\n");
100
101#
102# STRUCT TEST
103#
104print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee82664432007-09-19 06:49:30 +0000105my $out = new ThriftTest::Xtruct();
106$out->string_thing("Zero");
107$out->byte_thing(1);
108$out->i32_thing(-3);
109$out->i64_thing(-5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000110my $in = $testClient->testStruct($out);
Mark Slee82664432007-09-19 06:49:30 +0000111print(" = {\"".$in->string_thing."\", ".
112 $in->byte_thing.", ".
113 $in->i32_thing.", ".
114 $in->i64_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000115
116#
117# NESTED STRUCT TEST
118#
119print("testNest({1, {\"Zero\", 1, -3, -5}, 5}");
Mark Slee82664432007-09-19 06:49:30 +0000120my $out2 = new ThriftTest::Xtruct2();
121$out2->byte_thing(1);
122$out2->struct_thing($out);
123$out2->i32_thing(5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000124my $in2 = $testClient->testNest($out2);
Mark Slee82664432007-09-19 06:49:30 +0000125$in = $in2->struct_thing;
126print(" = {".$in2->byte_thing.", {\"".
127 $in->string_thing."\", ".
128 $in->byte_thing.", ".
129 $in->i32_thing.", ".
130 $in->i64_thing."}, ".
131 $in2->i32_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000132
133#
134# MAP TEST
135#
136my $mapout = {};
137for (my $i = 0; $i < 5; ++$i) {
138 $mapout->{$i} = $i-10;
139}
140print("testMap({");
141my $first = 1;
142while( my($key,$val) = each %$mapout) {
143 if ($first) {
144 $first = 0;
145 } else {
146 print(", ");
147 }
148 print("$key => $val");
149}
150print("})");
151
152
153my $mapin = $testClient->testMap($mapout);
154print(" = {");
155
156$first = 1;
157while( my($key,$val) = each %$mapin){
158 if ($first) {
159 $first = 0;
160 } else {
161 print(", ");
162 }
163 print("$key => $val");
164}
165print("}\n");
166
167#
168# SET TEST
169#
170my $setout = [];
171for (my $i = -2; $i < 3; ++$i) {
172 push(@$setout, $i);
173}
174
175print("testSet({".join(",",@$setout)."})");
176
177my $setin = $testClient->testSet($setout);
178
179print(" = {".join(",",@$setout)."}\n");
180
181#
182# LIST TEST
183#
184my $listout = [];
185for (my $i = -2; $i < 3; ++$i) {
186 push(@$listout, $i);
187}
188
189print("testList({".join(",",@$listout)."})");
190
191my $listin = $testClient->testList($listout);
192
193print(" = {".join(",",@$listin)."}\n");
194
195#
196# ENUM TEST
197#
198print("testEnum(ONE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000199my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000200print(" = $ret\n");
201
202print("testEnum(TWO)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000203$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000204print(" = $ret\n");
205
206print("testEnum(THREE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000207$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000208print(" = $ret\n");
209
210print("testEnum(FIVE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000211$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000212print(" = $ret\n");
213
214print("testEnum(EIGHT)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000215$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000216print(" = $ret\n");
217
218#
219# TYPEDEF TEST
220#
221print("testTypedef(309858235082523)");
222my $uid = $testClient->testTypedef(309858235082523);
223print(" = $uid\n");
224
225#
226# NESTED MAP TEST
227#
228print("testMapMap(1)");
229my $mm = $testClient->testMapMap(1);
230print(" = {");
231while( my ($key,$val) = each %$mm) {
232 print("$key => {");
233 while( my($k2,$v2) = each %$val) {
234 print("$k2 => $v2, ");
235 }
236 print("}, ");
237}
238print("}\n");
239
240#
241# INSANITY TEST
242#
Mark Slee82664432007-09-19 06:49:30 +0000243my $insane = new ThriftTest::Insanity();
T Jake Luciani41687fc2008-12-23 03:45:43 +0000244$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
Mark Slee82664432007-09-19 06:49:30 +0000245my $truck = new ThriftTest::Xtruct();
246$truck->string_thing("Truck");
247$truck->byte_thing(8);
248$truck->i32_thing(8);
249$truck->i64_thing(8);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000250push(@{$insane->{xtructs}}, $truck);
251
252print("testInsanity()");
253my $whoa = $testClient->testInsanity($insane);
254print(" = {");
255while( my ($key,$val) = each %$whoa) {
256 print("$key => {");
257 while( my($k2,$v2) = each %$val) {
258 print("$k2 => {");
259 my $userMap = $v2->{userMap};
260 print("{");
261 if (ref($userMap) eq "HASH") {
262 while( my($k3,$v3) = each %$userMap) {
263 print("$k3 => $v3, ");
264 }
265 }
266 print("}, ");
267
268 my $xtructs = $v2->{xtructs};
269 print("{");
270 if (ref($xtructs) eq "ARRAY") {
271 foreach my $x (@$xtructs) {
272 print("{\"".$x->{string_thing}."\", ".
273 $x->{byte_thing}.", ".$x->{i32_thing}.", ".$x->{i64_thing}."}, ");
274 }
275 }
276 print("}");
277
278 print("}, ");
279 }
280 print("}, ");
281}
282print("}\n");
283
284#
285# EXCEPTION TEST
286#
287print("testException('Xception')");
288eval {
289 $testClient->testException('Xception');
290 print(" void\nFAILURE\n");
Mark Slee82664432007-09-19 06:49:30 +0000291}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000292 print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
293}
294
295
296#
297# Normal tests done.
298#
299my $stop = gettimeofday();
300my $elp = sprintf("%d",1000*($stop - $start), 0);
301print("Total time: $elp ms\n");
302
303#
304# Extraneous "I don't trust PHP to pack/unpack integer" tests
305#
306
307# Max I32
308my $num = 2**30 + 2**30 - 1;
309my $num2 = $testClient->testI32($num);
310if ($num != $num2) {
311 print "Missed max32 $num = $num2\n";
312}
313
314# Min I32
315$num = 0 - 2**31;
316$num2 = $testClient->testI32($num);
317if ($num != $num2) {
318 print "Missed min32 $num = $num2\n";
319}
320
321# Max Number I can get out of my perl
322$num = 2**40;
323$num2 = $testClient->testI64($num);
324if ($num != $num2) {
325 print "Missed max64 $num = $num2\n";
326}
327
328# Max Number I can get out of my perl
329$num = 0 - 2**40;
330$num2 = $testClient->testI64($num);
331if ($num != $num2) {
332 print "Missed min64 $num = $num2\n";
333}
334
335$transport->close();
336
337
338