Skip to content

Commit

Permalink
Make testing stubs fully thread-safe
Browse files Browse the repository at this point in the history
This change makes the initialization of the hash upon missing key fully thread-safe. Before this change, initialization that would occur in two threads could overwrite each other, as illustrated here:

ruby-concurrency/concurrent-ruby#970
  • Loading branch information
mensfeld authored Nov 20, 2022
1 parent 2497eb0 commit 2039754
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion activesupport/lib/active_support/testing/time_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class SimpleStubs # :nodoc:
Stub = Struct.new(:object, :method_name, :original_method)

def initialize
@stubs = Concurrent::Map.new { |h, k| h[k] = {} }
@stubs = Concurrent::Map.new do |h, k|
h.compute_if_absent(k) { {} }
end
end

# Stubs object.method_name with the given block
Expand Down

0 comments on commit 2039754

Please sign in to comment.