blob: ca1d47e2881b0e7dd57145295f903e52d8b26fc4 [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#
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100102# BINARY TEST --- TODO
103#
104
105
106#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000107# STRUCT TEST
108#
109print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee82664432007-09-19 06:49:30 +0000110my $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 Slee3e3d7ad2007-05-16 02:35:58 +0000115my $in = $testClient->testStruct($out);
Mark Slee82664432007-09-19 06:49:30 +0000116print(" = {\"".$in->string_thing."\", ".
117 $in->byte_thing.", ".
118 $in->i32_thing.", ".
119 $in->i64_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000120
121#
122# NESTED STRUCT TEST
123#
124print("testNest({1, {\"Zero\", 1, -3, -5}, 5}");
Mark Slee82664432007-09-19 06:49:30 +0000125my $out2 = new ThriftTest::Xtruct2();
126$out2->byte_thing(1);
127$out2->struct_thing($out);
128$out2->i32_thing(5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000129my $in2 = $testClient->testNest($out2);
Mark Slee82664432007-09-19 06:49:30 +0000130$in = $in2->struct_thing;
131print(" = {".$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 Slee3e3d7ad2007-05-16 02:35:58 +0000137
138#
139# MAP TEST
140#
141my $mapout = {};
142for (my $i = 0; $i < 5; ++$i) {
143 $mapout->{$i} = $i-10;
144}
145print("testMap({");
146my $first = 1;
147while( my($key,$val) = each %$mapout) {
148 if ($first) {
149 $first = 0;
150 } else {
151 print(", ");
152 }
153 print("$key => $val");
154}
155print("})");
156
157
158my $mapin = $testClient->testMap($mapout);
159print(" = {");
160
161$first = 1;
162while( my($key,$val) = each %$mapin){
163 if ($first) {
164 $first = 0;
165 } else {
166 print(", ");
167 }
168 print("$key => $val");
169}
170print("}\n");
171
172#
173# SET TEST
174#
175my $setout = [];
176for (my $i = -2; $i < 3; ++$i) {
177 push(@$setout, $i);
178}
179
180print("testSet({".join(",",@$setout)."})");
181
182my $setin = $testClient->testSet($setout);
183
184print(" = {".join(",",@$setout)."}\n");
185
186#
187# LIST TEST
188#
189my $listout = [];
190for (my $i = -2; $i < 3; ++$i) {
191 push(@$listout, $i);
192}
193
194print("testList({".join(",",@$listout)."})");
195
196my $listin = $testClient->testList($listout);
197
198print(" = {".join(",",@$listin)."}\n");
199
200#
201# ENUM TEST
202#
203print("testEnum(ONE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000204my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000205print(" = $ret\n");
206
207print("testEnum(TWO)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000208$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000209print(" = $ret\n");
210
211print("testEnum(THREE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000212$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000213print(" = $ret\n");
214
215print("testEnum(FIVE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000216$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000217print(" = $ret\n");
218
219print("testEnum(EIGHT)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000220$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000221print(" = $ret\n");
222
223#
224# TYPEDEF TEST
225#
226print("testTypedef(309858235082523)");
227my $uid = $testClient->testTypedef(309858235082523);
228print(" = $uid\n");
229
230#
231# NESTED MAP TEST
232#
233print("testMapMap(1)");
234my $mm = $testClient->testMapMap(1);
235print(" = {");
236while( my ($key,$val) = each %$mm) {
237 print("$key => {");
238 while( my($k2,$v2) = each %$val) {
239 print("$k2 => $v2, ");
240 }
241 print("}, ");
242}
243print("}\n");
244
245#
246# INSANITY TEST
247#
Mark Slee82664432007-09-19 06:49:30 +0000248my $insane = new ThriftTest::Insanity();
T Jake Luciani41687fc2008-12-23 03:45:43 +0000249$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
Mark Slee82664432007-09-19 06:49:30 +0000250my $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 Slee3e3d7ad2007-05-16 02:35:58 +0000255push(@{$insane->{xtructs}}, $truck);
256
257print("testInsanity()");
258my $whoa = $testClient->testInsanity($insane);
259print(" = {");
260while( 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}
287print("}\n");
288
289#
290# EXCEPTION TEST
291#
292print("testException('Xception')");
293eval {
294 $testClient->testException('Xception');
295 print(" void\nFAILURE\n");
Mark Slee82664432007-09-19 06:49:30 +0000296}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000297 print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
298}
299
300
301#
302# Normal tests done.
303#
304my $stop = gettimeofday();
305my $elp = sprintf("%d",1000*($stop - $start), 0);
306print("Total time: $elp ms\n");
307
308#
309# Extraneous "I don't trust PHP to pack/unpack integer" tests
310#
311
312# Max I32
313my $num = 2**30 + 2**30 - 1;
314my $num2 = $testClient->testI32($num);
315if ($num != $num2) {
316 print "Missed max32 $num = $num2\n";
317}
318
319# Min I32
320$num = 0 - 2**31;
321$num2 = $testClient->testI32($num);
322if ($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);
329if ($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);
336if ($num != $num2) {
337 print "Missed min64 $num = $num2\n";
338}
339
340$transport->close();
341
342
343