Example #1
0
    async def test_connect_pool_already_created(self):
        with patch("aioredis.create_pool") as create_pool:
            instance = Instance('localhost', 6379)
            fake_pool = FakePool()
            instance._pool = fake_pool

            pool = await instance.connect()

            assert not create_pool.called
            assert pool is fake_pool
Example #2
0
    async def test_connect_pool_already_created(self):

        with patch('aioredlock.redis.Instance._create_redis_pool') as \
                create_redis_pool:
            instance = Instance(('localhost', 6379))
            fake_pool = FakePool()
            instance._pool = fake_pool

            pool = await instance.connect()

            assert not create_redis_pool.called
            assert pool is fake_pool
            assert pool.script_load.called is False
Example #3
0
 def fake_instance(self):
     with patch('aioredlock.redis.Instance._create_redis_pool') as \
             create_redis_pool:
         fake_pool = FakePool()
         create_redis_pool.side_effect = fake_create_redis_pool(fake_pool)
         instance = Instance(('localhost', 6379))
         yield instance
Example #4
0
    async def test_connect_pool_not_created(self, connection, address,
                                            redis_kwargs):
        with patch('aioredlock.redis.Instance._create_redis_pool') as \
                create_redis_pool:

            fake_pool = FakePool()
            create_redis_pool.side_effect = fake_create_redis_pool(fake_pool)
            instance = Instance(connection)

            assert instance._pool is None
            pool = await instance.connect()

            create_redis_pool.assert_called_once_with(address,
                                                      **redis_kwargs,
                                                      minsize=1,
                                                      maxsize=100)
            assert pool is fake_pool
            assert instance._pool is fake_pool

            # scripts
            assert pool.script_load.call_count == len(self.script_names)
            for name in self.script_names:
                digest = getattr(instance, '%s_sha1' % name.lower())
                assert digest
                assert digest in pool.script_cache
Example #5
0
    async def test_connect_pool_not_created_with_minsize_and_maxsize(self):
        connection = {
            'host': 'localhost',
            'port': 6379,
            'db': 0,
            'password': '******',
            'minsize': 2,
            'maxsize': 5
        }
        address = ('localhost', 6379)
        redis_kwargs = {'db': 0, 'password': '******'}
        with patch('aioredlock.redis.Instance._create_redis_pool') as \
                create_redis_pool:

            fake_pool = FakePool()
            create_redis_pool.side_effect = fake_create_redis_pool(fake_pool)
            instance = Instance(connection)

            assert instance._pool is None
            pool = await instance.connect()

            create_redis_pool.assert_called_once_with(address,
                                                      **redis_kwargs,
                                                      minsize=2,
                                                      maxsize=5)
            assert pool is fake_pool
            assert instance._pool is fake_pool
Example #6
0
 def fake_instance(self):
     with patch('aioredlock.redis.Instance._create_redis_pool') as \
             create_redis_pool:
         fake_pool = FakePool()
         create_redis_pool.return_value = fake_pool
         instance = Instance(('localhost', 6379))
         yield instance
Example #7
0
    def test_initialization(self):

        instance = Instance(('localhost', 6379))

        assert instance.connection == ('localhost', 6379)
        assert instance._pool is None
        assert isinstance(instance._lock, asyncio.Lock)
Example #8
0
    async def test_connect_pool_aioredis_instance(self):
        with patch('aioredlock.redis.Instance._create_redis_pool') as \
                create_redis_pool:
            redis_connection = await aioredis.create_redis_pool(
                'redis://localhost')
            instance = Instance(redis_connection)

            await instance.connect()
            assert not create_redis_pool.called
Example #9
0
    def test_initialization(self):

        instance = Instance(('localhost', 6379))

        assert instance.connection == ('localhost', 6379)
        assert instance._pool is None
        assert isinstance(instance._lock, asyncio.Lock)

        # scripts
        for name in self.script_names:
            assert getattr(instance, '%s_sha1' % name.lower()) is None
Example #10
0
    async def test_connect_pool_not_created(self):
        with patch("aioredis.create_pool") as create_pool:
            fake_pool = FakePool()
            create_pool.return_value = fake_pool
            instance = Instance('localhost', 6379)

            assert instance._pool is None
            pool = await instance.connect()

            create_pool.assert_called_once_with(('localhost', 6379), minsize=1, maxsize=100)
            assert pool is fake_pool
            assert instance._pool is fake_pool
Example #11
0
    async def test_connect_pool_aioredis_instance(self):
        def awaiter(self):
            yield from []

        pool = FakePool()
        redis_connection = aioredis.Redis(pool)
        instance = Instance(redis_connection)

        assert instance._pool is None
        await instance.connect()
        assert pool.execute.call_count == 2
        assert instance.set_lock_script_sha1 is not None
        assert instance.unset_lock_script_sha1 is not None
Example #12
0
    async def test_connect_pool_aioredis_instance(self):
        def awaiter(self):
            yield from []

        with patch('aioredlock.redis.Instance._create_redis_pool') as create_redis_pool, \
                patch.object(aioredis.Redis, '__await__', awaiter):
            redis_connection = aioredis.Redis(
                aioredis.ConnectionsPool(('127.0.0.1', 6379),
                                         minsize=1,
                                         maxsize=100), )
            instance = Instance(redis_connection)

            await instance.connect()
            assert not create_redis_pool.called
Example #13
0
    async def test_connect_pool_not_created(self):
        with patch('aioredlock.redis.Instance._create_redis_pool') as \
                create_redis_pool:
            fake_pool = FakePool()
            create_redis_pool.return_value = fake_pool
            instance = Instance('localhost', 6379)

            assert instance._pool is None
            pool = await instance.connect()

            create_redis_pool.assert_called_once_with(
                ('localhost', 6379),
                db=0, password=None,
                minsize=1, maxsize=100)
            assert pool is fake_pool
            assert instance._pool is fake_pool
Example #14
0
    async def test_connect_pool_aioredis_instance_with_sentinel(self):

        sentinel = Sentinel(('127.0.0.1', 26379), master='leader')
        pool = FakePool()
        redis_connection = aioredis.Redis(pool)
        with patch.object(sentinel, 'get_master', return_value=asyncio.Future()) as mock_redis:
            if sys.version_info < (3, 8, 0):
                mock_redis.return_value.set_result(redis_connection)
            else:
                mock_redis.return_value = redis_connection
            instance = Instance(sentinel)

            assert instance._pool is None
            await instance.connect()
        assert pool.execute.call_count == len(self.script_names)
        assert instance.set_lock_script_sha1 is not None
        assert instance.unset_lock_script_sha1 is not None
Example #15
0
    async def test_connect_pool_not_created(self, connection, address,
                                            redis_kwargs):
        with patch('aioredlock.redis.Instance._create_redis_pool') as \
                create_redis_pool:

            fake_pool = FakePool()
            create_redis_pool.side_effect = fake_create_redis_pool(fake_pool)
            instance = Instance(connection)

            assert instance._pool is None
            pool = await instance.connect()

            create_redis_pool.assert_called_once_with(address,
                                                      **redis_kwargs,
                                                      minsize=1,
                                                      maxsize=100)
            assert pool is fake_pool
            assert instance._pool is fake_pool
Example #16
0
    async def test_connect_pool_aioredis_instance_with_sentinel(self):
        def awaiter(self):
            yield from []

        with patch('aioredlock.redis.Instance._create_redis_pool') as create_redis_pool, \
                patch.object(aioredis.Redis, '__await__', awaiter):
            sentinel = Sentinel(('127.0.0.1', 26379), master='leader')
            redis_connection = aioredis.Redis(
                aioredis.ConnectionsPool(('127.0.0.1', 6379),
                                         minsize=1,
                                         maxsize=100), )
            with patch.object(sentinel,
                              'get_master',
                              return_value=asyncio.Future()) as mock_redis:
                mock_redis.return_value.set_result(redis_connection)
                instance = Instance(sentinel)

                await instance.connect()
            assert not create_redis_pool.called
Example #17
0
 def test_initialization(self):
     instance = Instance('localhost', 6379)
     assert instance.host == 'localhost'
     assert instance.port == 6379
     assert instance._pool is None
     assert isinstance(instance._lock, asyncio.Lock)