Skip to content
Browse files

fixes #2175

  • Loading branch information...
1 parent 4a4c5dd commit 3eaaceae946160bb74a5cbeb1e2ee2a5b7c533e6 @zeitgeist zeitgeist committed Aug 19, 2011
Showing with 72 additions and 6 deletions.
  1. +36 −3 src/ezk_highlander.erl
  2. +36 −3 test/ezk_highlander_SUITE.erl
View
39 src/ezk_highlander.erl
@@ -28,6 +28,10 @@
-behaviour(gen_server).
+
+-type zk_conn() :: pid().
+-type zk_nodename() :: string().
+
-record(high_state, {is_active = false,
ident,
my_path,
@@ -38,7 +42,7 @@
connection_pid
}).
--export([start/4, start_link/4, failover/2]).
+-export([start/4, start_link/4, failover/2,wait_for/3]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-export([behaviour_info/1]).
@@ -56,6 +60,31 @@ start_link(ConnectionPId, Module, Parameters, NodeList) ->
gen_server:start_link(?MODULE, [ConnectionPId, Module, Parameters, NodeList], []).
+%% wait for a highlander to appear. stop with error(timeout, Nodename) in case of timeout
+%% @param NodeName path to the Highlander's
+-spec wait_for(zk_conn(), zk_nodename(), pos_integer()) ->
+ ok.
+wait_for(ConnectionPid, Nodename, Timeout) ->
+ Timer = erlang:start_timer(Timeout, self(), timeout),
+ ok = wait_for_get(ConnectionPid, Nodename, Timer),
+ erlang:cancel_timer(Timer),
+ receive {timeout, Timer, _} -> ok after 0 -> ok end,
+ ok.
+
+wait_for_get(ConnectionPid, Nodename, Timer) ->
+ receive {timeout, Timer, _} ->
+ erlang:error(timeout, Nodename)
+ after 0 -> ok
+ end,
+ case ezk:get(ConnectionPid, Nodename) of
+ {ok, _} ->
+ ok;
+ _ ->
+ timer:sleep(50),
+ wait_for_get(ConnectionPid, Nodename, Timer)
+ end.
+
+
%% The init function trys every given path once and ensures that all
%% needed ZK Nodes are there. After trying once without success it goes
%% into normal genserver state and waits for messages ( = changes in the important nodes)
@@ -114,14 +143,18 @@ terminate(Reason, State) ->
_Else ->
Motto = Module:motto(Path)
end,
- timer:sleep(2000).
+ ok.
+% timer:sleep(2000).
%% Called by the failover function
handle_call({failover, Reason}, _From, State) ->
{stop, {shutdown, Reason}, ok, State};
%% Called by the is_active function.
handle_call(isactive, _From, State) ->
- {reply, State#high_state.is_active, State}.
+ {reply, State#high_state.is_active, State};
+handle_call(stop, _From, State) ->
+ {stop, normal, ok, State}.
+
%% If a watch is triggered this Message comes to the Highlander.
%% Path is the Path of the Node to make, not the one of the Father.
View
39 test/ezk_highlander_SUITE.erl
@@ -28,6 +28,7 @@
-compile(export_all).
-include_lib("common_test/include/ct.hrl").
+-include_lib("eunit/include/eunit.hrl").
-define(LOG, ct_log:log).
-define(LOGSUITEINIT, ct_log:suite_init).
@@ -73,12 +74,16 @@ end_per_testcase(_TestCase, _Config) ->
ok.
groups() ->
- [].
+ [
+ {wait_for_hl, [parallel], [wait_for_highlander, start_stop_highlander]}
+ ].
all() ->
%% {skip, test}.
- [high_test
- , high2_test
+ [
+ {group, wait_for_hl},
+ high_test,
+ high2_test
].
high_test(Config) ->
@@ -155,6 +160,34 @@ receiver2(Child, Caller, Father, Path, Cycles) ->
Caller ! {ended, Father, Path}.
+%% ---------------------------- high 2 test------------------------
+
+start_stop_highlander() ->
+ [].
+
+start_stop_highlander(Config) ->
+ timer:sleep(300),
+ CPid = ?config(connection_pid, Config),
+ {ok, HL} = ?HIGHIMPL(CPid, self(), 1),
+ timer:sleep(2000),
+ gen_server:call(HL,stop),
+
+% HLPid = receive {init, P, _, _} -> P end,
+% HLPid ! die,
+% receive die -> ok end,
+ ok.
+
+wait_for_highlander() ->
+ [].
+
+wait_for_highlander(Config) ->
+ Nodename = "/highlander/test/node1",
+ CPid = ?config(connection_pid, Config),
+ ?assertError(timeout, ezk_highlander:wait_for(CPid, Nodename, 10)),
+ ok = ezk_highlander:wait_for(CPid, Nodename, 510).
+
+
+
%% ---------------------------- free for all ---------------------------

0 comments on commit 3eaacea

Please sign in to comment.
Something went wrong with that request. Please try again.