Example #1
0
    def __enter__(self):
        """Start of a context where all the bus' transports have been replaced with mocks"""
        rpc = TestRpcTransport()
        result = TestResultTransport(require_mocking=self.require_mocking)
        event = TestEventTransport(require_mocking=self.require_mocking)
        schema = TestSchemaTransport()

        new_registry = TransportRegistry()
        new_registry.set_schema_transport(schema)

        self.old_transport_registry = self.bus.client.transport_registry

        for api_name, entry in self.old_transport_registry._registry.items():
            new_registry.set_rpc_transport(api_name, rpc)
            new_registry.set_result_transport(api_name, result)

            if hasattr(entry.event, "child_transport"):
                parent_transport = copy(entry.event)
                parent_transport.child_transport = event
                new_registry.set_event_transport(api_name, parent_transport)
            else:
                new_registry.set_event_transport(api_name, event)

        self.bus.client.transport_registry = new_registry
        return MockResult(rpc, result, event, schema)
Example #2
0
    def __enter__(self):
        rpc = TestRpcTransport()
        result = TestResultTransport()
        event = TestEventTransport()
        schema = TestSchemaTransport()

        new_registry = TransportRegistry()
        new_registry.set_schema_transport(schema)

        self.old_transport_registry = self.bus.client.transport_registry

        for api_name, entry in self.old_transport_registry._registry.items():
            new_registry.set_rpc_transport(api_name, rpc)
            new_registry.set_result_transport(api_name, result)

            if hasattr(entry.event, "child_transport"):
                parent_transport = copy(entry.event)
                parent_transport.child_transport = event
                new_registry.set_event_transport(api_name, parent_transport)
            else:
                new_registry.set_event_transport(api_name, event)

        self.bus.client.transport_registry = new_registry
        return MockResult(rpc, result, event, schema)
Example #3
0
async def create_async(
    config: Union[dict, Config] = None,
    *,
    config_file: str = None,
    service_name: str = None,
    process_name: str = None,
    rpc_transport: Optional["RpcTransport"] = None,
    result_transport: Optional["ResultTransport"] = None,
    event_transport: Optional["EventTransport"] = None,
    schema_transport: Optional["SchemaTransport"] = None,
    client_class=BusClient,
    node_class=BusPath,
    plugins=None,
    flask: bool = False,
    **kwargs,
) -> BusPath:
    """
    Create a new bus instance which can be used to access the bus.

    Typically this will be used as follows:

        import lightbus

        bus = await lightbus.create_async()

    This will be a `BusPath` instance. If you wish to access the lower
    level `BusClient` you can do so via `bus.client`.

    See Also:

        `create()` - The synchronous wrapper for this function

    Args:
        config (dict, Config): The config object or dictionary to load
        config_file (str): The path to a config file to load (should end in .json or .yaml)
        service_name (str): The name of this service - will be used when creating event consumer groups
        process_name (str): The unique name of this process - used when retrieving unprocessed events following a crash
        rpc_transport (RpcTransport): The RPC transport instance to use, defaults to Redis
        result_transport (ResultTransport): The result transport instance to use, defaults to Redis
        event_transport (EventTransport): The event transport instance to use, defaults to Redis
        schema_transport (SchemaTransport): The schema transport instance to use, defaults to Redis
        client_class (BusClient): The class from which the bus client will be instantiated
        node_class (BusPath): The class from which the bus path will be instantiated
        plugins (dict): A dictionary of plugins to load, where keys are the plugin name defined in the plugin's entrypoint
        flask (bool): Are we using flask? If so we will make sure we don't start lightbus in the reloader process
        **kwargs (): Any additional instantiation arguments to be passed to `client_class`.

    Returns:

    """
    if flask:
        in_flask_server = sys.argv[0].endswith("flask") and "run" in sys.argv
        if in_flask_server and os.environ.get("WERKZEUG_RUN_MAIN",
                                              "").lower() != "true":
            # Flask has a reloader process that shouldn't start a lightbus client
            return

    from lightbus.config import Config

    # If were are running via the Lightbus CLI then we may have
    # some command line arguments we need to apply.
    from lightbus.commands import COMMAND_PARSED_ARGS

    config_file = COMMAND_PARSED_ARGS.get("config_file", None) or config_file
    service_name = COMMAND_PARSED_ARGS.get("service_name",
                                           None) or service_name
    process_name = COMMAND_PARSED_ARGS.get("process_name",
                                           None) or process_name

    if config is None:
        config = load_config(from_file=config_file,
                             service_name=service_name,
                             process_name=process_name)

    if isinstance(config, Mapping):
        config = Config.load_dict(config or {})

    transport_registry = TransportRegistry().load_config(config)

    # Set transports if specified
    if rpc_transport:
        transport_registry.set_rpc_transport("default", rpc_transport)

    if result_transport:
        transport_registry.set_result_transport("default", result_transport)

    if event_transport:
        transport_registry.set_event_transport("default", event_transport)

    if schema_transport:
        transport_registry.set_schema_transport(schema_transport)

    client = client_class(transport_registry=transport_registry,
                          config=config,
                          **kwargs)
    await client.setup_async(plugins=plugins)

    return node_class(name="", parent=None, client=client)