blob: 5a9a6f1c743ebbf55a9437ad7c4bafc78bc98544 [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
Roger Meier41ad4342015-03-24 22:30:40 +010044foreach my $arg (@ARGV) {
45 if($arg =~ /^--port=([0-9]+)/) {
46 $port = $1;
47 }
48}
Mark Slee3e3d7ad2007-05-16 02:35:58 +000049
50my $socket = new Thrift::Socket($host, $port);
51
52my $bufferedSocket = new Thrift::BufferedTransport($socket, 1024, 1024);
53my $transport = $bufferedSocket;
54my $protocol = new Thrift::BinaryProtocol($transport);
T Jake Luciani41687fc2008-12-23 03:45:43 +000055my $testClient = new ThriftTest::ThriftTestClient($protocol);
Mark Slee3e3d7ad2007-05-16 02:35:58 +000056
57eval{
58$transport->open();
59}; if($@){
60 die(Dumper($@));
61}
62my $start = gettimeofday();
63
64#
65# VOID TEST
66#
67print("testVoid()");
68$testClient->testVoid();
69print(" = void\n");
70
71#
72# STRING TEST
73#
74print("testString(\"Test\")");
75my $s = $testClient->testString("Test");
76print(" = \"$s\"\n");
77
78#
79# BYTE TEST
80#
81print("testByte(1)");
82my $u8 = $testClient->testByte(1);
83print(" = $u8\n");
84
85#
86# I32 TEST
87#
88print("testI32(-1)");
89my $i32 = $testClient->testI32(-1);
90print(" = $i32\n");
91
92#
93#I64 TEST
94#
95print("testI64(-34359738368)");
96my $i64 = $testClient->testI64(-34359738368);
97print(" = $i64\n");
98
99#
100# DOUBLE TEST
101#
102print("testDouble(-852.234234234)");
103my $dub = $testClient->testDouble(-852.234234234);
104print(" = $dub\n");
105
106#
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100107# BINARY TEST --- TODO
108#
109
110
111#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000112# STRUCT TEST
113#
114print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee82664432007-09-19 06:49:30 +0000115my $out = new ThriftTest::Xtruct();
116$out->string_thing("Zero");
117$out->byte_thing(1);
118$out->i32_thing(-3);
119$out->i64_thing(-5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000120my $in = $testClient->testStruct($out);
Mark Slee82664432007-09-19 06:49:30 +0000121print(" = {\"".$in->string_thing."\", ".
122 $in->byte_thing.", ".
123 $in->i32_thing.", ".
124 $in->i64_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000125
126#
127# NESTED STRUCT TEST
128#
129print("testNest({1, {\"Zero\", 1, -3, -5}, 5}");
Mark Slee82664432007-09-19 06:49:30 +0000130my $out2 = new ThriftTest::Xtruct2();
131$out2->byte_thing(1);
132$out2->struct_thing($out);
133$out2->i32_thing(5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000134my $in2 = $testClient->testNest($out2);
Mark Slee82664432007-09-19 06:49:30 +0000135$in = $in2->struct_thing;
136print(" = {".$in2->byte_thing.", {\"".
137 $in->string_thing."\", ".
138 $in->byte_thing.", ".
139 $in->i32_thing.", ".
140 $in->i64_thing."}, ".
141 $in2->i32_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000142
143#
144# MAP TEST
145#
146my $mapout = {};
147for (my $i = 0; $i < 5; ++$i) {
148 $mapout->{$i} = $i-10;
149}
150print("testMap({");
151my $first = 1;
152while( my($key,$val) = each %$mapout) {
153 if ($first) {
154 $first = 0;
155 } else {
156 print(", ");
157 }
158 print("$key => $val");
159}
160print("})");
161
162
163my $mapin = $testClient->testMap($mapout);
164print(" = {");
165
166$first = 1;
167while( my($key,$val) = each %$mapin){
168 if ($first) {
169 $first = 0;
170 } else {
171 print(", ");
172 }
173 print("$key => $val");
174}
175print("}\n");
176
177#
178# SET TEST
179#
180my $setout = [];
181for (my $i = -2; $i < 3; ++$i) {
182 push(@$setout, $i);
183}
184
185print("testSet({".join(",",@$setout)."})");
186
187my $setin = $testClient->testSet($setout);
188
189print(" = {".join(",",@$setout)."}\n");
190
191#
192# LIST TEST
193#
194my $listout = [];
195for (my $i = -2; $i < 3; ++$i) {
196 push(@$listout, $i);
197}
198
199print("testList({".join(",",@$listout)."})");
200
201my $listin = $testClient->testList($listout);
202
203print(" = {".join(",",@$listin)."}\n");
204
205#
206# ENUM TEST
207#
208print("testEnum(ONE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000209my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000210print(" = $ret\n");
211
212print("testEnum(TWO)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000213$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000214print(" = $ret\n");
215
216print("testEnum(THREE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000217$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000218print(" = $ret\n");
219
220print("testEnum(FIVE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000221$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000222print(" = $ret\n");
223
224print("testEnum(EIGHT)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000225$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000226print(" = $ret\n");
227
228#
229# TYPEDEF TEST
230#
231print("testTypedef(309858235082523)");
232my $uid = $testClient->testTypedef(309858235082523);
233print(" = $uid\n");
234
235#
236# NESTED MAP TEST
237#
238print("testMapMap(1)");
239my $mm = $testClient->testMapMap(1);
240print(" = {");
241while( my ($key,$val) = each %$mm) {
242 print("$key => {");
243 while( my($k2,$v2) = each %$val) {
244 print("$k2 => $v2, ");
245 }
246 print("}, ");
247}
248print("}\n");
249
250#
251# INSANITY TEST
252#
Mark Slee82664432007-09-19 06:49:30 +0000253my $insane = new ThriftTest::Insanity();
T Jake Luciani41687fc2008-12-23 03:45:43 +0000254$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
Mark Slee82664432007-09-19 06:49:30 +0000255my $truck = new ThriftTest::Xtruct();
256$truck->string_thing("Truck");
257$truck->byte_thing(8);
258$truck->i32_thing(8);
259$truck->i64_thing(8);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000260push(@{$insane->{xtructs}}, $truck);
261
262print("testInsanity()");
263my $whoa = $testClient->testInsanity($insane);
264print(" = {");
265while( my ($key,$val) = each %$whoa) {
266 print("$key => {");
267 while( my($k2,$v2) = each %$val) {
268 print("$k2 => {");
269 my $userMap = $v2->{userMap};
270 print("{");
271 if (ref($userMap) eq "HASH") {
272 while( my($k3,$v3) = each %$userMap) {
273 print("$k3 => $v3, ");
274 }
275 }
276 print("}, ");
277
278 my $xtructs = $v2->{xtructs};
279 print("{");
280 if (ref($xtructs) eq "ARRAY") {
281 foreach my $x (@$xtructs) {
282 print("{\"".$x->{string_thing}."\", ".
283 $x->{byte_thing}.", ".$x->{i32_thing}.", ".$x->{i64_thing}."}, ");
284 }
285 }
286 print("}");
287
288 print("}, ");
289 }
290 print("}, ");
291}
292print("}\n");
293
294#
295# EXCEPTION TEST
296#
297print("testException('Xception')");
298eval {
299 $testClient->testException('Xception');
300 print(" void\nFAILURE\n");
Mark Slee82664432007-09-19 06:49:30 +0000301}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000302 print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
303}
304
305
306#
307# Normal tests done.
308#
309my $stop = gettimeofday();
310my $elp = sprintf("%d",1000*($stop - $start), 0);
311print("Total time: $elp ms\n");
312
313#
314# Extraneous "I don't trust PHP to pack/unpack integer" tests
315#
316
317# Max I32
318my $num = 2**30 + 2**30 - 1;
319my $num2 = $testClient->testI32($num);
320if ($num != $num2) {
321 print "Missed max32 $num = $num2\n";
322}
323
324# Min I32
325$num = 0 - 2**31;
326$num2 = $testClient->testI32($num);
327if ($num != $num2) {
328 print "Missed min32 $num = $num2\n";
329}
330
331# Max Number I can get out of my perl
332$num = 2**40;
333$num2 = $testClient->testI64($num);
334if ($num != $num2) {
335 print "Missed max64 $num = $num2\n";
336}
337
338# Max Number I can get out of my perl
339$num = 0 - 2**40;
340$num2 = $testClient->testI64($num);
341if ($num != $num2) {
342 print "Missed min64 $num = $num2\n";
343}
344
345$transport->close();
346
347
348