Skip to content

Commit f2985bd

Browse files
committed
Fix Concurrent::Map default_proc arguments
* Fixes #993
1 parent 8cccba9 commit f2985bd

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Current
22

3+
## Release v1.2.2 (24 Feb 2023)
4+
5+
* (#993) Fix arguments passed to `Concurrent::Map`'s `default_proc`.
36

47
## Release v1.2.1 (24 Feb 2023)
58

lib/concurrent-ruby/concurrent/collection/map/non_concurrent_map_backend.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class NonConcurrentMapBackend
1414
# reasons.
1515
def initialize(options = nil, &default_proc)
1616
validate_options_hash!(options) if options.kind_of?(::Hash)
17-
@backend = Hash.new(&default_proc)
17+
set_backend(default_proc)
1818
@default_proc = default_proc
1919
end
2020

@@ -113,9 +113,17 @@ def get_or_default(key, default_value)
113113

114114
private
115115

116+
def set_backend(default_proc)
117+
if default_proc
118+
@backend = ::Hash.new { |_h, key| default_proc.call(self, key) }
119+
else
120+
@backend = {}
121+
end
122+
end
123+
116124
def initialize_copy(other)
117125
super
118-
@backend = Hash.new(&@default_proc)
126+
set_backend(@default_proc)
119127
self
120128
end
121129

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Concurrent
2-
VERSION = '1.2.1'
2+
VERSION = '1.2.2'
33
end

spec/concurrent/map_spec.rb

+25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ module Concurrent
1010
@cache = described_class.new
1111
end
1212

13+
it 'default_proc is called with the Concurrent::Map and the key' do
14+
map = Concurrent::Map.new do |h, k|
15+
[h, k]
16+
end
17+
expect(map[:foo][0]).to be(map)
18+
expect(map[:foo][1]).to eq(:foo)
19+
end
20+
21+
it 'default_proc is called with the Concurrent::Map and the key after #dup' do
22+
map = Concurrent::Map.new do |h, k|
23+
[h, k]
24+
end
25+
copy = map.dup
26+
expect(copy[:foo][0]).to be(copy)
27+
expect(copy[:foo][1]).to eq(:foo)
28+
end
29+
1330
it 'concurrency' do
1431
(1..Concurrent::ThreadSafe::Test::THREADS).map do |i|
1532
in_thread do
@@ -45,6 +62,14 @@ module Concurrent
4562
end
4663

4764
describe '#compute_if_absent' do
65+
it 'works in default_proc' do
66+
map = Concurrent::Map.new do |map, key|
67+
map.compute_if_absent(key) { key * 2 }
68+
end
69+
expect(map[3]).to eq 6
70+
expect(map.keys).to eq [3]
71+
end
72+
4873
it 'common' do
4974
with_or_without_default_proc do
5075
expect_size_change(3) do

0 commit comments

Comments
 (0)