internetee-registry/test/lib/retryable_test.rb
oleghasjanov a11c0fca2d fix: handle HTTPClient::KeepAliveDisconnected in OrgRegistrantPhoneCheckerJob
This commit implements a reliable connection error handling solution for the
Company Register API integration. The job previously failed when connection
errors occurred without proper recovery mechanisms.
The implementation:
Adds a lightweight Retryable module with configurable retry logic
Implements smart caching of API responses (1 day expiration)
Handles common network errors like KeepAliveDisconnected and timeouts
Provides a fallback mechanism when all retry attempts fail
Ensures test reliability with cache-skipping in test environment
Testing:
Added specific tests for both recovery and fallback scenarios
Verified cache behavior in production and test environments
Resolves connection errors observed in production logs without adding
unnecessary complexity to the codebase.
2025-03-06 11:38:09 +02:00

68 lines
No EOL
1.7 KiB
Ruby

# frozen_string_literal: true
require 'test_helper'
class RetryableTest < ActiveSupport::TestCase
class TestClass
include Retryable
attr_reader :call_count
def initialize
@call_count = 0
end
def test_retriable_method(should_fail: false, fail_count: 2)
with_retry(
max_attempts: 3,
retry_delay: 0.1,
logger: Rails.logger
) do
@call_count += 1
if should_fail && @call_count <= fail_count
raise StandardError, "Тестовая ошибка #{@call_count}"
end
'success'
end
end
def test_retriable_with_fallback(should_fail: true)
with_retry(
max_attempts: 2,
retry_delay: 0.1,
logger: Rails.logger,
fallback: -> { 'fallback' }
) do
@call_count += 1
raise StandardError, 'Постоянная ошибка' if should_fail
'success'
end
end
end
test 'should retry specified number of times and succeed' do
test_object = TestClass.new
result = test_object.test_retriable_method(should_fail: true, fail_count: 2)
assert_equal 3, test_object.call_count
assert_equal 'success', result
end
test 'should succeed on first try if no errors' do
test_object = TestClass.new
result = test_object.test_retriable_method(should_fail: false)
assert_equal 1, test_object.call_count
assert_equal 'success', result
end
test 'should use fallback when all retries fail' do
test_object = TestClass.new
result = test_object.test_retriable_with_fallback(should_fail: true)
assert_equal 2, test_object.call_count
assert_equal 'fallback', result
end
end