Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_aioredlock_two_locks_on_different_resources_two_instances(
self,
redis_two_connections):
await self.check_two_locks_on_different_resources(Aioredlock(redis_two_connections))
def test_default_initialization(self):
with patch("aioredlock.algorithm.Redis.__init__") as mock_redis:
mock_redis.return_value = None
lock_manager = Aioredlock()
mock_redis.assert_called_once_with(
[{'host': 'localhost', 'port': 6379}],
lock_manager.lock_timeout
)
assert lock_manager.redis
assert lock_manager.drift == pytest.approx(0.102)
def test_validator(method, exc_message):
with pytest.raises(ValueError) as exc_info:
getattr(Aioredlock, method)(None, None, -1)
assert str(exc_info.value) == exc_message
async def test_aioredlock_lock_with_first_failed_try_two_instances(
self,
redis_two_connections
):
lock_manager = Aioredlock(redis_two_connections)
resource = str(uuid.uuid4())
garbage_value = 'garbage'
first_redis = await aioredis.create_redis(
(redis_two_connections[0]['host'],
redis_two_connections[0]['port'])
)
# write garbage to resource key in first instance
await first_redis.set(resource, garbage_value)
is_garbage = True
# this patched sleep function will remove garbage from
# frist instance before second try
real_sleep = asyncio.sleep
async def test_simple_aioredlock_two_instances(
self,
redis_two_connections):
await self.check_simple_lock(Aioredlock(redis_two_connections))
async def basic_lock():
lock_manager = Aioredlock([{
'host': 'localhost',
'port': 6379,
'db': 0,
'password': None
}])
lock = await lock_manager.lock("resource")
assert lock.valid is True
# Do your stuff having the lock
await lock_manager.unlock(lock)
assert lock.valid is False
await lock_manager.destroy()
async def get_lock_manager(cls) -> aioredlock.Aioredlock:
if cls.lock_manager is not None:
return cls.lock_manager
cls.lock_manager = aioredlock.Aioredlock([await cls.get_redis()], lock_timeout=300, retry_count=3, retry_delay_min=30, retry_delay_max=90)
return cls.lock_manager
async def get_lock_manager(cls) -> aioredlock.Aioredlock:
if cls.lock_manager is not None:
return cls.lock_manager
cls.lock_manager = aioredlock.Aioredlock([await cls.get_redis()], lock_timeout=300, retry_count=3, retry_delay_min=30, retry_delay_max=90)
return cls.lock_manager
async def __ainit__(self) -> None:
log.info('Session scheduler started')
self.tick_task = asyncio.create_task(self.generate_scheduling_tick())
self.registry.event_dispatcher.consume('kernel_enqueued', None, self.schedule)
self.registry.event_dispatcher.consume('kernel_terminated', None, self.schedule)
self.registry.event_dispatcher.consume('instance_started', None, self.schedule)
self.registry.event_dispatcher.consume('do_schedule', None, self.schedule)
# TODO: add events for resource configuration changes and subscribe them here.
self.lock_manager = aioredlock.Aioredlock([
{'host': str(self.config['redis']['addr'][0]),
'port': self.config['redis']['addr'][1],
'password': self.config['redis']['password'] if self.config['redis']['password'] else None,
'db': REDIS_LIVE_DB},
])
async def __ainit__(self) -> None:
log.info('Session scheduler started')
loop = current_loop()
self.tick_task = loop.create_task(self.generate_scheduling_tick())
self.registry.event_dispatcher.consume('kernel_enqueued', None, self.schedule)
self.registry.event_dispatcher.consume('kernel_terminated', None, self.schedule)
self.registry.event_dispatcher.consume('instance_started', None, self.schedule)
# TODO: add events for resource configuration changes and subscribe them here.
self.lock_manager = aioredlock.Aioredlock([
{'host': str(self.config['redis']['addr'][0]),
'port': self.config['redis']['addr'][1],
'password': self.config['redis']['password'] if self.config['redis']['password'] else None,
'db': REDIS_LIVE_DB},
])