Skip to content

Commit

Permalink
Merge pull request #40 from manuelmeurer/no-wait
Browse files Browse the repository at this point in the history
[feat]: Allow wait to accept 0, false and nil to disable waiting.
  • Loading branch information
nickfloyd authored Sep 29, 2023
2 parents 9b7e9da + 44b3185 commit 9de9443
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Octopoller exposes a single function `poll`. Here is what the API looks like:
#
# wait - The time delay in seconds between polls (default is 1 second)
# - When given the argument `:exponentially` the action will be retried with exponetial backoff
# - When given 0, false or nil, the action will be retried without wait
# timeout - The maximum number of seconds the poller will execute
# retries - The maximum number of times the action will be retried
# yield - A block that will execute, and if `:re_poll` is returned it will re-run
Expand Down
4 changes: 3 additions & 1 deletion lib/octopoller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class TooManyAttemptsError < StandardError; end
# raise - Raises an Octopoller::TimeoutError if the timeout is reached
# raise - Raises an Octopoller::TooManyAttemptsError if the retries is reached
def poll(wait: 1, timeout: nil, retries: nil)
wait = 0 if [nil, false].include?(wait)

Octopoller.validate_arguments(wait, timeout, retries)
exponential_backoff = (wait == :exponentially)

Expand Down Expand Up @@ -45,7 +47,7 @@ def self.validate_arguments(wait, timeout, retries)
raise ArgumentError, "Must pass an argument to either `timeout` or `retries`"
end
exponential_backoff = wait == :exponentially
raise ArgumentError, "Cannot wait backwards in time" unless exponential_backoff || wait.positive?
raise ArgumentError, "Cannot wait backwards in time" if !exponential_backoff && wait.negative?
raise ArgumentError, "Timed out without even being able to try" if timeout&.negative?
raise ArgumentError, "Cannot retry something a negative number of times" if retries&.negative?
end
Expand Down
25 changes: 21 additions & 4 deletions spec/octopoller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
.to raise_error(ArgumentError, "Timed out without even being able to try")
end

it "polls without wait" do
[0, false, nil].each do |wait|
tries, start, duration = 0, Time.now, nil
result = Octopoller.poll(wait: wait, timeout: 0.05) do
tries += 1
if tries == 3
duration = Time.now - start
"Success!"
else
:re_poll
end
end
expect(result).to eq("Success!")
expect(duration).to be < 0.1
end
end

it "polls until timeout" do
expect do
Octopoller.poll(wait: 0.01, timeout: 0.05) do
Expand Down Expand Up @@ -66,7 +83,7 @@

it "poll until max retries reached" do
expect do
Octopoller.poll(wait: 0.01, retries: 2) do
Octopoller.poll(wait: false, retries: 2) do
:re_poll
end
end.to raise_error(Octopoller::TooManyAttemptsError, "Polled maximum number of attempts")
Expand All @@ -75,7 +92,7 @@
it "tries exactly the number of retries" do
attempts = 0
expect do
Octopoller.poll(wait: 0.01, retries: 3) do
Octopoller.poll(wait: false, retries: 3) do
attempts += 1
:re_poll
end
Expand Down Expand Up @@ -107,7 +124,7 @@

it "try until successful return" do
tried = false
result = Octopoller.poll(wait: 0.01, retries: 1) do
result = Octopoller.poll(wait: false, retries: 1) do
if tried
"Success!"
else
Expand All @@ -120,7 +137,7 @@

it "doesn't swallow a raised error" do
expect do
Octopoller.poll(wait: 0.01, retries: 0) do
Octopoller.poll(wait: false, retries: 0) do
raise StandardError, "An error occured"
end
end.to raise_error(StandardError, "An error occured")
Expand Down

0 comments on commit 9de9443

Please sign in to comment.