blob: 86760823801251b25738c1330f48b19bc2831d9b [file] [log] [blame]
Jens Geyere5ff9a82014-11-11 01:39:38 +01001/*
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
Jens Geyera420a242025-02-07 01:58:30 +010020package tests;
21import tests.TestBase;
Jens Geyere5ff9a82014-11-11 01:39:38 +010022
Jens Geyer18564d22022-06-05 11:36:40 +020023#if ! (flash || html5 || js)
24
Jens Geyere5ff9a82014-11-11 01:39:38 +010025import haxe.Int64;
26import haxe.Int32;
27
28import org.apache.thrift.*;
29import org.apache.thrift.protocol.*;
30import org.apache.thrift.transport.*;
31import org.apache.thrift.server.*;
32import org.apache.thrift.meta_data.*;
33
34// debug only
35import org.apache.thrift.protocol.TProtocolDecorator;
36import org.apache.thrift.protocol.TMultiplexedProtocol;
37import org.apache.thrift.protocol.TMultiplexedProcessor;
38
39// generated code imports
40import Aggr;
41import AggrImpl;
42import AggrProcessor;
43import BenchmarkService;
44import BenchmarkServiceImpl;
45import BenchmarkServiceProcessor;
46import Error;
47
Jens Geyerce1d3142022-06-06 14:29:38 +020048class BenchmarkServiceHandler implements BenchmarkService_service
Jens Geyere5ff9a82014-11-11 01:39:38 +010049{
50 public function new() {
51 }
52
Jens Geyer76283922022-06-11 14:24:33 +020053 public function fibonacci(n : haxe.Int32) : haxe.Int32 {
Jens Geyere5ff9a82014-11-11 01:39:38 +010054 trace('Benchmark.fibonacci($n)');
55 var next : Int;
56 var prev = 0;
57 var result = 1;
58 while( n > 0)
59 {
60 next = result + prev;
61 prev = result;
62 result = next;
63 --n;
64 }
Jens Geyer76283922022-06-11 14:24:33 +020065 return result;
Jens Geyere5ff9a82014-11-11 01:39:38 +010066 }
67}
68
69
Jens Geyerce1d3142022-06-06 14:29:38 +020070class AggrServiceHandler implements Aggr_service
Jens Geyere5ff9a82014-11-11 01:39:38 +010071{
72 private var values : List<haxe.Int32> = new List<haxe.Int32>();
73
74 public function new() {
75 }
76
77 public function addValue(value : haxe.Int32) : Void {
78 trace('Aggr.addValue($value)');
79 values.add( value);
80 }
81
82 public function getValues() : List< haxe.Int32> {
83 trace('Aggr.getValues()');
84 return values;
85 }
86}
87
88
89
Jens Geyera420a242025-02-07 01:58:30 +010090class MultiplexTest extends tests.TestBase {
Jens Geyere5ff9a82014-11-11 01:39:38 +010091
92 private inline static var NAME_BENCHMARKSERVICE : String = "BenchmarkService";
93 private inline static var NAME_AGGR : String = "Aggr";
94
95
Jens Geyerce1d3142022-06-06 14:29:38 +020096 public static function Run(server : Bool) : Void {
Jens Geyere5ff9a82014-11-11 01:39:38 +010097 if ( server) {
98 RunMultiplexServer();
99 } else {
100 RunMultiplexClient();
101 RunDefaultClient();
102 }
103 }
104
105
106 // run the multiplex server
Jens Geyerce1d3142022-06-06 14:29:38 +0200107 public static function RunMultiplexServer() : Void {
Jens Geyere5ff9a82014-11-11 01:39:38 +0100108 try
109 {
Jens Geyerce1d3142022-06-06 14:29:38 +0200110 var benchHandler : BenchmarkService_service = new BenchmarkServiceHandler();
Jens Geyere5ff9a82014-11-11 01:39:38 +0100111 var benchProcessor : TProcessor = new BenchmarkServiceProcessor( benchHandler);
112
Jens Geyerce1d3142022-06-06 14:29:38 +0200113 var aggrHandler : Aggr_service = new AggrServiceHandler();
Jens Geyere5ff9a82014-11-11 01:39:38 +0100114 var aggrProcessor : TProcessor = new AggrProcessor( aggrHandler);
115
116 var multiplex : TMultiplexedProcessor = new TMultiplexedProcessor();
117 multiplex.RegisterProcessor( NAME_BENCHMARKSERVICE, benchProcessor, true); // default
118 multiplex.RegisterProcessor( NAME_AGGR, aggrProcessor);
119
120 // protocol+transport stack
121 var protfact : TProtocolFactory = new TBinaryProtocolFactory(true,true);
122 var servertrans : TServerTransport = new TServerSocket( 9090, 5, false);
123 var transfact : TTransportFactory = new TFramedTransportFactory();
124
125 var server : TServer = new TSimpleServer( multiplex, servertrans, transfact, protfact);
126
127 trace("Starting the server ...");
128 server.Serve();
129 }
130 catch( e : TApplicationException)
131 {
Jens Geyera420a242025-02-07 01:58:30 +0100132 tests.TestBase.Expect(false,'${e.errorID} ${e.errorMsg}');
Jens Geyere5ff9a82014-11-11 01:39:38 +0100133 }
134 catch( e : TException)
135 {
Jens Geyera420a242025-02-07 01:58:30 +0100136 tests.TestBase.Expect(false,'$e');
Jens Geyere5ff9a82014-11-11 01:39:38 +0100137 }
138 }
139
140
141 // run multiplex client against multiplex server
Jens Geyerce1d3142022-06-06 14:29:38 +0200142 public static function RunMultiplexClient() : Void {
Jens Geyere5ff9a82014-11-11 01:39:38 +0100143 try
144 {
145 var trans : TTransport;
146 trans = new TSocket("localhost", 9090);
147 trans = new TFramedTransport(trans);
148 trans.open();
149
150 var protocol : TProtocol = new TBinaryProtocol(trans,true,true);
151 var multiplex : TMultiplexedProtocol;
152
153 multiplex = new TMultiplexedProtocol( protocol, NAME_BENCHMARKSERVICE);
154 var bench = new BenchmarkServiceImpl( multiplex);
155
156 multiplex = new TMultiplexedProtocol( protocol, NAME_AGGR);
157 var aggr = new AggrImpl( multiplex);
158
159 trace('calling aggr.add( bench.fibo())...');
160 for( i in 1 ... 10)
161 {
162 trace('$i');
163 aggr.addValue( bench.fibonacci(i));
164 }
165
166 trace('calling aggr ...');
167 var i = 1;
168 var values = aggr.getValues();
Jens Geyera420a242025-02-07 01:58:30 +0100169 tests.TestBase.Expect(values != null,'aggr.getValues() == null');
Jens Geyere5ff9a82014-11-11 01:39:38 +0100170 for( k in values)
171 {
172 trace('fib($i) = $k');
173 ++i;
174 }
175
176 trans.close();
177 trace('done.');
178
179 }
180 catch( e : TApplicationException)
181 {
Jens Geyera420a242025-02-07 01:58:30 +0100182 tests.TestBase.Expect(false,'${e.errorID} ${e.errorMsg}');
Jens Geyere5ff9a82014-11-11 01:39:38 +0100183 }
184 catch( e : TException)
185 {
Jens Geyera420a242025-02-07 01:58:30 +0100186 tests.TestBase.Expect(false,'$e');
Jens Geyere5ff9a82014-11-11 01:39:38 +0100187 }
188 }
189
190
191 // run non-multiplex client against multiplex server to test default fallback
Jens Geyerce1d3142022-06-06 14:29:38 +0200192 public static function RunDefaultClient() : Void {
Jens Geyere5ff9a82014-11-11 01:39:38 +0100193 try
194 {
195 var trans : TTransport;
196 trans = new TSocket("localhost", 9090);
197 trans = new TFramedTransport(trans);
198 trans.open();
199
200 var protocol : TProtocol = new TBinaryProtocol(trans,true,true);
201
202 var bench = new BenchmarkServiceImpl( protocol);
203
204 trace('calling bench (via default) ...');
205 for( i in 1 ... 10)
206 {
207 var k = bench.fibonacci(i);
208 trace('fib($i) = $k');
209 }
210
211 trans.close();
212 trace('done.');
213 }
214 catch( e : TApplicationException)
215 {
Jens Geyera420a242025-02-07 01:58:30 +0100216 tests.TestBase.Expect(false,'${e.errorID} ${e.errorMsg}');
Jens Geyere5ff9a82014-11-11 01:39:38 +0100217 }
218 catch( e : TException)
219 {
Jens Geyera420a242025-02-07 01:58:30 +0100220 tests.TestBase.Expect(false,'$e');
Jens Geyere5ff9a82014-11-11 01:39:38 +0100221 }
222 }
223
224}
225
Jens Geyer18564d22022-06-05 11:36:40 +0200226#end
Jens Geyere5ff9a82014-11-11 01:39:38 +0100227