Jens Geyer | c214572 | 2015-04-02 22:54:02 +0200 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
| 20 | use Test::More tests => 6; |
| 21 | |
| 22 | use strict; |
| 23 | use warnings; |
| 24 | |
Jens Geyer | c214572 | 2015-04-02 22:54:02 +0200 | [diff] [blame] | 25 | use Thrift::BinaryProtocol; |
Jens Geyer | c214572 | 2015-04-02 22:54:02 +0200 | [diff] [blame] | 26 | use Thrift::FramedTransport; |
| 27 | use Thrift::MemoryBuffer; |
James E. King, III | 177c37c | 2017-03-30 17:09:04 -0400 | [diff] [blame^] | 28 | use Thrift::MessageType; |
| 29 | use Thrift::MultiplexedProcessor; |
| 30 | use Thrift::Server; |
| 31 | use Thrift::Socket; |
Jens Geyer | c214572 | 2015-04-02 22:54:02 +0200 | [diff] [blame] | 32 | |
| 33 | use BenchmarkService; |
| 34 | use Aggr; |
| 35 | |
| 36 | use constant NAME_BENCHMARKSERVICE => 'BenchmarkService'; |
| 37 | use constant NAME_AGGR => 'Aggr'; |
| 38 | |
| 39 | my $buffer = Thrift::MemoryBuffer->new(1024); |
| 40 | my $aggr_protocol = Thrift::MultiplexedProtocol->new(Thrift::BinaryProtocol->new($buffer), NAME_AGGR); |
| 41 | my $aggr_client = AggrClient->new($aggr_protocol); |
| 42 | my $benchmark_protocol = Thrift::MultiplexedProtocol->new(Thrift::BinaryProtocol->new($buffer), NAME_BENCHMARKSERVICE); |
| 43 | my $benchmark_client = BenchmarkServiceClient->new($benchmark_protocol); |
| 44 | |
| 45 | $buffer->open(); |
| 46 | |
| 47 | for(my $i = 1; $i <= 5; $i++) { |
| 48 | $aggr_client->send_addValue($i); |
| 49 | $aggr_client->{seqid}++; |
| 50 | } |
| 51 | |
| 52 | $aggr_client->send_getValues(); |
| 53 | |
| 54 | for(my $i = 1; $i <= 5; $i++) { |
| 55 | $benchmark_client->send_fibonacci($i); |
| 56 | $benchmark_client->{seqid}++; |
| 57 | } |
| 58 | $benchmark_client->{seqid}--; |
| 59 | |
| 60 | my $client_command_binary = $buffer->getBuffer; |
| 61 | $buffer->resetBuffer; |
| 62 | |
| 63 | |
| 64 | # Process by server |
| 65 | my $server_output_binary; |
| 66 | { |
| 67 | my $benchmark_handler = My::BenchmarkService->new(); |
| 68 | my $benchmark_processor = BenchmarkServiceProcessor->new($benchmark_handler); |
| 69 | my $aggr_handler = My::Aggr->new(); |
| 70 | my $aggr_processor = AggrProcessor->new($aggr_handler); |
| 71 | |
| 72 | my $protocol_factory = Thrift::BinaryProtocolFactory->new(); |
| 73 | |
| 74 | my $input_buffer = Thrift::MemoryBuffer->new(); |
| 75 | $input_buffer->write($client_command_binary); |
| 76 | |
| 77 | my $input_protocol = $protocol_factory->getProtocol($input_buffer); |
| 78 | |
| 79 | my $output_buffer = Thrift::MemoryBuffer->new(); |
| 80 | my $output_protocol = $protocol_factory->getProtocol($output_buffer); |
| 81 | |
| 82 | my $processor = Thrift::MultiplexedProcessor->new(); |
| 83 | |
| 84 | $processor->registerProcessor(NAME_BENCHMARKSERVICE, $benchmark_processor); |
| 85 | $processor->registerProcessor(NAME_AGGR, $aggr_processor); |
| 86 | my $result; |
| 87 | for(my $i = 1; $i <= 11; $i++) { |
| 88 | $result = $processor->process($input_protocol, $output_protocol); |
| 89 | print "process resulted in $result\n"; |
| 90 | } |
| 91 | |
| 92 | $server_output_binary = $output_buffer->getBuffer(); |
| 93 | } |
| 94 | |
| 95 | $buffer->write($server_output_binary); |
| 96 | |
| 97 | |
| 98 | |
| 99 | for(my $i = 1; $i <= 5; $i++) { |
| 100 | my ($function_name, $message_type, $sequence_id); |
| 101 | |
| 102 | $aggr_protocol->readMessageBegin(\$function_name, \$message_type, \$sequence_id); |
| 103 | |
James E. King, III | 177c37c | 2017-03-30 17:09:04 -0400 | [diff] [blame^] | 104 | if ($message_type == Thrift::TMessageType::EXCEPTION) { |
Jens Geyer | c214572 | 2015-04-02 22:54:02 +0200 | [diff] [blame] | 105 | die; |
| 106 | } |
| 107 | |
| 108 | my $aggr_result = Aggr_addValue_result->new(); |
| 109 | $aggr_result->read($aggr_protocol); |
| 110 | $aggr_protocol->readMessageEnd(); |
| 111 | } |
| 112 | |
| 113 | my ($function_name, $message_type, $sequence_id); |
| 114 | |
| 115 | $aggr_protocol->readMessageBegin(\$function_name, \$message_type, \$sequence_id); |
| 116 | |
James E. King, III | 177c37c | 2017-03-30 17:09:04 -0400 | [diff] [blame^] | 117 | if ($message_type == Thrift::TMessageType::EXCEPTION) { |
Jens Geyer | c214572 | 2015-04-02 22:54:02 +0200 | [diff] [blame] | 118 | die; |
| 119 | } |
| 120 | |
| 121 | my $aggr_result = Aggr_getValues_result->new(); |
| 122 | $aggr_result->read($aggr_protocol); |
| 123 | $aggr_protocol->readMessageEnd(); |
| 124 | |
| 125 | is_deeply($aggr_result->success(), [1,2,3,4,5]); |
| 126 | |
| 127 | |
| 128 | foreach my $val((1,2,3,5,8)) { |
| 129 | my ($function_name, $message_type, $sequence_id); |
| 130 | |
| 131 | $benchmark_protocol->readMessageBegin(\$function_name, \$message_type, \$sequence_id); |
| 132 | |
James E. King, III | 177c37c | 2017-03-30 17:09:04 -0400 | [diff] [blame^] | 133 | if ($message_type == Thrift::TMessageType::EXCEPTION) { |
Jens Geyer | c214572 | 2015-04-02 22:54:02 +0200 | [diff] [blame] | 134 | die; |
| 135 | } |
| 136 | my $benchmark_result = BenchmarkService_fibonacci_result->new(); |
| 137 | $benchmark_result->read($benchmark_protocol); |
| 138 | $benchmark_protocol->readMessageEnd(); |
| 139 | |
| 140 | is($benchmark_result->success(), $val); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | package My::Aggr; |
| 145 | use base qw(AggrIf); |
| 146 | |
| 147 | use strict; |
| 148 | use warnings; |
| 149 | |
| 150 | sub new { |
| 151 | my $classname = shift; |
| 152 | my $self = {}; |
| 153 | |
| 154 | $self->{values} = (); |
| 155 | |
| 156 | return bless($self,$classname); |
| 157 | } |
| 158 | |
| 159 | sub addValue{ |
| 160 | my $self = shift; |
| 161 | my $value = shift; |
| 162 | |
| 163 | push (@{$self->{values}}, $value); |
| 164 | } |
| 165 | |
| 166 | sub getValues{ |
| 167 | my $self = shift; |
| 168 | |
| 169 | return $self->{values}; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | |
| 174 | package My::BenchmarkService; |
| 175 | use base qw(BenchmarkServiceIf); |
| 176 | |
| 177 | use strict; |
| 178 | use warnings; |
| 179 | |
| 180 | sub new { |
| 181 | my $class = shift; |
| 182 | return bless {}, $class; |
| 183 | } |
| 184 | |
| 185 | sub fibonacci { |
| 186 | my ($self, $n) = @_; |
| 187 | |
| 188 | my $prev = 0; |
| 189 | my $next; |
| 190 | my $result = 1; |
| 191 | |
| 192 | while ($n > 0) { |
| 193 | $next = $result + $prev; |
| 194 | $prev = $result; |
| 195 | $result = $next; |
| 196 | --$n; |
| 197 | } |
| 198 | |
| 199 | return $result; |
| 200 | } |
| 201 | |