Class: OpenHAB::RSpec::Mocks::EventAdmin
- Inherits:
-
Object
- Object
- OpenHAB::RSpec::Mocks::EventAdmin
- Defined in:
- lib/openhab/rspec/mocks/event_admin.rb
Instance Method Summary collapse
- #addingService(reference) ⇒ Object
-
#initialize(bundle_context) ⇒ EventAdmin
constructor
A new instance of EventAdmin.
- #postEvent(event) ⇒ Object
- #sendEvent(event) ⇒ Object
Constructor Details
#initialize(bundle_context) ⇒ EventAdmin
Returns a new instance of EventAdmin.
92 93 94 95 96 97 98 |
# File 'lib/openhab/rspec/mocks/event_admin.rb', line 92 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/openhab/rspec/mocks/event_admin.rb', line 100 def addingService(reference) topics = Array(reference.get_property(org.osgi.service.event.EventConstants::EVENT_TOPIC)) topics = nil if topics.empty? || topics.include?("*") service = 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
122 123 124 |
# File 'lib/openhab/rspec/mocks/event_admin.rb', line 122 def postEvent(event) sendEvent(event) end |
#sendEvent(event) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/openhab/rspec/mocks/event_admin.rb', line 126 def sendEvent(event) # 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 |