def test_acquire_wait_until_host_id_is_acquired(fake_sanlock): sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) # Starts async host id acquire... sl.acquireHostId(HOST_ID, async=True) def monitor(): # Simulate the domain monitor checking if host id was acquire every 10 # seconds... for i in range(3): sl.hasHostId(HOST_ID) time.sleep(0.3) fake_sanlock.complete_async(LS_NAME) # Discover that host id was acquired, and wake up threads waiting on # acquire(). sl.hasHostId(HOST_ID) t = concurrent.thread(monitor) t.start() try: # Acquire should wait until host id acquire is completed. sl.acquire(HOST_ID, LEASE) res = fake_sanlock.read_resource(LEASE.path, LEASE.offset) finally: t.join() assert res["acquired"]
def test_inquire_lease(fake_sanlock): sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) sl.acquireHostId(HOST_ID, async=False) sl.acquire(HOST_ID, LEASE) version, owner = sl.inquire(LEASE) assert version == 0 assert owner == HOST_ID
def test_release(fake_sanlock): sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) sl.acquireHostId(HOST_ID, async=False) sl.acquire(HOST_ID, LEASE) sl.release(LEASE) res = fake_sanlock.read_resource(LEASE.path, LEASE.offset) assert not res["acquired"]
def test_acquire_after_inq_lockspace_failure(fake_sanlock): sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) # Starts async host id acquire... sl.acquireHostId(HOST_ID, async=True) def monitor(): time.sleep(0.3) # Simulate failing hasHostId... fake_sanlock.errors["inq_lockspace"] = fake_sanlock.SanlockException(1) try: sl.hasHostId(HOST_ID) except fake_sanlock.SanlockException: pass time.sleep(0.3) # Make the next try successful fake_sanlock.complete_async(LS_NAME) del fake_sanlock.errors["inq_lockspace"] sl.hasHostId(HOST_ID) t = concurrent.thread(monitor) t.start() try: # Acquire should wait until host id acquire is completed. sl.acquire(HOST_ID, LEASE) res = fake_sanlock.read_resource(LEASE.path, LEASE.offset) finally: t.join() assert res["acquired"]
def test_acquire_timeout_waiting_for_host_id(fake_sanlock, monkeypatch): # Make this test fast monkeypatch.setattr(clusterlock.SANLock, "ACQUIRE_HOST_ID_TIMEOUT", 0.0) sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) # Starts async host id acquire that will never complete... sl.acquireHostId(HOST_ID, async=True) # Acquire should time out pytest.raises(se.AcquireHostIdFailure, sl.acquire, HOST_ID, LEASE)
def test_inquire_owner_status(fake_sanlock, status, expected_owner_id): sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) sl.acquireHostId(HOST_ID, async=False) sl.acquire(HOST_ID, LEASE) # we are simulating another host inquiring the lease fake_sanlock.hosts[HOST_ID]["flags"] = status version, owner = sl.inquire(LEASE) assert version == 0 assert owner == expected_owner_id
def test_inquire_owner_reconnected(fake_sanlock): sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) # This simulates a host reconnecting to the lockspace. # The lease should have no owner since the generation # increases each time a host reconnects to the lockspace sl.acquireHostId(HOST_ID, async=False) sl.acquire(HOST_ID, LEASE) sl.releaseHostId(HOST_ID, async=False, unused=True) sl.acquireHostId(HOST_ID, async=False) version, owner = sl.inquire(LEASE) assert version == 0 assert owner is None
def test_inquire_smaller_host_generation(fake_sanlock): sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) sl.acquireHostId(HOST_ID, async=False) sl.releaseHostId(HOST_ID, async=False, unused=True) sl.acquireHostId(HOST_ID, async=False) sl.acquire(HOST_ID, LEASE) # Setting the host generation to be smaller than the # generation on the lease (an invalid state), the # lease should have no owner fake_sanlock.hosts[HOST_ID]["generation"] = 0 version, owner = sl.inquire(LEASE) assert version == 0 assert owner is None
def lock(monkeypatch): # Reset class attributes to keep tests isolated. monkeypatch.setattr(clusterlock.SANLock, "_process_fd", None) monkeypatch.setattr(clusterlock.SANLock, "_lease_count", 0) # Monkeypatch clusterlock.panic() to allow testing panic without killing # the tests process. def fake_panic(msg): raise FakePanic(msg) monkeypatch.setattr(clusterlock, "panic", fake_panic) # Create new sanlock instance. sanlock = clusterlock.SANLock(LS_NAME.decode("utf-8"), LS_PATH, LEASE) sanlock.initLock(LEASE) return sanlock
def lock(): sanlock = clusterlock.SANLock(LS_NAME.decode("utf-8"), LS_PATH, LEASE) sanlock.initLock(LEASE) return sanlock
def test_release_host_id_async(fake_sanlock): sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) sl.acquireHostId(HOST_ID, async=False) sl.releaseHostId(HOST_ID, async=True, unused=False) acquired = fake_sanlock.inq_lockspace(LS_NAME, HOST_ID, LS_PATH, LS_OFF) assert acquired is None
def test_acquire_after_relases_host_id(fake_sanlock): sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) sl.acquireHostId(HOST_ID, async=False) sl.releaseHostId(HOST_ID, async=False, unused=False) pytest.raises(concurrent.InvalidEvent, sl.acquire, HOST_ID, LEASE)
def test_inquire_lease_has_no_owner(fake_sanlock): sl = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) sl.acquireHostId(HOST_ID, async=False) version, owner = sl.inquire(LEASE) assert version is None assert owner is None
def lock(): sanlock = clusterlock.SANLock(LS_NAME, LS_PATH, LEASE) sanlock.initLock(LEASE) return sanlock