blob: 90a9b4d024286f09c29c34c1f316fea43a37a3f8 [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
Jens Geyerc2145722015-04-02 22:54:02 +020025use Thrift::BinaryProtocol;
Jens Geyerc2145722015-04-02 22:54:02 +020026use Thrift::FramedTransport;
27use Thrift::MemoryBuffer;
James E. King, III177c37c2017-03-30 17:09:04 -040028use Thrift::MessageType;
29use Thrift::MultiplexedProcessor;
30use Thrift::Server;
31use Thrift::Socket;
Jens Geyerc2145722015-04-02 22:54:02 +020032
33use BenchmarkService;
34use Aggr;
35
36use constant NAME_BENCHMARKSERVICE => 'BenchmarkService';
37use constant NAME_AGGR => 'Aggr';
38
39my $buffer = Thrift::MemoryBuffer->new(1024);
40my $aggr_protocol = Thrift::MultiplexedProtocol->new(Thrift::BinaryProtocol->new($buffer), NAME_AGGR);
41my $aggr_client = AggrClient->new($aggr_protocol);
42my $benchmark_protocol = Thrift::MultiplexedProtocol->new(Thrift::BinaryProtocol->new($buffer), NAME_BENCHMARKSERVICE);
43my $benchmark_client = BenchmarkServiceClient->new($benchmark_protocol);
44
45$buffer->open();
46
47for(my $i = 1; $i <= 5; $i++) {
48 $aggr_client->send_addValue($i);
49 $aggr_client->{seqid}++;
50}
51
52$aggr_client->send_getValues();
53
54for(my $i = 1; $i <= 5; $i++) {
55 $benchmark_client->send_fibonacci($i);
56 $benchmark_client->{seqid}++;
57}
58$benchmark_client->{seqid}--;
59
60my $client_command_binary = $buffer->getBuffer;
61$buffer->resetBuffer;
62
63
64# Process by server
65my $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
99for(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, III177c37c2017-03-30 17:09:04 -0400104 if ($message_type == Thrift::TMessageType::EXCEPTION) {
Jens Geyerc2145722015-04-02 22:54:02 +0200105 die;
106 }
107
108 my $aggr_result = Aggr_addValue_result->new();
109 $aggr_result->read($aggr_protocol);
110 $aggr_protocol->readMessageEnd();
111}
112
113my ($function_name, $message_type, $sequence_id);
114
115$aggr_protocol->readMessageBegin(\$function_name, \$message_type, \$sequence_id);
116
James E. King, III177c37c2017-03-30 17:09:04 -0400117if ($message_type == Thrift::TMessageType::EXCEPTION) {
Jens Geyerc2145722015-04-02 22:54:02 +0200118 die;
119}
120
121my $aggr_result = Aggr_getValues_result->new();
122$aggr_result->read($aggr_protocol);
123$aggr_protocol->readMessageEnd();
124
125is_deeply($aggr_result->success(), [1,2,3,4,5]);
126
127
128foreach 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, III177c37c2017-03-30 17:09:04 -0400133 if ($message_type == Thrift::TMessageType::EXCEPTION) {
Jens Geyerc2145722015-04-02 22:54:02 +0200134 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
144package My::Aggr;
145use base qw(AggrIf);
146
147use strict;
148use warnings;
149
150sub new {
151 my $classname = shift;
152 my $self = {};
153
154 $self->{values} = ();
155
156 return bless($self,$classname);
157}
158
159sub addValue{
160 my $self = shift;
161 my $value = shift;
162
163 push (@{$self->{values}}, $value);
164}
165
166sub getValues{
167 my $self = shift;
168
169 return $self->{values};
170}
171
172
173
174package My::BenchmarkService;
175use base qw(BenchmarkServiceIf);
176
177use strict;
178use warnings;
179
180sub new {
181 my $class = shift;
182 return bless {}, $class;
183}
184
185sub 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