Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete Test Coverage #10

Merged
merged 12 commits into from
Aug 3, 2016
4 changes: 2 additions & 2 deletions src/wpool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ send_all_state_event(Sup, Event) ->
send_all_state_event(Sup, Event, available_worker) ->
wpool_pool:send_all_event_to_available_worker(Sup, Event);
send_all_state_event(Sup, Event, {hash_worker, HashKey}) ->
wpool_fsm_process:send_all_state_event(wpool_pool:hash_worker(Sup, HashKey)
, Event);
wpool_fsm_process:send_all_state_event(
wpool_pool:hash_worker(Sup, HashKey), Event);
send_all_state_event(Sup, Event, Fun) when is_function(Fun) ->
wpool_fsm_process:send_all_state_event(Fun(Sup), Event);
send_all_state_event(Sup, Event, Strategy) ->
Expand Down
167 changes: 27 additions & 140 deletions src/wpool_fsm_process.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
, options :: [ {time_checker|queue_manager, atom()}
| wpool:option()
]
, born = os:timestamp() :: erlang:timestamp()
, fsm_state :: fsm_state()
}).
-type state() :: #state{}.
Expand All @@ -40,7 +39,6 @@
, sync_send_all_state_event/2
, sync_send_all_state_event/3
, cast_call/3
, age/1
]).
-export([ dispatch_state/2
, dispatch_state/3
Expand Down Expand Up @@ -95,10 +93,6 @@ sync_send_all_state_event(Process, Event, Timeout) ->
cast_call(Process, From, Event) ->
gen_fsm:send_event(Process, {sync_send_event, From, Event}).

%% @doc Report how old a process is in <b>microseconds</b>
-spec age(wpool:name() | pid()) -> non_neg_integer().
age(Process) -> gen_fsm:sync_send_all_state_event(Process, age).

%%%===================================================================
%%% init, terminate, code_change, info callbacks
%%%===================================================================
Expand Down Expand Up @@ -153,8 +147,11 @@ code_change(OldVsn, StateName, State, Extra) ->
-spec handle_info(any(), fsm_state(), state()) ->
{next_state, dispatch_state, state()} | {stop, term(), state()}.
handle_info(Info, StateName, StateData) ->
try (StateData#state.mod):handle_info(
Info, StateName, StateData#state.state) of
case do_try(
fun() ->
(StateData#state.mod):handle_info(
Info, StateName, StateData#state.state)
end) of
{next_state, NextStateName, NewStateData} ->
{next_state, dispatch_state, StateData#state{ state = NewStateData
, fsm_state = NextStateName
Expand All @@ -169,21 +166,6 @@ handle_info(Info, StateName, StateData) ->
};
{stop, Reason, NewStateData} ->
{stop, Reason, StateData#state{state = NewStateData}}
catch
_:{next_state, NextStateName, NewStateData} ->
{next_state, dispatch_state, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}};
_:{next_state, NextStateName, NewStateData, Timeout} ->
{ next_state
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
, Timeout
};
_:{stop, Reason, NewStateData} ->
{stop, Reason, StateData#state{state = NewStateData}}
end.

-spec format_status(normal | terminate, list()) -> term().
Expand All @@ -205,8 +187,11 @@ handle_event(Event, _StateName, StateData) ->
notify_queue_manager(
worker_busy, StateData#state.name, StateData#state.options),
Reply =
try (StateData#state.mod):handle_event(Event,
StateData#state.fsm_state, StateData#state.state) of
case do_try(
fun() ->
(StateData#state.mod):handle_event(Event,
StateData#state.fsm_state, StateData#state.state)
end) of
{next_state, NextStateName, NewStateData} ->
{next_state, dispatch_state, StateData#state{ state = NewStateData
, fsm_state = NextStateName
Expand All @@ -221,21 +206,6 @@ handle_event(Event, _StateName, StateData) ->
};
{stop, Reason, NewState} ->
{stop, Reason, StateData#state{state = NewState}}
catch
_:{next_state, NextStateName, NewStateData} ->
{next_state, dispatch_state, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}};
_:{next_state, NextStateName, NewStateData, Timeout} ->
{ next_state
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
, Timeout
};
_:{stop, Reason, NewState} ->
{stop, Reason, StateData#state{state = NewState}}
end,
task_end(Task),
ok =
Expand All @@ -247,8 +217,6 @@ handle_event(Event, _StateName, StateData) ->
{reply, term(), dispatch_state, state()}
| {next_state, dispatch_state, state()}
| {stop, term(), state()}.
handle_sync_event(age, _From, _StateName, #state{born=Born} = State) ->
{reply, timer:now_diff(os:timestamp(), Born), dispatch_state, State};
handle_sync_event(Event, From, _StateName, StateData) ->
Task =
task_init(
Expand All @@ -259,8 +227,11 @@ handle_sync_event(Event, From, _StateName, StateData) ->
notify_queue_manager(
worker_busy, StateData#state.name, StateData#state.options),
Result =
try (StateData#state.mod):handle_sync_event(
Event, From, StateData#state.fsm_state, StateData#state.state) of
case do_try(
fun() ->
(StateData#state.mod):handle_sync_event(
Event, From, StateData#state.fsm_state, StateData#state.state)
end) of
{reply, Reply, NextStateName, NewStateData} ->
{ reply
, Reply
Expand Down Expand Up @@ -297,43 +268,6 @@ handle_sync_event(Event, From, _StateName, StateData) ->
{stop, Reason, StateData#state{state = NewState}};
{stop, Reason, Response, NewState} ->
{stop, Reason, Response, StateData#state{state = NewState}}
catch
_:{reply, Reply, NextStateName, NewStateData} ->
{ reply
, Reply
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
};
_:{reply, Reply, NextStateName, NewStateData, Timeout} ->
{ reply
, Reply
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
, Timeout
};
_:{next_state, NextStateName, NewStateData} ->
{ next_state
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
};
_:{next_state, NextStateName, NewStateData, Timeout} ->
{ next_state
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
, Timeout
};
_:{stop, Reason, NewState} ->
{stop, Reason, StateData#state{state = NewState}};
_:{stop, Reason, Response, NewState} ->
{stop, Reason, Response, StateData#state{state = NewState}}
end,
task_end(Task),
ok =
Expand Down Expand Up @@ -366,8 +300,11 @@ dispatch_state(Event, StateData) ->
notify_queue_manager(
worker_busy, StateData#state.name, StateData#state.options),
Reply =
try (StateData#state.mod):(StateData#state.fsm_state)(Event,
StateData#state.state) of
case do_try(
fun() ->
(StateData#state.mod):(StateData#state.fsm_state)(
Event, StateData#state.state)
end) of
{next_state, NextStateName, NewStateData} ->
{ next_state
, dispatch_state
Expand All @@ -385,24 +322,6 @@ dispatch_state(Event, StateData) ->
};
{stop, Reason, NewStateData} ->
{stop, Reason, StateData#state{state = NewStateData}}
catch
_:{next_state, NextStateName, NewStateData} ->
{ next_state
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
};
_:{next_state, NextStateName, NewStateData, Timeout} ->
{ next_state
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
, Timeout
};
_:{stop, Reason, NewStateData} ->
{stop, Reason, StateData#state{state = NewStateData}}
end,
task_end(Task),
ok =
Expand All @@ -423,8 +342,11 @@ dispatch_state(Event, From, StateData) ->
notify_queue_manager(
worker_busy, StateData#state.name, StateData#state.options),
Result =
try (StateData#state.mod):(StateData#state.fsm_state)(
Event, From, StateData#state.state) of
case do_try(
fun() ->
(StateData#state.mod):(StateData#state.fsm_state)(
Event, From, StateData#state.state)
end) of
{reply, Reply, NextStateName, NewStateData} ->
{ reply
, Reply
Expand Down Expand Up @@ -461,43 +383,6 @@ dispatch_state(Event, From, StateData) ->
{stop, Reason, StateData#state{state = NewStateData}};
{stop, Reason, Reply, NewStateData} ->
{stop, Reason, Reply, StateData#state{state = NewStateData}}
catch
_:{reply, Reply, NextStateName, NewStateData} ->
{ reply
, Reply
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
};
_:{reply, Reply, NextStateName, NewStateData, Timeout} ->
{ reply
, Reply
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
, Timeout
};
_:{next_state, NextStateName, NewStateData} ->
{ next_state
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
};
_:{next_state, NextStateName, NewStateData, Timeout} ->
{ next_state
, dispatch_state
, StateData#state{ state = NewStateData
, fsm_state = NextStateName
}
, Timeout
};
_:{stop, Reason, NewStateData} ->
{stop, Reason, StateData#state{state = NewStateData}};
_:{stop, Reason, Reply, NewStateData} ->
{stop, Reason, Reply, StateData#state{state = NewStateData}}
end,
task_end(Task),
ok =
Expand Down Expand Up @@ -540,3 +425,5 @@ get_task(Event, StateData) ->
{dispatch_state, Event},
proplists:get_value(time_checker, StateData#state.options, undefined),
proplists:get_value(overrun_warning, StateData#state.options, infinity)).

do_try(Fun) -> try Fun() catch _:Error -> Error end.