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

Fix Map#each and #each_pair not returning enumerator outside of MRI #644

Merged
merged 2 commits into from Apr 2, 2017
Merged
Show file tree
Hide file tree
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
Expand Up @@ -95,7 +95,6 @@ def clear
end

def each_pair
return enum_for :each_pair unless block_given?
dupped_backend.each_pair do |k, v|
yield k, v
end
Expand Down
5 changes: 5 additions & 0 deletions lib/concurrent/map.rb
Expand Up @@ -171,6 +171,11 @@ def each_value
each_pair {|k, v| yield v}
end unless method_defined?(:each_value)

def each_pair
return enum_for :each_pair unless block_given?
super
end

alias_method :each, :each_pair unless method_defined?(:each)

def key(value)
Expand Down
16 changes: 16 additions & 0 deletions spec/concurrent/collection_each_shared.rb
Expand Up @@ -42,4 +42,20 @@
end
end
end

context 'when no block is given' do
it 'returns an enumerator' do
@cache[:a] = 1
@cache[:b] = 2

expect(@cache.send(method)).to be_a Enumerator
end

it 'returns an object which is enumerable' do
@cache[:a] = 1
@cache[:b] = 2

expect(@cache.send(method).to_a).to contain_exactly([:a, 1], [:b, 2])
end
end
end
4 changes: 4 additions & 0 deletions spec/concurrent/map_spec.rb
Expand Up @@ -255,6 +255,10 @@ module Concurrent
end

it 'updates dont block reads' do
if defined?(Concurrent::Collection::SynchronizedMapBackend) && described_class <= Concurrent::Collection::SynchronizedMapBackend
skip("Test does not apply to #{Concurrent::Collection::SynchronizedMapBackend}")
end

getters_count = 20
key_klass = Concurrent::ThreadSafe::Test::HashCollisionKey
keys = [key_klass.new(1, 100),
Expand Down