Skip to content

Commit

Permalink
Add extra tests for retry behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp committed Oct 28, 2024
1 parent 86cccb9 commit 565fff7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
47 changes: 47 additions & 0 deletions gems/aws-sdk-core/spec/aws/plugins/retry_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,53 @@ module Plugins
handle_with_retry(test_case_def)
end

it 'fails due to retry quota bucket exhaustion' do
config.max_attempts = 5
config.retry_quota.instance_variable_set(:@available_capacity, 10)

test_case_def = [
{
response: { status_code: 500, error: service_error },
expect: { available_capacity: 5, retries: 1, delay: 1 }
},
{
response: { status_code: 502, error: service_error },
expect: { available_capacity: 0, retries: 2, delay: 2 }
},
{
response: { status_code: 503, error: service_error },
expect: { available_capacity: 0, retries: 2 }
}
]

handle_with_retry(test_case_def)
end

it 'recovers after successful responses' do
config.max_attempts = 5
config.retry_quota.instance_variable_set(:@available_capacity, 15)

test_case_def = [
{
response: { status_code: 500, error: service_error },
expect: { available_capacity: 10, retries: 1, delay: 1 }
},
{
response: { status_code: 502, error: service_error },
expect: { available_capacity: 5, retries: 2, delay: 2 }
},
{
response: { status_code: 200, error: nil },
expect: { available_capacity: 10, retries: 2 }
},
{
response: { status_code: 500, error: service_error },
expect: { available_capacity: 5, retries: 1, delay: 1 }
}
]
handle_with_retry(test_case_def)
end

it 'corrects and retries clock skew errors' do
clock_skew_error = RetryErrorsSvc::Errors::RequestTimeTooSkewed
.new(nil, nil)
Expand Down
29 changes: 14 additions & 15 deletions gems/aws-sdk-core/spec/retry_errors_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@ def handle(send_handler = nil, &block)
# expect: delay, available_capacity, retries, calculated_rate,
# measured_tx_rate, fill_rate
def handle_with_retry(test_cases)
# Apply delay expectations first
test_cases.each do |test_case|
if test_case[:expect][:delay]
expect(Kernel).to receive(:sleep).with(test_case[:expect][:delay])
end
end

i = 0
handle do |_context|
apply_expectations(test_cases[i - 1]) if i > 0
apply_delay(test_cases[i])
apply_expectations(test_cases[i - 1]) if i.positive?

# Setup the next response
setup_next_response(test_cases[i])
Expand All @@ -36,17 +30,20 @@ def handle_with_retry(test_cases)
resp
end

expect(i).to(
eq(test_cases.size),
"Wrong number of retries. Handler was called #{i} times but "\
"#{test_cases.size} test cases were defined."
)

# Handle has finished called. Apply final expectations.
apply_expectations(test_cases[i - 1])
end

# apply the expectations from a test case
# apply a delay to the current test case
# See handle_with_retry for test case definition
def apply_delay(test_case)
expected = test_case[:expect]
return unless expected[:delay]

expect(Kernel).to receive(:sleep).with(expected[:delay])
end

# apply the expectations from a previous test case
# See handle_with_retry for test case definition
def apply_expectations(test_case)
expected = test_case[:expect]
Expand Down Expand Up @@ -95,6 +92,8 @@ def apply_expectations(test_case)
end
end

# setup the next response for the handler
# See handle_with_retry for test case definition
def setup_next_response(test_case)
response = test_case[:response]
resp.context.http_response.status_code = response[:status_code]
Expand Down

0 comments on commit 565fff7

Please sign in to comment.