def test_ensure_will_lock_will_call_function_and_release(self, lock): called = False with distributedlock.distributedlock('ola', lock=lock): called = True assert called assert lock.acquired_called == 1 assert lock.release_called == 1
def test_with_function(self): def bar(t): time.sleep(t) return t foo = 0 with distributedlock(key='periodic_task', lock=self.lock): foo = bar(1) self.assertEqual(foo, 1)
def test_after_raise_exception_release_is_called(self, lock): myError = RuntimeError("myError") try: with distributedlock.distributedlock('ola2', lock=lock): raise myError assert False, "Never run" except Exception, e: assert e is myError assert lock.acquired_called == 1 assert lock.release_called == 1
def test_after_raise_exception_release_is_called(self, lock): myError = RuntimeError("myError") try: with distributedlock.distributedlock('ola2', lock=lock): raise myError assert False, "Never run" except Exception as e: assert e is myError assert lock.acquired_called == 1 assert lock.release_called == 1
def test_if_block_throws_an_exception_it_will_not_be_captured(self, lock, monkeypatch): myerror = RuntimeError('myerror2') def raise_exception(blocking): raise myerror monkeypatch.setattr(lock, 'acquire', raise_exception) try: with distributedlock.distributedlock('ola', lock=lock): assert False, 'Never can run' assert False, 'Never can run' except Exception, e: assert e is myerror
def test_when_memcacheclient_added_method_returns_0_raise_runtimeerror(self): class MClient(object): def add(self, key, val, timeout): return 0 try: lock = create_mlock('ola', MClient()) with distributedlock.distributedlock('ola', lock=lock): assert False assert False except RuntimeError: pass
def test_when_memcacheclient_added_method_returns_False_and_no_blocking_raise_LockNotAcquiredError(self): class MClient(object): def add(self, key, val, timeout): return False try: lock = create_mlock('ola', MClient()) with distributedlock.distributedlock('ola', lock=lock, blocking=False): assert False assert False except distributedlock.LockNotAcquiredError: pass
def test_when_memcacheclient_added_method_returns_0_raise_runtimeerror( self): class MClient(object): def add(self, key, val, timeout): return 0 try: lock = create_mlock('ola', MClient()) with distributedlock.distributedlock('ola', lock=lock): assert False assert False except RuntimeError: pass
def test_lock_not_acquired_and_noblocking_raise_exception(self, lock, monkeypatch): # no lock def never_acquire(blocking): assert not blocking return False monkeypatch.setattr(lock, 'acquire', never_acquire) try: with distributedlock.distributedlock('ola', lock=lock, blocking=False): raise RuntimeError("There is a problem!!!") assert False except distributedlock.LockNotAcquiredError: pass
def test_if_block_throws_an_exception_it_will_not_be_captured( self, lock, monkeypatch): myerror = RuntimeError('myerror2') def raise_exception(blocking): raise myerror monkeypatch.setattr(lock, 'acquire', raise_exception) try: with distributedlock.distributedlock('ola', lock=lock): assert False, 'Never can run' assert False, 'Never can run' except Exception as e: assert e is myerror
def test_when_memcacheclient_added_method_returns_False_and_no_blocking_raise_LockNotAcquiredError( self): class MClient(object): def add(self, key, val, timeout): return False try: lock = create_mlock('ola', MClient()) with distributedlock.distributedlock('ola', lock=lock, blocking=False): assert False assert False except distributedlock.LockNotAcquiredError: pass
def test_lock_not_acquired_and_noblocking_raise_exception( self, lock, monkeypatch): # no lock def never_acquire(blocking): assert not blocking return False monkeypatch.setattr(lock, 'acquire', never_acquire) try: with distributedlock.distributedlock('ola', lock=lock, blocking=False): raise RuntimeError("There is a problem!!!") assert False except distributedlock.LockNotAcquiredError: pass
def test_when_memcacheclient_added_method_returns_False_and_blocking_wait_forever(self, monkeypatch): self.counter = 3 class MClient(object): def add(selfclient, key, val, timeout): selfclient.val = val return self.counter == 0 def get(self, key): return self.val def delete(self, key): pass def sleep(i): self.counter -= 1 monkeypatch.setattr(time, 'sleep', sleep) lock = create_mlock('ola', MClient()) called = False with distributedlock.distributedlock('ola', lock=lock, blocking=True): called = True assert called
def locking_handle(self, *args, **options): with distributedlock(command_name): handle(self, *args, **options)
def NonBlockingCacheLock(key, lock=None, blocking=False, timeout=10000): if lock is None: lock = MemcachedLock(key=key, client=cache, timeout=timeout) return distributedlock(key, lock, blocking)