Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 1 | %% |
| 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 Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 20 | -module(multiplexing_test). |
| 21 | |
| 22 | -include_lib("eunit/include/eunit.hrl"). |
| 23 | |
| 24 | -export([ |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 25 | handle_function/2, |
| 26 | handle_error/2 |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 27 | ]). |
| 28 | |
| 29 | start_multiplexed_server_test() -> |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 30 | Port = 9090, |
| 31 | Services = [ |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 32 | {"Multiplexing_Calculator", multiplexing__calculator_thrift}, |
| 33 | {"Multiplexing_WeatherReport", multiplexing__weather_report_thrift} |
| 34 | ], |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 35 | |
| 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 Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 42 | {"error_handler", ?MODULE}, |
| 43 | {"Multiplexing_Calculator", ?MODULE}, |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 44 | {"Multiplexing_WeatherReport", ?MODULE} |
| 45 | ]} |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 46 | ]), |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 47 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 48 | {ok, [ |
| 49 | {"Multiplexing_Calculator", CalculatorClient0}, |
| 50 | {"Multiplexing_WeatherReport", WeatherReportClient0} |
| 51 | ]} = thrift_client_util:new_multiplexed("127.0.0.1", Port, Services, []), |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 52 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 53 | ?assertMatch( |
| 54 | {_, {error, {bad_args, _, _}}}, |
| 55 | thrift_client:call(WeatherReportClient0, getTemperature, [1]) |
| 56 | ), |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 57 | ?assertMatch({_, {error, {bad_args, _, _}}}, thrift_client:call(CalculatorClient0, add, [1])), |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 58 | ?assertMatch( |
| 59 | {_, {error, {bad_args, _, _}}}, thrift_client:call(CalculatorClient0, add, [1, 1, 1]) |
| 60 | ), |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 61 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 62 | ?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 Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 68 | |
| 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 |
| 77 | handle_function(add, {X, Y}) -> |
| 78 | {reply, X + Y}; |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 79 | %% WeatherReport handles |
| 80 | handle_function(getTemperature, {}) -> |
| 81 | {reply, 42.0}. |
| 82 | |
| 83 | handle_error(_F, _Reason) -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 84 | %% ?debugHere, ?debugVal({_F, _Reason}), |
| 85 | ok. |