Skip to content

Commit 4877099

Browse files
authored
Merge pull request #63 from tomjnixon/fix_adjacent_weekly
fix for adjacent days in weekly pattern
2 parents 8b2f0b0 + c18724f commit 4877099

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/ecrn_agent.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ until_next_time2(State, {monthly, When, Period}) ->
435435

436436
until_next_time_from_now(State, Period, 0, NextDays) when is_function(NextDays, 0) ->
437437
CurrentTime = current_time(State),
438-
F = fun() -> until_days_from_now(State, Period, NextDays()-1) end,
438+
F = fun() -> until_days_from_now(State, Period, NextDays()) end,
439439
case last_time(Period) of
440440
T when CurrentTime > T ->
441441
F();

test/ecrn_test.erl

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ cron_test_() ->
2424
fun(_) ->
2525
application:stop(erlcron)
2626
end,
27-
{timeout, 30, [
27+
[{timeout, 30, [
2828
?FuncTest(set_alarm),
2929
?FuncTest(travel_back_in_time),
3030
?FuncTest(cancel_alarm),
3131
?FuncTest(big_time_jump),
3232
?FuncTest(cron),
3333
?FuncTest(validation)
34-
]}}.
34+
]},
35+
{timeout, 30, [
36+
?FuncTest(weekly)
37+
]},
38+
{timeout, 30, [
39+
?FuncTest(weekly_every)
40+
]}
41+
]}.
3542

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

149+
weekly() ->
150+
DateF = fun (Offset) -> {2000, 1, 1 + Offset} end,
151+
erlcron:set_datetime({DateF(0), {7,0,0}}),
152+
Self = self(),
153+
erlcron:cron(weekly, {{weekly, [sat, sun], {9,0,0}}, fun() -> Self ! weekly end}),
154+
Pattern = [1, 1, 0, 0, 0, 0, 0, 1],
155+
collect_weekly(DateF, {8, 0, 0}, {10, 0, 0}, Pattern),
156+
erlcron:cancel(weekly).
157+
158+
weekly_every() ->
159+
DateF = fun (Offset) -> {2000, 1, 1 + Offset} end,
160+
erlcron:set_datetime({DateF(0), {7,0,0}}),
161+
Self = self(),
162+
erlcron:cron(weekly, {{weekly, [sat, mon],
163+
{every, {29, sec}, {between, {9, 0, 0}, {9, 1, 0}}}},
164+
fun() -> Self ! weekly end}),
165+
Pattern = [3, 0, 3, 0, 0, 0, 0, 3],
166+
collect_weekly(DateF, {8, 0, 0}, {10, 0, 0}, Pattern),
167+
erlcron:cancel(weekly).
168+
142169
%%%===================================================================
143170
%%% Internal Functions
144171
%%%===================================================================
@@ -155,3 +182,16 @@ collect(Msg, Timeout, I, Count) ->
155182
after
156183
Timeout -> I
157184
end.
185+
186+
% check that for each day generated by DateF(I) for increasing I, Pattern[I]
187+
% weekly messages are received
188+
collect_weekly(DateF, TimeBefore, TimeAfter, Pattern) ->
189+
collect_weekly(DateF, TimeBefore, TimeAfter, Pattern, 0).
190+
191+
collect_weekly(DateF, TimeBefore, TimeAfter, [N | PatternTail], I) ->
192+
erlcron:set_datetime({DateF(I), TimeBefore}),
193+
?assertMatch(0, collect(weekly, 1000, 1)),
194+
erlcron:set_datetime({DateF(I), TimeAfter}),
195+
?assertMatch(N, collect(weekly, 1000, N)),
196+
collect_weekly(DateF, TimeBefore, TimeAfter, PatternTail, I+1);
197+
collect_weekly(_DateF, _TimeBefore, _TimeAfter, [], _I) -> ok.

0 commit comments

Comments
 (0)