Skip to content

Commit f3f59c5

Browse files
authored
Merge pull request #28 from launchdarkly/eb/sc-136142/jruby-zero-delay
fix error when delay is zero in JRuby
2 parents 030d2e7 + 145c82b commit f3f59c5

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/ld-eventsource/impl/backoff.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ def next_interval
4141
@last_good_time = nil
4242
target = ([@base_interval * (2 ** @attempts), @max_interval].min).to_f
4343
@attempts += 1
44-
(target / 2) + @jitter_rand.rand(target / 2)
44+
if target == 0
45+
0 # in some Ruby versions it's illegal to call rand(0)
46+
else
47+
(target / 2) + @jitter_rand.rand(target / 2)
48+
end
4549
end
4650

4751
#

spec/backoff_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ module Impl
4747
expect(interval).to be <= (initial * 2)
4848
expect(interval).to be >= initial
4949
end
50+
51+
it "always returns zero if the initial delay is zero" do
52+
initial = 0
53+
max = 60
54+
b = Backoff.new(initial, max)
55+
56+
for i in 1..6 do
57+
interval = b.next_interval
58+
expect(interval).to eq(0)
59+
end
60+
end
5061
end
5162
end
5263
end

0 commit comments

Comments
 (0)