|
| 1 | +%% @doc Integration tests for pool connection release behavior |
| 2 | +-module(hackney_pool_integration_tests). |
| 3 | +-include_lib("eunit/include/eunit.hrl"). |
| 4 | + |
| 5 | +-define(PORT, 9877). |
| 6 | +-define(POOL, test_pool_integration). |
| 7 | + |
| 8 | +%% Setup/teardown for integration tests |
| 9 | +setup() -> |
| 10 | + {ok, _} = application:ensure_all_started(hackney), |
| 11 | + {ok, _} = application:ensure_all_started(cowboy), |
| 12 | + Dispatch = cowboy_router:compile([{'_', [{"/[...]", test_http_resource, []}]}]), |
| 13 | + {ok, _} = cowboy:start_clear(test_pool_int_http, [{port, ?PORT}], #{ |
| 14 | + env => #{dispatch => Dispatch} |
| 15 | + }), |
| 16 | + %% Create a test pool with small limits for easy testing |
| 17 | + hackney_pool:start_pool(?POOL, [{max_connections, 10}]), |
| 18 | + ok. |
| 19 | + |
| 20 | +cleanup(_) -> |
| 21 | + hackney_pool:stop_pool(?POOL), |
| 22 | + cowboy:stop_listener(test_pool_int_http), |
| 23 | + ok. |
| 24 | + |
| 25 | +url(Path) -> |
| 26 | + <<"http://localhost:", (integer_to_binary(?PORT))/binary, Path/binary>>. |
| 27 | + |
| 28 | +%% ============================================================================= |
| 29 | +%% Pool Integration Tests |
| 30 | +%% ============================================================================= |
| 31 | + |
| 32 | +pool_integration_test_() -> |
| 33 | + {setup, |
| 34 | + fun setup/0, |
| 35 | + fun cleanup/1, |
| 36 | + [ |
| 37 | + {"connection released after with_body request", fun test_with_body_release/0}, |
| 38 | + {"connection released after manual body read", fun test_manual_body_release/0}, |
| 39 | + {"connection released after close", fun test_close_release/0}, |
| 40 | + {"multiple requests reuse connections", fun test_connection_reuse_integration/0}, |
| 41 | + {"concurrent requests respect pool limits", fun test_concurrent_requests/0}, |
| 42 | + {"connection released on error response", fun test_error_response_release/0}, |
| 43 | + {"pool stats accurate during requests", fun test_pool_stats_accuracy/0} |
| 44 | + ]}. |
| 45 | + |
| 46 | +%% Test that connection is released after with_body request |
| 47 | +test_with_body_release() -> |
| 48 | + %% Get initial stats |
| 49 | + InitStats = hackney_pool:get_stats(?POOL), |
| 50 | + InitFree = proplists:get_value(free_count, InitStats), |
| 51 | + InitInUse = proplists:get_value(in_use_count, InitStats), |
| 52 | + |
| 53 | + %% Make request with with_body |
| 54 | + {ok, 200, _Headers, _Body} = hackney:request(get, url(<<"/get">>), [], <<>>, |
| 55 | + [{pool, ?POOL}, {with_body, true}]), |
| 56 | + |
| 57 | + %% Allow time for async checkin |
| 58 | + timer:sleep(50), |
| 59 | + |
| 60 | + %% Check stats after - should have one more free connection |
| 61 | + AfterStats = hackney_pool:get_stats(?POOL), |
| 62 | + AfterFree = proplists:get_value(free_count, AfterStats), |
| 63 | + AfterInUse = proplists:get_value(in_use_count, AfterStats), |
| 64 | + |
| 65 | + %% Connection should be returned to pool (free increased or same) |
| 66 | + ?assert(AfterFree >= InitFree orelse AfterInUse =< InitInUse), |
| 67 | + %% No connections should be in use |
| 68 | + ?assertEqual(0, AfterInUse). |
| 69 | + |
| 70 | +%% Test that connection is released after manual body read |
| 71 | +test_manual_body_release() -> |
| 72 | + %% Make request without with_body |
| 73 | + {ok, 200, _Headers, Ref} = hackney:request(get, url(<<"/get">>), [], <<>>, |
| 74 | + [{pool, ?POOL}]), |
| 75 | + |
| 76 | + %% Check stats during - connection should be in use |
| 77 | + DuringStats = hackney_pool:get_stats(?POOL), |
| 78 | + DuringInUse = proplists:get_value(in_use_count, DuringStats), |
| 79 | + ?assert(DuringInUse >= 1), |
| 80 | + |
| 81 | + %% Read body |
| 82 | + {ok, _Body} = hackney:body(Ref), |
| 83 | + |
| 84 | + %% Allow time for async checkin |
| 85 | + timer:sleep(50), |
| 86 | + |
| 87 | + %% Check stats after - no connections in use |
| 88 | + AfterStats = hackney_pool:get_stats(?POOL), |
| 89 | + AfterInUse = proplists:get_value(in_use_count, AfterStats), |
| 90 | + ?assertEqual(0, AfterInUse). |
| 91 | + |
| 92 | +%% Test that connection is released after explicit close |
| 93 | +test_close_release() -> |
| 94 | + %% Make request without reading body |
| 95 | + {ok, 200, _Headers, Ref} = hackney:request(get, url(<<"/get">>), [], <<>>, |
| 96 | + [{pool, ?POOL}]), |
| 97 | + |
| 98 | + %% Check stats during |
| 99 | + DuringStats = hackney_pool:get_stats(?POOL), |
| 100 | + DuringInUse = proplists:get_value(in_use_count, DuringStats), |
| 101 | + ?assert(DuringInUse >= 1), |
| 102 | + |
| 103 | + %% Close without reading body |
| 104 | + ok = hackney:close(Ref), |
| 105 | + |
| 106 | + %% Allow time for process cleanup |
| 107 | + timer:sleep(50), |
| 108 | + |
| 109 | + %% Check stats after - no connections in use |
| 110 | + AfterStats = hackney_pool:get_stats(?POOL), |
| 111 | + AfterInUse = proplists:get_value(in_use_count, AfterStats), |
| 112 | + ?assertEqual(0, AfterInUse). |
| 113 | + |
| 114 | +%% Test that connections are reused |
| 115 | +test_connection_reuse_integration() -> |
| 116 | + %% Make first request |
| 117 | + {ok, 200, _, _} = hackney:request(get, url(<<"/get">>), [], <<>>, |
| 118 | + [{pool, ?POOL}, {with_body, true}]), |
| 119 | + timer:sleep(50), |
| 120 | + |
| 121 | + Stats1 = hackney_pool:get_stats(?POOL), |
| 122 | + Free1 = proplists:get_value(free_count, Stats1), |
| 123 | + |
| 124 | + %% Make second request - should reuse connection |
| 125 | + {ok, 200, _, _} = hackney:request(get, url(<<"/get">>), [], <<>>, |
| 126 | + [{pool, ?POOL}, {with_body, true}]), |
| 127 | + timer:sleep(50), |
| 128 | + |
| 129 | + Stats2 = hackney_pool:get_stats(?POOL), |
| 130 | + Free2 = proplists:get_value(free_count, Stats2), |
| 131 | + |
| 132 | + %% Should have same number of free connections (connection reused) |
| 133 | + ?assertEqual(Free1, Free2). |
| 134 | + |
| 135 | +%% Test concurrent requests respect pool limits |
| 136 | +test_concurrent_requests() -> |
| 137 | + %% Start 5 concurrent requests |
| 138 | + Self = self(), |
| 139 | + NumRequests = 5, |
| 140 | + |
| 141 | + Pids = [spawn(fun() -> |
| 142 | + Result = hackney:request(get, url(<<"/get">>), [], <<>>, |
| 143 | + [{pool, ?POOL}, {with_body, true}]), |
| 144 | + Self ! {done, self(), Result} |
| 145 | + end) || _ <- lists:seq(1, NumRequests)], |
| 146 | + |
| 147 | + %% Collect results |
| 148 | + Results = [receive {done, Pid, R} -> R end || Pid <- Pids], |
| 149 | + |
| 150 | + %% All should succeed |
| 151 | + lists:foreach(fun(R) -> |
| 152 | + ?assertMatch({ok, 200, _, _}, R) |
| 153 | + end, Results), |
| 154 | + |
| 155 | + %% Allow time for cleanup |
| 156 | + timer:sleep(100), |
| 157 | + |
| 158 | + %% No connections in use after all complete |
| 159 | + Stats = hackney_pool:get_stats(?POOL), |
| 160 | + InUse = proplists:get_value(in_use_count, Stats), |
| 161 | + ?assertEqual(0, InUse). |
| 162 | + |
| 163 | +%% Test connection released after error response (4xx/5xx) |
| 164 | +test_error_response_release() -> |
| 165 | + %% Make request to 404 endpoint |
| 166 | + {ok, 404, _Headers, Body} = hackney:request(get, url(<<"/not-found">>), [], <<>>, |
| 167 | + [{pool, ?POOL}, {with_body, true}]), |
| 168 | + ?assertMatch(<<"{\"error\":", _/binary>>, Body), |
| 169 | + |
| 170 | + timer:sleep(50), |
| 171 | + |
| 172 | + %% Connection should still be returned to pool |
| 173 | + Stats = hackney_pool:get_stats(?POOL), |
| 174 | + InUse = proplists:get_value(in_use_count, Stats), |
| 175 | + ?assertEqual(0, InUse). |
| 176 | + |
| 177 | +%% Test pool stats are accurate during request lifecycle |
| 178 | +test_pool_stats_accuracy() -> |
| 179 | + %% Initial state |
| 180 | + Stats0 = hackney_pool:get_stats(?POOL), |
| 181 | + InUse0 = proplists:get_value(in_use_count, Stats0), |
| 182 | + |
| 183 | + %% Start request without with_body |
| 184 | + {ok, 200, _Headers, Ref} = hackney:request(get, url(<<"/get">>), [], <<>>, |
| 185 | + [{pool, ?POOL}]), |
| 186 | + |
| 187 | + %% During request - connection in use |
| 188 | + Stats1 = hackney_pool:get_stats(?POOL), |
| 189 | + InUse1 = proplists:get_value(in_use_count, Stats1), |
| 190 | + ?assertEqual(InUse0 + 1, InUse1), |
| 191 | + |
| 192 | + %% Read body |
| 193 | + {ok, _Body} = hackney:body(Ref), |
| 194 | + timer:sleep(50), |
| 195 | + |
| 196 | + %% After body read - connection returned |
| 197 | + Stats2 = hackney_pool:get_stats(?POOL), |
| 198 | + InUse2 = proplists:get_value(in_use_count, Stats2), |
| 199 | + ?assertEqual(InUse0, InUse2). |
0 commit comments