Skip to content

Commit f020eb2

Browse files
authored
Merge pull request #12985 from rabbitmq/wait-for-etcd-start
rabbitmq_peer_discovery_etcd: Wait for etcd start in system_SUITE
2 parents 38ebcc9 + d54b2bf commit f020eb2

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

deps/rabbitmq_peer_discovery_etcd/test/system_SUITE.erl

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
init_per_testcase/2,
2727
end_per_testcase/2,
2828

29-
etcd_connection_sanity_check_test/1,
3029
init_opens_a_connection_test/1,
3130
registration_with_locking_test/1,
3231
start_one_member_at_a_time/1,
@@ -41,7 +40,6 @@ all() ->
4140
groups() ->
4241
[
4342
{v3_client, [], [
44-
etcd_connection_sanity_check_test,
4543
init_opens_a_connection_test,
4644
registration_with_locking_test
4745
]},
@@ -202,15 +200,57 @@ start_etcd(Config) ->
202200
"--initial-cluster-state", "new",
203201
"--initial-cluster-token", "test-token",
204202
"--log-level", "debug", "--log-outputs", "stdout"],
205-
EtcdPid = spawn(fun() -> rabbit_ct_helpers:exec(Cmd) end),
206-
%% Wait for etcd to start its listeners.
207-
timer:sleep(2000),
203+
EtcdPid = spawn(fun() -> do_start_etcd(Cmd) end),
208204

209-
EtcdEndpoint = rabbit_misc:format("~s:~b", [EtcdHost, EtcdClientPort]),
210-
rabbit_ct_helpers:set_config(
211-
Config,
212-
[{etcd_pid, EtcdPid},
213-
{etcd_endpoints, [EtcdEndpoint]}]).
205+
EtcdEndpoints = [rabbit_misc:format("~s:~b", [EtcdHost, EtcdClientPort])],
206+
Config1 = rabbit_ct_helpers:set_config(
207+
Config,
208+
[{etcd_pid, EtcdPid},
209+
{etcd_endpoints, EtcdEndpoints}]),
210+
211+
#{level := Level} = logger:get_primary_config(),
212+
logger:set_primary_config(level, critical),
213+
try
214+
wait_for_etcd(EtcdEndpoints),
215+
Config1
216+
catch
217+
exit:{test_case_failed, _} ->
218+
stop_etcd(Config1),
219+
{skip, "Failed to start etcd"}
220+
after
221+
logger:set_primary_config(level, Level)
222+
end.
223+
224+
do_start_etcd(Cmd) ->
225+
case rabbit_ct_helpers:exec(Cmd) of
226+
{ok, Stdout} ->
227+
ct:pal("etcd daemon exited:~n~s", [Stdout]);
228+
{error, Reason, Stdout} ->
229+
ct:pal(
230+
"etcd daemon exited with error ~0p:~n~s",
231+
[Reason, Stdout])
232+
end.
233+
234+
wait_for_etcd(EtcdEndpoints) ->
235+
application:ensure_all_started(eetcd),
236+
237+
Timeout = 60000,
238+
rabbit_ct_helpers:await_condition(
239+
fun() ->
240+
case eetcd:open(test, EtcdEndpoints) of
241+
{ok, _Pid} -> true;
242+
_ -> false
243+
end
244+
end, Timeout),
245+
246+
Condition1 = fun() -> 1 =:= length(eetcd_conn_sup:info()) end,
247+
try
248+
rabbit_ct_helpers:await_condition(Condition1, Timeout)
249+
after
250+
eetcd:close(test)
251+
end,
252+
Condition2 = fun() -> 0 =:= length(eetcd_conn_sup:info()) end,
253+
rabbit_ct_helpers:await_condition(Condition2, Timeout).
214254

215255
stop_etcd(Config) ->
216256
case rabbit_ct_helpers:get_config(Config, etcd_pid) of
@@ -228,24 +268,6 @@ stop_etcd(Config) ->
228268
%% Test cases
229269
%%
230270

231-
etcd_connection_sanity_check_test(Config) ->
232-
application:ensure_all_started(eetcd),
233-
Endpoints = ?config(etcd_endpoints, Config),
234-
?assertMatch({ok, _Pid}, eetcd:open(test, Endpoints)),
235-
236-
Condition1 = fun() ->
237-
1 =:= length(eetcd_conn_sup:info())
238-
end,
239-
try
240-
rabbit_ct_helpers:await_condition(Condition1, 60000)
241-
after
242-
eetcd:close(test)
243-
end,
244-
Condition2 = fun() ->
245-
0 =:= length(eetcd_conn_sup:info())
246-
end,
247-
rabbit_ct_helpers:await_condition(Condition2, 60000).
248-
249271
init_opens_a_connection_test(Config) ->
250272
Endpoints = ?config(etcd_endpoints, Config),
251273
{ok, Pid} = start_client(Endpoints),

0 commit comments

Comments
 (0)