Skip to content

Commit ac9a7b3

Browse files
committed
Added IVar#set? to unify Channel::Probe.
1 parent 5581667 commit ac9a7b3

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

lib/concurrent/ivar.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ def fail(reason = StandardError.new)
127127
set { raise reason }
128128
end
129129

130+
def set?(value = NO_VALUE, &block)
131+
set(value, &block)
132+
true
133+
rescue MultipleAssignmentError
134+
false
135+
end
136+
130137
protected
131138

132139
# @!visibility private

spec/concurrent/ivar_shared.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,66 @@
9393
expect(subject.fail).to eq subject
9494
end
9595
end
96+
97+
describe '#set?' do
98+
99+
context 'when unset' do
100+
101+
it 'assigns the value' do
102+
subject.set?(32)
103+
expect(subject.value).to eq 32
104+
end
105+
106+
it 'assigns the block result' do
107+
subject.set?{ 32 }
108+
expect(subject.value).to eq 32
109+
end
110+
111+
it 'returns true' do
112+
expect(subject.set?('hi')).to eq true
113+
end
114+
end
115+
116+
context 'when fulfilled' do
117+
118+
before(:each) { subject.set(27) }
119+
120+
it 'does not assign the value' do
121+
subject.set?(88)
122+
expect(subject.value).to eq 27
123+
end
124+
125+
it 'does not assign the block result' do
126+
subject.set?{ 88 }
127+
expect(subject.value).to eq 27
128+
end
129+
130+
it 'returns false' do
131+
expect(subject.set?('hello')).to eq false
132+
end
133+
end
134+
135+
context 'when rejected' do
136+
137+
before(:each) { subject.fail }
138+
139+
it 'does not assign the value' do
140+
subject.set?(88)
141+
expect(subject).to be_rejected
142+
end
143+
144+
it 'does not assign the block result' do
145+
subject.set?{ 88 }
146+
expect(subject).to be_rejected
147+
end
148+
149+
it 'has a nil value' do
150+
expect(subject.value).to be_nil
151+
end
152+
153+
it 'returns false' do
154+
expect(subject.set?('hello')).to eq false
155+
end
156+
end
157+
end
96158
end

0 commit comments

Comments
 (0)