Browse Source

Merge pull request #143 from seriyps/connect-failure-test

Add test case for connect network error
Sergey Prokhorov 7 years ago
parent
commit
b9f3bf1de9
4 changed files with 42 additions and 18 deletions
  1. 15 1
      test/epgsql_SUITE.erl
  2. 10 8
      test/epgsql_cast.erl
  3. 7 1
      test/epgsql_cth.erl
  4. 10 8
      test/epgsql_incremental.erl

+ 15 - 1
test/epgsql_SUITE.erl

@@ -49,7 +49,8 @@ groups() ->
             connect_with_invalid_user,
             connect_with_invalid_password,
             connect_with_ssl,
-            connect_with_client_cert
+            connect_with_client_cert,
+            connect_to_closed_port
             | ?MAPS_TESTS
         ]},
         {types, [parallel], [
@@ -266,6 +267,19 @@ connect_map(Config) ->
     ok.
 -endif.
 
+connect_to_closed_port(Config) ->
+    {Host, Port} = epgsql_ct:connection_data(Config),
+    Module = ?config(module, Config),
+    Trap = process_flag(trap_exit, true),
+    ?assertEqual({error, econnrefused},
+                 Module:connect(
+                   Host,
+                   "epgsql_test",
+                   "epgsql_test",
+                   [{port, Port + 1}, {database, "epgsql_test_db1"}])),
+    ?assertMatch({'EXIT', _, econnrefused}, receive Stop -> Stop end),
+    process_flag(trap_exit, Trap).
+
 prepared_query(Config) ->
     Module = ?config(module, Config),
     epgsql_ct:with_connection(Config, fun(C) ->

+ 10 - 8
test/epgsql_cast.erl

@@ -19,29 +19,31 @@
 
 connect(Opts) ->
     Ref = epgsqla:connect(Opts),
-    await_connect(Ref).
+    await_connect(Ref, Opts).
 
 connect(Host, Opts) ->
     Ref = epgsqla:connect(Host, Opts),
-    await_connect(Ref).
+    await_connect(Ref, Opts).
 
 connect(Host, Username, Opts) ->
     Ref = epgsqla:connect(Host, Username, Opts),
-    await_connect(Ref).
+    await_connect(Ref, Opts).
 
 connect(Host, Username, Password, Opts) ->
     Ref = epgsqla:connect(Host, Username, Password, Opts),
     %% TODO connect timeout
-    await_connect(Ref).
+    await_connect(Ref, Opts).
 
-await_connect(Ref) ->
+await_connect(Ref, Opts0) ->
+    Opts = epgsql_cth:to_proplist(Opts0),
+    Timeout = proplists:get_value(timeout, Opts, 5000),
     receive
         {C, Ref, connected} ->
             {ok, C};
         {_C, Ref, Error = {error, _}} ->
-            Error;
-        {'EXIT', _C, _Reason} ->
-            {error, closed}
+            Error
+    after Timeout ->
+            error(timeout)
     end.
 
 close(C) ->

+ 7 - 1
test/epgsql_cth.erl

@@ -3,7 +3,8 @@
 -export([
          init/2,
          terminate/1,
-         pre_init_per_suite/3
+         pre_init_per_suite/3,
+         to_proplist/1
         ]).
 
 -include_lib("common_test/include/ct.hrl").
@@ -221,3 +222,8 @@ ts_add({Mega, Sec, Micro}, Timeout) ->
     {V div 1000000000000,
      V div 1000000 rem 1000000,
      V rem 1000000}.
+
+to_proplist(List) when is_list(List) ->
+    List;
+to_proplist(Map) ->
+    maps:to_list(Map).

+ 10 - 8
test/epgsql_incremental.erl

@@ -18,28 +18,30 @@
 
 connect(Opts) ->
     Ref = epgsqli:connect(Opts),
-    await_connect(Ref).
+    await_connect(Ref, Opts).
 
 connect(Host, Opts) ->
     Ref = epgsqli:connect(Host, Opts),
-    await_connect(Ref).
+    await_connect(Ref, Opts).
 
 connect(Host, Username, Opts) ->
     Ref = epgsqli:connect(Host, Username, Opts),
-    await_connect(Ref).
+    await_connect(Ref, Opts).
 
 connect(Host, Username, Password, Opts) ->
     Ref = epgsqli:connect(Host, Username, Password, Opts),
-    await_connect(Ref).
+    await_connect(Ref, Opts).
 
-await_connect(Ref) ->
+await_connect(Ref, Opts0) ->
+    Opts = epgsql_cth:to_proplist(Opts0),
+    Timeout = proplists:get_value(timeout, Opts, 5000),
     receive
         {C, Ref, connected} ->
             {ok, C};
         {_C, Ref, Error = {error, _}} ->
-            Error;
-        {'EXIT', _C, _Reason} ->
-            {error, closed}
+            Error
+    after Timeout ->
+            error(timeout)
     end.
 
 close(C) ->