Skip to content

Commit

Permalink
Merge pull request #205 from inaka/chore/adopt-otp-27
Browse files Browse the repository at this point in the history
Adopt Erlang/OTP 27
  • Loading branch information
paulo-ferraz-oliveira authored Jul 10, 2024
2 parents c22ccf8 + 93f2a9f commit 0ec5ee5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 39 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/erlang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ jobs:

runs-on: ubuntu-22.04

strategy:
matrix:
otp: ['25', '26']
rebar: ['3.22']

steps:
- uses: actions/checkout@v3
- uses: erlef/setup-beam@v1
id: setup-beam
with:
otp-version: ${{matrix.otp}}
rebar3-version: ${{matrix.rebar}}
version-type: strict
version-file: .tool-versions
- name: Restore _build
uses: actions/cache@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
erlang 27.0.1
rebar 3.23.0
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{erl_opts,
[warn_unused_import, warn_export_vars, warnings_as_errors, verbose, report, debug_info]}.

{minimum_otp_vsn, "21"}.
{minimum_otp_vsn, "27"}.

{profiles,
[{test,
Expand Down
33 changes: 21 additions & 12 deletions src/wpool_process.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@

%% 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, format_status/2]).
handle_continue/2, format_status/1]).

%%%===================================================================
%%% API
Expand Down Expand Up @@ -79,6 +86,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 Expand Up @@ -177,19 +192,13 @@ handle_continue(Continue, State) ->
end.

%% @private
-spec format_status(normal | terminate, [[{_, _}] | state(), ...]) -> term().
format_status(Opt, [PDict, State]) ->
case erlang:function_exported(State#state.mod, format_status, 2) of
-spec format_status(gen_server:format_status()) -> gen_server:format_status().
format_status(#{state := #state{mod = Mod}} = Status) ->
case erlang:function_exported(Mod, format_status, 1) of
false ->
case Opt % This is copied from gen_server:format_status/4
of
terminate ->
State#state.state;
normal ->
[{data, [{"State", State#state.state}]}]
end;
Status;
true ->
(State#state.mod):format_status(Opt, [PDict, State#state.state])
Mod:format_status(Status)
end.

%%%===================================================================
Expand Down
9 changes: 4 additions & 5 deletions test/echo_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

%% gen_server callbacks
-export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2,
handle_continue/2, format_status/2]).
handle_continue/2, format_status/1]).

-dialyzer([no_behaviours]).

Expand Down Expand Up @@ -59,7 +59,6 @@ handle_call(Call, _From, _State) ->
handle_continue(Continue, _State) ->
Continue.

-spec format_status(normal | terminate, [[{_, _}] | State, ...]) ->
{formatted_state, State}.
format_status(_, [_PDict, State]) ->
{formatted_state, State}.
-spec format_status(gen_server:format_status()) -> gen_server:format_status().
format_status(State) ->
State.
30 changes: 16 additions & 14 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 @@ -329,13 +324,20 @@ complete_coverage(_Config) ->
{comment, []}.

%% @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}.
%% 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/2
%% @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}, SItems} = sys:get_status(Pid),
[State] = [S || SItemList = [_ | _] <- SItems, {formatted_state, S} <- SItemList],
State.
{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).

0 comments on commit 0ec5ee5

Please sign in to comment.