Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
#!/usr/bin/env perl
2
 
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
 
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';
29
use lib 'gen-perl';
30
 
31
use Thrift;
32
use Thrift::BinaryProtocol;
33
use Thrift::Socket;
34
use Thrift::BufferedTransport;
35
 
36
use ThriftTest::ThriftTest;
37
use ThriftTest::Types;
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);
50
my $testClient = new ThriftTest::ThriftTestClient($protocol);
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
#
102
# STRUCT TEST
103
#
104
print("testStruct({\"Zero\", 1, -3, -5})");
105
my $out = new ThriftTest::Xtruct();
106
$out->string_thing("Zero");
107
$out->byte_thing(1);
108
$out->i32_thing(-3);
109
$out->i64_thing(-5);
110
my $in = $testClient->testStruct($out);
111
print(" = {\"".$in->string_thing."\", ".
112
        $in->byte_thing.", ".
113
        $in->i32_thing.", ".
114
        $in->i64_thing."}\n");
115
 
116
#
117
# NESTED STRUCT TEST
118
#
119
print("testNest({1, {\"Zero\", 1, -3, -5}, 5}");
120
my $out2 = new ThriftTest::Xtruct2();
121
$out2->byte_thing(1);
122
$out2->struct_thing($out);
123
$out2->i32_thing(5);
124
my $in2 = $testClient->testNest($out2);
125
$in = $in2->struct_thing;
126
print(" = {".$in2->byte_thing.", {\"".
127
      $in->string_thing."\", ".
128
      $in->byte_thing.", ".
129
      $in->i32_thing.", ".
130
      $in->i64_thing."}, ".
131
      $in2->i32_thing."}\n");
132
 
133
#
134
# MAP TEST
135
#
136
my $mapout = {};
137
for (my $i = 0; $i < 5; ++$i) {
138
  $mapout->{$i} = $i-10;
139
}
140
print("testMap({");
141
my $first = 1;
142
while( my($key,$val) = each %$mapout) {
143
    if ($first) {
144
        $first = 0;
145
    } else {
146
        print(", ");
147
    }
148
    print("$key => $val");
149
}
150
print("})");
151
 
152
 
153
my $mapin = $testClient->testMap($mapout);
154
print(" = {");
155
 
156
$first = 1;
157
while( my($key,$val) = each %$mapin){
158
    if ($first) {
159
        $first = 0;
160
    } else {
161
        print(", ");
162
    }
163
    print("$key => $val");
164
}
165
print("}\n");
166
 
167
#
168
# SET TEST
169
#
170
my $setout = [];
171
for (my $i = -2; $i < 3; ++$i) {
172
    push(@$setout, $i);
173
}
174
 
175
print("testSet({".join(",",@$setout)."})");
176
 
177
my $setin = $testClient->testSet($setout);
178
 
179
print(" = {".join(",",@$setout)."}\n");
180
 
181
#
182
# LIST TEST
183
#
184
my $listout = [];
185
for (my $i = -2; $i < 3; ++$i) {
186
    push(@$listout, $i);
187
}
188
 
189
print("testList({".join(",",@$listout)."})");
190
 
191
my $listin = $testClient->testList($listout);
192
 
193
print(" = {".join(",",@$listin)."}\n");
194
 
195
#
196
# ENUM TEST
197
#
198
print("testEnum(ONE)");
199
my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
200
print(" = $ret\n");
201
 
202
print("testEnum(TWO)");
203
$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
204
print(" = $ret\n");
205
 
206
print("testEnum(THREE)");
207
$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
208
print(" = $ret\n");
209
 
210
print("testEnum(FIVE)");
211
$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
212
print(" = $ret\n");
213
 
214
print("testEnum(EIGHT)");
215
$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
216
print(" = $ret\n");
217
 
218
#
219
# TYPEDEF TEST
220
#
221
print("testTypedef(309858235082523)");
222
my $uid = $testClient->testTypedef(309858235082523);
223
print(" = $uid\n");
224
 
225
#
226
# NESTED MAP TEST
227
#
228
print("testMapMap(1)");
229
my $mm = $testClient->testMapMap(1);
230
print(" = {");
231
while( my ($key,$val) = each %$mm) {
232
    print("$key => {");
233
    while( my($k2,$v2) = each %$val) {
234
        print("$k2 => $v2, ");
235
    }
236
    print("}, ");
237
}
238
print("}\n");
239
 
240
#
241
# INSANITY TEST
242
#
243
my $insane = new ThriftTest::Insanity();
244
$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
245
my $truck = new ThriftTest::Xtruct();
246
$truck->string_thing("Truck");
247
$truck->byte_thing(8);
248
$truck->i32_thing(8);
249
$truck->i64_thing(8);
250
push(@{$insane->{xtructs}}, $truck);
251
 
252
print("testInsanity()");
253
my $whoa = $testClient->testInsanity($insane);
254
print(" = {");
255
while( 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
}
282
print("}\n");
283
 
284
#
285
# EXCEPTION TEST
286
#
287
print("testException('Xception')");
288
eval {
289
    $testClient->testException('Xception');
290
    print("  void\nFAILURE\n");
291
}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
292
    print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
293
}
294
 
295
 
296
#
297
# Normal tests done.
298
#
299
my $stop = gettimeofday();
300
my $elp  = sprintf("%d",1000*($stop - $start), 0);
301
print("Total time: $elp ms\n");
302
 
303
#
304
# Extraneous "I don't trust PHP to pack/unpack integer" tests
305
#
306
 
307
# Max I32
308
my $num = 2**30 + 2**30 - 1;
309
my $num2 = $testClient->testI32($num);
310
if ($num != $num2) {
311
    print "Missed max32 $num = $num2\n";
312
}
313
 
314
# Min I32
315
$num = 0 - 2**31;
316
$num2 = $testClient->testI32($num);
317
if ($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);
324
if ($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);
331
if ($num != $num2) {
332
    print "Missed min64 $num = $num2\n";
333
}
334
 
335
$transport->close();
336
 
337
 
338