blob: 5752e53a4d4254e04eb185f13fe239c1f6094700 [file] [log] [blame]
Sergei Elin45764092022-09-23 23:21:31 +03001%%
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
David Robakowskiae971ce2013-08-02 12:16:00 +020020-module(multiplexing_test).
21
22-include_lib("eunit/include/eunit.hrl").
23
24-export([
Sergei Elin45764092022-09-23 23:21:31 +030025 handle_function/2,
26 handle_error/2
David Robakowskiae971ce2013-08-02 12:16:00 +020027]).
28
29start_multiplexed_server_test() ->
David Robakowskiae971ce2013-08-02 12:16:00 +020030 Port = 9090,
31 Services = [
Sergei Elin45764092022-09-23 23:21:31 +030032 {"Multiplexing_Calculator", multiplexing__calculator_thrift},
33 {"Multiplexing_WeatherReport", multiplexing__weather_report_thrift}
34 ],
David Robakowskiae971ce2013-08-02 12:16:00 +020035
36 {ok, Pid} = thrift_socket_server:start([
37 {ip, "127.0.0.1"},
38 {port, Port},
39 {name, ?MODULE},
40 {service, Services},
41 {handler, [
Sergei Elin45764092022-09-23 23:21:31 +030042 {"error_handler", ?MODULE},
43 {"Multiplexing_Calculator", ?MODULE},
David Robakowskiae971ce2013-08-02 12:16:00 +020044 {"Multiplexing_WeatherReport", ?MODULE}
45 ]}
Sergei Elin45764092022-09-23 23:21:31 +030046 ]),
David Robakowskiae971ce2013-08-02 12:16:00 +020047
Sergei Elin45764092022-09-23 23:21:31 +030048 {ok, [
49 {"Multiplexing_Calculator", CalculatorClient0},
50 {"Multiplexing_WeatherReport", WeatherReportClient0}
51 ]} = thrift_client_util:new_multiplexed("127.0.0.1", Port, Services, []),
David Robakowskiae971ce2013-08-02 12:16:00 +020052
Sergei Elin45764092022-09-23 23:21:31 +030053 ?assertMatch(
54 {_, {error, {bad_args, _, _}}},
55 thrift_client:call(WeatherReportClient0, getTemperature, [1])
56 ),
David Robakowskiae971ce2013-08-02 12:16:00 +020057 ?assertMatch({_, {error, {bad_args, _, _}}}, thrift_client:call(CalculatorClient0, add, [1])),
Sergei Elin45764092022-09-23 23:21:31 +030058 ?assertMatch(
59 {_, {error, {bad_args, _, _}}}, thrift_client:call(CalculatorClient0, add, [1, 1, 1])
60 ),
David Robakowskiae971ce2013-08-02 12:16:00 +020061
Sergei Elin45764092022-09-23 23:21:31 +030062 ?assertMatch(
63 {_, {error, {no_function, _}}}, thrift_client:call(CalculatorClient0, getTemperature, [])
64 ),
65 ?assertMatch(
66 {_, {error, {no_function, _}}}, thrift_client:call(WeatherReportClient0, add, [41, 1])
67 ),
David Robakowskiae971ce2013-08-02 12:16:00 +020068
69 ?assertMatch({_, {ok, 42}}, thrift_client:call(CalculatorClient0, add, [41, 1])),
70 ?assertMatch({_, {ok, 42.0}}, thrift_client:call(WeatherReportClient0, getTemperature, [])),
71
72 thrift_socket_server:stop(Pid).
73
74%% HANDLE FUNCTIONS
75
76%% Calculator handles
77handle_function(add, {X, Y}) ->
78 {reply, X + Y};
David Robakowskiae971ce2013-08-02 12:16:00 +020079%% WeatherReport handles
80handle_function(getTemperature, {}) ->
81 {reply, 42.0}.
82
83handle_error(_F, _Reason) ->
Sergei Elin45764092022-09-23 23:21:31 +030084 %% ?debugHere, ?debugVal({_F, _Reason}),
85 ok.