Skip to content

Commit

Permalink
Use timer:sleep instead of receive...after timout end in newly introd…
Browse files Browse the repository at this point in the history
…uced tests, handle open_socket/x results better while cancelling the query
  • Loading branch information
enidgjoleka committed Mar 26, 2020
1 parent 0709950 commit 4ae561e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
9 changes: 3 additions & 6 deletions src/epgsql_sock.erl
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,9 @@ handle_cast(cancel, State = #state{backend = {Pid, Key},
SockOpts = [{active, false}, {packet, raw}, binary],
Msg = <<16:?int32, 80877102:?int32, Pid:?int32, Key:?int32>>,
case epgsql_cmd_connect:open_socket(SockOpts, ConnectOpts) of
{ok, Mode, Sock} when Mode == gen_tcp ->
ok = gen_tcp:send(Sock, Msg),
gen_tcp:close(Sock);
{ok, Mode, Sock} when Mode == ssl ->
ok = ssl:send(Sock, Msg),
ssl:close(Sock);
{ok, Mode, Sock} ->
ok = apply(Mode, send, [Sock, Msg]),
apply(Mode, close, [Sock]);
{error, _Reason} ->
noop
end,
Expand Down
41 changes: 16 additions & 25 deletions test/epgsql_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -307,24 +307,19 @@ cancel_query_for_connection_with_ssl(Config) ->
{ok, C} = Module:connect(Host, "epgsql_test", Args2),
?assertMatch({ok, _Cols, [{true}]},
Module:equery(C, "select ssl_is_used()")),
process_flag(trap_exit, true),
Self = self(),
spawn_link(fun() ->
?assertMatch(?QUERY_CANCELED, Module:equery(C, "SELECT pg_sleep(5)")),
Self ! done
end),
%% this will never match but introduces 1 second latency needed
%% for the test not to be flaky
receive none ->
noop
after 1000 ->
epgsql:cancel(C),
receive done ->
?assert(true)
after 5000 ->
epgsql:close(C),
?assert(false)
end
%% this timer is needed for the test not to be flaky
timer:sleep(1000),
epgsql:cancel(C),
receive done ->
?assert(true)
after 5000 ->
epgsql:close(C),
?assert(false)
end,
epgsql_ct:flush().

Expand All @@ -342,18 +337,14 @@ cancel_query_for_connection_with_gen_tcp(Config) ->
?assertMatch(?QUERY_CANCELED, Module:equery(C, "SELECT pg_sleep(5)")),
Self ! done
end),
%% this is will never match but it introduces the 1 second latency needed
%% for the test not to be flaky
receive none ->
noop
after 1000 ->
epgsql:cancel(C),
receive done ->
?assert(true)
after 5000 ->
epgsql:close(C),
?assert(false)
end
%% this timer is needed for the test not to be flaky
timer:sleep(1000),
epgsql:cancel(C),
receive done ->
?assert(true)
after 5000 ->
epgsql:close(C),
?assert(false)
end,
epgsql_ct:flush().

Expand Down

0 comments on commit 4ae561e

Please sign in to comment.