def __enter__(self, *args, **kwargs): acquired = self.acquire(*args, **kwargs) if not acquired: msg = u'Acquiring lock %s failed' % self.name raise coordination.LockAcquireFailed(msg) return self
def __enter__(self, blocking=True): acquired = self.acquire(blocking) if not acquired: msg = u'Acquiring lock %s failed' % self.name raise coordination.LockAcquireFailed(msg) return self
def grab_lock_from_pool(): name = next(names) # NOTE(pas-ha) currently all tooz backends support locking API. # In case this changes, this should be wrapped to not respin # lock grabbing on NotImplemented exception. lock = self.coordinator.get_lock(name.encode()) locked = lock.acquire(blocking=False) if not locked: raise coordination.LockAcquireFailed( "Failed to acquire lock %s" % name) return lock
def test_lock_contextmanager_fail(self, log_mock, wait_mock, stop_mock): coord = mock.Mock() lock_mock = mock.Mock() coord.get_lock.return_value = lock_mock lock_mock.acquire.side_effect = coordination.LockAcquireFailed('SPAM!') def test_call(): with ngs_lock.PoolLock(coord, locks_pool_size=2, timeout=1): pass self.assertRaises(coordination.LockAcquireFailed, test_call) log_mock.assert_called_once_with(mock.ANY, exc_info=True) lock_mock.release.assert_not_called() stop_mock.assert_called_once_with(1)