Skip to content

Commit

Permalink
Make stubs thread safe by adding a mutex per stubbed request (#2679)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods authored Mar 10, 2022
1 parent f072efb commit 881740c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
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 - Make stubs thread safe by creating new responses for each operation call (#2675).

3.129.0 (2022-03-08)
------------------

Expand Down
6 changes: 5 additions & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/client_stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,17 @@ def apply_stubs(operation_name, stubs)
end

def convert_stub(operation_name, stub)
case stub
stub = case stub
when Proc then stub
when Exception, Class then { error: stub }
when String then service_error_stub(stub)
when Hash then http_response_stub(operation_name, stub)
else { data: stub }
end
if Hash === stub
stub[:mutex] = Mutex.new
end
stub
end

def service_error_stub(error_code)
Expand Down
6 changes: 5 additions & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/plugins/stub_responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def call(context)
stub = context.client.next_stub(context)
resp = Seahorse::Client::Response.new(context: context)
async_mode = context.client.is_a? Seahorse::Client::AsyncBase
apply_stub(stub, resp, async_mode)
if Hash === stub && stub[:mutex]
stub[:mutex].synchronize { apply_stub(stub, resp, async_mode) }
else
apply_stub(stub, resp, async_mode)
end

async_mode ? Seahorse::Client::AsyncResponse.new(
context: context, stream: context[:input_event_stream_handler].event_emitter.stream, sync_queue: Queue.new) : resp
Expand Down

0 comments on commit 881740c

Please sign in to comment.