Skip to content

Commit 2039754

Browse files
authored
Make testing stubs fully thread-safe
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
1 parent 2497eb0 commit 2039754

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

activesupport/lib/active_support/testing/time_helpers.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class SimpleStubs # :nodoc:
1111
Stub = Struct.new(:object, :method_name, :original_method)
1212

1313
def initialize
14-
@stubs = Concurrent::Map.new { |h, k| h[k] = {} }
14+
@stubs = Concurrent::Map.new do |h, k|
15+
h.compute_if_absent(k) { {} }
16+
end
1517
end
1618

1719
# Stubs object.method_name with the given block

0 commit comments

Comments
 (0)