예제 #1
0
    def open(cls, address, *, auth=None, timeout=None, **config):
        """ Open a new Bolt connection to a given server address.

        :param address:
        :param auth:
        :param timeout:
        :param config:
        :return:
        """
        config = PoolConfig.consume(config)
        s, config.protocol_version = connect(address,
                                             timeout=timeout,
                                             config=config)

        if config.protocol_version == (3, 0):
            from neo4j.io._bolt3 import Bolt3
            connection = Bolt3(address, s, auth=auth, **config)
        else:
            log.debug("[#%04X]  S: <CLOSE>", s.getpeername()[1])
            s.shutdown(SHUT_RDWR)
            s.close()
            raise ProtocolError(
                "Driver does not support Bolt protocol version: 0x%06X%02X",
                config.protocol_version[0], config.protocol_version[1])

        connection.hello()
        return connection
예제 #2
0
    def open(cls, address, *, auth=None, timeout=None, **pool_config):
        """ Open a new Bolt connection to a given server address.

        :param address:
        :param auth:
        :param timeout: The connection timeout
        :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)
        elif pool_config.protocol_version == (4, 0):
            # Carry out Bolt subclass imports locally to avoid circular dependency issues.
            from neo4j.io._bolt4x0 import Bolt4x0
            connection = Bolt4x0(address,
                                 s,
                                 pool_config.max_connection_lifetime,
                                 auth=auth,
                                 user_agent=pool_config.user_agent)
        elif pool_config.protocol_version == (4, 1):
            # Carry out Bolt subclass imports locally to avoid circular dependency issues.
            from neo4j.io._bolt4x1 import Bolt4x1
            connection = Bolt4x1(address,
                                 s,
                                 pool_config.max_connection_lifetime,
                                 auth=auth,
                                 user_agent=pool_config.user_agent)
        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)

        connection.hello()
        return connection
def test_simple_pull(fake_socket):
    address = ("127.0.0.1", 7687)
    socket = fake_socket(address)
    connection = Bolt3(address, socket)
    connection.pull()
    connection.send_all()
    tag, fields = socket.pop_message()
    assert tag == b"\x3F"
    assert len(fields) == 0
def test_simple_discard(fake_socket):
    address = ("127.0.0.1", 7687)
    socket = fake_socket(address)
    connection = Bolt3(address, socket, PoolConfig.max_connection_lifetime)
    connection.discard()
    connection.send_all()
    tag, fields = socket.pop_message()
    assert tag == b"\x2F"
    assert len(fields) == 0
def test_n_extra_not_supported_in_pull(fake_socket):
    address = ("127.0.0.1", 7687)
    connection = Bolt3(address, fake_socket(address))
    with pytest.raises(ValueError):
        connection.pull(n=666)
def test_qid_extra_not_supported_in_discard(fake_socket):
    address = ("127.0.0.1", 7687)
    connection = Bolt3(address, fake_socket(address))
    with pytest.raises(ValueError):
        connection.discard(qid=666)
def test_db_extra_not_supported_in_run(fake_socket):
    address = ("127.0.0.1", 7687)
    connection = Bolt3(address, fake_socket(address))
    with pytest.raises(ValueError):
        connection.run("", db="something")
def test_conn_not_timed_out(fake_socket):
    address = ("127.0.0.1", 7687)
    connection = Bolt3(address,
                       fake_socket(address),
                       max_connection_lifetime=999999999)
    assert connection.timedout() is False
def test_db_extra_not_supported_in_run(fake_socket):
    address = ("127.0.0.1", 7687)
    connection = Bolt3(address, fake_socket(address),
                       PoolConfig.max_connection_lifetime)
    with pytest.raises(ConfigurationError):
        connection.run("", db="something")
def test_conn_not_timed_out_if_not_enabled(fake_socket):
    address = ("127.0.0.1", 7687)
    max_connection_lifetime = -1
    connection = Bolt3(address, fake_socket(address), max_connection_lifetime)
    assert connection.timedout() is False
def test_conn_timed_out(fake_socket):
    address = ("127.0.0.1", 7687)
    max_connection_lifetime = 0
    connection = Bolt3(address, fake_socket(address), max_connection_lifetime)
    assert connection.timedout() is True
def test_n_extra_not_supported_in_discard(fake_socket):
    address = ("127.0.0.1", 7687)
    connection = Bolt3(address, fake_socket(address), PoolConfig.max_connection_lifetime)
    with pytest.raises(ValueError):
        connection.discard(n=666)
def test_conn_not_timed_out():
    address = ("127.0.0.1", 7687)
    connection = Bolt3(address, FakeSocket(address), max_age=999999999)
    assert connection.timedout() is False