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

test: reproduce possible Hash inconsistency when [] / []= concurrently #384

Merged
merged 1 commit into from Oct 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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