Skip to content

Commit

Permalink
add tests for weekly jobs
Browse files Browse the repository at this point in the history
see previous commit
  • Loading branch information
tomjnixon committed Dec 6, 2020
1 parent f447e3a commit c18724f
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions test/ecrn_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ cron_test_() ->
fun(_) ->
application:stop(erlcron)
end,
{timeout, 30, [
[{timeout, 30, [
?FuncTest(set_alarm),
?FuncTest(travel_back_in_time),
?FuncTest(cancel_alarm),
?FuncTest(big_time_jump),
?FuncTest(cron),
?FuncTest(validation)
]}}.
]},
{timeout, 30, [
?FuncTest(weekly)
]},
{timeout, 30, [
?FuncTest(weekly_every)
]}
]}.

set_alarm() ->
erlcron:set_datetime({{2000,1,1},{8,0,0}}),
Expand Down Expand Up @@ -139,6 +146,26 @@ validation() ->
?assertMatch({error,{invalid_days_in_schedule,{monthly,"A",{55,am}}}},
ecrn_agent:validate({monthly, 65, {55, am}})).

weekly() ->
DateF = fun (Offset) -> {2000, 1, 1 + Offset} end,
erlcron:set_datetime({DateF(0), {7,0,0}}),
Self = self(),
erlcron:cron(weekly, {{weekly, [sat, sun], {9,0,0}}, fun() -> Self ! weekly end}),
Pattern = [1, 1, 0, 0, 0, 0, 0, 1],
collect_weekly(DateF, {8, 0, 0}, {10, 0, 0}, Pattern),
erlcron:cancel(weekly).

weekly_every() ->
DateF = fun (Offset) -> {2000, 1, 1 + Offset} end,
erlcron:set_datetime({DateF(0), {7,0,0}}),
Self = self(),
erlcron:cron(weekly, {{weekly, [sat, mon],
{every, {29, sec}, {between, {9, 0, 0}, {9, 1, 0}}}},
fun() -> Self ! weekly end}),
Pattern = [3, 0, 3, 0, 0, 0, 0, 3],
collect_weekly(DateF, {8, 0, 0}, {10, 0, 0}, Pattern),
erlcron:cancel(weekly).

%%%===================================================================
%%% Internal Functions
%%%===================================================================
Expand All @@ -155,3 +182,16 @@ collect(Msg, Timeout, I, Count) ->
after
Timeout -> I
end.

% check that for each day generated by DateF(I) for increasing I, Pattern[I]
% weekly messages are received
collect_weekly(DateF, TimeBefore, TimeAfter, Pattern) ->
collect_weekly(DateF, TimeBefore, TimeAfter, Pattern, 0).

collect_weekly(DateF, TimeBefore, TimeAfter, [N | PatternTail], I) ->
erlcron:set_datetime({DateF(I), TimeBefore}),
?assertMatch(0, collect(weekly, 1000, 1)),
erlcron:set_datetime({DateF(I), TimeAfter}),
?assertMatch(N, collect(weekly, 1000, N)),
collect_weekly(DateF, TimeBefore, TimeAfter, PatternTail, I+1);
collect_weekly(_DateF, _TimeBefore, _TimeAfter, [], _I) -> ok.

0 comments on commit c18724f

Please sign in to comment.