def test_logged_lock(): lock = LoggedRLock("test", lease_expiration=1, log_interval=.2) step1 = threading.Event() step2 = threading.Event() def do_lock(): with lock: step1.set() step2.wait() with concurrent(do_lock): # wait for thread to hold the lock step1.wait() # we'll mock the logger so we can ensure it logged with patch("easypy.sync._logger") as _logger: assert not lock.acquire(timeout=0.5) # below the lease_expiration # the expiration mechanism should kick in with pytest.raises(LockLeaseExpired): lock.acquire() # let other thread finish step2.set() with lock: pass assert sum(c == call("%s - waiting...", lock) for c in _logger.debug.call_args_list) > 3
def disable_test_logged_lock_races(): lease_expiration = 1 lock = LoggedRLock("test", lease_expiration=lease_expiration, log_interval=.1) import logging def do_lock(idx): sleep(random.random()) if lock.acquire(timeout=1, blocking=random.random() > 0.5): logging.info("%02d: acquired", idx) sleep(random.random() * lease_expiration * 0.9) lock.release() logging.info("%02d: released", idx) else: logging.info("%02d: timed out", idx) with ExitStack() as stack: for i in range(30): stack.enter_context(concurrent(do_lock, idx=i, loop=True, sleep=0)) sleep(5)