From 1c8e436a64eeb377972a75f43483442ed574c153 Mon Sep 17 00:00:00 2001 From: Scott Lystig Fritchie Date: Wed, 21 Oct 2015 15:01:11 +0900 Subject: [PATCH] Fix race #3 --- src/machi_util.erl | 20 +++++++++++++++----- test/machi_cr_client_test.erl | 6 ++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/machi_util.erl b/src/machi_util.erl index 37a2ea2..bd35fd4 100644 --- a/src/machi_util.erl +++ b/src/machi_util.erl @@ -45,12 +45,11 @@ %% List twiddling permutations/1, perms/1, combinations/1, ordered_combinations/1, - mk_order/2 + mk_order/2, + %% Other + wait_for_death/2, wait_for_life/2 ]). -%% TODO: Leave this in place? --compile(export_all). - -include("machi.hrl"). -include("machi_projection.hrl"). -include_lib("kernel/include/file.hrl"). @@ -316,11 +315,22 @@ wait_for_death(Pid, Iters) when is_pid(Pid) -> case erlang:is_process_alive(Pid) of false -> ok; - true -> + true -> timer:sleep(1), wait_for_death(Pid, Iters-1) end. +wait_for_life(Reg, 0) -> + exit({not_alive_yet, Reg}); +wait_for_life(Reg, Iters) when is_atom(Reg) -> + case erlang:whereis(Reg) of + Pid when is_pid(Pid) -> + {ok, Pid}; + _ -> + timer:sleep(1), + wait_for_life(Reg, Iters-1) + end. + %%%%%%%%%%%%%%%%% %% @doc Create a TCP connection to a remote Machi server. diff --git a/test/machi_cr_client_test.erl b/test/machi_cr_client_test.erl index 41f1bda..c0c81b3 100644 --- a/test/machi_cr_client_test.erl +++ b/test/machi_cr_client_test.erl @@ -32,6 +32,7 @@ smoke_test_() -> {timeout, 1*60, fun() -> smoke_test2() end}. setup_smoke_test(Host, PortBase, Os, Witness_list) -> os:cmd("rm -rf ./data.a ./data.b ./data.c"), + {ok, _} = machi_util:wait_for_life(machi_flu_sup, 100), F = fun(X) -> case lists:member(X, Witness_list) of true -> @@ -185,9 +186,10 @@ smoke_test2() -> ok after + exit(SupPid, normal), + machi_util:wait_for_death(SupPid, 100), error_logger:tty(true), - catch application:stop(machi), - exit(SupPid, normal) + catch application:stop(machi) end. witness_smoke_test_() -> {timeout, 1*60, fun() -> witness_smoke_test2() end}.