Esempio n. 1
0
async def test_client_with_multiple_bad_servers_async(
        with_partition_awareness):
    with pytest.raises(ReconnectError, match="Can not connect."):
        client = AioClient(partition_aware=with_partition_awareness)
        async with client.connect([("127.0.0.1", 10900),
                                   ("127.0.0.1", 10901)]):
            pass
async def test_auth_failed_async(username, password, with_ssl, ssl_params):
    ssl_params['use_ssl'] = with_ssl

    with pytest.raises(AuthenticationError):
        client = AioClient(username=username, password=password, **ssl_params)
        async with client.connect("127.0.0.1", 10801):
            pass
async def test_auth_success_async(with_ssl, ssl_params):
    ssl_params['use_ssl'] = with_ssl
    client = AioClient(username=DEFAULT_IGNITE_USERNAME,
                       password=DEFAULT_IGNITE_PASSWORD,
                       **ssl_params)
    async with client.connect("127.0.0.1", 10801):
        assert all(node.alive for node in client._nodes)
    async def inner_async():
        client = AioClient(**kwargs)
        async with client.connect("127.0.0.1", 10801):
            async with get_or_create_cache_async(client,
                                                 'test-cache') as cache:
                await cache.put(1, 1)

                assert (await cache.get(1)) == 1
async def async_client(event_loop):
    client = AioClient()
    try:
        await client.connect('127.0.0.1', 10801)
        yield client
    finally:
        await client.close()
Esempio n. 6
0
async def async_client(connection_param):
    client = AioClient(partition_aware=True)
    try:
        await client.connect(connection_param)
        yield client
    finally:
        await client.close()
Esempio n. 7
0
async def async_client_routed():
    client = AioClient(partition_aware=True)
    try:
        await client.connect(client_routed_connection_string)
        yield client
    finally:
        await client.close()
Esempio n. 8
0
async def test_connection_context_async(connection_param, partition_aware):
    is_partition_aware = partition_aware == 'with_partition_aware'
    client = AioClient(partition_aware=is_partition_aware)

    # Check async context manager.
    async with client.connect(connection_param):
        await __check_open(client, is_partition_aware)
    __check_closed(client)

    # Check standard way.
    try:
        await client.connect(connection_param)
        await __check_open(client, is_partition_aware)
    finally:
        await client.close()
        __check_closed(client)
async def async_main():
    print("Running async ExpiryPolicy example.")

    client = AioClient()
    async with client.connect('127.0.0.1', 10800):
        print("Create cache with expiry policy.")
        try:
            ttl_cache = await client.create_cache({
                PROP_NAME:
                'test',
                PROP_EXPIRY_POLICY:
                ExpiryPolicy(create=1.0)
            })
        except NotSupportedByClusterError:
            print(
                "'ExpiryPolicy' API is not supported by cluster. Finishing...")
            return

        try:
            await ttl_cache.put(1, 1)
            await asyncio.sleep(0.5)
            value = await ttl_cache.get(1)
            print(f"key = {1}, value = {value}")
            # key = 1, value = 1
            await asyncio.sleep(1.2)
            value = await ttl_cache.get(1)
            print(f"key = {1}, value = {value}")
            # key = 1, value = None
        finally:
            await ttl_cache.destroy()

        print("Create simple Cache and set TTL through `with_expire_policy`")
        simple_cache = await client.create_cache('test')
        try:
            ttl_cache = simple_cache.with_expire_policy(access=1.0)
            await ttl_cache.put(1, 1)
            await asyncio.sleep(0.5)
            value = await ttl_cache.get(1)
            print(f"key = {1}, value = {value}")
            # key = 1, value = 1
            await asyncio.sleep(1.7)
            value = await ttl_cache.get(1)
            print(f"key = {1}, value = {value}")
            # key = 1, value = None
        finally:
            await simple_cache.destroy()
Esempio n. 10
0
async def test_client_with_failed_server_async(request,
                                               with_partition_awareness):
    srv = start_ignite(idx=4)
    try:
        client = AioClient(partition_aware=with_partition_awareness)
        async with client.connect([("127.0.0.1", 10804)]):
            cache = await client.get_or_create_cache(request.node.name)
            await cache.put(1, 1)
            kill_process_tree(srv.pid)

            if with_partition_awareness:
                ex_class = (ReconnectError, ConnectionResetError)
            else:
                ex_class = ConnectionResetError

            with pytest.raises(ex_class):
                await cache.get(1)
    finally:
        kill_process_tree(srv.pid)
async def test_cluster_set_active_async(with_persistence):
    key = 42
    val = 42
    start_state = ClusterState.INACTIVE if with_persistence else ClusterState.ACTIVE

    client = AioClient()
    async with client.connect([("127.0.0.1", 10801), ("127.0.0.1", 10802)]):
        cluster = client.get_cluster()
        assert await cluster.get_state() == start_state

        await cluster.set_state(ClusterState.ACTIVE)
        assert await cluster.get_state() == ClusterState.ACTIVE

        cache = await client.get_or_create_cache("test_cache")
        await cache.put(key, val)
        assert await cache.get(key) == val

        await cluster.set_state(ClusterState.ACTIVE_READ_ONLY)
        assert await cluster.get_state() == ClusterState.ACTIVE_READ_ONLY

        assert await cache.get(key) == val
        with pytest.raises(CacheError):
            await cache.put(key, val + 1)

        await cluster.set_state(ClusterState.INACTIVE)
        assert await cluster.get_state() == ClusterState.INACTIVE

        with pytest.raises(CacheError):
            await cache.get(key)

        with pytest.raises(CacheError):
            await cache.put(key, val + 1)

        await cluster.set_state(ClusterState.ACTIVE)
        assert await cluster.get_state() == ClusterState.ACTIVE

        await cache.put(key, val + 2)
        assert await cache.get(key) == val + 2
Esempio n. 12
0
async def test_client_with_recovered_server_async(request,
                                                  with_partition_awareness):
    srv = start_ignite(idx=4)
    try:
        client = AioClient(partition_aware=with_partition_awareness)
        async with client.connect([("127.0.0.1", 10804)]):
            cache = await client.get_or_create_cache(request.node.name)
            await cache.put(1, 1)

            # Kill and restart server
            kill_process_tree(srv.pid)
            srv = start_ignite(idx=4)

            # First request may fail.
            try:
                await cache.put(1, 2)
            except connection_errors:
                pass

            # Retry succeeds
            await cache.put(1, 2)
            assert await cache.get(1) == 2
    finally:
        kill_process_tree(srv.pid)
async def test_connection_error_with_incorrect_config_async(
        invalid_ssl_params):
    with pytest.raises(ReconnectError):
        client = AioClient(**invalid_ssl_params)
        async with client.connect([("127.0.0.1", 10801)]):
            pass
Esempio n. 14
0
async def main():
    # establish connection
    client = AioClient()
    async with client.connect('127.0.0.1', 10800):
        # create tables
        for query in [
                COUNTRY_CREATE_TABLE_QUERY,
                CITY_CREATE_TABLE_QUERY,
                LANGUAGE_CREATE_TABLE_QUERY,
        ]:
            await client.sql(query)

        # create indices
        for query in [CITY_CREATE_INDEX, LANGUAGE_CREATE_INDEX]:
            await client.sql(query)

        # load data concurrently.
        await asyncio.gather(*[
            client.sql(COUNTRY_INSERT_QUERY, query_args=row)
            for row in COUNTRY_DATA
        ])

        await asyncio.gather(*[
            client.sql(CITY_INSERT_QUERY, query_args=row) for row in CITY_DATA
        ])

        await asyncio.gather(*[
            client.sql(LANGUAGE_INSERT_QUERY, query_args=row)
            for row in LANGUAGE_DATA
        ])

        # 10 most populated cities (with pagination)
        MOST_POPULATED_QUERY = '''
        SELECT name, population FROM City ORDER BY population DESC LIMIT 10'''

        async with client.sql(MOST_POPULATED_QUERY) as cursor:
            print('Most 10 populated cities:')

            async for row in cursor:
                print(row)
        # Most 10 populated cities:
        # ['Mumbai (Bombay)', 10500000]
        # ['Shanghai', 9696300]
        # ['New York', 8008278]
        # ['Peking', 7472000]
        # ['Delhi', 7206704]
        # ['Chongqing', 6351600]
        # ['Tianjin', 5286800]
        # ['Calcutta [Kolkata]', 4399819]
        # ['Wuhan', 4344600]
        # ['Harbin', 4289800]

        # 10 most populated cities in 3 countries (with pagination and header row)
        MOST_POPULATED_IN_3_COUNTRIES_QUERY = '''
        SELECT country.name as country_name, city.name as city_name, MAX(city.population) AS max_pop FROM country
            JOIN city ON city.countrycode = country.code
            WHERE country.code IN ('USA','IND','CHN')
            GROUP BY country.name, city.name ORDER BY max_pop DESC LIMIT 10
        '''

        async with client.sql(MOST_POPULATED_IN_3_COUNTRIES_QUERY,
                              include_field_names=True) as cursor:
            print('Most 10 populated cities in USA, India and China:')
            print(await cursor.__anext__())
            print('----------------------------------------')
            async for row in cursor:
                print(row)
        # Most 10 populated cities in USA, India and China:
        # ['COUNTRY_NAME', 'CITY_NAME', 'MAX_POP']
        # ----------------------------------------
        # ['India', 'Mumbai (Bombay)', 10500000]
        # ['China', 'Shanghai', 9696300]
        # ['United States', 'New York', 8008278]
        # ['China', 'Peking', 7472000]
        # ['India', 'Delhi', 7206704]
        # ['China', 'Chongqing', 6351600]
        # ['China', 'Tianjin', 5286800]
        # ['India', 'Calcutta [Kolkata]', 4399819]
        # ['China', 'Wuhan', 4344600]
        # ['China', 'Harbin', 4289800]

        # show city info
        CITY_INFO_QUERY = '''SELECT * FROM City WHERE id = ?'''

        async with client.sql(CITY_INFO_QUERY,
                              query_args=[3802],
                              include_field_names=True) as cursor:
            field_names = await cursor.__anext__()
            field_data = await cursor.__anext__()

            print('City info:')
            for field_name, field_value in zip(field_names * len(field_data),
                                               field_data):
                print('{}: {}'.format(field_name, field_value))
        # City info:
        # ID: 3802
        # NAME: Detroit
        # COUNTRYCODE: USA
        # DISTRICT: Michigan
        # POPULATION: 951270

        # clean up concurrently.
        await asyncio.gather(*[
            client.sql(DROP_TABLE_QUERY.format(table_name)) for table_name in [
                CITY_TABLE_NAME,
                LANGUAGE_TABLE_NAME,
                COUNTRY_TABLE_NAME,
            ]
        ])