From d0791f9c1d122965e1b57f70236f1e61755763f5 Mon Sep 17 00:00:00 2001 From: "Paulo F. Oliveira" Date: Wed, 10 Jul 2024 17:39:03 +0100 Subject: [PATCH] Fix tests as per changes to code We separate (again) results into pre-25 and 25+ --- src/wpool_process.erl | 15 ++++++++++++++ test/echo_server.erl | 2 +- test/wpool_process_SUITE.erl | 38 +++++++++++++++++++++++++++--------- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/wpool_process.erl b/src/wpool_process.erl index 08df09f..92b6866 100644 --- a/src/wpool_process.erl +++ b/src/wpool_process.erl @@ -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]). @@ -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 %%%=================================================================== diff --git a/test/echo_server.erl b/test/echo_server.erl index d214248..cf25bc3 100644 --- a/test/echo_server.erl +++ b/test/echo_server.erl @@ -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. diff --git a/test/wpool_process_SUITE.erl b/test/wpool_process_SUITE.erl index 36f7a0d..c12868f 100644 --- a/test/wpool_process_SUITE.erl +++ b/test/wpool_process_SUITE.erl @@ -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, []}. @@ -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 @@ -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.