Skip to content

Commit

Permalink
revert to the backwards compatible behaviour when no_reconnect option…
Browse files Browse the repository at this point in the history
… is passed
  • Loading branch information
sumerman authored and Kwisatx committed Sep 12, 2018
1 parent dd0d7e9 commit f6d759a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/eredis_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,16 @@ init([Host, Port, Database, Password, ReconnectSleep, ConnectTimeout]) ->
parser_state = eredis_parser:init(),
queue = queue:new()},

self() ! initiate_connection,
{ok, State}.
case ReconnectSleep of
no_reconnect ->
case connect(State) of
{ok, _NewState} = Res -> Res;
{error, Reason} -> {stop, Reason}
end;
T when is_integer(T) ->
self() ! initiate_connection,
{ok, State}
end.

handle_call({request, Req}, From, State) ->
do_request(Req, From, State);
Expand Down Expand Up @@ -361,12 +369,10 @@ maybe_reconnect(Reason, #state{reconnect_sleep = no_reconnect, queue = Queue} =
reply_all({error, Reason}, Queue),
%% If we aren't going to reconnect, then there is nothing else for
%% this process to do.
Reason1 = case Reason of
tcp_closed -> normal;
_Else -> Reason
end,
{stop, Reason1, State#state{socket = undefined}};
{stop, normal, State#state{socket = undefined}};
maybe_reconnect(Reason, #state{queue = Queue} = State) ->
error_logger:error_msg("Re-establishing connection to ~p:~p due to ~p",
[State#state.host, State#state.port, Reason]),
Self = self(),
spawn_link(fun() -> reconnect_loop(Self, State) end),

Expand Down
17 changes: 13 additions & 4 deletions test/eredis_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,24 @@ multibulk_test_() ->
undefined_database_test() ->
?assertMatch({ok,_}, eredis:start_link("localhost", 6379, undefined)).

connection_failure_on_start_no_reconnect_test() ->
connection_failure_during_start_no_reconnect_test() ->
process_flag(trap_exit, true),
Res = eredis:start_link("this_host_does_not_exist", 6379, 0, "", no_reconnect),
?assertMatch({error, _}, Res),
IsDied = receive {'EXIT', _, _} -> died
after 1000 -> still_alive end,
process_flag(trap_exit, false),
?assertEqual(died, IsDied).

connection_failure_during_start_reconnect_test() ->
process_flag(trap_exit, true),
Res = eredis:start_link("this_host_does_not_exist", 6379, 0, "", 100),
?assertMatch({ok, _}, Res),
{ok, ClientPid} = Res,
process_flag(trap_exit, true),
IsDied = receive {'EXIT', ClientPid, _} -> died
after 1000 -> still_alive end,
after 400 -> still_alive end,
process_flag(trap_exit, false),
?assertEqual(died, IsDied).
?assertEqual(still_alive, IsDied).

tcp_closed_test() ->
C = c(),
Expand Down

0 comments on commit f6d759a

Please sign in to comment.