def test_recursive(self): lock = SHLock() lock.acquire(shared=True) lock.acquire(shared=True) self.assertEqual(lock.is_shared, 2) lock.release() lock.release() self.assertFalse(lock.is_shared) lock.acquire(shared=False) lock.acquire(shared=False) self.assertEqual(lock.is_exclusive, 2) lock.release() lock.release() self.assertFalse(lock.is_exclusive)
def test_owns_locks(self): lock = SHLock() self.assertFalse(lock.owns_lock()) lock.acquire(shared=True) self.assertTrue(lock.owns_lock()) lock.release() self.assertFalse(lock.owns_lock()) lock.acquire(shared=False) self.assertTrue(lock.owns_lock()) lock.release() self.assertFalse(lock.owns_lock()) done = [] def test(): if not lock.owns_lock(): done.append(True) lock.acquire() t = Thread(target=test) t.daemon = True t.start() t.join(1) self.assertEqual(len(done), 1) lock.release()
def test_downgrade(self): lock = SHLock() lock.acquire(shared=False) self.assertRaises(LockingError, lock.acquire, shared=True) lock.release()
def test_acquire(self): lock = SHLock() def get_lock(shared): lock.acquire(shared=shared) time.sleep(1) lock.release() shared = Thread(target=get_lock, args=(True,)) shared.daemon = True shared.start() time.sleep(0.1) self.assertTrue(lock.acquire(shared=True, blocking=False)) lock.release() self.assertFalse(lock.acquire(shared=False, blocking=False)) lock.acquire(shared=False) shared.join(1) self.assertFalse(shared.is_alive()) lock.release() self.assertTrue(lock.acquire(shared=False, blocking=False)) lock.release() exclusive = Thread(target=get_lock, args=(False,)) exclusive.daemon = True exclusive.start() time.sleep(0.1) self.assertFalse(lock.acquire(shared=False, blocking=False)) self.assertFalse(lock.acquire(shared=True, blocking=False)) lock.acquire(shared=True) exclusive.join(1) self.assertFalse(exclusive.is_alive()) lock.release() lock.acquire(shared=False) lock.release() lock.acquire(shared=True) lock.release() self.assertFalse(lock.is_shared) self.assertFalse(lock.is_exclusive)
def test_acquire(self): lock = SHLock() def get_lock(shared): lock.acquire(shared=shared) time.sleep(1) lock.release() shared = Thread(target=get_lock, args=(True, )) shared.daemon = True shared.start() time.sleep(0.1) self.assertTrue(lock.acquire(shared=True, blocking=False)) lock.release() self.assertFalse(lock.acquire(shared=False, blocking=False)) lock.acquire(shared=False) shared.join(1) self.assertFalse(shared.is_alive()) lock.release() self.assertTrue(lock.acquire(shared=False, blocking=False)) lock.release() exclusive = Thread(target=get_lock, args=(False, )) exclusive.daemon = True exclusive.start() time.sleep(0.1) self.assertFalse(lock.acquire(shared=False, blocking=False)) self.assertFalse(lock.acquire(shared=True, blocking=False)) lock.acquire(shared=True) exclusive.join(1) self.assertFalse(exclusive.is_alive()) lock.release() lock.acquire(shared=False) lock.release() lock.acquire(shared=True) lock.release() self.assertFalse(lock.is_shared) self.assertFalse(lock.is_exclusive)