def test_dispatcher_custom_event(EventType, event_id): dispatcher = EventDispatcher() listener = mock.MagicMock() event = EventType() dispatcher.add_listener(event_id, listener) e = dispatcher.dispatch(event_id, event) assert listener.call_count == 1 assert e == event
def test_no_listener(): dispatcher = EventDispatcher() for event_id in ( STRING_EVENT_ID, OBJECT_EVENT_ID, NUMERIC_EVENT_ID, ): assert not dispatcher.has_listeners(event_id) e = dispatcher.dispatch(event_id) assert not e.propagation_stopped
def test_listen_decorator(): dispatcher = EventDispatcher() for event_id in ( STRING_EVENT_ID, OBJECT_EVENT_ID, NUMERIC_EVENT_ID, ): listener = mock.MagicMock() dispatcher.listen(event_id)(listener) e = dispatcher.dispatch(event_id) assert listener.call_count == 1 assert not e.propagation_stopped
def __init__(self, graph, plugins=None, services=None, dispatcher=None): self.dispatcher = dispatcher or EventDispatcher() self.graph = graph self.nodes = [ self.create_node_execution_context_for(node) for node in self.graph ] self.plugins = [ self.create_plugin_execution_context_for(plugin) for plugin in plugins or () ] self.services = create_container(services) # Probably not a good idea to use it unless you really know what you're doing. But you can access the context. self.services['__graph_context'] = self for i, node_context in enumerate(self): outputs = self.graph.outputs_of(i) if len(outputs): node_context.outputs = [self[j].input for j in outputs] node_context.input.on_begin = partial(node_context._send, BEGIN, _control=True) node_context.input.on_end = partial(node_context._send, END, _control=True) node_context.input.on_finalize = partial(node_context.stop)
def test_propagation(): dispatcher = EventDispatcher() for event_id in ( STRING_EVENT_ID, OBJECT_EVENT_ID, NUMERIC_EVENT_ID, ): listener1 = mock.MagicMock() def listener2(event): event.stop_propagation() listener3 = mock.MagicMock() dispatcher.add_listener(event_id, listener1) dispatcher.add_listener(event_id, listener2) dispatcher.add_listener(event_id, listener3) assert listener1.call_count == 0 assert listener3.call_count == 0 e = dispatcher.dispatch(event_id) assert listener1.call_count == 1 assert listener3.call_count == 0 assert e.propagation_stopped e = dispatcher.dispatch(event_id) assert listener1.call_count == 2 assert listener3.call_count == 0 assert e.propagation_stopped assert dispatcher.get_listeners(event_id) == [ listener1, listener2, listener3 ] listeners = dispatcher.get_listeners() assert len(listeners) == 3 for event_id in ( STRING_EVENT_ID, OBJECT_EVENT_ID, NUMERIC_EVENT_ID, ): assert len(listeners[event_id]) == 3
def test_register_unregister(): plugin = ConsoleOutputPlugin() dispatcher = EventDispatcher() plugin.register(dispatcher) assert plugin.setup in dispatcher.get_listeners(events.START) assert plugin.tick in dispatcher.get_listeners(events.TICK) assert plugin.teardown in dispatcher.get_listeners(events.STOPPED) plugin.unregister(dispatcher) assert plugin.setup not in dispatcher.get_listeners(events.START) assert plugin.tick not in dispatcher.get_listeners(events.TICK) assert plugin.teardown not in dispatcher.get_listeners(events.STOPPED)
def test_one_pass(): plugin = ConsoleOutputPlugin() dispatcher = EventDispatcher() plugin.register(dispatcher) graph = bonobo.Graph() context = MagicMock(spec=GraphExecutionContext(graph)) dispatcher.dispatch(events.START, events.ExecutionEvent(context)) dispatcher.dispatch(events.TICK, events.ExecutionEvent(context)) dispatcher.dispatch(events.STOPPED, events.ExecutionEvent(context)) plugin.unregister(dispatcher)
def __init__(self, dispatcher: EventDispatcher): self._configs = {} self._features = {} self._pipelines = {} self._variables = OrderedDict() self.dispatcher = dispatcher self.resources = OrderedDict() def register_feature(ext): self._features[ext.name] = ext.plugin def on_load_feature_failure(mgr, entrypoint, err): logger.exception( "Exception caught while loading {}.".format(entrypoint), err) mgr = ExtensionManager( namespace="medikit.feature", on_load_failure_callback=on_load_feature_failure) mgr.map(register_feature) dispatcher.add_listener(medikit.on_end, self.write_resources)
def test_dispatcher(event_id): dispatcher = EventDispatcher() listener = mock.MagicMock() assert not dispatcher.has_listeners(event_id) dispatcher.add_listener(event_id, listener) assert dispatcher.has_listeners(event_id) assert listener.call_count == 0 e = dispatcher.dispatch(event_id) assert listener.call_count == 1 assert not e.propagation_stopped e = dispatcher.dispatch(event_id) assert listener.call_count == 2 assert not e.propagation_stopped
def test_dispatcher(self): dispatcher = EventDispatcher() for event_id in ( STRING_EVENT_ID, OBJECT_EVENT_ID, NUMERIC_EVENT_ID, ): listener = mock.MagicMock() assert not dispatcher.has_listeners(event_id) dispatcher.add_listener(event_id, listener) assert dispatcher.has_listeners(event_id) assert listener.call_count == 0 e = dispatcher.dispatch(event_id) assert listener.call_count == 1 assert not e.propagation_stopped e = dispatcher.dispatch(event_id) assert listener.call_count == 2 assert not e.propagation_stopped
def register_services(configuration_file: str) -> None: """Register worker services""" with open(file=configuration_file, encoding="utf-8") as worker_configuration: configuration = safe_load(worker_configuration) # Base logger channel di[logging.Logger] = get_logger(channel="service") di["worker_logger-service"] = di[logging.Logger] # Worker event dispatcher service di[EventDispatcher] = EventDispatcher() # PREPARE CONFIGURATION # Extract configuration for Database services from configuration file database_configuration = configuration.get("database", {}) assert isinstance(database_configuration, dict) di["db_host"] = database_configuration.get("host") di["db_username"] = database_configuration.get("user") di["db_password"] = database_configuration.get("passwd") di["db_database"] = database_configuration.get("db") di["db_engine"] = create_engine( f"mysql+pymysql://{di['db_username']}:{di['db_password']}@{di['db_host']}/{di['db_database']}", echo=False, ) di[Session] = Session(di["db_engine"]) di["db_session"] = di[Session] # Initialize all database models DevicesModuleBase.metadata.create_all(di["db_engine"]) TriggersModuleBase.metadata.create_all(di["db_engine"]) # Extract configuration for Redis services from configuration file redis_configuration = configuration.get("redis", {}) assert isinstance(redis_configuration, dict) di["redis_host"] = str(redis_configuration.get("host", "127.0.0.1")) di["redis_port"] = int(str(redis_configuration.get("port", 6379))) if redis_configuration.get("username", None) is not None: di["redis_username"] = str(redis_configuration.get("username", None)) if redis_configuration.get("password", None) is not None: di["redis_password"] = str(redis_configuration.get("password", None)) # INITIALIZE PLUGINS register_services_exchange() register_services_redisdb_storage_plugin(logger=get_logger( channel="storage")) register_services_redisdb_exchange_plugin( settings={ "host": di["redis_host"], "port": di["redis_port"], "username": di["redis_username"] if "redis_username" in di else None, "password": di["redis_password"] if "redis_password" in di else None, }, logger=get_logger(channel="exchange"), ) # INITIALIZE STATES SERVICES # DEVICES MODULE di[DevicePropertiesStatesManager] = DevicePropertiesStatesManager( # type: ignore[call-arg] host=di["redis_host"], port=di["redis_port"], username=di["redis_username"] if "redis_username" in di else None, password=di["redis_password"] if "redis_password" in di else None, database=int(str(redis_configuration.get("database", 0))), ) di[DevicePropertiesStatesRepository] = DevicePropertiesStatesRepository( # type: ignore[call-arg] host=di["redis_host"], port=di["redis_port"], username=di["redis_username"] if "redis_username" in di else None, password=di["redis_password"] if "redis_password" in di else None, database=int(str(redis_configuration.get("database", 0))), ) di[ChannelPropertiesStatesManager] = ChannelPropertiesStatesManager( # type: ignore[call-arg] host=di["redis_host"], port=di["redis_port"], username=di["redis_username"] if "redis_username" in di else None, password=di["redis_password"] if "redis_password" in di else None, database=int(str(redis_configuration.get("database", 0))), ) di[ChannelPropertiesStatesRepository] = ChannelPropertiesStatesRepository( # type: ignore[call-arg] host=di["redis_host"], port=di["redis_port"], username=di["redis_username"] if "redis_username" in di else None, password=di["redis_password"] if "redis_password" in di else None, database=int(str(redis_configuration.get("database", 0))), ) # TRIGGERS MODULE di[ActionsStatesManager] = ActionsStatesManager( # type: ignore[call-arg] host=di["redis_host"], port=di["redis_port"], username=di["redis_username"] if "redis_username" in di else None, password=di["redis_password"] if "redis_password" in di else None, database=int(str(redis_configuration.get("database", 1))), ) di[ActionStatesRepository] = ActionStatesRepository( # type: ignore[call-arg] host=di["redis_host"], port=di["redis_port"], username=di["redis_username"] if "redis_username" in di else None, password=di["redis_password"] if "redis_password" in di else None, database=int(str(redis_configuration.get("database", 1))), ) di[ConditionsStatesManager] = ConditionsStatesManager( # type: ignore[call-arg] host=di["redis_host"], port=di["redis_port"], username=di["redis_username"] if "redis_username" in di else None, password=di["redis_password"] if "redis_password" in di else None, database=int(str(redis_configuration.get("database", 1))), ) di[ConditionStatesRepository] = ConditionStatesRepository( # type: ignore[call-arg] host=di["redis_host"], port=di["redis_port"], username=di["redis_username"] if "redis_username" in di else None, password=di["redis_password"] if "redis_password" in di else None, database=int(str(redis_configuration.get("database", 1))), ) # INITIALIZE MODULES modules_logger = get_logger(channel="module") register_services_devices_module(logger=modules_logger) register_services_triggers_module(logger=modules_logger)