Skip to content

Commit

Permalink
Cleanup of tests
Browse files Browse the repository at this point in the history
* Adding tests to improve coverage
* Adding elvis code formatting
* Group the parser tests
* Comment out example code in eredis_sub
  • Loading branch information
bjosv committed May 14, 2020
1 parent 312baa5 commit af3caa0
Show file tree
Hide file tree
Showing 5 changed files with 424 additions and 244 deletions.
21 changes: 19 additions & 2 deletions elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,29 @@
, {elvis_style, dont_repeat_yourself,
#{ min_complexity => 15
}}
%% , {elvis_style, no_debug_call}
, {elvis_style, no_debug_call}
]
},
#{ dirs => ["test"]
, filter => "*.erl"
, rules => [ ]
, rules => [ {elvis_style, no_tabs}
, {elvis_style, no_trailing_whitespace}
, {elvis_style, operator_spaces,
#{ rules => [ {right,","}
, {right,"+"}
, {left,"+"}
, {right,"*"}
, {left,"*"}
, {right,"--"}
, {left,"--"}
, {right,"++"}
, {left,"++"}
]
}}
, {elvis_style, no_nested_try_catch}
, {elvis_style, used_ignored_variable}
, {elvis_style, no_behavior_info}
]
}
]
}
Expand Down
88 changes: 38 additions & 50 deletions src/eredis_sub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

-export([psubscribe/2, punsubscribe/2]).

-export([receiver/1, sub_example/0, pub_example/0]).

-export([psub_example/0, ppub_example/0]).

%%
%% PUBLIC API
%%
Expand All @@ -40,7 +36,6 @@ start_link(Host, Port, Password, ReconnectSleep,
eredis_sub_client:start_link(Host, Port, Password, ReconnectSleep,
MaxQueueSize, QueueBehaviour).


%% @doc: Callback for starting from poolboy
-spec start_link(server_args()) -> {ok, Pid::pid()} | {error, Reason::term()}.
start_link(Args) ->
Expand All @@ -56,7 +51,6 @@ start_link(Args) ->
stop(Pid) ->
eredis_sub_client:stop(Pid).


-spec controlling_process(Client::pid()) -> ok.
%% @doc: Make the calling process the controlling process. The
%% controlling process received pubsub-related messages, of which
Expand Down Expand Up @@ -104,14 +98,12 @@ controlling_process(Client, Pid) ->
controlling_process(Client, Pid, Timeout) ->
gen_server:call(Client, {controlling_process, Pid}, Timeout).


-spec ack_message(Client::pid()) -> ok.
%% @doc: acknowledge the receipt of a pubsub message. each pubsub
%% message must be acknowledged before the next one is received
ack_message(Client) ->
gen_server:cast(Client, {ack_message, self()}).


%% @doc: Subscribe to the given channels. Returns immediately. The
%% result will be delivered to the controlling process as any other
%% message. Delivers {subscribed, Channel::binary(), pid()}
Expand All @@ -126,11 +118,11 @@ subscribe(Client, Channels) ->
psubscribe(Client, Channels) ->
gen_server:cast(Client, {psubscribe, self(), Channels}).



-spec unsubscribe(pid(), [channel()]) -> ok.
unsubscribe(Client, Channels) ->
gen_server:cast(Client, {unsubscribe, self(), Channels}).

-spec punsubscribe(pid(), [channel()]) -> ok.
punsubscribe(Client, Channels) ->
gen_server:cast(Client, {punsubscribe, self(), Channels}).

Expand All @@ -141,46 +133,42 @@ punsubscribe(Client, Channels) ->
channels(Client) ->
gen_server:call(Client, get_channels).



%%
%% STUFF FOR TRYING OUT PUBSUB
%% Examples
%%

receiver(Sub) ->
receive
Msg ->
io:format("received ~p~n", [Msg]),
ack_message(Sub),
?MODULE:receiver(Sub)
end.

sub_example() ->
{ok, Sub} = start_link(),
Receiver = spawn_link(fun () ->
controlling_process(Sub),
subscribe(Sub, [<<"foo">>]),
receiver(Sub)
end),
{Sub, Receiver}.

psub_example() ->
{ok, Sub} = start_link(),
Receiver = spawn_link(fun () ->
controlling_process(Sub),
psubscribe(Sub, [<<"foo*">>]),
receiver(Sub)
end),
{Sub, Receiver}.

pub_example() ->
{ok, P} = eredis:start_link(),
eredis:q(P, ["PUBLISH", "foo", "bar"]),
eredis_client:stop(P).

ppub_example() ->
{ok, P} = eredis:start_link(),
eredis:q(P, ["PUBLISH", "foo123", "bar"]),
eredis_client:stop(P).


%% receiver(Sub) ->
%% receive
%% Msg ->
%% io:format("received ~p~n", [Msg]),
%% ack_message(Sub),
%% ?MODULE:receiver(Sub)
%% end.

%% sub_example() ->
%% {ok, Sub} = start_link(),
%% Receiver = spawn_link(fun () ->
%% controlling_process(Sub),
%% subscribe(Sub, [<<"foo">>]),
%% receiver(Sub)
%% end),
%% {Sub, Receiver}.

%% psub_example() ->
%% {ok, Sub} = start_link(),
%% Receiver = spawn_link(fun () ->
%% controlling_process(Sub),
%% psubscribe(Sub, [<<"foo*">>]),
%% receiver(Sub)
%% end),
%% {Sub, Receiver}.

%% pub_example() ->
%% {ok, P} = eredis:start_link(),
%% eredis:q(P, ["PUBLISH", "foo", "bar"]),
%% eredis_client:stop(P).

%% ppub_example() ->
%% {ok, P} = eredis:start_link(),
%% eredis:q(P, ["PUBLISH", "foo123", "bar"]),
%% eredis_client:stop(P).
Loading

0 comments on commit af3caa0

Please sign in to comment.