Skip to content

Commit

Permalink
Refactor funbox_SUITE
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrivereshchagin committed Jul 27, 2024
1 parent edb2846 commit ce5a704
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
21 changes: 11 additions & 10 deletions test/funbox_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").

-include("funbox_t.hrl").

%%%===================================================================
%%% Common Test callbacks
%%%===================================================================

all() ->
[filterer_polls_queue,
specified_number_of_filterers_are_started,
producer_pushes_to_queue].
[test_filterer_polls_queue,
test_specified_number_of_filterers_are_started,
test_producer_pushes_to_queue].

init_per_suite(Config) ->
_ = application:unload(funbox),
Expand All @@ -37,19 +39,18 @@ end_per_testcase(_TestCase, _Config) ->
%%% Test cases
%%%===================================================================

filterer_polls_queue(Config) ->
test_filterer_polls_queue(Config) ->
FunboxConfig = ?config(funbox_config, Config),
QueueKey = funbox_config:queue_key(FunboxConfig),
ResultSetKey = funbox_config:result_set_key(FunboxConfig),
{ok, _} = funbox_filterer:start_link(FunboxConfig),
?assertEqual(0, q(["SCARD", ResultSetKey])),
_ = q(["LPUSH", QueueKey, 1, 2, 4, 5, "x"]),
ct:sleep(500),
?assertEqual(1, q(["SISMEMBER", ResultSetKey, 2])),
?assertEqual(1, q(["SISMEMBER", ResultSetKey, 5])),
_ = q(["LPUSH", QueueKey, 1, 2, 4, "x", 5]),
?WAIT_UNTIL(q(["SISMEMBER", ResultSetKey, 2]) =:= 1),
?WAIT_UNTIL(q(["SISMEMBER", ResultSetKey, 5]) =:= 1),
?assertEqual(2, q(["SCARD", ResultSetKey])).

specified_number_of_filterers_are_started(Config) ->
test_specified_number_of_filterers_are_started(Config) ->
FunboxConfig = ?config(funbox_config, Config),
FunboxConfig1 = FunboxConfig#{num_filterers := 2},
{ok, Pid1} = funbox_filterer_sup:start_link(FunboxConfig1),
Expand All @@ -60,7 +61,7 @@ specified_number_of_filterers_are_started(Config) ->
Counts2 = supervisor:count_children(Pid2),
?assertEqual(4, proplists:get_value(active, Counts2)).

producer_pushes_to_queue(Config) ->
test_producer_pushes_to_queue(Config) ->
FunboxConfig = ?config(funbox_config, Config),
QueueKey = funbox_config:queue_key(FunboxConfig),
?assertEqual(0, q(["LLEN", QueueKey])),
Expand Down
38 changes: 38 additions & 0 deletions test/funbox_t.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-module(funbox_t).

%% API
-export([wait_until/2]).

%%%===================================================================
%%% Macro definitions
%%%===================================================================

-define(NOW(), erlang:monotonic_time(millisecond)).

%%%===================================================================
%%% API
%%%===================================================================

-spec wait_until(fun(() -> boolean()), [Option]) ->
ok | timeout when
Option :: {timeout, timeout()} |
{delay, non_neg_integer()}.
wait_until(Pred, Opts) ->
Time = proplists:get_value(timeout, Opts, 5000),
Delay = proplists:get_value(delay, Opts, 100),
wait_until(Pred, ?NOW() + Time, Delay).

%%%===================================================================
%%% Internal functions
%%%===================================================================

wait_until(Pred, Deadline, Delay) ->
case {Pred(), ?NOW()} of
{true, _Now} ->
ok;
{false, Now} when Now < Deadline ->
timer:sleep(Delay),
wait_until(Pred, Deadline, Delay);
{false, _Now} ->
timeout
end.
14 changes: 14 additions & 0 deletions test/funbox_t.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-ifndef(FUNBOX_T_HRL).
-define(FUNBOX_T_HRL, true).

-define(WAIT_UNTIL(Expr), ?WAIT_UNTIL((Expr), [])).

-define(WAIT_UNTIL(Expr, Opts),
case funbox_t:wait_until(fun() -> (Expr) end, (Opts)) of
timeout ->
erlang:error({wait_timeout, [{expression, (??Expr)}]});
ok ->
ok
end).

-endif.

0 comments on commit ce5a704

Please sign in to comment.