Commit dc61262

mo <mo@mokhan.ca>
2018-11-11 02:39:33
fix off by one on retry logic.
1 parent 2edc669
lib/net/hippie/client.rb
@@ -71,11 +71,11 @@ module Net
       # attempt 7 -> delay 64 second
       # attempt 8 -> delay 128 second
       def with_retry(retries: 3)
-        retries = 3 if retries <= 0
+        retries = 0 if retries.nil? || retries.negative?
         0.upto(retries) do |n|
           return yield self
         rescue *::Net::Hippie::CONNECTION_ERRORS => error
-          raise error if n >= retries
+          raise error if n == retries
 
           delay = (2**n) + rand(0.5) # delay + jitter
           warn("`#{error.message}` Retry: #{n + 1}/#{retries} Delay: #{delay}s")
lib/net/hippie/version.rb
@@ -2,6 +2,6 @@
 
 module Net
   module Hippie
-    VERSION = '0.2.0'
+    VERSION = '0.2.1'
   end
 end
test/net/client_test.rb
@@ -21,6 +21,7 @@ class ClientTest < Minitest::Test
   def test_get_with_retry
     uri = URI.parse('https://www.example.org/api/scim/v2/schemas')
     WebMock.stub_request(:get, uri.to_s)
+      .to_timeout.then
       .to_timeout.then
       .to_timeout.then
       .to_return(status: 200, body: { 'success' => 'true' }.to_json)
@@ -28,8 +29,21 @@ class ClientTest < Minitest::Test
       client.get(uri)
     end
     refute_nil response
-    assert_equal response.class, Net::HTTPOK
-    assert_equal('true', JSON.parse(response.body)['success'])
+    assert_equal Net::HTTPOK, response.class
+    assert_equal JSON.parse(response.body)['success'], 'true'
+  end
+
+  def test_exceeds_retries
+    uri = URI.parse('https://www.example.org/api/scim/v2/schemas')
+    WebMock.stub_request(:get, uri.to_s)
+      .to_timeout.then
+      .to_return(status: 200, body: { 'success' => 'true' }.to_json)
+
+    assert_raises Net::OpenTimeout do
+      subject.with_retry(retries: 0) do |client|
+        client.get(uri)
+      end
+    end
   end
 
   def test_get_with_string_uri
.gitlab-ci.yml
@@ -9,4 +9,3 @@ before_script:
 ci:
   script:
     - bin/cibuild
-
.travis.yml
@@ -2,6 +2,8 @@ sudo: false
 language: ruby
 cache: bundler
 rvm:
-  - 2.5.1
+  - 2.3.8
+  - 2.4.5
+  - 2.5.3
 script:
   - bin/cibuild