Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
    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
Ejemplo n.º 10
0
    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
Ejemplo n.º 11
0
    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
Ejemplo n.º 12
0
    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
Ejemplo n.º 13
0
    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
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
 def locking_handle(self, *args, **options):
     with distributedlock(command_name):
         handle(self, *args, **options)
Ejemplo n.º 16
0
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)
Ejemplo n.º 17
0
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)