blob: 76f2706ffe340bb48f35a89da38e7aadb21b2dc0 [file] [log] [blame]
Jens Geyerc2145722015-04-02 22:54:02 +02001#
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
20use Test::More tests => 6;
21
22use strict;
23use warnings;
24
25use Thrift;
26use Thrift::Socket;
27use Thrift::Server;
28use Thrift::MultiplexedProcessor;
29use Thrift::BinaryProtocol;
30use Thrift::MemoryBuffer;
31use Thrift::FramedTransport;
32use Thrift::MemoryBuffer;
33
34
35use BenchmarkService;
36use Aggr;
37
38use constant NAME_BENCHMARKSERVICE => 'BenchmarkService';
39use constant NAME_AGGR => 'Aggr';
40
41my $buffer = Thrift::MemoryBuffer->new(1024);
42my $aggr_protocol = Thrift::MultiplexedProtocol->new(Thrift::BinaryProtocol->new($buffer), NAME_AGGR);
43my $aggr_client = AggrClient->new($aggr_protocol);
44my $benchmark_protocol = Thrift::MultiplexedProtocol->new(Thrift::BinaryProtocol->new($buffer), NAME_BENCHMARKSERVICE);
45my $benchmark_client = BenchmarkServiceClient->new($benchmark_protocol);
46
47$buffer->open();
48
49for(my $i = 1; $i <= 5; $i++) {
50 $aggr_client->send_addValue($i);
51 $aggr_client->{seqid}++;
52}
53
54$aggr_client->send_getValues();
55
56for(my $i = 1; $i <= 5; $i++) {
57 $benchmark_client->send_fibonacci($i);
58 $benchmark_client->{seqid}++;
59}
60$benchmark_client->{seqid}--;
61
62my $client_command_binary = $buffer->getBuffer;
63$buffer->resetBuffer;
64
65
66# Process by server
67my $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
101for(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
115my ($function_name, $message_type, $sequence_id);
116
117$aggr_protocol->readMessageBegin(\$function_name, \$message_type, \$sequence_id);
118
119if ($message_type == TMessageType::EXCEPTION) {
120 die;
121}
122
123my $aggr_result = Aggr_getValues_result->new();
124$aggr_result->read($aggr_protocol);
125$aggr_protocol->readMessageEnd();
126
127is_deeply($aggr_result->success(), [1,2,3,4,5]);
128
129
130foreach 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
146package My::Aggr;
147use base qw(AggrIf);
148
149use strict;
150use warnings;
151
152sub new {
153 my $classname = shift;
154 my $self = {};
155
156 $self->{values} = ();
157
158 return bless($self,$classname);
159}
160
161sub addValue{
162 my $self = shift;
163 my $value = shift;
164
165 push (@{$self->{values}}, $value);
166}
167
168sub getValues{
169 my $self = shift;
170
171 return $self->{values};
172}
173
174
175
176package My::BenchmarkService;
177use base qw(BenchmarkServiceIf);
178
179use strict;
180use warnings;
181
182sub new {
183 my $class = shift;
184 return bless {}, $class;
185}
186
187sub 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