def test_run_logging_config(event_loop, logging_config): """Test that logging initialization happens as expected.""" with patch('asphalt.core.runner.basicConfig') as basicConfig,\ patch('asphalt.core.runner.dictConfig') as dictConfig: run_application(ShutdownComponent(), logging=logging_config) assert basicConfig.call_count == (1 if logging_config == logging.INFO else 0) assert dictConfig.call_count == (1 if isinstance(logging_config, dict) else 0)
def test_dict_config(caplog): """Test that component configuration passed as a dictionary works.""" component_class = '{0.__module__}:{0.__name__}'.format(ShutdownComponent) run_application(component={'type': component_class}) records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 2 assert records[0].message == 'Starting application' assert records[1].message == 'Application stopped'
def test_event_loop_policy(caplog, policy, policy_name): """Test that a the runner switches to a different event loop policy when instructed to.""" component = ShutdownComponent() run_application(component, event_loop_policy=policy) records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 3 assert records[0].message == 'Switched event loop policy to %s' % policy_name assert records[1].message == 'Starting application' assert records[2].message == 'Application stopped'
def test_run_sysexit(caplog): """Test that calling sys.exit() will gracefully shut down the application.""" component = ShutdownComponent(method='exit') run_application(component) assert component.finish_callback_called records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 2 assert records[0].message == 'Starting application' assert records[1].message == 'Application stopped'
def run(self, ip="0.0.0.0", port=4444, component=None): # pragma: no cover """ Runs the Kyoukai server from within your code. This is not normally invoked - instead Asphalt should invoke the Kyoukai component. However, this is here for convenience. """ if not component: from kyoukai.asphalt import KyoukaiComponent component = KyoukaiComponent(self, ip, port) run_application(component)
def test_dict_config(event_loop, caplog): """Test that component configuration passed as a dictionary works.""" component_class = '{0.__module__}:{0.__name__}'.format(ShutdownComponent) run_application(component={'type': component_class}) records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 5 assert records[0].message == 'Running in development mode' assert records[1].message == 'Starting application' assert records[2].message == 'Application started' assert records[3].message == 'Stopping application' assert records[4].message == 'Application stopped'
def test_run_sysexit(caplog): """Test that calling sys.exit() will gracefully shut down the application.""" component = ShutdownComponent(method='exit') run_application(component) assert component.finish_callback_called records = [ record for record in caplog.records if record.name == 'asphalt.core.runner' ] assert len(records) == 2 assert records[0].message == 'Starting application' assert records[1].message == 'Application stopped'
def test_run_max_threads(event_loop, max_threads): """ Test that a new default executor is installed if and only if the max_threads argument is given. """ component = ShutdownComponent() with patch('asphalt.core.runner.ThreadPoolExecutor') as mock_executor: run_application(component, max_threads=max_threads) if max_threads: mock_executor.assert_called_once_with(max_threads) else: assert not mock_executor.called
def test_event_loop_policy(caplog, policy, policy_name): """Test that a the runner switches to a different event loop policy when instructed to.""" component = ShutdownComponent() run_application(component, event_loop_policy=policy) records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 6 assert records[0].message == 'Running in development mode' assert records[1].message == 'Switched event loop policy to %s' % policy_name assert records[2].message == 'Starting application' assert records[3].message == 'Application started' assert records[4].message == 'Stopping application' assert records[5].message == 'Application stopped'
def test_run_cli_application(event_loop, caplog): with pytest.raises(SystemExit) as exc: run_application(DummyCLIApp()) assert exc.value.code == 20 records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 5 assert records[0].message == 'Running in development mode' assert records[1].message == 'Starting application' assert records[2].message == 'Application started' assert records[3].message == 'Stopping application' assert records[4].message == 'Application stopped'
def test_dict_config(event_loop, caplog): """Test that component configuration passed as a dictionary works.""" caplog.set_level(logging.INFO) component_class = '{0.__module__}:{0.__name__}'.format(ShutdownComponent) run_application(component={'type': component_class}) records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 5 assert records[0].message == 'Running in development mode' assert records[1].message == 'Starting application' assert records[2].message == 'Application started' assert records[3].message == 'Stopping application' assert records[4].message == 'Application stopped'
def test_run_callbacks(caplog): """ Test that the "finished" callbacks are run when the application is started and shut down properly and that the proper logging messages are emitted. """ component = ShutdownComponent() run_application(component) assert component.finish_callback_called records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 2 assert records[0].message == 'Starting application' assert records[1].message == 'Application stopped'
def test_run_cli_application(event_loop, caplog): caplog.set_level(logging.INFO) with pytest.raises(SystemExit) as exc: run_application(DummyCLIApp()) assert exc.value.code == 20 records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 5 assert records[0].message == 'Running in development mode' assert records[1].message == 'Starting application' assert records[2].message == 'Application started' assert records[3].message == 'Stopping application' assert records[4].message == 'Application stopped'
def run(configfile, unsafe: bool, loop: Optional[str]): # Read the configuration from the supplied YAML files config = {} for path in configfile: config_data = yaml.load(path) if unsafe else yaml.safe_load(path) assert isinstance(config_data, dict), 'the document root element must be a dictionary' config = merge_config(config, config_data) # Override the event loop policy if specified if loop: config['event_loop_policy'] = loop # Start the application run_application(**config)
def test_clean_exit(event_loop, caplog, method): """ Test that when Ctrl+C is pressed during event_loop.run_forever(), run_application() exits cleanly. """ component = ShutdownComponent(method=method) run_application(component) records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 5 assert records[0].message == 'Running in development mode' assert records[1].message == 'Starting application' assert records[2].message == 'Application started' assert records[3].message == 'Stopping application' assert records[4].message == 'Application stopped'
def test_run_callbacks(event_loop, caplog): """ Test that the teardown callbacks are run when the application is started and shut down properly and that the proper logging messages are emitted. """ component = ShutdownComponent() run_application(component) assert component.teardown_callback_called records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 5 assert records[0].message == 'Running in development mode' assert records[1].message == 'Starting application' assert records[2].message == 'Application started' assert records[3].message == 'Stopping application' assert records[4].message == 'Application stopped'
def test_clean_exit(event_loop, caplog, method): """ Test that when Ctrl+C is pressed during event_loop.run_forever(), run_application() exits cleanly. """ caplog.set_level(logging.INFO) component = ShutdownComponent(method=method) run_application(component) records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 5 assert records[0].message == 'Running in development mode' assert records[1].message == 'Starting application' assert records[2].message == 'Application started' assert records[3].message == 'Stopping application' assert records[4].message == 'Application stopped'
def run(configfile, unsafe: bool, loop: Optional[str], service: Optional[str]): # Read the configuration from the supplied YAML files config = {} # type: Dict[str, Any] for path in configfile: config_data = yaml.load(path, Loader=Loader if unsafe else SafeLoader) assert isinstance(config_data, dict), 'the document root element must be a dictionary' config = merge_config(config, config_data) # Override the event loop policy if specified if loop: config['event_loop_policy'] = loop services = config.pop('services', {}) if not isinstance(services, dict): raise click.ClickException('The "services" key must be a dict, not {}'.format( qualified_name(services))) # If "component" was defined, use that as the default service if one has not been defined yet if 'component' in config: component = config.pop('component') services.setdefault('default', dict(component=component)) # Try to figure out which service to launch service = service or os.getenv('ASPHALT_SERVICE') if len(services) == 0: raise click.ClickException('No services have been defined') elif service: try: service_config = services[service] except KeyError: raise click.ClickException( 'Service {!r} has not been defined'.format(service)) from None elif len(services) == 1: service_config = next(iter(services.values())) elif 'default' in services: service_config = services['default'] else: raise click.ClickException( 'Multiple services present in configuration file but no default service has been ' 'defined and no service was explicitly selected with -s / --service') # Merge the service-level configuration with the top level one config = merge_config(config, service_config) # Start the application run_application(**config)
def test_run_callbacks(caplog): """ Test that the "finished" callbacks are run when the application is started and shut down properly and that the proper logging messages are emitted. """ component = ShutdownComponent() run_application(component) assert component.finish_callback_called records = [ record for record in caplog.records if record.name == 'asphalt.core.runner' ] assert len(records) == 2 assert records[0].message == 'Starting application' assert records[1].message == 'Application stopped'
def run(configfile, unsafe: bool, loop: Optional[str], service: Optional[str]): # Read the configuration from the supplied YAML files config = {} # type: Dict[str, Any] for path in configfile: config_data = yaml.load(path, Loader=Loader) if unsafe else yaml.safe_load(path) assert isinstance(config_data, dict), 'the document root element must be a dictionary' config = merge_config(config, config_data) # Override the event loop policy if specified if loop: config['event_loop_policy'] = loop services = config.pop('services', {}) if not isinstance(services, dict): raise click.ClickException('The "services" key must be a dict, not {}'.format( qualified_name(services))) # If "component" was defined, use that as the default service if one has not been defined yet if 'component' in config: component = config.pop('component') services.setdefault('default', dict(component=component)) # Try to figure out which service to launch service = service or os.getenv('ASPHALT_SERVICE') if len(services) == 0: raise click.ClickException('No services have been defined') elif service: try: service_config = services[service] except KeyError: raise click.ClickException( 'Service {!r} has not been defined'.format(service)) from None elif len(services) == 1: service_config = next(iter(services.values())) elif 'default' in services: service_config = services['default'] else: raise click.ClickException( 'Multiple services present in configuration file but no default service has been ' 'defined and no service was explicitly selected with -s / --service') # Merge the service-level configuration with the top level one config = merge_config(config, service_config) # Start the application run_application(**config)
def test_run_callbacks(event_loop, caplog): """ Test that the teardown callbacks are run when the application is started and shut down properly and that the proper logging messages are emitted. """ caplog.set_level(logging.INFO) component = ShutdownComponent() run_application(component) assert component.teardown_callback_called records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 5 assert records[0].message == 'Running in development mode' assert records[1].message == 'Starting application' assert records[2].message == 'Application started' assert records[3].message == 'Stopping application' assert records[4].message == 'Application stopped'
def test_event_loop_policy(caplog, policy, policy_name): """Test that a the runner switches to a different event loop policy when instructed to.""" component = ShutdownComponent() old_policy = asyncio.get_event_loop_policy() try: run_application(component, event_loop_policy=policy) except DistributionNotFound as e: pytest.skip(str(e)) finally: asyncio.set_event_loop_policy(old_policy) records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 6 assert records[0].message == 'Running in development mode' assert records[1].message == 'Switched event loop policy to %s' % policy_name assert records[2].message == 'Starting application' assert records[3].message == 'Application started' assert records[4].message == 'Stopping application' assert records[5].message == 'Application stopped'
def run(configfile, unsafe: bool, loop: Optional[str]): # Read the configuration from the supplied YAML file config_data = yaml.load(configfile) if unsafe else yaml.safe_load(configfile) assert isinstance(config_data, dict), 'the document root element must be a dictionary' # Override the event loop policy if specified if loop: config_data['event_loop_policy'] = loop # Instantiate the root component try: component_config = config_data.pop('component') except KeyError: raise LookupError('missing configuration key: component') from None else: component = component_types.create_object(**component_config) # Start the application run_application(component, **config_data)
def test_event_loop_policy(caplog, policy, policy_name): """Test that a the runner switches to a different event loop policy when instructed to.""" caplog.set_level(logging.INFO) component = ShutdownComponent() old_policy = asyncio.get_event_loop_policy() try: run_application(component, event_loop_policy=policy) except DistributionNotFound as e: pytest.skip(str(e)) finally: asyncio.set_event_loop_policy(old_policy) records = [record for record in caplog.records if record.name == 'asphalt.core.runner'] assert len(records) == 6 assert records[0].message == 'Running in development mode' assert records[1].message == 'Switched event loop policy to %s' % policy_name assert records[2].message == 'Starting application' assert records[3].message == 'Application started' assert records[4].message == 'Stopping application' assert records[5].message == 'Application stopped'
def run(configfile, unsafe: bool, loop: Optional[str]): # Read the configuration from the supplied YAML file config_data = yaml.load(configfile) if unsafe else yaml.safe_load( configfile) assert isinstance(config_data, dict), 'the document root element must be a dictionary' # Override the event loop policy if specified if loop: config_data['event_loop_policy'] = loop # Instantiate the root component try: component_config = config_data.pop('component') except KeyError: raise LookupError('missing configuration key: component') from None else: component = component_types.create_object(**component_config) # Start the application run_application(component, **config_data)
# flake8: noqa from asphalt.core.runner import run_application run_application(component={ "type": "bashhub_server.container:AppContainer" })