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 race condition when using semantic methods #356

Merged
merged 1 commit into from
Nov 25, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/openhab/core/items/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ def members
__getobj__.members
end

# Several methods can just return nil when it's a dummy item
# This helps when you're doing something like `items.locations.select {}`
# when items are getting created and removed in a concurrent thread to
# not have errors because an item disappeared
%i[
equipment
equipment?
equipment_type
location
location?
location_type
member_of?
point?
point_type
property_type
semantic?
semantic_type
tagged?
].each do |m|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{m}(*args) # def equipment(*args)
target = __getobj__ # target = __getobj__
return nil if target.nil? # return nil if target.nil?
#
target.#{m}(*args) # target.equipment(*args)
end # end
RUBY
end

# @return [String]
def to_s
return name if __getobj__.nil?
Expand Down
4 changes: 4 additions & 0 deletions spec/openhab/core/items/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
expect(item).not_to respond_to(:command)
expect { item.command }.to raise_error(NoMethodError)
end

it "disappears when calling semantic predicates on an array" do
expect([item].locations).to eql []
end
end

it "does not respond to GroupItem#members if it's backed by a non-GroupItem" do
Expand Down