diff --git a/rebar.config b/rebar.config index 9fc9ffb..4f1b4c8 100644 --- a/rebar.config +++ b/rebar.config @@ -78,7 +78,6 @@ , unmatched_returns , error_handling ]}, - {get_warnings, true}, {plt_apps, top_level_deps}, {plt_extra_apps, []}, {plt_location, local}, diff --git a/src/wpool_fsm_process.erl b/src/wpool_fsm_process.erl index 1961ce3..7682b56 100644 --- a/src/wpool_fsm_process.erl +++ b/src/wpool_fsm_process.erl @@ -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 diff --git a/src/wpool_fsm_worker.erl b/src/wpool_fsm_worker.erl index 62c6200..13b1046 100644 --- a/src/wpool_fsm_worker.erl +++ b/src/wpool_fsm_worker.erl @@ -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). diff --git a/test/echo_fsm.erl b/test/echo_fsm.erl index 4130c0a..c7149be 100644 --- a/test/echo_fsm.erl +++ b/test/echo_fsm.erl @@ -21,6 +21,7 @@ , handle_sync_event/4 , terminate/3 , code_change/4 + , format_status/2 ]). -export([ state_one/2 , state_two/2 @@ -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. @@ -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. diff --git a/test/wpool_fsm_pool_SUITE.erl b/test/wpool_fsm_pool_SUITE.erl index 9e0ad27..8b2b429 100644 --- a/test/wpool_fsm_pool_SUITE.erl +++ b/test/wpool_fsm_pool_SUITE.erl @@ -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, []}. diff --git a/test/wpool_fsm_process_SUITE.erl b/test/wpool_fsm_process_SUITE.erl index c6a2e92..9417f6b 100644 --- a/test/wpool_fsm_process_SUITE.erl +++ b/test/wpool_fsm_process_SUITE.erl @@ -29,6 +29,7 @@ , info/1 , async_states/1 , sync_states/1 + , complete_coverage/1 ]). -spec all() -> [atom()]. @@ -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, []}. @@ -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), @@ -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, []}. @@ -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, []}.