diff --git a/test/ecrn_test.erl b/test/ecrn_test.erl index 5b64f63..9ef77d4 100644 --- a/test/ecrn_test.erl +++ b/test/ecrn_test.erl @@ -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}}), @@ -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 %%%=================================================================== @@ -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.