def test_acquire_already_taken_lock_timeout(self): if not self.is_set_up: return name = rand_string() default_ns = rand_string() lock_manager = LockManager(self.backend_type, default_ns) lock1 = lock_manager.acquire(name, ttl=2) self.assertEquals(lock1.acquired, True) # This raises LockTimeout because we wait for 1 second only and the lock above has ttl of 2 seconds. try: lock_manager.acquire(name, block=1) except LockTimeout, e: expected = 'Could not obtain lock for `{}` `{}` within 1s'.format(default_ns, name) self.assertEquals(e.args[0], expected)
def test_acquire_already_taken_auto_release(self): if not self.is_set_up: return name = rand_string() default_ns = rand_string() lock_manager = LockManager(self.backend_type, default_ns) lock1 = lock_manager.acquire(name, ttl=1) self.assertEquals(lock1.acquired, True) # This will not obtain the lock because it's just been taken above .. lock2 = lock_manager.acquire(name, block=False) self.assertEquals(lock2.acquired, False) # .. however, if we wait a moment we will get it because the original will have expired. sleep(2) lock3 = lock_manager.acquire(name) self.assertEquals(lock3.acquired, True)
def test_acquire_already_taken_manual_release(self): if not self.is_set_up: return name = rand_string() default_ns = rand_string() lock_manager = LockManager(self.backend_type, default_ns) lock1 = lock_manager.acquire(name, ttl=10) self.assertEquals(lock1.acquired, True) # This will not obtain the lock because it's just been taken above .. lock2 = lock_manager.acquire(name, block=False) self.assertEquals(lock2.acquired, False) # .. however, if we release the lock manually it will become available straightaway. lock1.release() lock3 = lock_manager.acquire(name) self.assertEquals(lock3.acquired, True)