Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lhttpc_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ start_link(Options0) ->
%% @end
%%------------------------------------------------------------------------------
-spec ensure_call(pool_id(), pid(), host(), port_num(), boolean(), options()) ->
socket() | 'no_socket'.
socket() | 'undefined'.
ensure_call(Pool, Pid, Host, Port, Ssl, Options) ->
SocketRequest = {socket, Pid, Host, Port, Ssl},
try gen_server:call(Pool, SocketRequest, infinity) of
Expand Down Expand Up @@ -224,6 +224,8 @@ ensure_call(Pool, Pid, Host, Port, Ssl, Options) ->
case lhttpc:add_pool(Pool, ConnTimeout, PoolMaxSize) of
{ok, _Pid} ->
ensure_call(Pool, Pid, Host, Port, Ssl, Options);
{error, already_exists} ->
ensure_call(Pool, Pid, Host, Port, Ssl, Options);
_ ->
%% Failed to create pool, exit as expected
exit({noproc, Reason})
Expand Down
40 changes: 39 additions & 1 deletion test/lhttpc_manager_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ manager_test_() ->
?_test(one_socket()),
{timeout, 60, ?_test(connection_timeout())},
{timeout, 60, ?_test(many_sockets())},
{timeout, 60, ?_test(closed_race_cond())}
{timeout, 60, ?_test(closed_race_cond())},
{timeout, 60, ?_test(dyn_pool_race())}
]}
}.

Expand Down Expand Up @@ -320,6 +321,43 @@ closed_race_cond() ->
unlink(whereis(lhttpc_manager)),
ok.

dyn_pool_race() ->
?assertEqual(undefined, whereis(mypool)),

%% suspend the supervisor so both process reaches
%% and gets stuck in lhttpc:add_pool/3
erlang:suspend_process(whereis(lhttpc_sup)),

Parent = self(),
Pid1 = spawn_pool_req(Parent),
Pid2 = spawn_pool_req(Parent),

erlang:yield(), % make sure that the spawned processes have run
erlang:resume_process(whereis(lhttpc_sup)),

worker_done([Pid1, Pid2]),
?assertNotEqual(undefined, whereis(mypool)),
ok.

spawn_pool_req(Parent) ->
spawn_link(
fun() ->
Opts = [{pool, mypool}, {pool_ensure, true}],
undefined = lhttpc_manager:ensure_call(
mypool, self(), ?HOST, get_port(), ?SSL, Opts),
Parent ! {self(), done}
end).

worker_done([Pid|Pids]) ->
receive
{Pid, done} -> worker_done(Pids)
after
1000 -> error({error, worker_timeout})
end;
worker_done([]) ->
ok.


%%% Helpers functions

spawn_client() ->
Expand Down