Skip to content

Commit

Permalink
Rescue ArithmeticError in schedule_update_on_error
Browse files Browse the repository at this point in the history
  • Loading branch information
cruessler committed Feb 19, 2024
1 parent ab6fc41 commit c95b5e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/ex_rss/feed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,15 @@ defmodule ExRss.Feed do
retries = Changeset.get_field(changeset, :retries)

new_timeout =
(:math.pow(1.5, retries) * @base_timeout)
|> round
|> min(@max_timeout)
try do
(:math.pow(1.5, retries) * @base_timeout)
|> round
|> min(@max_timeout)
rescue
# `:math.pow` throws an `ArithmeticError` once `retries` crosses a
# certain threshold.
ArithmeticError -> @max_timeout
end

next_update_at =
changeset
Expand Down
12 changes: 12 additions & 0 deletions test/ex_rss/feed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ defmodule ExRss.FeedTest do
assert Timex.compare(changeset.changes.next_update_at, @now) == 1
end

test "schedule_update_on_error with retries that cause an ArithmeticError" do
changeset =
%Feed{retries: 2000, next_update_at: @now}
|> Changeset.change()
|> Feed.schedule_update_on_error(@now)

diff = Timex.diff(changeset.changes.next_update_at, @now, :seconds)

assert %{retries: 2001} = changeset.changes
assert diff == @max_timeout
end

test "schedule_update has a maximum timeout" do
changeset =
%Feed{retries: 20, next_update_at: @now}
Expand Down

0 comments on commit c95b5e2

Please sign in to comment.