Skip to content

Commit

Permalink
test: reproduce possible Hash inconsistency when [] / []= concurrently
Browse files Browse the repository at this point in the history
... a sometimes reproducer  for ruby-i18n#51 (needs a GIL free Ruby such as JRuby)
  • Loading branch information
kares committed Oct 1, 2017
1 parent 6ab51c7 commit 7e7a399
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/backend/memoize_test.rb
Expand Up @@ -44,4 +44,39 @@ def test_resets_available_locales_on_store_translations
assert I18n.available_locales.include?(:copa)
assert_equal 1, I18n.backend.spy_calls
end

module TestLookup
def lookup(locale, key, scope = [], options = {})
keys = I18n.normalize_keys(locale, key, scope, options[:separator])
keys.inspect
end
end

def test_lookup_concurrent_consistency
backend_impl = Class.new(I18n::Backend::Simple) do
include TestLookup
include I18n::Backend::Memoize
end
backend = backend_impl.new

memoized_lookup = backend.send(:memoized_lookup)
# make the 'default_proc' execution artificially slower to help reproduce :
default_proc = memoized_lookup.default_proc
memoized_lookup.default_proc = Proc.new { |h, k| sleep 0.1; default_proc.call(h, k) }

assert_equal "[:foo, :scoped, :sample]", backend.translate('foo', scope = [:scoped, :sample])

results = []
30.times.inject([]) do |memo, i|
memo << Thread.new do
backend.translate('bar', scope); backend.translate(:baz, scope)
end
end.each(&:join)

memoized_lookup = backend.send(:memoized_lookup)
puts memoized_lookup.inspect if $VERBOSE
assert_equal 3, memoized_lookup.size, "NON-THREAD-SAFE lookup memoization backend: #{memoized_lookup.class}"
# if a plain Hash is used might eventually end up in a weird (inconsistent) state
end

end

0 comments on commit 7e7a399

Please sign in to comment.