Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle expected boolean values for waiter error matcher #3073

Merged
merged 15 commits into from
Aug 8, 2024
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Update waiters to handle expected boolean values when matching errors.

3.201.3 (2024-07-23)
------------------

Expand Down
11 changes: 8 additions & 3 deletions gems/aws-sdk-core/lib/aws-sdk-core/waiters/poller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,13 @@ def matches_status?(acceptor, response)
end

def matches_error?(acceptor, response)
Aws::Errors::ServiceError === response.error &&
response.error.code == acceptor['expected'].delete('.')
case acceptor['expected']
when 'false' then response.error.nil?
when 'true' then !!response.error
jterapin marked this conversation as resolved.
Show resolved Hide resolved
else
response.error.is_a?(Aws::Errors::ServiceError) &&
response.error.code == acceptor['expected'].delete('.')
end
end

def path(acceptor)
Expand All @@ -107,7 +112,7 @@ def path(acceptor)
def non_empty_array(acceptor, response, &block)
if response.data
values = JMESPath.search(path(acceptor), response.data)
Array === values && values.count > 0 ? yield(values) : false
values.is_a?(Array) && values.count > 0 ? yield(values) : false
else
false
end
Expand Down
53 changes: 49 additions & 4 deletions gems/aws-sdk-core/spec/aws/waiters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ module Waiters
end
end

describe 'matching on expectd errors' do

it 'succeedes when an expected error is encountered' do
describe 'status matcher' do
it 'succeeds when an expected status is encountered' do
attempts = 0
client.handle do |context|
attempts += 1
Expand All @@ -190,14 +189,60 @@ module Waiters
expect(attempts).to be(1)
end

it 'fails when an expected error is not encountered' do
it 'fails when an expected status is not encountered' do
expect {
client.wait_until(:table_not_exists, table_name:'table') do |w|
w.delay = 0
end
}.to raise_error(Errors::TooManyAttemptsError)
end
end

describe 'error matcher' do
jterapin marked this conversation as resolved.
Show resolved Hide resolved
context 'expected is an error code' do
it 'succeeds when matched' do
client.stub_responses(:describe_table, 'ResourceNotFoundException')
expect do
client.wait_until(:error_waiter_1, table_name: 'table') do |w|
w.max_attempts = 1
end
end.not_to raise_error
end

it 'fails when there are no matches encountered' do
expect do
client.wait_until(:error_waiter_1, table_name: 'table') do |w|
w.delay = 0
end
end.to raise_error(Errors::TooManyAttemptsError)
end
end

context 'expected is true' do
it 'succeeds when matched with any error' do
client.stub_responses(:describe_table, RuntimeError.new)
expect do
client.wait_until(:error_waiter_1, table_name: 'table') do |w|
w.max_attempts = 1
end
end.not_to raise_error
end
end

context 'expected is false' do
it 'succeeds when a response with no error is received' do
client.stub_responses(
:describe_table,
'ResourceNotFoundException',
table: { table_status: 'ACTIVE' }
)
expect do
client.wait_until(:error_waiter_2, table_name: 'table') do |w|
w.delay = 0
end
end.not_to raise_error
end
end
end

describe 'errors' do
Expand Down
34 changes: 34 additions & 0 deletions gems/aws-sdk-core/spec/fixtures/waiters/dynamodb.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@
"state": "success"
}
]
},
"ErrorWaiter1": {
"delay": 20,
"operation": "DescribeTable",
"maxAttempts": 25,
"acceptors": [
{
"expected": "ResourceNotFoundException",
"matcher": "error",
"state": "success"
},
{
"expected": "true",
"matcher": "error",
"state": "success"
}
]
},
"ErrorWaiter2": {
"delay": 20,
"operation": "DescribeTable",
"maxAttempts": 25,
"acceptors": [
{
"expected": "ResourceNotFoundException",
"matcher": "error",
"state": "retry"
},
{
"expected": "false",
"matcher": "error",
"state": "success"
}
]
}
}
}
Loading