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 | |
| 25 | use Thrift; |
| 26 | use Thrift::Socket; |
| 27 | use Thrift::Server; |
| 28 | use Thrift::MultiplexedProcessor; |
| 29 | use Thrift::BinaryProtocol; |
| 30 | use Thrift::MemoryBuffer; |
| 31 | use Thrift::FramedTransport; |
| 32 | use Thrift::MemoryBuffer; |
| 33 | |
| 34 | |
| 35 | use BenchmarkService; |
| 36 | use Aggr; |
| 37 | |
| 38 | use constant NAME_BENCHMARKSERVICE => 'BenchmarkService'; |
| 39 | use constant NAME_AGGR => 'Aggr'; |
| 40 | |
| 41 | my $buffer = Thrift::MemoryBuffer->new(1024); |
| 42 | my $aggr_protocol = Thrift::MultiplexedProtocol->new(Thrift::BinaryProtocol->new($buffer), NAME_AGGR); |
| 43 | my $aggr_client = AggrClient->new($aggr_protocol); |
| 44 | my $benchmark_protocol = Thrift::MultiplexedProtocol->new(Thrift::BinaryProtocol->new($buffer), NAME_BENCHMARKSERVICE); |
| 45 | my $benchmark_client = BenchmarkServiceClient->new($benchmark_protocol); |
| 46 | |
| 47 | $buffer->open(); |
| 48 | |
| 49 | for(my $i = 1; $i <= 5; $i++) { |
| 50 | $aggr_client->send_addValue($i); |
| 51 | $aggr_client->{seqid}++; |
| 52 | } |
| 53 | |
| 54 | $aggr_client->send_getValues(); |
| 55 | |
| 56 | for(my $i = 1; $i <= 5; $i++) { |
| 57 | $benchmark_client->send_fibonacci($i); |
| 58 | $benchmark_client->{seqid}++; |
| 59 | } |
| 60 | $benchmark_client->{seqid}--; |
| 61 | |
| 62 | my $client_command_binary = $buffer->getBuffer; |
| 63 | $buffer->resetBuffer; |
| 64 | |
| 65 | |
| 66 | # Process by server |
| 67 | my $server_output_binary; |
| 68 | { |
| 69 | my $benchmark_handler = My::BenchmarkService->new(); |
| 70 | my $benchmark_processor = BenchmarkServiceProcessor->new($benchmark_handler); |
| 71 | my $aggr_handler = My::Aggr->new(); |
| 72 | my $aggr_processor = AggrProcessor->new($aggr_handler); |
| 73 | |
| 74 | my $protocol_factory = Thrift::BinaryProtocolFactory->new(); |
| 75 | |
| 76 | my $input_buffer = Thrift::MemoryBuffer->new(); |
| 77 | $input_buffer->write($client_command_binary); |
| 78 | |
| 79 | my $input_protocol = $protocol_factory->getProtocol($input_buffer); |
| 80 | |
| 81 | my $output_buffer = Thrift::MemoryBuffer->new(); |
| 82 | my $output_protocol = $protocol_factory->getProtocol($output_buffer); |
| 83 | |
| 84 | my $processor = Thrift::MultiplexedProcessor->new(); |
| 85 | |
| 86 | $processor->registerProcessor(NAME_BENCHMARKSERVICE, $benchmark_processor); |
| 87 | $processor->registerProcessor(NAME_AGGR, $aggr_processor); |
| 88 | my $result; |
| 89 | for(my $i = 1; $i <= 11; $i++) { |
| 90 | $result = $processor->process($input_protocol, $output_protocol); |
| 91 | print "process resulted in $result\n"; |
| 92 | } |
| 93 | |
| 94 | $server_output_binary = $output_buffer->getBuffer(); |
| 95 | } |
| 96 | |
| 97 | $buffer->write($server_output_binary); |
| 98 | |
| 99 | |
| 100 | |
| 101 | for(my $i = 1; $i <= 5; $i++) { |
| 102 | my ($function_name, $message_type, $sequence_id); |
| 103 | |
| 104 | $aggr_protocol->readMessageBegin(\$function_name, \$message_type, \$sequence_id); |
| 105 | |
| 106 | if ($message_type == TMessageType::EXCEPTION) { |
| 107 | die; |
| 108 | } |
| 109 | |
| 110 | my $aggr_result = Aggr_addValue_result->new(); |
| 111 | $aggr_result->read($aggr_protocol); |
| 112 | $aggr_protocol->readMessageEnd(); |
| 113 | } |
| 114 | |
| 115 | my ($function_name, $message_type, $sequence_id); |
| 116 | |
| 117 | $aggr_protocol->readMessageBegin(\$function_name, \$message_type, \$sequence_id); |
| 118 | |
| 119 | if ($message_type == TMessageType::EXCEPTION) { |
| 120 | die; |
| 121 | } |
| 122 | |
| 123 | my $aggr_result = Aggr_getValues_result->new(); |
| 124 | $aggr_result->read($aggr_protocol); |
| 125 | $aggr_protocol->readMessageEnd(); |
| 126 | |
| 127 | is_deeply($aggr_result->success(), [1,2,3,4,5]); |
| 128 | |
| 129 | |
| 130 | foreach my $val((1,2,3,5,8)) { |
| 131 | my ($function_name, $message_type, $sequence_id); |
| 132 | |
| 133 | $benchmark_protocol->readMessageBegin(\$function_name, \$message_type, \$sequence_id); |
| 134 | |
| 135 | if ($message_type == TMessageType::EXCEPTION) { |
| 136 | die; |
| 137 | } |
| 138 | my $benchmark_result = BenchmarkService_fibonacci_result->new(); |
| 139 | $benchmark_result->read($benchmark_protocol); |
| 140 | $benchmark_protocol->readMessageEnd(); |
| 141 | |
| 142 | is($benchmark_result->success(), $val); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | package My::Aggr; |
| 147 | use base qw(AggrIf); |
| 148 | |
| 149 | use strict; |
| 150 | use warnings; |
| 151 | |
| 152 | sub new { |
| 153 | my $classname = shift; |
| 154 | my $self = {}; |
| 155 | |
| 156 | $self->{values} = (); |
| 157 | |
| 158 | return bless($self,$classname); |
| 159 | } |
| 160 | |
| 161 | sub addValue{ |
| 162 | my $self = shift; |
| 163 | my $value = shift; |
| 164 | |
| 165 | push (@{$self->{values}}, $value); |
| 166 | } |
| 167 | |
| 168 | sub getValues{ |
| 169 | my $self = shift; |
| 170 | |
| 171 | return $self->{values}; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | package My::BenchmarkService; |
| 177 | use base qw(BenchmarkServiceIf); |
| 178 | |
| 179 | use strict; |
| 180 | use warnings; |
| 181 | |
| 182 | sub new { |
| 183 | my $class = shift; |
| 184 | return bless {}, $class; |
| 185 | } |
| 186 | |
| 187 | sub fibonacci { |
| 188 | my ($self, $n) = @_; |
| 189 | |
| 190 | my $prev = 0; |
| 191 | my $next; |
| 192 | my $result = 1; |
| 193 | |
| 194 | while ($n > 0) { |
| 195 | $next = $result + $prev; |
| 196 | $prev = $result; |
| 197 | $result = $next; |
| 198 | --$n; |
| 199 | } |
| 200 | |
| 201 | return $result; |
| 202 | } |
| 203 | |