1: %%============================================================================== 2: %% Copyright 2016 Erlang Solutions Ltd. 3: %% 4: %% Licensed under the Apache License, Version 2.0 (the "License"); 5: %% you may not use this file except in compliance with the License. 6: %% You may obtain a copy of the License at 7: %% 8: %% http://www.apache.org/licenses/LICENSE-2.0 9: %% 10: %% Unless required by applicable law or agreed to in writing, software 11: %% distributed under the License is distributed on an "AS IS" BASIS, 12: %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13: %% See the License for the specific language governing permissions and 14: %% limitations under the License. 15: %%============================================================================== 16: -module(http_client_SUITE). 17: 18: -compile([export_all, nowarn_export_all]). 19: 20: -include_lib("common_test/include/ct.hrl"). 21: -include_lib("eunit/include/eunit.hrl"). 22: 23: all() -> 24: [get_test, 25: no_pool_test, 26: post_test, 27: request_timeout_test, 28: pool_timeout_test 29: ]. 30: 31: init_per_suite(Config) -> 32: http_helper:start(8080, '_', fun process_request/1), 33: Pid = self(), 34: spawn(fun() -> 35: register(test_helper, self()), 36: mim_ct_sup:start_link(ejabberd_sup), 37: mongoose_wpool:ensure_started(), 38: Pid ! ready, 39: receive stop -> ok end 40: end), 41: receive ready -> ok end, 42: Config. 43: 44: process_request(Req) -> 45: QS = cowboy_req:parse_qs(Req), 46: case proplists:get_value(<<"sleep">>, QS) of 47: <<"true">> -> timer:sleep(100); 48: _ -> ok 49: end, 50: cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, <<"OK">>, Req). 51: 52: end_per_suite(_Config) -> 53: http_helper:stop(), 54: exit(whereis(ejabberd_sup), shutdown), 55: whereis(test_helper) ! stop. 56: 57: init_per_testcase(request_timeout_test, Config) -> 58: mongoose_wpool:start_configured_pools([{http, global, pool(), [], 59: [{server, "http://localhost:8080"}, 60: {request_timeout, 10}]}], 61: [<<"a.com">>]), 62: Config; 63: init_per_testcase(pool_timeout_test, Config) -> 64: mongoose_wpool:start_configured_pools([{http, global, pool(), 65: [{workers, 1}, 66: {max_overflow, 0}, 67: {strategy, available_worker}, 68: {call_timeout, 10}], 69: [{server, "http://localhost:8080"}]}], 70: [<<"a.com">>]), 71: Config; 72: init_per_testcase(_TC, Config) -> 73: mongoose_wpool:start_configured_pools([{http, global, pool(), [], 74: [{server, "http://localhost:8080"}]}], 75: [<<"a.com">>]), 76: Config. 77: 78: end_per_testcase(_TC, _Config) -> 79: mongoose_wpool:stop(http, global, pool()). 80: 81: get_test(_Config) -> 82: Result = mongoose_http_client:get(global, pool(), <<"some/path">>, []), 83: ?assertEqual({ok, {<<"200">>, <<"OK">>}}, Result). 84: 85: no_pool_test(_Config) -> 86: Result = mongoose_http_client:get(global, non_existent_pool, <<"some/path">>, []), 87: ?assertEqual({error, pool_not_started}, Result). 88: 89: post_test(_Config) -> 90: Result = mongoose_http_client:post(global, pool(), <<"some/path">>, [], <<"test request">>), 91: ?assertEqual({ok, {<<"200">>, <<"OK">>}}, Result). 92: 93: request_timeout_test(_Config) -> 94: Result = mongoose_http_client:get(global, pool(), <<"some/path?sleep=true">>, []), 95: ?assertEqual({error, request_timeout}, Result). 96: 97: pool_timeout_test(_Config) -> 98: Pid = self(), 99: spawn(fun() -> 100: mongoose_http_client:get(global, pool(), <<"/some/path?sleep=true">>, []), 101: Pid ! finished 102: end), 103: timer:sleep(10), % wait for the only pool worker to start handling the request 104: Result = mongoose_http_client:get(global, pool(), <<"some/path">>, []), 105: ?assertEqual({error, pool_timeout}, Result), 106: receive finished -> ok after 1000 -> error(no_finished_message) end. 107: 108: pool() -> tmp_pool.