diff options
Diffstat (limited to '')
-rw-r--r-- | src/jaegertracing/thrift/test/perl/Makefile.am | 31 | ||||
-rwxr-xr-x | src/jaegertracing/thrift/test/perl/TestClient.pl | 470 | ||||
-rw-r--r-- | src/jaegertracing/thrift/test/perl/TestServer.pl | 411 |
3 files changed, 912 insertions, 0 deletions
diff --git a/src/jaegertracing/thrift/test/perl/Makefile.am b/src/jaegertracing/thrift/test/perl/Makefile.am new file mode 100644 index 000000000..1dbaf28b0 --- /dev/null +++ b/src/jaegertracing/thrift/test/perl/Makefile.am @@ -0,0 +1,31 @@ +# +# 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. +# + +stubs: ../ThriftTest.thrift + $(THRIFT) --gen perl ../ThriftTest.thrift + +precross: stubs + +check: stubs + +clean-local: + $(RM) -r gen-perl/ + +dist-hook: + $(RM) -r $(distdir)/gen-perl/ diff --git a/src/jaegertracing/thrift/test/perl/TestClient.pl b/src/jaegertracing/thrift/test/perl/TestClient.pl new file mode 100755 index 000000000..96e3bec77 --- /dev/null +++ b/src/jaegertracing/thrift/test/perl/TestClient.pl @@ -0,0 +1,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(); + + + diff --git a/src/jaegertracing/thrift/test/perl/TestServer.pl b/src/jaegertracing/thrift/test/perl/TestServer.pl new file mode 100644 index 000000000..d2b9a38ab --- /dev/null +++ b/src/jaegertracing/thrift/test/perl/TestServer.pl @@ -0,0 +1,411 @@ +#!/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); + +$SIG{INT} = \&sigint_handler; + +use lib '../../lib/perl/lib'; +use lib 'gen-perl'; + +use Thrift; +use Thrift::BinaryProtocol; +use Thrift::BufferedTransport; +use Thrift::FramedTransport; +use Thrift::MultiplexedProcessor; +use Thrift::SSLServerSocket; +use Thrift::ServerSocket; +use Thrift::Server; +use Thrift::UnixServerSocket; + +use ThriftTest::SecondService; +use ThriftTest::ThriftTest; +use ThriftTest::Types; + +$|++; + +sub usage { + print <<"EOF"; +Usage: $0 [OPTIONS] + +Options: (default) + --ca Certificate authority file (optional). + --cert Certificate file. + Required if using --ssl. + --ciphers Acceptable cipher list. + --domain-socket <file> Use a unix domain socket. + --help Show usage. + --key Private key file for certificate. + Required if using --ssl and private key is + not in the certificate file. + --port <portnum> 9090 Port to use. + --protocol {binary} binary Protocol to use. + --ssl If present, use SSL/TLS. + --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 + domain-socket=s + help + host=s + key=s + port=i + protocol=s + ssl + transport=s +)) || exit 1; + +if ($opts{help}) { + usage(); + exit 0; +} + +if ($opts{ssl} and not defined $opts{cert}) { + usage(); + exit 1; +} + +my $handler = ThriftTestHandler->new(); +my $handler2 = SecondServiceHandler->new(); +my $processor = ThriftTest::ThriftTestProcessor->new($handler); +my $processor2 = ThriftTest::SecondServiceProcessor->new($handler2); + +my $serversocket; +if ($opts{'domain-socket'}) { + unlink($opts{'domain-socket'}); + $serversocket = Thrift::UnixServerSocket->new($opts{'domain-socket'}); +} +elsif ($opts{ssl}) { + $serversocket = Thrift::SSLServerSocket->new(\%opts); +} +else { + $serversocket = Thrift::ServerSocket->new(\%opts); +} +my $transport; +if ($opts{transport} eq 'buffered') { + $transport = Thrift::BufferedTransportFactory->new(); +} +elsif ($opts{transport} eq 'framed') { + $transport = Thrift::FramedTransportFactory->new(); +} +else { + usage(); + exit 1; +} +my $protocol; +if ($opts{protocol} eq 'binary' || $opts{protocol} eq 'multi') { + $protocol = Thrift::BinaryProtocolFactory->new(); +} +else { + usage(); + exit 1; +} + +if (index($opts{protocol}, 'multi') == 0) { + my $newProcessor = Thrift::MultiplexedProcessor->new($protocol); + $newProcessor->defaultProcessor($processor); + $newProcessor->registerProcessor('ThriftTest', $processor); + $newProcessor->registerProcessor('SecondService', $processor2); + $processor = $newProcessor; +} + +my $ssltag = ''; +if ($opts{ssl}) { + $ssltag = '(SSL)'; +} +my $listening_on = "$opts{port} $ssltag"; +if ($opts{'domain-socket'}) { + $listening_on = $opts{'domain-socket'}; +} +my $server = Thrift::SimpleServer->new($processor, $serversocket, $transport, $protocol); +print qq|Starting "simple" server ($opts{transport}/$opts{protocol}) listen on: $listening_on\n|; +$server->serve(); +print "done.\n"; + +sub sigint_handler { + print "received SIGINT, stopping...\n"; + $server->stop(); +} + +### +### Test server implementation +### + +package ThriftTestHandler; + +use base qw( ThriftTest::ThriftTestIf ); + +sub new { + my $classname = shift; + my $self = {}; + return bless($self, $classname); +} + +sub testVoid { + print("testVoid()\n"); +} + +sub testString { + my $self = shift; + my $thing = shift; + print("testString($thing)\n"); + return $thing; +} + +sub testBool { + my $self = shift; + my $thing = shift; + my $str = $thing ? 'true' : 'false'; + print("testBool($str)\n"); + return $thing; +} + +sub testByte { + my $self = shift; + my $thing = shift; + print("testByte($thing)\n"); + return $thing; +} + +sub testI32 { + my $self = shift; + my $thing = shift; + print("testI32($thing)\n"); + return $thing; +} + +sub testI64 { + my $self = shift; + my $thing = shift; + print("testI64($thing)\n"); + return $thing; +} + +sub testDouble { + my $self = shift; + my $thing = shift; + print("testDouble($thing)\n"); + return $thing; +} + +sub testBinary { + my $self = shift; + my $thing = shift; + my @bytes = split //, $thing; + print 'testBinary('; + printf( '%02lx', ord $_ ) foreach (@bytes); + print ")\n"; + return $thing; +} + +sub testStruct { + my $self = shift; + my $thing = shift; + printf(qq|testStruct({"%s", %d, %d, %lld})\n|, + $thing->{string_thing}, + $thing->{byte_thing}, + $thing->{i32_thing}, + $thing->{i64_thing}); + return $thing; +} + +sub testNest { + my $self = shift; + my $nest = shift; + my $thing = $nest->{struct_thing}; + printf(qq|testNest({%d, {"%s", %d, %d, %lld}, %d})\n|, + $nest->{byte_thing}, + $thing->{string_thing}, + $thing->{byte_thing}, + $thing->{i32_thing}, + $thing->{i64_thing}, + $nest->{i32_thing}); + return $nest; +} + +sub testMap { + my $self = shift; + my $thing = shift; + printf "testMap({%s})\n", + join( ', ', + map { $_ . ' => ' . $thing->{$_} } + sort keys %$thing + ); + return $thing; +} + +sub testStringMap { + my $self = shift; + my $thing = shift; + printf "testStringMap({%s})\n", + join( ', ', + map { $_ . ' => ' . $thing->{$_} } + sort keys %$thing + ); + return $thing; +} + +sub testSet { + my $self = shift; + my $thing = shift; + my @result = sort keys %$thing; + printf "testSet({%s})\n", join(', ', @result ); + return \@result; +} + +sub testList { + my $self = shift; + my $thing = shift; + print "testList({%s})\n", join(', ', @$thing); + return $thing; +} + +sub testEnum { + my $self = shift; + my $thing = shift; + print "testEnum($thing)\n"; + return $thing; +} + +sub testTypedef { + my $self = shift; + my $thing = shift; + print("testTypedef($thing)\n"); + return $thing; +} + +sub testMapMap { + my $self = shift; + my $hello = shift; + + printf("testMapMap(%d)\n", $hello); + my $result = { 4 => { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }, -4 => { -1 => -1, -2 => -2, -3 => -3, -4 => -4 } }; + return $result; +} + +sub testInsanity { + my $self = shift; + my $argument = shift; + print("testInsanity()\n"); + + my $hello = ThriftTest::Xtruct->new({string_thing => 'Hello2', byte_thing => 2, i32_thing => 2, i64_thing => 2}); + my @hellos; + push(@hellos, $hello); + my $goodbye = ThriftTest::Xtruct->new({string_thing => 'Goodbye4', byte_thing => 4, i32_thing => 4, i64_thing => 4}); + my @goodbyes; + push(@goodbyes, $goodbye); + my $crazy = ThriftTest::Insanity->new({userMap => { ThriftTest::Numberz::EIGHT => 8 }, xtructs => \@goodbyes}); + my $loony = ThriftTest::Insanity->new(); + my $result = { 1 => { ThriftTest::Numberz::TWO => $argument, ThriftTest::Numberz::THREE => $argument }, + 2 => { ThriftTest::Numberz::SIX => $loony } }; + return $result; +} + +sub testMulti { + my $self = shift; + my $arg0 = shift; + my $arg1 = shift; + my $arg2 = shift; + my $arg3 = shift; + my $arg4 = shift; + my $arg5 = shift; + + print("testMulti()\n"); + return ThriftTest::Xtruct->new({string_thing => 'Hello2', byte_thing => $arg0, i32_thing => $arg1, i64_thing => $arg2}); +} + +sub testException { + my $self = shift; + my $arg = shift; + print("testException($arg)\n"); + if ($arg eq 'Xception') { + die ThriftTest::Xception->new({errorCode => 1001, message => $arg}); + } + elsif ($arg eq 'TException') { + die 'astring'; # all unhandled exceptions become TExceptions + } + else { + return ThriftTest::Xtruct->new({string_thing => $arg}); + } +} + +sub testMultiException { + my $self = shift; + my $arg0 = shift; + my $arg1 = shift; + + printf("testMultiException(%s, %s)\n", $arg0, $arg1); + if ($arg0 eq 'Xception') { + die ThriftTest::Xception->new({errorCode => 1001, message => 'This is an Xception'}); + } + elsif ($arg0 eq 'Xception2') { + my $struct_thing = ThriftTest::Xtruct->new({string_thing => 'This is an Xception2'}); + die ThriftTest::Xception2->new({errorCode => 2002, struct_thing => $struct_thing}); + } + else { + return ThriftTest::Xtruct->new({string_thing => $arg1}); + } +} + +sub testOneway { + my $self = shift; + my $num = shift; + print("testOneway($num): received\n"); +} + +### +### Test server implementation +### + +package SecondServiceHandler; + +use base qw( ThriftTest::SecondServiceIf ); + +sub new { + my $classname = shift; + my $self = {}; + return bless($self, $classname); +} + +sub secondtestString { + my $self = shift; + my $thing = shift; + print("testString($thing)\n"); + return qq|testString("$thing")|; +} + +1; |