def test_should_call_get_routing_tables_with_context(): with StubCluster({9001: "v3/get_routing_table_with_context.script"}): address = ("127.0.0.1", 9001) routing_context = {"name": "molly", "age": "1"} with RoutingConnectionPool(connector, UNREACHABLE_ADDRESS, routing_context) as pool: pool.fetch_routing_info(address)
def test_roll_back_to_initial_server_if_failed_update_with_existing_routers(self): with StubCluster({9001: "v1/router.script"}): initial_address = ("127.0.0.1", 9001) # roll back addresses routers = [("127.0.0.1", 9002), ("127.0.0.1", 9003)] # not reachable servers with RoutingConnectionPool(connector, initial_address, {}, *routers) as pool: pool.update_routing_table() table = pool.routing_table assert table.routers == {("127.0.0.1", 9001), ("127.0.0.1", 9002), ("127.0.0.1", 9003)} assert table.readers == {("127.0.0.1", 9004), ("127.0.0.1", 9005)} assert table.writers == {("127.0.0.1", 9006)} assert table.ttl == 300
def __new__(cls, uri, **config): from neobolt.addressing import SocketAddress from neobolt.direct import DEFAULT_PORT, connect from neobolt.routing import RoutingConnectionPool from neobolt.security import ENCRYPTION_OFF, ENCRYPTION_ON, SSL_AVAILABLE, SecurityPlan cls._check_uri(uri) instance = object.__new__(cls) instance.initial_address = initial_address = SocketAddress.from_uri(uri, DEFAULT_PORT) if config.get("encrypted") is None: config["encrypted"] = ENCRYPTION_ON if SSL_AVAILABLE else ENCRYPTION_OFF instance.security_plan = security_plan = SecurityPlan.build(**config) instance.encrypted = security_plan.encrypted routing_context = SocketAddress.parse_routing_context(uri) if not security_plan.routing_compatible: # this error message is case-specific as there is only one incompatible # scenario right now raise ValueError("TRUST_ON_FIRST_USE is not compatible with routing") def connector(address, **kwargs): return connect(address, **dict(config, **kwargs)) pool = RoutingConnectionPool(connector, initial_address, routing_context, initial_address, **config) try: pool.update_routing_table() except: pool.close() raise else: instance._pool = pool instance._max_retry_time = config.get("max_retry_time", default_config["max_retry_time"]) return instance
def __new__(cls, uri, **config): from neobolt.addressing import SocketAddress from neobolt.direct import DEFAULT_PORT, connect from neobolt.routing import RoutingConnectionPool from neobolt.security import make_ssl_context cls._check_uri(uri) instance = object.__new__(cls) instance.initial_address = initial_address = SocketAddress.from_uri( uri, DEFAULT_PORT) if config.get("encrypted") is None: config["encrypted"] = False instance._ssl_context = make_ssl_context(**config) instance.encrypted = instance._ssl_context is not None routing_context = SocketAddress.parse_routing_context(uri) def connector(address, **kwargs): return connect(address, **dict(config, **kwargs)) pool = RoutingConnectionPool(connector, initial_address, routing_context, initial_address, **config) try: pool.update_routing_table() except: pool.close() raise else: instance._pool = pool instance._max_retry_time = config.get( "max_retry_time", default_config["max_retry_time"]) return instance
def test_try_initial_server_first_if_missing_writer(self): with StubCluster({9001: "v1/router.script"}): initial_address = ("127.0.0.1", 9001) with RoutingConnectionPool(connector, initial_address, {}) as pool: pool.missing_writer = True pool.update_routing_table() table = pool.routing_table assert table.routers == {("127.0.0.1", 9001), ("127.0.0.1", 9002), ("127.0.0.1", 9003)} assert table.readers == {("127.0.0.1", 9004), ("127.0.0.1", 9005)} assert table.writers == {("127.0.0.1", 9006)} assert table.ttl == 300 assert not pool.missing_writer
def open(self, cx_data): def connector(address_, **kwargs): return connect(address_, auth=cx_data["auth"], **kwargs) address = (cx_data["host"], cx_data["port"]) self.pool = RoutingConnectionPool(connector, address, {})
def RoutingPool(*routers): return RoutingConnectionPool(connector, UNREACHABLE_ADDRESS, {}, *routers)
def test_should_call_get_routing_tables(self): with StubCluster({9001: "v1/get_routing_table.script"}): address = ("127.0.0.1", 9001) with RoutingConnectionPool(connector, UNREACHABLE_ADDRESS, {}) as pool: pool.fetch_routing_info(address)
def test_should_populate_initial_router(self): initial_router = ("127.0.0.1", 9001) router = ("127.0.0.1", 9002) with RoutingConnectionPool(connector, initial_router, {}, router) as pool: assert pool.routing_table.routers == {("127.0.0.1", 9002)}