Skip to content

Commit

Permalink
Merge pull request #63 from tomjnixon/fix_adjacent_weekly
Browse files Browse the repository at this point in the history
fix for adjacent days in weekly pattern
  • Loading branch information
Santiniis authored Jul 7, 2021
2 parents 8b2f0b0 + c18724f commit 4877099
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ecrn_agent.erl
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ until_next_time2(State, {monthly, When, Period}) ->

until_next_time_from_now(State, Period, 0, NextDays) when is_function(NextDays, 0) ->
CurrentTime = current_time(State),
F = fun() -> until_days_from_now(State, Period, NextDays()-1) end,
F = fun() -> until_days_from_now(State, Period, NextDays()) end,
case last_time(Period) of
T when CurrentTime > T ->
F();
Expand Down
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 4877099

Please sign in to comment.