Class: RSpec::OpenHAB::Core::Mocks::EventAdmin

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/openhab/core/mocks/event_admin.rb

Instance Method Summary collapse

Constructor Details

#initialize(bundle_context) ⇒ EventAdmin

Returns a new instance of EventAdmin.



91
92
93
94
95
96
97
# File 'lib/rspec/openhab/core/mocks/event_admin.rb', line 91

def initialize(bundle_context)
  super(bundle_context, "org.osgi.service.event.EventHandler", nil)

  @handlers_matching_all_events = []
  @handlers_matching_topics = Hash.new { |h, k| h[k] = [] }
  open
end

Instance Method Details

#addingService(reference) ⇒ Object

rubocop:disable Naming/MethodName



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rspec/openhab/core/mocks/event_admin.rb', line 99

def addingService(reference) # rubocop:disable Naming/MethodName
  topics = Array(reference.get_property(org.osgi.service.event.EventConstants::EVENT_TOPIC))
  topics = nil if topics.empty? || topics.include?("*")

  service = ::OpenHAB::Core::OSGI.send(:bundle_context).get_service(reference)

  if reference.get_property("component.name") == "org.openhab.core.internal.events.OSGiEventManager"
    # OSGiEventManager will create a ThreadedEventHandler on OSGi activation;
    # we're skipping that, and directly sending to a non-threaded event handler.
    service.class.field_reader :typedEventFactories, :typedEventSubscribers
    service = OSGiEventManager.new(service.typedEventFactories, service.typedEventSubscribers)
  end
  if topics.nil?
    @handlers_matching_all_events << service
  else
    topics.each do |topic|
      @handlers_matching_topics[topic] << service
    end
  end
  service
end

#postEvent(event) ⇒ Object

rubocop:disable Naming/MethodName



121
122
123
# File 'lib/rspec/openhab/core/mocks/event_admin.rb', line 121

def postEvent(event) # rubocop:disable Naming/MethodName
  sendEvent(event)
end

#sendEvent(event) ⇒ Object

rubocop:disable Naming/MethodName



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rspec/openhab/core/mocks/event_admin.rb', line 125

def sendEvent(event) # rubocop:disable Naming/MethodName
  # prevent re-entrancy
  if (pending_events = Thread.current[:event_admin_pending_events])
    pending_events << event
    return
  end

  pending_events = Thread.current[:event_admin_pending_events] = []
  handle_event(event)
  handle_event(pending_events.shift) until pending_events.empty?
  Thread.current[:event_admin_pending_events] = nil
end