def test_recursive_local_lock(fake_redis): """Test if a local value can be modified by the same thread, but blocks when being accessed from more than one. """ root_prox = Proxy(path=('QualityControl', )) root_prox.clear() thread_died_yet = False def _lock_in_thread(): """The root_prox lock shoud block until it was released""" with root_prox: # This timeout here is to make sure thread_died_yet was set. time.sleep(0.05) assert thread_died_yet with root_prox: # Spin up a thread and see if it blocks as expected. thr = threading.Thread(target=_lock_in_thread) thr.start() thr.join(0.25) # This flag is needed since _lock_in_thread will (rightfully) # acquire the lock once this context manager left. thread_died_yet = True time.sleep(0.5)
def test_basic_locking(fake_redis): """Check if single process locking works""" root_prox = Proxy(path=('QualityControl', )) root_prox.clear() root_prox.acquire() assert root_prox.is_locked() root_prox.set('child', {}) root_prox.release()
def test_basic_set(fake_redis): """See if the very basic testcases work""" root_prox = Proxy(path=('TestQualityControl', )) root_prox.clear() root_prox.set('a', 2) assert root_prox.get('a').val() == 2 root_prox.set('a.b', 3) assert root_prox.get('a.b').val() == 3 assert root_prox.get('a').val() == {'b': 3} root_prox.set('a.c', None) assert root_prox.get('a.c').val() is None assert root_prox.get('a').val() == {'b': 3, 'c': None} root_prox.set('a', {'b': 42, 'e': 3}) assert root_prox.get('a').val() == {'b': 42, 'e': 3}
def test_locking_edge_cases(fake_redis): """See if locking edge cases like recursive locks work""" root_prox = Proxy(path=('QualityControl', )) root_prox.clear() assert not root_prox.is_locked() # This should work for the same thread: with root_prox: assert root_prox.is_locked() with root_prox: assert root_prox.is_locked() assert not root_prox.is_locked() # See if the lock is released when an exception happens # inside the with block. with pytest.raises(KeyError): with root_prox: raise KeyError("Inside job") assert not root_prox.is_locked()