예제 #1
0
    async def f(url: str = request.config.getoption("--redis-url"), **kwargs):
        single = kwargs.pop("single_connection_client",
                            False) or single_connection
        parser_class = kwargs.pop("parser_class", None) or parser_cls
        url_options = parse_url(url)
        url_options.update(kwargs)
        pool = redis.ConnectionPool(parser_class=parser_class, **url_options)
        client: redis.Redis = redis.Redis(connection_pool=pool)
        if single:
            client = client.client()
            await client.initialize()

        def teardown():
            async def ateardown():
                if "username" in kwargs:
                    return
                try:
                    await client.flushdb()
                except redis.ConnectionError:
                    # handle cases where a test disconnected a client
                    # just manually retry the flushdb
                    await client.flushdb()
                await client.close()
                await client.connection_pool.disconnect()

            if event_loop.is_running():
                event_loop.create_task(ateardown())
            else:
                event_loop.run_until_complete(ateardown())

        request.addfinalizer(teardown)

        return client
 async def test_on_connect_error(self):
     """
     An error in Connection.on_connect should disconnect from the server
     see for details: https://github.com/andymccurdy/redis-py/issues/368
     """
     # this assumes the Redis server being tested against doesn't have
     # 9999 databases ;)
     bad_connection = redis.Redis(db=9999)
     # an error should be raised on connect
     with pytest.raises(redis.RedisError):
         await bad_connection.info()
     pool = bad_connection.connection_pool
     assert len(pool._available_connections) == 1
     assert not pool._available_connections[0]._reader
예제 #3
0
 async def test_channel_subscribe(self, r: redis.Redis):
     r = redis.Redis(host="localhost", port=6390)
     p = r.pubsub()
     with pytest.raises(ConnectionError):
         await p.subscribe("foo")