Example #1
0
def test_conflict_resolution_called(rk):
    pytest.importorskip('riak')

    uid = next(rk)
    lock = Lock.from_dict(uid, {'identity': uid})
    lock.save()
    clear_cache()

    # manual mock like because riak_bucket disallow delattr
    # which is used by mock
    class PseudoMock(object):
        def __init__(self):
            self.call_count = 0

        def __enter__(self):
            def _manual_pseudo_mock(riak_object):
                self.call_count += 1
                assert len(riak_object.siblings) == 2
                return Lock.bucket._orig_resolver(riak_object)

            Lock.bucket._orig_resolver = Lock.bucket.resolver
            Lock.bucket.resolver = _manual_pseudo_mock
            return self

        def __exit__(self, *exc_info):
            Lock.bucket.resolver = Lock.bucket._orig_resolver
            del Lock.bucket._orig_resolver
            return False

    with PseudoMock() as m:
        lock1 = Lock.from_dict(uid, {'identity': uid})
        lock1.save()
        assert m.call_count == 1
Example #2
0
def test_conflict_resolution_called(rk):
    pytest.importorskip('riak')

    uid = next(rk)
    lock = Lock.from_dict(uid, {'identity': uid})
    lock.save()
    clear_cache()

    # manual mock like because riak_bucket disallow delattr
    # which is used by mock
    class PseudoMock(object):

        def __init__(self):
            self.call_count = 0

        def __enter__(self):
            def _manual_pseudo_mock(riak_object):
                self.call_count += 1
                assert len(riak_object.siblings) == 2
                return Lock.bucket._orig_resolver(riak_object)
            Lock.bucket._orig_resolver = Lock.bucket.resolver
            Lock.bucket.resolver = _manual_pseudo_mock
            return self

        def __exit__(self, *exc_info):
            Lock.bucket.resolver = Lock.bucket._orig_resolver
            del Lock.bucket._orig_resolver
            return False

    with PseudoMock() as m:
        lock1 = Lock.from_dict(uid, {'identity': uid})
        lock1.save()
        assert m.call_count == 1
Example #3
0
def test_non_unique_key(rk):
    peewee = pytest.importorskip('peewee')

    uid = next(rk)
    lock = Lock.from_dict(uid, {'identity': '1'})
    lock.save(force_insert=True)
    clear_cache()
    lock1 = Lock.from_dict(uid, {'identity': '2'})
    with pytest.raises(peewee.IntegrityError):
        lock1.save(force_insert=True)
Example #4
0
def test_non_unique_key(rk):
    peewee = pytest.importorskip('peewee')

    uid = next(rk)
    lock = Lock.from_dict(uid, {'identity': '1'})
    lock.save(force_insert=True)
    clear_cache()
    lock1 = Lock.from_dict(uid, {'identity': '2'})
    with pytest.raises(peewee.IntegrityError):
        lock1.save(force_insert=True)
Example #5
0
def test_raise_riak_error_on_incorrect_update(rk):
    riak = pytest.importorskip('riak')

    uid = next(rk)
    lock = Lock.from_dict(uid, {'identity': uid})
    lock.save()
    clear_cache()

    with pytest.raises(riak.RiakError):
        lock1 = Lock.from_dict(uid, {'identity': uid})
        lock1.save()
Example #6
0
def test_raise_riak_error_on_incorrect_update(rk):
    riak = pytest.importorskip('riak')

    uid = next(rk)
    lock = Lock.from_dict(uid, {'identity': uid})
    lock.save()
    clear_cache()

    with pytest.raises(riak.RiakError):
        lock1 = Lock.from_dict(uid, {'identity': uid})
        lock1.save()
Example #7
0
def test_return_siblings_on_write(rk):
    pytest.importorskip('riak')

    uid = next(rk)
    lock = Lock.from_dict(uid, {'identity': uid})
    lock.save()
    clear_cache()

    with pytest.raises(SiblingsError):
        lock1 = Lock.from_dict(uid, {'identity': uid})
        lock1.save()
    s1, s2 = lock1._riak_object.siblings
    assert s1.data == s2.data
Example #8
0
 def _acquire(cls, uid, identity, stamp):
     try:
         log.debug('Create lock UID %s for %s', uid, identity)
         lk = DBLock.from_dict(uid, {'identity': identity})
         lk.save(force=True)
         return lk
     except RiakError as exc:
         # TODO object shouldnt be cached before successful save
         del DBLock._c.obj_cache[lk.key]
         # check documentation for error message
         # http://docs.basho.com/riak/latest/dev/advanced/strong-consistency/#Error-Messages
         if 'failed' in str(exc):
             lk = DBLock.get(uid)
             log.debug('Lock %s already acquired by %s', uid, lk.identity)
             return lk
         raise
Example #9
0
 def _acquire(cls, uid, identity, stamp):
     try:
         log.debug(
             'Create lock UID %s for %s', uid, identity)
         lk = DBLock.from_dict(uid, {'identity': identity})
         lk.save(force=True)
         return lk
     except RiakError as exc:
         # TODO object shouldnt be cached before successful save
         del DBLock._c.obj_cache[lk.key]
         # check documentation for error message
         # http://docs.basho.com/riak/latest/dev/advanced/strong-consistency/#Error-Messages
         if 'failed' in str(exc):
             lk = DBLock.get(uid)
             log.debug('Lock %s already acquired by %s', uid, lk.identity)
             return lk
         raise
Example #10
0
def test_locker_logic():
    uid = '11'
    l = DBLock.from_dict(uid, {})

    l.lockers = [['a', -1, 'x'], ['a', 1, 'y'], ['b', 1, 'z']]
    l.reduce()
    assert l.am_i_locking('b')
    l.who_is_locking() == 'b'
Example #11
0
def test_locker_logic():
    uid = '11'
    l = DBLock.from_dict(uid, {})

    l.lockers = [['a', -1, 'x'], ['a', 1, 'y'], ['b', 1, 'z']]
    l.reduce()
    assert l.am_i_locking('b')
    l.who_is_locking() == 'b'
Example #12
0
 def _acquire(cls, uid, identity, stamp):
     try:
         del DBLock._c.obj_cache[uid]
     except KeyError:
         pass
     _check = True
     try:
         lk = DBLock.get(uid)
     except DBLayerNotFound:
         log.debug(
             'Create new lock UID %s for %s', uid, identity)
         lk = DBLock.from_dict(uid, {})
         lk.change_locking_state(identity, 1, stamp)
         lk.save(force=True)
         if len(lk.sum_all().keys()) != 1:
             # concurrent create
             lk.change_locking_state(identity, -1, stamp)
             lk.save(force=True)
             log.debug("Concurrent lock %s create", uid)
         else:
             _check = False
     if _check:
         locking = lk.who_is_locking()
         if locking is not None:
             log.debug(
                 'Found lock with UID %s, owned by %s,'
                 ' owner %r, lockers %s',
                 uid, locking, lk.am_i_locking(identity), lk.lockers)
             return lk
         else:
             log.debug(
                 'Create lock UID %s for %s', uid, identity)
             lk.change_locking_state(identity, 1, stamp)
             lk.save(force=True)
     summed = lk.sum_all()
     if len(summed.keys()) != 1:
         log.debug("More than one acquire")
         if identity in summed:
             lk.change_locking_state(identity, -1, stamp)
             lk.save(force=True)
             log.debug("I may be not locking, so removing me %s", identity)
     return lk
Example #13
0
 def _acquire(cls, uid, identity, stamp):
     try:
         del DBLock._c.obj_cache[uid]
     except KeyError:
         pass
     _check = True
     try:
         lk = DBLock.get(uid)
     except DBLayerNotFound:
         log.debug('Create new lock UID %s for %s', uid, identity)
         lk = DBLock.from_dict(uid, {})
         lk.change_locking_state(identity, 1, stamp)
         lk.save(force=True)
         if len(lk.sum_all().keys()) != 1:
             # concurrent create
             lk.change_locking_state(identity, -1, stamp)
             lk.save(force=True)
             log.debug("Concurrent lock %s create", uid)
         else:
             _check = False
     if _check:
         locking = lk.who_is_locking()
         if locking is not None:
             log.debug(
                 'Found lock with UID %s, owned by %s,'
                 ' owner %r, lockers %s', uid, locking,
                 lk.am_i_locking(identity), lk.lockers)
             return lk
         else:
             log.debug('Create lock UID %s for %s', uid, identity)
             lk.change_locking_state(identity, 1, stamp)
             lk.save(force=True)
     summed = lk.sum_all()
     if len(summed.keys()) != 1:
         log.debug("More than one acquire")
         if identity in summed:
             lk.change_locking_state(identity, -1, stamp)
             lk.save(force=True)
             log.debug("I may be not locking, so removing me %s", identity)
     return lk
Example #14
0
 def _release(cls, uid, identity, stamp):
     log.debug("Release lock %s with %s", uid, identity)
     lk = DBLock.get(uid)
     lk.change_locking_state(identity, -1, stamp)
     lk.save(force=True)
Example #15
0
 def _release(cls, uid, identity):
     lk = DBLock.get(uid)
     log.debug('Release lock %s with %s', uid, identity)
     lk.delete()
Example #16
0
 def _release(cls, uid, identity, stamp):
     log.debug("Release lock %s with %s", uid, identity)
     lk = DBLock.get(uid)
     lk.change_locking_state(identity, -1, stamp)
     lk.save(force=True)
Example #17
0
 def _release(cls, uid, identity):
     lk = DBLock.get(uid)
     log.debug('Release lock %s with %s', uid, identity)
     lk.delete()