def test_n_extra_in_discard(fake_socket):
    address = ("127.0.0.1", 7687)
    socket = fake_socket(address)
    connection = Bolt4x1(address, socket, PoolConfig.max_connection_lifetime)
    connection.discard(n=666)
    connection.send_all()
    tag, fields = socket.pop_message()
    assert tag == b"\x2F"
    assert len(fields) == 1
    assert fields[0] == {"n": 666}
def test_qid_extra_in_discard(fake_socket, test_input, expected):
    address = ("127.0.0.1", 7687)
    socket = fake_socket(address)
    connection = Bolt4x1(address, socket, PoolConfig.max_connection_lifetime)
    connection.discard(qid=test_input)
    connection.send_all()
    tag, fields = socket.pop_message()
    assert tag == b"\x2F"
    assert len(fields) == 1
    assert fields[0] == expected
def test_n_and_qid_extras_in_pull(fake_socket):
    address = ("127.0.0.1", 7687)
    socket = fake_socket(address)
    connection = Bolt4x1(address, socket, PoolConfig.max_connection_lifetime)
    connection.pull(n=666, qid=777)
    connection.send_all()
    tag, fields = socket.pop_message()
    assert tag == b"\x3F"
    assert len(fields) == 1
    assert fields[0] == {"n": 666, "qid": 777}
def test_db_extra_in_begin(fake_socket):
    address = ("127.0.0.1", 7687)
    socket = fake_socket(address)
    connection = Bolt4x1(address, socket, PoolConfig.max_connection_lifetime)
    connection.begin(db="something")
    connection.send_all()
    tag, fields = socket.pop_message()
    assert tag == b"\x11"
    assert len(fields) == 1
    assert fields[0] == {"db": "something"}
def test_qid_extra_in_pull(fake_socket, test_input, expected):
    # python -m pytest tests/unit/io/test_class_bolt4x0.py -s -k test_qid_extra_in_pull
    address = ("127.0.0.1", 7687)
    socket = fake_socket(address)
    connection = Bolt4x1(address, socket, PoolConfig.max_connection_lifetime)
    connection.pull(qid=test_input)
    connection.send_all()
    tag, fields = socket.pop_message()
    assert tag == b"\x3F"
    assert len(fields) == 1
    assert fields[0] == expected
def test_hello_passes_routing_metadata(fake_socket_pair):
    address = ("127.0.0.1", 7687)
    sockets = fake_socket_pair(address)
    sockets.server.send_message(0x70, {"server": "Neo4j/4.1.0"})
    connection = Bolt4x1(address, sockets.client, PoolConfig.max_connection_lifetime,
                         routing_context={"foo": "bar"})
    connection.hello()
    tag, fields = sockets.server.pop_message()
    assert tag == 0x01
    assert len(fields) == 1
    assert fields[0]["routing"] == {"foo": "bar"}
def test_hello_passes_routing_metadata(fake_socket_pair):
    address = ("127.0.0.1", 7687)
    sockets = fake_socket_pair(address)
    # TODO helper method for encoding messages
    sockets.server.sendall(b"\x00\x03\xB1\x70\xA0\x00\x00")
    connection = Bolt4x1(address,
                         sockets.client,
                         PoolConfig.max_connection_lifetime,
                         routing_context={"foo": "bar"})
    connection.hello()
    tag, fields = sockets.server.pop_message()
    assert tag == 0x01
    assert len(fields) == 1
    assert fields[0]["routing"] == {"foo": "bar"}
def test_conn_not_timed_out(fake_socket):
    address = ("127.0.0.1", 7687)
    max_connection_lifetime = 999999999
    connection = Bolt4x1(address, fake_socket(address),
                         max_connection_lifetime)
    assert connection.timedout() is False
Ejemplo n.º 9
0
    def open(cls,
             address,
             *,
             auth=None,
             timeout=None,
             routing_context=None,
             **pool_config):
        """ Open a new Bolt connection to a given server address.

        :param address:
        :param auth:
        :param timeout: the connection timeout in seconds
        :param routing_context: dict containing routing context
        :param pool_config:
        :return:
        :raise BoltHandshakeError: raised if the Bolt Protocol can not negotiate a protocol version.
        :raise ServiceUnavailable: raised if there was a connection issue.
        """
        pool_config = PoolConfig.consume(pool_config)
        s, pool_config.protocol_version, handshake, data = connect(
            address,
            timeout=timeout,
            custom_resolver=pool_config.resolver,
            ssl_context=pool_config.get_ssl_context(),
            keep_alive=pool_config.keep_alive,
        )

        if pool_config.protocol_version == (3, 0):
            # Carry out Bolt subclass imports locally to avoid circular dependency issues.
            from neo4j.io._bolt3 import Bolt3
            connection = Bolt3(address,
                               s,
                               pool_config.max_connection_lifetime,
                               auth=auth,
                               user_agent=pool_config.user_agent,
                               routing_context=routing_context)
        elif pool_config.protocol_version == (4, 0):
            # Carry out Bolt subclass imports locally to avoid circular dependency issues.
            from neo4j.io._bolt4 import Bolt4x0
            connection = Bolt4x0(address,
                                 s,
                                 pool_config.max_connection_lifetime,
                                 auth=auth,
                                 user_agent=pool_config.user_agent,
                                 routing_context=routing_context)
        elif pool_config.protocol_version == (4, 1):
            # Carry out Bolt subclass imports locally to avoid circular dependency issues.
            from neo4j.io._bolt4 import Bolt4x1
            connection = Bolt4x1(address,
                                 s,
                                 pool_config.max_connection_lifetime,
                                 auth=auth,
                                 user_agent=pool_config.user_agent,
                                 routing_context=routing_context)
        elif pool_config.protocol_version == (4, 2):
            # Carry out Bolt subclass imports locally to avoid circular dependency issues.
            from neo4j.io._bolt4 import Bolt4x2
            connection = Bolt4x2(address,
                                 s,
                                 pool_config.max_connection_lifetime,
                                 auth=auth,
                                 user_agent=pool_config.user_agent,
                                 routing_context=routing_context)
        else:
            log.debug("[#%04X]  S: <CLOSE>", s.getpeername()[1])
            s.shutdown(SHUT_RDWR)
            s.close()

            supported_versions = Bolt.protocol_handlers().keys()
            raise BoltHandshakeError(
                "The Neo4J server does not support communication with this driver. This driver have support for Bolt Protocols {}"
                .format(supported_versions),
                address=address,
                request_data=handshake,
                response_data=data)

        try:
            connection.hello()
        except Exception as error:
            log.debug("[#%04X]  C: <CLOSE> %s", s.getsockname()[1], str(error))
            s.shutdown(SHUT_RDWR)
            s.close()
            raise error

        return connection