Skip to content

Commit

Permalink
Test restructuring (Nordix#12)
Browse files Browse the repository at this point in the history
Move tests that needs a running Redis instance to ct
  • Loading branch information
bjosv authored Jun 14, 2020
1 parent b3b34fe commit 437715f
Show file tree
Hide file tree
Showing 7 changed files with 564 additions and 491 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ dist: bionic
services:
- docker

# Install latest rebar3 and elvis
before_install:
- git clone https://github.com/erlang/rebar3.git && cd rebar3 && git checkout 3.13.1 && ./bootstrap && sudo mv rebar3 /usr/local/bin/ && cd ..
- git clone https://github.com/inaka/elvis.git && cd elvis && rebar3 escriptize && sudo cp _build/default/bin/elvis /usr/bin && cd ..
- sudo docker info

notifications:
email: false

Expand All @@ -24,6 +18,12 @@ otp_release:
- 21.2
- 20.3.8

install:
- sudo apt-get install -qq --no-install-recommends faketime
- sudo docker info
- git clone https://github.com/erlang/rebar3.git && cd rebar3 && git checkout 3.13.1 && ./bootstrap && sudo mv rebar3 /usr/local/bin/ && cd ..
- git clone https://github.com/inaka/elvis.git && cd elvis && rebar3 escriptize && sudo cp _build/default/bin/elvis /usr/bin && cd ..

script:
- make elvis

Expand Down
27 changes: 12 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,27 @@ clean:
@rebar3 clean
@rm -rf _build

test: test-tls test-tcp
test: ut ct

test-tcp:
-@docker rm -f redis
@docker run --name redis -d --net=host redis:$(REDIS_VERSION)
@rebar3 eunit -v --cover_export_name tcp \
--suite eredis_parser_tests,eredis_sub_tests,eredis_tests || \
{ docker logs redis; exit 1; }
@docker rm -f redis
ut:
@rebar3 eunit -v --cover_export_name ut

ct: ct-tcp ct-tls

test-tls:
ct-tcp:
-@docker rm -f redis
@docker run --name redis -d --net=host -v $(shell pwd)/priv/configs:/conf:ro \
redis:$(REDIS_VERSION) redis-server /conf/redis_tls.conf
@rebar3 eunit -v --cover_export_name tls \
--suite eredis_tls_tests || { docker logs redis; exit 1; }
@docker run --name redis -d --net=host redis:$(REDIS_VERSION)
@rebar3 ct -v --cover_export_name ct-tcp \
--suite eredis_tcp_SUITE,eredis_pubsub_SUITE || { docker logs redis; exit 1; }
@docker rm -f redis

ct:
ct-tls:
@priv/update-client-cert.sh tls_soon_expired_client_certs
-@docker rm -f redis
@docker run --name redis -d --net=host -v $(shell pwd)/priv/configs:/conf:ro \
redis:$(REDIS_VERSION) redis-server /conf/redis_tls.conf
@rebar3 ct -v --cover_export_name ct || { docker logs redis; exit 1; }
@rebar3 ct -v --cover_export_name ct-tls \
--suite eredis_tls_SUITE || { docker logs redis; exit 1; }
@docker rm -f redis

edoc:
Expand Down
78 changes: 0 additions & 78 deletions test/eredis_SUITE.erl

This file was deleted.

179 changes: 98 additions & 81 deletions test/eredis_sub_tests.erl → test/eredis_pubsub_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
-module(eredis_sub_tests).

-include_lib("eunit/include/eunit.hrl").
-module(eredis_pubsub_SUITE).

%% Test framework
-export([ init_per_suite/1
, end_per_suite/1
, all/0
, suite/0
]).

%% Test cases
-export([ t_pubsub/1
, t_pubsub2/1
, t_pubsub_manage_subscribers/1
, t_pubsub_connect_disconnect_messages/1
, t_drop_queue/1
, t_crash_queue/1
, t_dynamic_channels/1
, t_pubsub_pattern/1
]).

-include_lib("common_test/include/ct.hrl").
-include_lib("stdlib/include/assert.hrl").
-include("eredis.hrl").
-include("eredis_sub.hrl").

c() ->
Res = eredis:start_link(),
?assertMatch({ok, _}, Res),
{ok, C} = Res,
C.
init_per_suite(Config) ->
Config.

s() ->
Res = eredis_sub:start_link("127.0.0.1", 6379, ""),
?assertMatch({ok, _}, Res),
{ok, C} = Res,
C.
end_per_suite(_Config) ->
ok.

add_channels(Sub, Channels) ->
ok = eredis_sub:controlling_process(Sub),
ok = eredis_sub:subscribe(Sub, Channels),
lists:foreach(
fun (C) ->
receive M ->
?assertEqual({subscribed, C, Sub}, M),
eredis_sub:ack_message(Sub)
end
end, Channels).
all() -> [F || {F, _A} <- module_info(exports),
case atom_to_list(F) of
"t_" ++ _ -> true;
_ -> false
end].

suite() -> [{timetrap, {minutes, 1}}].

pubsub_test() ->
%% Tests
t_pubsub(Config) when is_list(Config) ->
Pub = c(),
Sub = s(),
add_channels(Sub, [<<"chan1">>, <<"chan2">>]),
Expand All @@ -50,7 +61,7 @@ pubsub_test() ->
eredis_sub:stop(Sub).

%% Push size so high, the queue will be used
pubsub2_test() ->
t_pubsub2(Config) when is_list(Config) ->
Pub = c(),
Sub = s(),
add_channels(Sub, [<<"chan">>]),
Expand All @@ -64,7 +75,7 @@ pubsub2_test() ->
?assertEqual(500, length(Msgs)),
eredis_sub:stop(Sub).

pubsub_manage_subscribers_test() ->
t_pubsub_manage_subscribers(Config) when is_list(Config) ->
Pub = c(),
Sub = s(),
add_channels(Sub, [<<"chan">>]),
Expand All @@ -88,8 +99,7 @@ pubsub_manage_subscribers_test() ->
Ref = erlang:monitor(process, Sub),
receive {'DOWN', Ref, process, Sub, _} -> ok end.


pubsub_connect_disconnect_messages_test() ->
t_pubsub_connect_disconnect_messages(Config) when is_list(Config) ->
Pub = c(),
Sub = s(),
add_channels(Sub, [<<"chan">>]),
Expand All @@ -105,9 +115,7 @@ pubsub_connect_disconnect_messages_test() ->
?assertEqual({eredis_connected, Sub}, wait_for_msg(S)),
eredis_sub:stop(Sub).



drop_queue_test() ->
t_drop_queue(Config) when is_list(Config) ->
Pub = c(),
{ok, Sub} = eredis_sub:start_link("127.0.0.1", 6379, "", 100, 10, drop),
add_channels(Sub, [<<"foo">>]),
Expand All @@ -119,8 +127,7 @@ drop_queue_test() ->
receive M2 -> ?assertEqual({dropped, 11}, M2) end,
eredis_sub:stop(Sub).


crash_queue_test() ->
t_crash_queue(Config) when is_list(Config) ->
Pub = c(),
{ok, Sub} = eredis_sub:start_link("127.0.0.1", 6379, "", 100, 10, exit),
add_channels(Sub, [<<"foo">>]),
Expand All @@ -134,9 +141,7 @@ crash_queue_test() ->
receive M1 -> ?assertEqual({message, <<"foo">>, <<"1">>, Sub}, M1) end,
receive M2 -> ?assertEqual({'DOWN', Ref, process, Sub, max_queue_size}, M2) end.



dynamic_channels_test() ->
t_dynamic_channels(Config) when is_list(Config) ->
Pub = c(),
{ok, Sub} = eredis_sub:start_link(),
ok = eredis_sub:controlling_process(Sub),
Expand Down Expand Up @@ -172,6 +177,63 @@ dynamic_channels_test() ->

?assertEqual({ok, [<<"newchan">>]}, eredis_sub:channels(Sub)).

% Tests for Pattern Subscribe
t_pubsub_pattern(Config) when is_list(Config) ->
Pub = c(),
Sub = s(),
add_channels_pattern(Sub, [<<"chan1*">>, <<"chan2*">>]),
ok = eredis_sub:controlling_process(Sub),

?assertEqual({ok, <<"1">>}, eredis:q(Pub, ["PUBLISH", <<"chan123">>, <<"msg">>])),
receive
{pmessage, _Pattern, _Channel, _Message, _} = M ->
?assertEqual({pmessage, <<"chan1*">>, <<"chan123">>, <<"msg">>, Sub}, M)
after 10 ->
throw(timeout)
end,

eredis_sub:punsubscribe(Sub, [<<"chan1*">> , <<"chan2*">>]),
eredis_sub:ack_message(Sub),
eredis_sub:ack_message(Sub),
receive {unsubscribed, _, _} = M2 -> ?assertEqual({unsubscribed, <<"chan1*">>, Sub}, M2) end,
eredis_sub:ack_message(Sub),
receive {unsubscribed, _, _} = M3 -> ?assertEqual({unsubscribed, <<"chan2*">>, Sub}, M3) end,
eredis_sub:ack_message(Sub),

?assertEqual({ok, <<"0">>}, eredis:q(Pub, ["PUBLISH", <<"chan123">>, <<"msg">>])),
receive
Msg -> throw({unexpected_message, Msg})
after 10 ->
ok
end,

eredis_sub:stop(Sub).

%%
%% Helpers
%%
c() ->
Res = eredis:start_link(),
?assertMatch({ok, _}, Res),
{ok, C} = Res,
C.

s() ->
Res = eredis_sub:start_link("127.0.0.1", 6379, ""),
?assertMatch({ok, _}, Res),
{ok, C} = Res,
C.

add_channels(Sub, Channels) ->
ok = eredis_sub:controlling_process(Sub),
ok = eredis_sub:subscribe(Sub, Channels),
lists:foreach(
fun (C) ->
receive M ->
?assertEqual({subscribed, C, Sub}, M),
eredis_sub:ack_message(Sub)
end
end, Channels).

recv_all(Sub) ->
recv_all(Sub, []).
Expand Down Expand Up @@ -228,12 +290,6 @@ get_state([{data, [{"State", State}]} | _]) ->
get_state([_|Rest]) ->
get_state(Rest).





% Tests for Pattern Subscribe

add_channels_pattern(Sub, Channels) ->
ok = eredis_sub:controlling_process(Sub),
ok = eredis_sub:psubscribe(Sub, Channels),
Expand All @@ -244,42 +300,3 @@ add_channels_pattern(Sub, Channels) ->
eredis_sub:ack_message(Sub)
end
end, Channels).





pubsub_pattern_test() ->
Pub = c(),
Sub = s(),
add_channels_pattern(Sub, [<<"chan1*">>, <<"chan2*">>]),
ok = eredis_sub:controlling_process(Sub),

?assertEqual({ok, <<"1">>}, eredis:q(Pub, ["PUBLISH", <<"chan123">>, <<"msg">>])),
receive
{pmessage, _Pattern, _Channel, _Message, _} = M ->
?assertEqual({pmessage, <<"chan1*">>, <<"chan123">>, <<"msg">>, Sub}, M)
after 10 ->
throw(timeout)
end,

eredis_sub:punsubscribe(Sub, [<<"chan1*">> , <<"chan2*">>]),
eredis_sub:ack_message(Sub),
eredis_sub:ack_message(Sub),
receive {unsubscribed, _, _} = M2 -> ?assertEqual({unsubscribed, <<"chan1*">>, Sub}, M2) end,
eredis_sub:ack_message(Sub),
receive {unsubscribed, _, _} = M3 -> ?assertEqual({unsubscribed, <<"chan2*">>, Sub}, M3) end,
eredis_sub:ack_message(Sub),

?assertEqual({ok, <<"0">>}, eredis:q(Pub, ["PUBLISH", <<"chan123">>, <<"msg">>])),
receive
Msg -> throw({unexpected_message, Msg})
after 10 ->
ok
end,

eredis_sub:stop(Sub).




Loading

0 comments on commit 437715f

Please sign in to comment.