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
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)
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()
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
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'
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
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
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
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