Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/concurrent-ruby-ext/atomic_fixnum.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ VALUE method_atomic_fixnum_update(VALUE self) {
for (;;) {
old_value = method_atomic_fixnum_value(self);
new_value = rb_yield(old_value);
Check_Type(new_value, T_FIXNUM);
if (ir_compare_and_set(self, old_value, new_value) == Qtrue) {
return new_value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def compare_and_set(expect, update)
# @!macro atomic_fixnum_method_update
def update
synchronize do
@value = yield @value
ns_set(yield @value)
end
end

Expand Down
11 changes: 11 additions & 0 deletions spec/concurrent/atomic/atomic_fixnum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@
atomic = described_class.new(1000)
expect(atomic.update { |v| v + 1 }).to eq 1001
end

it 'rejects a non-integer result without changing the value' do
atomic = described_class.new(1000)

expect {
atomic.update { 'not an integer' }
}.to(raise_error { |error|
expect(error.class).to be(ArgumentError).or(be(TypeError))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you be specific here? Why would it be 2 different kinds of error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two implementations already expose different validation errors: the C extension uses Ruby’s Check_Type(...), which raises TypeError, while MutexAtomicFixnum#ns_set delegates to NativeInteger.ensure_integer, which raises ArgumentError. This shared spec runs against both implementations, so it accepts the existing implementation-specific class while asserting the important invariant: the failed update does not mutate the value.

})
expect(atomic.value).to eq 1000
end
end
end

Expand Down