Skip to content

Commit

Permalink
Merge pull request #203 from peek-travel/hotfix/fix-infinite-loop
Browse files Browse the repository at this point in the history
Infinite loop possible if "end time" is within "duration" of midnight
  • Loading branch information
vanvoljg authored Oct 18, 2021
2 parents 2c91fb9 + 30f7df2 commit 3dd3b6e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/cocktail/validation/time_range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ defmodule Cocktail.Validation.TimeRange do
defp generate_times(%__MODULE__{} = time_range) do
time_range.start_time
|> Stream.unfold(fn time ->
case Time.compare(time, time_range.end_time) do
:gt ->
current_gt_end = Time.compare(time, time_range.end_time)
next_time = Time.add(time, time_range.interval_seconds)
current_gt_next = Time.compare(time, next_time)

case {current_gt_end, current_gt_next} do
{:gt, _} ->
nil

{_, :gt} ->
nil

_ ->
{time, Time.add(time, time_range.interval_seconds)}
{time, next_time}
end
end)
|> Enum.to_list()
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Cocktail.Mixfile do
use Mix.Project

@version "0.10.0"
@version "0.10.1"

def project do
[
Expand Down
17 changes: 17 additions & 0 deletions test/cocktail/schedule_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ defmodule Cocktail.ScheduleTest do
assert times == [~N[2017-09-09 09:00:00]]
end

test "occurrences at the end of the day won't cause an infinite loop" do
assert [%{from: ~N[2021-10-15 23:00:00], until: ~N[2021-10-15 23:30:00]}] =
~N[2021-10-15 23:00:00]
|> Schedule.new(duration: 1800)
|> Schedule.add_recurrence_rule(
:daily,
time_range: %{
start_time: ~T[23:00:00],
end_time: ~T[23:55:00],
interval_seconds: 1800
},
until: ~N[2021-10-15 23:55:00]
)
|> Schedule.occurrences()
|> Enum.to_list()
end

test "add recurrence rule with bad options" do
schedule =
~N[2017-09-09 09:00:00]
Expand Down

0 comments on commit 3dd3b6e

Please sign in to comment.