コード例 #1
0
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)
コード例 #2
0
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)
コード例 #3
0
ファイル: test_runner.py プロジェクト: awesome-python/asphalt
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'
コード例 #4
0
ファイル: test_runner.py プロジェクト: jd-258/asphalt
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'
コード例 #5
0
ファイル: test_runner.py プロジェクト: jd-258/asphalt
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'
コード例 #6
0
ファイル: app.py プロジェクト: mplanchard/Kyoukai
    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)
コード例 #7
0
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'
コード例 #8
0
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'
コード例 #9
0
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
コード例 #10
0
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'
コード例 #11
0
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'
コード例 #12
0
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'
コード例 #13
0
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
コード例 #14
0
ファイル: test_runner.py プロジェクト: jd-258/asphalt
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'
コード例 #15
0
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'
コード例 #16
0
ファイル: command.py プロジェクト: awesome-python/asphalt
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)
コード例 #17
0
ファイル: command.py プロジェクト: dfee/asphalt
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)
コード例 #18
0
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'
コード例 #19
0
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'
コード例 #20
0
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'
コード例 #21
0
ファイル: cli.py プロジェクト: asphalt-framework/asphalt
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)
コード例 #22
0
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'
コード例 #23
0
ファイル: cli.py プロジェクト: selimb/asphalt
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)
コード例 #24
0
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'
コード例 #25
0
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'
コード例 #26
0
ファイル: command.py プロジェクト: jd-258/asphalt
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)
コード例 #27
0
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'
コード例 #28
0
ファイル: command.py プロジェクト: jd-258/asphalt
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)
コード例 #29
0
# flake8: noqa

from asphalt.core.runner import run_application

run_application(component={
    "type": "bashhub_server.container:AppContainer"
})