Skip to content

Commit

Permalink
[#321] In sumo_store add handle_info and terminate as optional …
Browse files Browse the repository at this point in the history
…callbacks
  • Loading branch information
cabol committed Jul 18, 2017
1 parent 7461e69 commit 67fc74e
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 36 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
sudo: false
language: erlang
otp_release:
- 19.2
- 18.3
- 19.3
before_install:
- ./ci before_install "${PWD:?}"/rebar3
install:
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
strict_validation,
warn_export_vars,
warn_exported_vars,
warn_missing_spec,
warn_untyped_record,
debug_info
]}.
Expand Down Expand Up @@ -62,13 +61,12 @@
strict_validation,
warn_export_vars,
warn_exported_vars,
warn_missing_spec,
warn_untyped_record,
debug_info
]}.

{ct_opts, [
{sys_config, ["test/test.config"]}
{sys_config, ["config/sys.config"]}
]}.

%% == Cover ==
Expand Down
2 changes: 1 addition & 1 deletion src/sumo_backend_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ start_link() ->
%%% Supervisor callbacks
%%%=============================================================================

-spec init(any()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
%% hidden
init([]) ->
{ok, Backends} = application:get_env(sumo_db, storage_backends),
Children = lists:map(fun({Name, Module, Options}) ->
Expand Down
8 changes: 6 additions & 2 deletions src/sumo_event.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ dispatch(DocName, Event, Args) ->
dispatch(DocName, EventId, Event, Args).

%% @doc Dispatch an event through gen_event:notify/2.
-spec dispatch(sumo:schema_name(), event_id(), term(), term()) ->
event_id() | no_event_managers.
-spec dispatch(DocName, EventId, Event, Args) -> Res when
DocName :: sumo:schema_name(),
EventId :: event_id(),
Event :: term(),
Args :: term(),
Res :: event_id() | no_event_managers.
dispatch(DocName, EventId, Event, Args) ->
case sumo_config:get_event_managers(DocName) of
[] ->
Expand Down
2 changes: 1 addition & 1 deletion src/sumo_event_manager_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%%% Supervisor callbacks
%%%=============================================================================

-spec init(any()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
%% hidden
init([]) ->
EventManagers = sumo_config:get_event_managers(),
ManagersList = [manager(EventManager) || EventManager <- EventManagers],
Expand Down
60 changes: 36 additions & 24 deletions src/sumo_store.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,34 @@
handler_state = undefined :: any()
}).

-type state() :: #state{}.

%%%=============================================================================
%%% Callbacks
%%%=============================================================================

-type result(R, S) :: {ok, R, S} | {error, term(), S}.
-type result(S) :: {ok, S} | {error, term(), S}.
-type affected_rows() :: unknown | non_neg_integer().

-export_type([result/2, result/1, affected_rows/0]).

%%%=============================================================================
%%% gen_server callbacks
%%%=============================================================================

-callback init(term()) -> {ok, term()}.

-callback handle_info(Info, State) -> Res when
Info :: term(),
State :: term(),
Res :: {noreply, State}
| {noreply, State, timeout()}
| {noreply, State, hibernate}
| {stop, Reason :: term(), State}.

-callback terminate(Reason, State) -> ok when
Reason :: normal | shutdown | {shutdown, term()} | term(),
State :: term().

%%%=============================================================================
%%% API callbacks
%%%=============================================================================

-callback create_schema(Schema, State) -> Res when
Schema :: sumo:schema(),
Res :: result(State).
Expand Down Expand Up @@ -136,6 +150,8 @@
Conditions :: sumo:conditions(),
Res :: result(non_neg_integer(), State).

-optional_callbacks([handle_info/2, terminate/2]).

%%%=============================================================================
%%% API
%%%=============================================================================
Expand Down Expand Up @@ -293,14 +309,12 @@ call(Name, DocName, Function, Args) ->

%% @doc Called by start_link.
%% @hidden
-spec init([term()]) -> {ok, state()}.
init([Module, Options]) ->
{ok, HState} = Module:init(Options),
{ok, #state{handler=Module, handler_state=HState}}.

%% @doc handles calls.
%% @hidden
-spec handle_call(term(), _, state()) -> {reply, tuple(), state()}.
handle_call(
{create_schema, Schema}, _From,
#state{handler = Handler, handler_state = HState} = State
Expand Down Expand Up @@ -403,29 +417,27 @@ handle_call(
{reply, {OkOrError, Reply}, State#state{handler_state=NewState}}.

%% @hidden
-spec handle_cast(term(), state()) ->
{noreply, state()} |
{noreply, state(), non_neg_integer()} |
{noreply, state(), hibernate} |
{stop, term(), state()}.
handle_cast(_Msg, State) ->
{noreply, State}.

%% @hidden
-spec handle_info(term(), state()) ->
{noreply, state()} |
{noreply, state(), non_neg_integer()} |
{noreply, state(), hibernate} |
{stop, term(), state()}.
handle_info(_Info, State) ->
{noreply, State}.
handle_info(Info, #state{handler = Handler, handler_state = HState} = State) ->
try erlang:function_exported(Handler, handle_info, 2) of
true -> Handler:handle_info(Info, HState);
false -> {noreply, State}
catch
_:Reason -> {stop, Reason, State}
end.

%% @hidden
-spec terminate(term(), state()) -> ok.
terminate(_Reason, _State) ->
ok.
terminate(Reason, #state{handler = Handler, handler_state = HState}) ->
try erlang:function_exported(Handler, terminate, 2) of
true -> Handler:terminate(Reason, HState);
false -> ok
catch
_:_ -> ok
end.

%% @hidden
-spec code_change(term(), state(), term()) -> {ok, state()} | {error, term()}.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
2 changes: 1 addition & 1 deletion src/sumo_store_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ start_link() ->
%%% Supervisor callbacks
%%%=============================================================================

-spec init(any()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
%% @hidden
init([]) ->
{ok, Stores} = application:get_env(sumo_db, stores),
Children = lists:map(fun({Name, Module, Options}) ->
Expand Down
9 changes: 7 additions & 2 deletions test/sumo_meta_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
]}
]).

-export([init_per_suite/1]).
-export([init_per_suite/1, end_per_suite/1]).

-type config() :: [{atom(), term()}].

-spec init_per_suite(config()) -> config().
init_per_suite(Config) -> [{application, sumo_db} | Config].
init_per_suite(Config) ->
[{application, sumo_db} | Config].

-spec end_per_suite(config()) -> config().
end_per_suite(Config) ->
Config.

0 comments on commit 67fc74e

Please sign in to comment.