Exemplo n.º 1
0
    def __enter__(self):
        """Start of a context where all the bus' transports have been replaced with mocks"""

        # Mutable structures which we will use this to get the mocked data into
        # the transports

        # RPC
        mock_responses = {}
        # Events
        mock_events = set()

        # Create our transport classes, into which we inject our mutable structures
        TestRpcTransport = make_test_rpc_transport()
        TestResultTransport = make_test_result_transport(mock_responses)
        TestEventTransport = make_test_event_transport(mock_events)
        TestSchemaTransport = make_test_schema_transport()

        new_registry = TransportRegistry()
        new_registry.set_schema_transport(TestSchemaTransport,
                                          TestSchemaTransport.Config(),
                                          self.bus.client.config)

        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, TestRpcTransport,
                                           TestRpcTransport.Config(),
                                           self.bus.client.config)
            new_registry.set_result_transport(
                api_name,
                TestResultTransport,
                TestResultTransport.Config(
                    require_mocking=self.require_mocking),
                self.bus.client.config,
            )
            new_registry.set_event_transport(
                api_name,
                TestEventTransport,
                TestEventTransport.Config(
                    require_mocking=self.require_mocking),
                self.bus.client.config,
            )

        # The docs are only available on the bus client during testing
        self.bus.client.event_dock.transport_registry = new_registry
        self.bus.client.rpc_result_dock.transport_registry = new_registry

        queue_mocker = BusQueueMockerContext(client=self.bus.client)
        bus_with_mocked_queues = queue_mocker.__enter__()
        self.stack.append(queue_mocker)
        return MockResult(bus_with_mocked_queues,
                          mock_responses=mock_responses,
                          mock_events=mock_events)
Exemplo n.º 2
0
def test_transport_registry_get_rpc_transports(redis_default_config):
    registry = TransportRegistry().load_config(redis_default_config)

    # Note how we set a config value below. We do this because
    # we need this transport to appear different from the default
    # transport for the purposes of this test. Also note that these
    # are not actual transports, but transport pools which wrap transports.
    # We are therefore actually comparing transport pools. Transport
    # pools are considered equal if they have the same transport class
    # and config. It is for this reason we modify the config below.
    registry.set_rpc_transport("redis1", RedisRpcTransport,
                               RedisRpcTransport.Config(rpc_timeout=99),
                               Config.default())
    registry.set_rpc_transport("redis2", RedisRpcTransport,
                               RedisRpcTransport.Config(rpc_timeout=99),
                               Config.default())
    registry.set_rpc_transport("debug1", DebugRpcTransport,
                               DebugRpcTransport.Config(), Config.default())
    registry.set_rpc_transport("debug2", DebugRpcTransport,
                               DebugRpcTransport.Config(), Config.default())

    transport_pools = registry.get_rpc_transports(
        ["default", "foo", "bar", "redis1", "redis2", "debug1", "debug2"])

    default_transport = registry.get_rpc_transport("default")
    redis_transport = registry.get_rpc_transport("redis1")
    debug_transport = registry.get_rpc_transport("debug1")

    assert set(transport_pools[default_transport]) == {"default", "foo", "bar"}
    assert set(transport_pools[debug_transport]) == {"debug1", "debug2"}
    assert set(transport_pools[redis_transport]) == {"redis1", "redis2"}