summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/thrift/test/perl/TestClient.pl
blob: 96e3bec7705dc0af26c83ec9e7fd0c6cb6ded321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env perl

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

use 5.10.0;
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long qw(GetOptions);
use Time::HiRes qw(gettimeofday);

use lib '../../lib/perl/lib';
use lib 'gen-perl';

use Thrift;
use Thrift::BinaryProtocol;
use Thrift::BufferedTransport;
use Thrift::FramedTransport;
use Thrift::MultiplexedProtocol;
use Thrift::SSLSocket;
use Thrift::Socket;
use Thrift::UnixSocket;

use ThriftTest::SecondService;
use ThriftTest::ThriftTest;
use ThriftTest::Types;

$|++;

sub usage {
    print <<"EOF";
Usage: $0 [OPTIONS]

Options:                          (default)
  --ca                                         CA to validate server with.
  --cert                                       Certificate to use.
                                               Required if using --ssl.
  --ciphers                                    Acceptable cipher list.
  --domain-socket <file>                       Use a unix domain socket.
  --help                                       Show usage.
  --key                                        Certificate key.
                                               Required if using --ssl.
  --port <portnum>                9090         Port to use.
  --protocol {binary}             binary       Protocol to use.
  --ssl                                        If present, use SSL.
  --transport {buffered|framed}   buffered     Transport to use.

EOF
}

my %opts = (
    'port' => 9090,
    'protocol' => 'binary',
    'transport' => 'buffered'
);

GetOptions(\%opts, qw (
    ca=s
    cert=s
    ciphers=s
    key=s
    domain-socket=s
    help
    host=s
    port=i
    protocol=s
    ssl
    transport=s
)) || exit 1;

if ($opts{help}) {
    usage();
    exit 0;
}

my $socket = undef;
if ($opts{'domain-socket'}) {
    $socket = Thrift::UnixSocket->new($opts{'domain-socket'});
}
elsif ($opts{ssl}) {
  $socket = Thrift::SSLSocket->new(\%opts);
}
else {
  $socket = Thrift::Socket->new($opts{host}, $opts{port});
}

my $transport;
if ($opts{transport} eq 'buffered') {
    $transport = Thrift::BufferedTransport->new($socket, 1024, 1024);
}
elsif ($opts{transport} eq 'framed') {
    $transport = Thrift::FramedTransport->new($socket);
}
else {
    usage();
    exit 1;
}

my $protocol;
my $protocol2;
if ($opts{protocol} eq 'binary' || $opts{protocol} eq 'multi') {
    $protocol = Thrift::BinaryProtocol->new($transport);
}
else {
    usage();
    exit 1;
}

my $secondService = undef;
if (index($opts{protocol}, 'multi') == 0) {
  $protocol2     = Thrift::MultiplexedProtocol->new($protocol, 'SecondService');
  $protocol      = Thrift::MultiplexedProtocol->new($protocol, 'ThriftTest');
  $secondService = ThriftTest::SecondServiceClient->new($protocol2);
}

my $testClient = ThriftTest::ThriftTestClient->new($protocol);

eval {
  $transport->open();
};
if($@){
    die(Dumper($@));
}

use constant ERR_BASETYPES => 1;
use constant ERR_STRUCTS => 2;
use constant ERR_CONTAINERS => 4;
use constant ERR_EXCEPTIONS => 8;
use constant ERR_PROTOCOL => 16;
use constant ERR_UNKNOWN => 64;

my $start = gettimeofday();

#
# VOID TEST
#
print('testVoid()');
$testClient->testVoid();
print(" = void\n");

#
# STRING TEST
#
print('testString("Test")');
my $s = $testClient->testString('Test');
print(qq| = "$s"\n|);
exit(ERR_BASETYPES) if ($s ne 'Test');

#
# MULTIPLEXED TEST
#
if (index($opts{protocol}, 'multi') == 0) {
    print('secondtestString("Test2")');
    $s = $secondService->secondtestString('Test2');
    print(qq| = "$s"\n|);
    exit(ERR_PROTOCOL) if ($s ne 'testString("Test2")');
}

#
# BOOL TEST
#
print('testBool(1)');
my $t = $testClient->testBool(1);
print(" = $t\n");
exit(ERR_BASETYPES) if ($t ne 1);
print('testBool(0)');
my $f = $testClient->testBool(0);
print(" = $f\n");
exit(ERR_BASETYPES) if ($f ne q||);


#
# BYTE TEST
#
print('testByte(1)');
my $u8 = $testClient->testByte(1);
print(" = $u8\n");

#
# I32 TEST
#
print('testI32(-1)');
my $i32 = $testClient->testI32(-1);
print(" = $i32\n");
exit(ERR_BASETYPES) if ($i32 ne -1);

#
# I64 TEST
#
print('testI64(-34359738368)');
my $i64 = $testClient->testI64(-34359738368);
print(" = $i64\n");
exit(ERR_BASETYPES) if ($i64 ne -34359738368);

#
# DOUBLE TEST
#
print('testDouble(-852.234234234)');
my $dub = $testClient->testDouble(-852.234234234);
print(" = $dub\n");
exit(ERR_BASETYPES) if ($dub ne -852.234234234);

#
# BINARY TEST   ---  TODO
#


#
# STRUCT TEST
#
print('testStruct({"Zero", 1, -3, -5})');
my $out = ThriftTest::Xtruct->new();
$out->string_thing('Zero');
$out->byte_thing(1);
$out->i32_thing(-3);
$out->i64_thing(-5);
my $in = $testClient->testStruct($out);
print(' = {"'.$in->string_thing.'", '.
        $in->byte_thing.', '.
        $in->i32_thing.', '.
        $in->i64_thing."}\n");

#
# NESTED STRUCT TEST
#
print('testNest({1, {"Zero", 1, -3, -5}, 5}');
my $out2 = ThriftTest::Xtruct2->new();
$out2->byte_thing(1);
$out2->struct_thing($out);
$out2->i32_thing(5);
my $in2 = $testClient->testNest($out2);
$in = $in2->struct_thing;
print(' = {'.$in2->byte_thing.', {"'.
      $in->string_thing.'", '.
      $in->byte_thing.', '.
      $in->i32_thing.', '.
      $in->i64_thing.'}, '.
      $in2->i32_thing."}\n");

#
# MAP TEST
#
my $mapout = {};
for (my $i = 0; $i < 5; ++$i) {
  $mapout->{$i} = $i-10;
}
print('testMap({');
my $first = 1;
while( my($key,$val) = each %$mapout) {
    if ($first) {
        $first = 0;
    }
    else {
        print(', ');
    }
    print("$key => $val");
}
print('})');


my $mapin = $testClient->testMap($mapout);
print(' = {');

$first = 1;
while( my($key,$val) = each %$mapin){
    if ($first) {
        $first = 0;
    }
    else {
        print(', ');
    }
    print("$key => $val");
}
print("}\n");

#
# SET TEST
#
my $setout = [];
for (my $i = -2; $i < 3; ++$i) {
    push(@$setout, $i);
}

print('testSet({'.join(',',@$setout).'})');

my $setin = $testClient->testSet($setout);

print(' = {'.join(',',@$setout)."}\n");

#
# LIST TEST
#
my $listout = [];
for (my $i = -2; $i < 3; ++$i) {
    push(@$listout, $i);
}

print('testList({'.join(',',@$listout).'})');

my $listin = $testClient->testList($listout);

print(' = {'.join(',',@$listin)."}\n");

#
# ENUM TEST
#
print('testEnum(ONE)');
my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
print(" = $ret\n");

print('testEnum(TWO)');
$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
print(" = $ret\n");

print('testEnum(THREE)');
$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
print(" = $ret\n");

print('testEnum(FIVE)');
$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
print(" = $ret\n");

print('testEnum(EIGHT)');
$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
print(" = $ret\n");

#
# TYPEDEF TEST
#
print('testTypedef(309858235082523)');
my $uid = $testClient->testTypedef(309858235082523);
print(" = $uid\n");

#
# NESTED MAP TEST
#
print('testMapMap(1)');
my $mm = $testClient->testMapMap(1);
print(' = {');
while( my ($key,$val) = each %$mm) {
    print("$key => {");
    while( my($k2,$v2) = each %$val) {
        print("$k2 => $v2, ");
    }
    print('}, ');
}
print("}\n");

#
# INSANITY TEST
#
my $insane = ThriftTest::Insanity->new();
$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
my $truck = ThriftTest::Xtruct->new();
$truck->string_thing('Hello2');
$truck->byte_thing(2);
$truck->i32_thing(2);
$truck->i64_thing(2);
my $truck2 = ThriftTest::Xtruct->new();
$truck2->string_thing('Goodbye4');
$truck2->byte_thing(4);
$truck2->i32_thing(4);
$truck2->i64_thing(4);
push(@{$insane->{xtructs}}, $truck);
push(@{$insane->{xtructs}}, $truck2);

print('testInsanity()');
my $whoa = $testClient->testInsanity($insane);
print(' = {');
while( my ($key,$val) = each %$whoa) {
    print("$key => {");
    while( my($k2,$v2) = each %$val) {
        print("$k2 => {");
        my $userMap = $v2->{userMap};
        print('{');
        if (ref($userMap) eq 'HASH') {
            while( my($k3,$v3) = each %$userMap) {
                print("$k3 => $v3, ");
            }
        }
        print('}, ');

        my $xtructs = $v2->{xtructs};
        print('{');
        if (ref($xtructs) eq 'ARRAY') {
            foreach my $x (@$xtructs) {
                print('{"'.$x->{string_thing}.'", '.
                      $x->{byte_thing}.', '.$x->{i32_thing}.', '.$x->{i64_thing}.'}, ');
            }
        }
        print('}');

        print('}, ');
    }
    print('}, ');
}
print("}\n");

#
# EXCEPTION TEST
#
print(q|testException('Xception')|);
eval {
    $testClient->testException('Xception');
    print("  void\nFAILURE\n");
}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
    print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
}


#
# Normal tests done.
#
my $stop = gettimeofday();
my $elp  = sprintf('%d',1000*($stop - $start), 0);
print("Total time: $elp ms\n");

#
# Extraneous "I don't trust PHP to pack/unpack integer" tests
#

# Max I32
my $num = 2**30 + 2**30 - 1;
my $num2 = $testClient->testI32($num);
if ($num != $num2) {
    print "Missed max32 $num = $num2\n";
}

# Min I32
$num = 0 - 2**31;
$num2 = $testClient->testI32($num);
if ($num != $num2) {
    print "Missed min32 $num = $num2\n";
}

# Max Number I can get out of my perl
$num = 2**40;
$num2 = $testClient->testI64($num);
if ($num != $num2) {
    print "Missed max64 $num = $num2\n";
}

# Max Number I can get out of my perl
$num = 0 - 2**40;
$num2 = $testClient->testI64($num);
if ($num != $num2) {
    print "Missed min64 $num = $num2\n";
}

$transport->close();