Skip to content

Commit

Permalink
Fix tests as per changes to code
Browse files Browse the repository at this point in the history
We separate (again) results into pre-25 and 25+
  • Loading branch information
paulo-ferraz-oliveira committed Jul 10, 2024
1 parent d80c6f9 commit d0791f9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
15 changes: 15 additions & 0 deletions src/wpool_process.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@

%% api
-export([start_link/4, call/3, cast/2, send_request/2]).

-ifdef(TEST).

-export([get_state/1]).

-endif.

%% gen_server callbacks
-export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2,
handle_continue/2]).
Expand Down Expand Up @@ -89,6 +96,14 @@ cast(Process, Cast) ->
send_request(Name, Request) ->
gen_server:send_request(Name, Request).

-ifdef(TEST).

-spec get_state(state()) -> term().
get_state(#state{state = State}) ->
State.

-endif.

%%%===================================================================
%%% init, terminate, code_change, info callbacks
%%%===================================================================
Expand Down
2 changes: 1 addition & 1 deletion test/echo_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ format_status(_, [_PDict, State]) ->

-spec format_status(gen_server:format_status()) -> gen_server:format_status().
format_status(State) ->
{formatted_state, State}.
State.

-endif.
38 changes: 29 additions & 9 deletions test/wpool_process_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,19 @@ continue(_Config) ->

-spec format_status(config()) -> {comment, []}.
format_status(_Config) ->
%% echo_server implements format_status/2
%% echo_server implements format_status/1
{ok, Pid} = wpool_process:start_link(?MODULE, echo_server, {ok, state}, []),
%% therefore it returns {formatted_state, State} as its status
{status, Pid, {module, gen_server}, SItems} = sys:get_status(Pid),
[state] = [S || SItemList = [_ | _] <- SItems, {formatted_state, S} <- SItemList],
%% this code is actually what we use to retrieve the state in other tests
%% therefore it returns State as its status
state = get_state(Pid),
{comment, []}.

-spec no_format_status(config()) -> {comment, []}.
no_format_status(_Config) ->
%% crashy_server doesn't implement format_status/2
%% crashy_server doesn't implement format_status/1
{ok, Pid} = wpool_process:start_link(?MODULE, crashy_server, state, []),
%% therefore it uses the default format for the stauts (but with the status of
%% the gen_server, not wpool_process)
{status, Pid, {module, gen_server}, SItems} = sys:get_status(Pid),
[state] =
[S || SItemList = [_ | _] <- SItems, {data, Data} <- SItemList, {"State", S} <- Data],
state = get_state(Pid),
{comment, []}.

-spec call(config()) -> {comment, []}.
Expand Down Expand Up @@ -328,6 +323,8 @@ complete_coverage(_Config) ->

{comment, []}.

-if(?OTP_RELEASE < 25).

%% @doc We can use this function in tests since echo_server implements
%% format_status/2 by returning the state as a tuple {formatted_state, S}.
%% We can safely grab it from the result of sys:get_status/1
Expand All @@ -339,3 +336,26 @@ get_state(Pid) ->
{status, Pid, {module, gen_server}, SItems} = sys:get_status(Pid),
[State] = [S || SItemList = [_ | _] <- SItems, {formatted_state, S} <- SItemList],
State.

- else .

%% @doc We can use this function in tests since echo_server implements
%% format_status/1 by returning the status as a map S.
%% We can safely grab it from the result of sys:get_status/1
%% @see gen_server:format_status/1
%% @see sys:get_status/2
get_state(Atom) when is_atom(Atom) ->
get_state(whereis(Atom));
get_state(Pid) ->
{status, Pid, {module, gen_server}, [_PDict, _SysState, _Parent, _Dbg, Misc]} =
sys:get_status(Pid),
[State] =
lists:filtermap(fun ({data, [{"State", State}]}) ->
{true, State};
(_) ->
false
end,
Misc),
wpool_process:get_state(State).

-endif.

0 comments on commit d0791f9

Please sign in to comment.