1-module(multiplexing_test).
2
3-include_lib("eunit/include/eunit.hrl").
4
5-export([
6     handle_function/2
7    ,handle_error/2
8]).
9
10start_multiplexed_server_test() ->
11
12    Port = 9090,
13    Services = [
14                {"Multiplexing_Calculator",    multiplexing__calculator_thrift},
15                {"Multiplexing_WeatherReport", multiplexing__weather_report_thrift}
16               ],
17
18    {ok, Pid} = thrift_socket_server:start([
19        {ip, "127.0.0.1"},
20        {port, Port},
21        {name, ?MODULE},
22        {service, Services},
23        {handler, [
24            {"error_handler",              ?MODULE},
25            {"Multiplexing_Calculator",    ?MODULE},
26            {"Multiplexing_WeatherReport", ?MODULE}
27        ]}
28     ]),
29
30    {ok, [{"Multiplexing_Calculator", CalculatorClient0},
31          {"Multiplexing_WeatherReport", WeatherReportClient0}]} = thrift_client_util:new_multiplexed("127.0.0.1", Port, Services, []),
32
33    ?assertMatch({_, {error, {bad_args, _, _}}}, thrift_client:call(WeatherReportClient0, getTemperature, [1])),
34    ?assertMatch({_, {error, {bad_args, _, _}}}, thrift_client:call(CalculatorClient0, add, [1])),
35    ?assertMatch({_, {error, {bad_args, _, _}}}, thrift_client:call(CalculatorClient0, add, [1,1,1])),
36
37    ?assertMatch({_, {error, {no_function, _}}}, thrift_client:call(CalculatorClient0, getTemperature, [])),
38    ?assertMatch({_, {error, {no_function, _}}}, thrift_client:call(WeatherReportClient0, add, [41, 1])),
39
40    ?assertMatch({_, {ok, 42}}, thrift_client:call(CalculatorClient0, add, [41, 1])),
41    ?assertMatch({_, {ok, 42.0}}, thrift_client:call(WeatherReportClient0, getTemperature, [])),
42
43    thrift_socket_server:stop(Pid).
44
45%% HANDLE FUNCTIONS
46
47%% Calculator handles
48handle_function(add, {X, Y}) ->
49    {reply, X + Y};
50
51%% WeatherReport handles
52handle_function(getTemperature, {}) ->
53    {reply, 42.0}.
54
55handle_error(_F, _Reason) ->
56%%     ?debugHere, ?debugVal({_F, _Reason}),
57    ok.