From 3797bfabcf038024e445e02f1217fcf0d71a1e9e Mon Sep 17 00:00:00 2001 From: Eugene Tokarev Date: Tue, 9 Jan 2024 18:32:25 -0600 Subject: [PATCH] Fix retry logic (#67) * fix retry logic and add a test for it * update changelog and version * kill extra line * ensure the configuration always gets reset back --- CHANGELOG.md | 6 ++++++ lib/procore/requestable.rb | 2 +- lib/procore/version.rb | 2 +- test/procore/requestable_test.rb | 20 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36a9da6..90bfc77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 1.3.1 (Jan 9, 2024) + +* Fix a bug with the retry logic in `with_response_handling` resulting in performing one more retry than expected . + + *Eugene Tokarev* + ## 1.3 (Jan 8, 2024) * Introduces :login_host configuration attribute used to set up OAuth2::Client. Please note, this will result Procore::Client use `https://login.procore.com/oauth/token` instead of `https://api.procore.com/oauth/token` for auth token generation. diff --git a/lib/procore/requestable.rb b/lib/procore/requestable.rb index bbcd586..fe0e025 100644 --- a/lib/procore/requestable.rb +++ b/lib/procore/requestable.rb @@ -253,7 +253,7 @@ def with_response_handling(request_body: nil) begin result = yield rescue *HTTP_EXCEPTIONS => e - if retries <= Procore.configuration.max_retries + if retries < Procore.configuration.max_retries retries += 1 sleep 1.5**retries retry diff --git a/lib/procore/version.rb b/lib/procore/version.rb index a2e2258..1538020 100644 --- a/lib/procore/version.rb +++ b/lib/procore/version.rb @@ -1,3 +1,3 @@ module Procore - VERSION = "1.3".freeze + VERSION = "1.3.1".freeze end diff --git a/test/procore/requestable_test.rb b/test/procore/requestable_test.rb index ea2457d..a40ffb5 100644 --- a/test/procore/requestable_test.rb +++ b/test/procore/requestable_test.rb @@ -332,4 +332,24 @@ def test_error_body assert_equal({ "errors" => "Unauthorized" }, e.response.body) end end + + def test_no_retry_logic + # Disable retries + max_retries_old = Procore.configuration.max_retries + + begin + Procore.configuration.max_retries = 0 + stub_request(:get, "http://test.com/rest/v1.0/retry_test") + .to_raise(RestClient::Exceptions::Timeout) + + # Attempt to perform the request and assert the correct exception is raised + assert_raises(Procore::APIConnectionError) do + Request.new(token: "token").get("retry_test") + end + + assert_requested :get, "http://test.com/rest/v1.0/retry_test", times: 1 + ensure + Procore.configuration.max_retries = max_retries_old + end + end end