Skip to content

Commit

Permalink
Merge pull request #82 from inaka/ferigis.79.fsm_coverage
Browse files Browse the repository at this point in the history
fsm coverage [#79]
  • Loading branch information
Brujo Benavides authored Aug 16, 2016
2 parents 6f1eadb + 343c70d commit 4f55cc7
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 11 deletions.
1 change: 0 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
, unmatched_returns
, error_handling
]},
{get_warnings, true},
{plt_apps, top_level_deps},
{plt_extra_apps, []},
{plt_location, local},
Expand Down
2 changes: 1 addition & 1 deletion src/wpool_fsm_process.erl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ code_change(OldVsn, StateName, State, Extra) ->
{ok, dispatch_state, State#state{ state = NewState
, fsm_state = NextStateName
}};
Error -> {error, Error}
_Error -> {ok, dispatch_state, State}
end.

%% @private
Expand Down
14 changes: 14 additions & 0 deletions src/wpool_fsm_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,25 @@ handle_sync_event(Event, _From, StateName, StateData) ->
%%%===================================================================
%% @private
-spec common_state(term(), term()) -> {next_state, common_state, term()}.
common_state(timeout, StateData) -> {next_state, common_state, StateData};
common_state(Msg, StateData) -> handle_event(Msg, common_state, StateData).

%% @private
-spec common_state(term(), term(), term()) ->
{reply, term(), common_state, term()}.
common_state(stop, _From, StateData) ->
{stop, normal, ok, StateData};
common_state(stop_without_reply, From, StateData) ->
gen_fsm:reply(From, ok),
{stop, normal, StateData};
common_state({timeout, Timeout}, _From, StateData) ->
{reply, ok, common_state, StateData, Timeout};
common_state(next_state, From, StateData) ->
gen_fsm:reply(From, ok),
{next_state, common_state, StateData};
common_state({next_state, Timeout}, From, StateData) ->
gen_fsm:reply(From, ok),
{next_state, common_state, StateData, Timeout};
common_state(Msg, From, StateData) ->
handle_sync_event(Msg, From, common_state, StateData).

Expand Down
25 changes: 22 additions & 3 deletions test/echo_fsm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
, handle_sync_event/4
, terminate/3
, code_change/4
, format_status/2
]).
-export([ state_one/2
, state_two/2
Expand All @@ -35,12 +36,16 @@
init(Something) -> Something.

-spec state_one(Event, any()) -> Event.
state_one(timeout, LoopData) ->
{next_state, state_one, LoopData};
state_one(Event, _LoopData) -> Event.

-spec state_one(Event, any(), any()) -> Event.
state_one(Event, _From, _LoopData) -> Event.

-spec state_two(Event, any()) -> Event.
state_two(timeout, LoopData) ->
{next_state, state_two, LoopData};
state_two(Event, _LoopData) -> Event.

-spec state_two(Event, any(), any()) -> Event.
Expand All @@ -55,12 +60,26 @@ handle_event(Event, _StateName, _StateData) -> Event.
-spec handle_sync_event(any(), any(), any(), any()) -> any().
handle_sync_event(state, _From, StateName, StateData) ->
{reply, StateData, StateName, StateData};
handle_sync_event({next_state, NextState, NewStateData}
, From, _StateName, _StateData) ->
gen_fsm:reply(From, ok),
{next_state, NextState, NewStateData};
handle_sync_event({next_state, NextState, NewStateData, Timeout}
, From, _StateName, _StateData) ->
gen_fsm:reply(From, ok),
{next_state, NextState, NewStateData, Timeout};
handle_sync_event({stop, Reason, StateData}
, From, _StateName, _StateData) ->
gen_fsm:reply(From, ok),
{stop, Reason, StateData};
handle_sync_event(Event, _From, _StateName, _StateData) ->
Event.

-spec terminate(any(), any(), any()) -> ok.
terminate(_Reason, _StateName, _StateData) -> ok.

-spec code_change(any(), any(), any(), any()) -> {ok, state_one, term()}.
code_change(_OldVsn, _StateName, StateData, _Extra) ->
{ok, state_one, StateData}.
-spec code_change(any(), any(), any(), any()) -> any().
code_change(_OldVsn, _StateName, _StateData, Extra) -> Extra.

-spec format_status(normal | terminate, [list() | term()]) -> term().
format_status(_Opt, [_PDict, StateData]) -> StateData.
15 changes: 15 additions & 0 deletions test/wpool_fsm_pool_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ available_worker(_Config) ->
{?WORKERS, UniqueWorkers, true} =
{?WORKERS, UniqueWorkers, (?WORKERS/2) >= length(UniqueWorkers)},

ct:log(
"We put all the workers to run again and test how manage
the messages on the queue"),
[wpool:send_event(
Pool, {timer, sleep, [2000]}) || _ <- lists:seq(1, ?WORKERS)],
ok = wpool:sync_send_event(Pool, {timeout, 0}, available_worker, 10000),
[wpool:send_event(
Pool, {timer, sleep, [2000]}) || _ <- lists:seq(1, ?WORKERS)],
ok = wpool:sync_send_event(Pool, stop, available_worker, 10000),
[wpool:send_event(
Pool, {timer, sleep, [2000]}) || _ <- lists:seq(1, ?WORKERS)],
ok = wpool:sync_send_event(Pool, next_state, available_worker, 10000),
ok = wpool:sync_send_event(Pool, {next_state, 0}, available_worker, 10000),
ok = wpool:sync_send_event(Pool, stop_without_reply, available_worker, 10000),

{comment, []}.

-spec best_worker(config()) -> {comment, []}.
Expand Down
59 changes: 53 additions & 6 deletions test/wpool_fsm_process_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
, info/1
, async_states/1
, sync_states/1
, complete_coverage/1
]).

-spec all() -> [atom()].
Expand Down Expand Up @@ -73,6 +74,8 @@ init_timeout(_Config) ->
wpool_fsm_process:start_link(
?MODULE, echo_fsm, {ok, state_one, [], 0}, []),
timer:sleep(1),
Pid ! {stop, normal, state},
timer:sleep(1),
false = erlang:is_process_alive(Pid),
{comment, []}.

Expand All @@ -83,7 +86,8 @@ info(_Config) ->
?MODULE, echo_fsm, {ok, state_one, []}, []),
Pid ! {next_state, state_two, newstate},
newstate = wpool_fsm_process:sync_send_all_state_event(?MODULE, state, 5000),
Pid ! {next_state, state_three, newstate, 0},
Pid ! {next_state, state_two, newstate, 0},
Pid ! {stop, normal, state},
timer:sleep(1),
false = erlang:is_process_alive(Pid),

Expand All @@ -96,9 +100,18 @@ async_states(_Config) ->
?MODULE, echo_fsm, {ok, state_one, []}, []),
wpool_fsm_process:send_event(Pid, {next_state, state_two, newstate}),
newstate = wpool_fsm_process:sync_send_all_state_event(?MODULE, state, 5000),
wpool_fsm_process:send_event(Pid, {next_state, state_one, newerstate, 0}),
wpool_fsm_process:send_event(Pid, {next_state, state_one, newerstate, 5000}),
wpool_fsm_process:send_all_state_event(Pid, {next_state, state_one
, newerstate, 5000}),
wpool_fsm_process:send_all_state_event(Pid, {stop, normal, state}),
timer:sleep(1),
false = erlang:is_process_alive(Pid),
{ok, Pid2} =
wpool_fsm_process:start_link(
?MODULE, echo_fsm, {ok, state_one, []}, []),
wpool_fsm_process:send_event(Pid2, {stop, normal, state}),
timer:sleep(1),
false = erlang:is_process_alive(Pid2),

{comment, []}.

Expand All @@ -107,14 +120,48 @@ sync_states(_Config) ->
{ok, Pid} =
wpool_fsm_process:start_link(
?MODULE, echo_fsm, {ok, state_one, []}, []),
ok1 =
wpool_fsm_process:sync_send_all_state_event(
Pid, {reply, ok1, state_one, newerstate, 5000}, 5000),
ok =
wpool_fsm_process:sync_send_all_state_event(
Pid, {next_state, state_one, newerstate, 5000}, 5000),
ok =
wpool_fsm_process:sync_send_all_state_event(
Pid, {next_state, state_one, newerstate}, 5000),
ok3 = wpool_fsm_process:sync_send_all_state_event(
Pid, {stop, normal, ok3, state}, 5000),
false = erlang:is_process_alive(Pid),
{ok, Pid2} =
wpool_fsm_process:start_link(
?MODULE, echo_fsm, {ok, state_one, []}, []),
ok = wpool_fsm_process:sync_send_all_state_event(
Pid2, {stop, normal, state}, 5000),
false = erlang:is_process_alive(Pid2),
{ok, Pid3} =
wpool_fsm_process:start_link(
?MODULE, echo_fsm, {ok, state_one, []}, []),
ok1 =
wpool_fsm_process:sync_send_event(
Pid, {reply, ok1, state_two, newstate}, 5000),
Pid3, {reply, ok1, state_two, newstate}, 5000),
newstate = wpool_fsm_process:sync_send_all_state_event(?MODULE, state, 5000),
ok2 =
wpool_fsm_process:sync_send_event(
Pid, {reply, ok2, state_one, newerstate, 0}, 5000),
timer:sleep(1),
false = erlang:is_process_alive(Pid),
Pid3, {reply, ok2, state_one, newerstate, 5000}, 5000),

{comment, []}.

-spec complete_coverage(config()) -> {comment, []}.
complete_coverage(_Config) ->
ct:comment("Code Change"),
{ok, dispatch_state, State} =
wpool_fsm_process:init({complete_coverage, echo_fsm
, {ok, state_one, []}, []}),
{ok, dispatch_state, _} = wpool_fsm_process:code_change("oldvsn"
, dispatch_state, State
, {ok, dispatch_state, []}),
{ok, dispatch_state, _} = wpool_fsm_process:code_change("oldvsn"
, dispatch_state, State, bad),
[] = wpool_fsm_process:format_status(normal, [[], State]),

{comment, []}.

0 comments on commit 4f55cc7

Please sign in to comment.