Browse Source

Use timer:sleep instead of receive...after timout end in newly introduced tests, handle open_socket/x results better while cancelling the query

Enid Gjoleka 5 years ago
parent
commit
4ae561e5a1
2 changed files with 19 additions and 31 deletions
  1. 3 6
      src/epgsql_sock.erl
  2. 16 25
      test/epgsql_SUITE.erl

+ 3 - 6
src/epgsql_sock.erl

@@ -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,

+ 16 - 25
test/epgsql_SUITE.erl

@@ -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().
 
@@ -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().