def init_component_handling(self, components): self.component_registry = create_registry() self.component_manager = ComponentManager(self.component_registry) self.component_factory = Factory(self.component_manager) self.messagebus = MessageBus(self.component_factory, Scope('session')) components.append( ComponentMock(name='ComponentFactory', mock=self.component_factory)) components.append( ComponentMock(name='MessageBus', mock=self.messagebus)) components.append(ComponentMock(name='Config', mock=self.config)) components.append( ComponentMock(name='ComponentManager', mock=self.component_manager)) components.append( ComponentMock(name='ExtensionManager', mock=MagicMock())) for comp in components: if type(comp) == ComponentMock: component(name=comp.name, scope=comp.scope, can=comp.can, provided_by_extension=comp.provided_by_extension)( comp, self.component_manager) else: self.component_manager.register_component(comp)
def setUp(self): self.component_manager = ComponentManager() self.component_manager.clear_component_registry() component(scope='session')(TestTestRunnerComponents.Verifier) self.load_components() self.verifier = TestTestRunnerComponents.Verifier() self.parent_scope = Scope('session', None) self.parent_scope.register_instance( _InstanceId(TestTestRunnerComponents.Verifier, ()), self.verifier) self.run_queue = [] self.messagebus = Mock() def send_request(message_id, endpoint_id=None, entities=None, data=None): futures = Mock() if message_id == SCHEDULE_NEXT_TEST: future = Mock() if self.run_queue: future.result.return_value = self.run_queue.pop(0) else: future.result.return_value = None futures.wait.return_value = [future] return futures self.messagebus.send_request.side_effect = send_request self.tr = TestRunner(self.messagebus, Factory(self.component_manager), 'suite-name', parent_scope=self.parent_scope)
def setUp(self): self.run_queue = [] messagebus = Mock() def send_request(message_id, endpoint_id=None, entities=None, data=None): futures = Mock() if message_id == SCHEDULE_NEXT_TEST: future = Mock() if self.run_queue: future.result.return_value = self.run_queue.pop(0) else: future.result.return_value = None futures.wait.return_value = [future] return futures messagebus.send_request.side_effect = send_request self.tr = TestRunner(messagebus=messagebus, component_factory=Factory(ComponentManager()), suite_name='suite-name') self.tr.QUEUE_TIMEOUT_SECONDS = 1 self.tr.EXECUTION_PAUSED_TIMEOUT_SECONDS = 1
def test_fail_when_no_serial_connection_is_registered_to_send_command(self): messagebus = MessageBus(Factory(ComponentManager(COMPONENT_REGISTRY))) messagebus.define_endpoints_and_messages({SERIAL_ENDPOINT: [LOG_LINE_RECEIVED]}) client = SerialClient(messagebus, 'entity') with self.assertRaises(SerialError) as error: client.send_line('line', timeout=1) assert 'No serial connection' in error.msg
def setUp(self): self.messagebus = MessageBus(Factory(ComponentManager({}))) self.endpoint = MOCK_ENDPOINT self.sut = Mock() self.sut.entity = 'entity' self.config = Mock() self.config.get = MagicMock(side_effect=config_get_log_sources) self.messagebus.define_endpoints_and_messages( {self.endpoint: [LOG_LINE_RECEIVED]})
def setUp(self): self.messagebus = MessageBus(Factory(ComponentManager({}))) self.sut = MagicMock() self.sut.entity = 'entity1' config = Mock() self.messagebus.define_endpoints_and_messages({ MOCK_ENDPOINT: [SUT_RESET_EXPECTED], SUTEVENTSCOMPONENT_ENDPOINT: [SUT_RESET_EXPECTED, SUT_RESET_DONE] }) self.sut_events = SutEvents(self.messagebus, config, self.sut)
def __init__(self, application_config, entry_points=['zaf.addons', 'zaf.local_addons'], signalhandler=None): root_logger = logging.getLogger() # Default config for rootlogger to not spam until logger is correctly configured root_logger.setLevel(logging.INFO) self.app_config = application_config self.signalhandler = signalhandler self.entry_points = entry_points self.extension_manager = ExtensionManager() self.component_manager = ComponentManager() self.component_factory = Factory(self.component_manager) self.session_scope = self.component_factory.enter_scope('session') self.messagebus = MessageBus(self.component_factory, self.session_scope) self.messagebus.define_endpoints_and_messages( {APPLICATION_ENDPOINT: [BEFORE_COMMAND, AFTER_COMMAND]}) self.config = ConfigManager() self.command = None self._exit_code = 1 self.app_config.apply_configuration(self.config) @component(name='MessageBus', scope='session') def messagebus(): """ Access the message bus. The message bus can be used to register dispatchers to specific endpoints, message_ids and entities and to send requests and events to the registered dispatchers. """ return self.messagebus @component(name='ComponentFactory', scope='session') def component_factory(): """ Access the component factory. The component factory can be used to call callables inside a scope and have the correct components instantiated in the scope. """ return self.component_factory @component(name='ComponentManager', scope='session') def component_manager(): """ Access the component manager. The component manager can be used to find out what components are available. """ return self.component_manager @component(name='Config', scope='session') def config(): """ Access the Config Manager. The Config components gives full access to all the config. """ return self.config @component(name='ExtensionManager', scope='session') def extension_manager(): """ Access the extension manager. The extension manager can be used to find out what extensions are loaded. """ return self.extension_manager
class Application(object): def __init__(self, application_config, entry_points=['zaf.addons', 'zaf.local_addons'], signalhandler=None): root_logger = logging.getLogger() # Default config for rootlogger to not spam until logger is correctly configured root_logger.setLevel(logging.INFO) self.app_config = application_config self.signalhandler = signalhandler self.entry_points = entry_points self.extension_manager = ExtensionManager() self.component_manager = ComponentManager() self.component_factory = Factory(self.component_manager) self.session_scope = self.component_factory.enter_scope('session') self.messagebus = MessageBus(self.component_factory, self.session_scope) self.messagebus.define_endpoints_and_messages( {APPLICATION_ENDPOINT: [BEFORE_COMMAND, AFTER_COMMAND]}) self.config = ConfigManager() self.command = None self._exit_code = 1 self.app_config.apply_configuration(self.config) @component(name='MessageBus', scope='session') def messagebus(): """ Access the message bus. The message bus can be used to register dispatchers to specific endpoints, message_ids and entities and to send requests and events to the registered dispatchers. """ return self.messagebus @component(name='ComponentFactory', scope='session') def component_factory(): """ Access the component factory. The component factory can be used to call callables inside a scope and have the correct components instantiated in the scope. """ return self.component_factory @component(name='ComponentManager', scope='session') def component_manager(): """ Access the component manager. The component manager can be used to find out what components are available. """ return self.component_manager @component(name='Config', scope='session') def config(): """ Access the Config Manager. The Config components gives full access to all the config. """ return self.config @component(name='ExtensionManager', scope='session') def extension_manager(): """ Access the extension manager. The extension manager can be used to find out what extensions are loaded. """ return self.extension_manager def run(self): with self as instance: instance._exit_code = instance.execute_command() return instance._exit_code def setup(self): application_config_options = [ ConfigOption(MESSAGEBUS_TIMEOUT, required=False), ConfigOption(CWD, required=True), ] loader = ExtensionLoader(self.extension_manager, self.config, self.messagebus, application_config_options, self.component_manager) self.command = loader.load_extensions(self.entry_points) def teardown(self): try: self.messagebus.wait_for_not_active( timeout=self.config.get(MESSAGEBUS_TIMEOUT)) except MessageBusTimeout as e: logger.critical( 'Waiting for messagebus to be inactive timed out,' ' shutting down anyway. State: {state}'.format(state=e)) finally: logger.debug( 'Dispatchers still registered to the messagebus: {dispatchers}' .format(dispatchers=self.messagebus.get_dispatchers())) self.extension_manager.destroy() for th in threading.enumerate(): try: if th.name != 'MainThread': logger.debug(th) logger.debug('\n'.join( traceback.format_stack( sys._current_frames()[th.ident]))) except KeyError: logger.debug(th) def execute_command(self): logger.debug( 'Executing command {command} for application {application} with version {version}' .format(command=self.command.name, application=self.app_config.name, version=self.app_config.version)) self._activate_signalhandler() self.messagebus.trigger_event(BEFORE_COMMAND, APPLICATION_ENDPOINT, data=self.command.name) result = 0 try: result = self.component_factory.call(self.command.callable, self.session_scope, self) return result if result is not None else 0 finally: logger.debug( 'Command {command} exited with exit code {result}'.format( command=self.command.name, result=result).format(command=self.command.name, application=self.app_config.name, version=self.app_config.version)) self.component_factory.exit_scope(self.session_scope) self.messagebus.trigger_event(AFTER_COMMAND, APPLICATION_ENDPOINT, data=self.command.name) def gather_metadata(self, metadata_filter=None): return ZafMetadata(self.extension_manager.all_extensions, self.component_manager.get_components_info(), self.config, self.app_config.entrypoint, self.extension_manager, metadata_filter) def __enter__(self): try: self.setup() except Exception: self.__exit__(*sys.exc_info()) raise return self def __exit__(self, *args): self.teardown() @property def exit_code(self): return self._exit_code def _activate_signalhandler(self): if self.signalhandler is not None: self.signalhandler.activate(self.messagebus) def _deactivate_signalhandler(self): if self.signalhandler is not None: self.signalhandler.deactivate()
def test_multiple_request_dispatchers_triggered_by_applicable_requests( self): messagebus = MessageBus(Factory(ComponentManager())) request_target = EndpointId('request_target', '') request_target2 = EndpointId('request_target2', '') request_target_entity1 = 'target_entity1' request_target_entity2 = 'target_entity2' sent_to_all = MessageId('request_to_all', '') sent_to_target = MessageId('request_to_target', '') sent_to_entity1 = MessageId('request_to_entity1', '') messagebus.define_endpoint(request_target) messagebus.define_endpoint(request_target2) messagebus.define_message(sent_to_all, request_target) messagebus.define_message(sent_to_target, request_target) messagebus.define_message(sent_to_entity1, request_target) messagebus.define_message(sent_to_all, request_target2) messagebus.define_message(sent_to_target, request_target2) messagebus.define_message(sent_to_entity1, request_target2) received = { request_target: [], request_target2: [], request_target_entity1: [], request_target_entity2: [] } def target1(message): received[request_target].append(message.message_id) messagebus.register_dispatcher( create_dispatcher(target1), [sent_to_all, sent_to_target, sent_to_entity1], [request_target]) def target2(message): received[request_target2].append(message.message_id) messagebus.register_dispatcher( create_dispatcher(target2), [sent_to_all, sent_to_target, sent_to_entity1], [request_target2]) def target_entity1(message): received[request_target_entity1].append(message.message_id) messagebus.register_dispatcher( create_dispatcher(target_entity1), [sent_to_all, sent_to_target, sent_to_entity1], [request_target], [request_target_entity1]) def target_entity2(message): received[request_target_entity2].append(message.message_id) messagebus.register_dispatcher( create_dispatcher(target_entity2), [sent_to_all, sent_to_target, sent_to_entity1], [request_target], [request_target_entity2]) messagebus.send_request(sent_to_all) messagebus.send_request(sent_to_target, request_target) messagebus.send_request(sent_to_entity1, request_target, request_target_entity1) self.assertEqual(received[request_target], [sent_to_all, sent_to_target]) self.assertEqual(received[request_target2], [sent_to_all]) self.assertEqual(received[request_target_entity1], [sent_to_all, sent_to_target, sent_to_entity1]) self.assertEqual(received[request_target_entity2], [sent_to_all, sent_to_target])
def test_multiple_message_dispatchers_triggered_by_applicable_messages( self): messagebus = MessageBus(Factory(ComponentManager())) message_endpoint = EndpointId('message_endpoint', '') message_endpoint2 = EndpointId('message_endpoint2', '') message_endpoint_entity1 = 'endpoint_entity1' message_endpoint_entity2 = 'endpoint_entity2' sent_from_endpoint = MessageId('sent_from_endpoint', '') sent_from_entity1 = MessageId('sent_from_entity1', '') messagebus.define_endpoint(message_endpoint) messagebus.define_endpoint(message_endpoint2) messagebus.define_message(sent_from_endpoint, message_endpoint) messagebus.define_message(sent_from_entity1, message_endpoint) messagebus.define_message(sent_from_endpoint, message_endpoint2) messagebus.define_message(sent_from_entity1, message_endpoint2) received = { message_endpoint: [], message_endpoint2: [], message_endpoint_entity1: [], message_endpoint_entity2: [] } def endpoint1(message): received[message_endpoint].append(message.message_id) messagebus.register_dispatcher(create_dispatcher(endpoint1), [sent_from_endpoint, sent_from_entity1], [message_endpoint]) def endpoint2(message): received[message_endpoint2].append(message.message_id) messagebus.register_dispatcher(create_dispatcher(endpoint2), [sent_from_endpoint, sent_from_entity1], [message_endpoint2]) def endpoint_entity1(message): received[message_endpoint_entity1].append(message.message_id) messagebus.register_dispatcher(create_dispatcher(endpoint_entity1), [sent_from_endpoint, sent_from_entity1], [message_endpoint], [message_endpoint_entity1]) def endpoint_entity2(message): received[message_endpoint_entity2].append(message.message_id) messagebus.register_dispatcher(create_dispatcher(endpoint_entity2), [sent_from_endpoint, sent_from_entity1], [message_endpoint], [message_endpoint_entity2]) messagebus.trigger_event(sent_from_endpoint, message_endpoint) messagebus.trigger_event(sent_from_entity1, message_endpoint, message_endpoint_entity1) self.assertEqual(received[message_endpoint], [sent_from_endpoint, sent_from_entity1]) self.assertEqual(received[message_endpoint2], []) self.assertEqual(received[message_endpoint_entity1], [sent_from_entity1]) self.assertEqual(received[message_endpoint_entity2], [])