def test_lease_wallet_on_lease_result_empty(): lease_wallet = LeaseWallet() assert None == lease_wallet.get_lease_state(resource='A') lease_use_result = _create_lease_use_result( LeaseProto.LeaseUseResult.STATUS_OK, _create_lease('A', 'B', [1, 0])) lease_wallet.on_lease_use_result(lease_use_result, resource='A') assert None == lease_wallet.get_lease_state(resource='A')
def test_lease_wallet_on_lease_result_ok(): lease_wallet = LeaseWallet() lease = _create_lease('A', 'epoch', [1]) lease_wallet.add(lease) # Assert that initial state of adding a lease looks fine. lease_state = lease_wallet.get_lease_state(resource='A') assert None != lease_state assert Lease.CompareResult.SAME == lease.compare(lease_state.lease_original) assert Lease.CompareResult.SUPER_LEASE == lease.compare(lease_state.lease_current) assert LeaseState.STATUS_SELF_OWNER == lease_state.lease_status # When an "OK" lease_use_result comes, LeaseState should not change. lease_use_result = _create_lease_use_result(LeaseProto.LeaseUseResult.STATUS_OK, lease_state.lease_current, None) lease_wallet.on_lease_use_result(lease_use_result, resource='A') new_lease_state = lease_wallet.get_lease_state(resource='A') assert None != new_lease_state assert Lease.CompareResult.SAME == lease.compare(new_lease_state.lease_original) assert Lease.CompareResult.SUPER_LEASE == lease.compare(new_lease_state.lease_current) assert LeaseState.STATUS_SELF_OWNER == new_lease_state.lease_status # When an "OK" lease_use_result arrives for a lease other than current one, LeaseState also should not change lease_wallet.advance(resource='A') lease_wallet.on_lease_use_result(lease_use_result, resource='A') newer_lease_state = lease_wallet.get_lease_state(resource='A') assert None != newer_lease_state assert Lease.CompareResult.SAME == lease.compare(newer_lease_state.lease_original) assert Lease.CompareResult.SUPER_LEASE == lease.compare(newer_lease_state.lease_current) assert Lease.CompareResult.OLDER == new_lease_state.lease_current.compare( newer_lease_state.lease_current) assert LeaseState.STATUS_SELF_OWNER == newer_lease_state.lease_status
def test_lease_wallet_normal_operation(): lease_wallet = LeaseWallet() lease = _create_lease(LLAMA, MESO, SEQ) lease_wallet.add(lease) active_lease = lease_wallet.advance(resource=LLAMA) assert Lease.CompareResult.SUPER_LEASE == lease.compare(active_lease) another_lease = lease_wallet.advance(resource=LLAMA) assert Lease.CompareResult.SUPER_LEASE == lease.compare(another_lease) assert Lease.CompareResult.OLDER == active_lease.compare(another_lease) lease_wallet.remove(lease)
def test_lease_keep_alive_empty_wallet(): # There should be no calls to RetainLease if the wallet is empty lease_wallet = LeaseWallet() lease_client = MockLeaseClient(lease_wallet) max_loops = MaxKeepAliveLoops(3) keep_alive = LeaseKeepAlive(lease_client, resource='A', rpc_interval_seconds=.1, keep_running_cb=max_loops) keep_alive.wait_until_done() assert 3 == max_loops.cur_loops assert 0 == lease_client.retain_lease_calls
def test_lease_wallet_on_lease_result_empty(): lease_wallet = LeaseWallet() with pytest.raises(NoSuchLease) as excinfo: lease_wallet.get_lease_state(resource='A') assert 'No lease for resource "A"' == str(excinfo.value) lease_use_result = _create_lease_use_result( LeaseProto.LeaseUseResult.STATUS_OK, _create_lease('A', 'B', [1, 0])) lease_wallet.on_lease_use_result(lease_use_result, resource='A') with pytest.raises(NoSuchLease) as excinfo: lease_wallet.get_lease_state(resource='A') assert 'No lease for resource "A"' == str(excinfo.value)
def test_lease_wallet_multithreaded(): lease_wallet = LeaseWallet() lease_A = _create_lease('A', 'epoch', [1]) lease_wallet.add(lease_A) lease_B = _create_lease('B', 'epoch', [3, 1]) lease_wallet.add(lease_B) threads = [LeaseWalletThread(lease_wallet) for i in range(10)] for thread in threads: thread.start() time.sleep(5) for thread in threads: thread.stop()
def test_lease_keep_alive_shutdown(): # Tests that shutdown will stop the loop. lease_wallet = LeaseWallet() lease_client = MockLeaseClient(lease_wallet) keep_alive = LeaseKeepAlive(lease_client, resource='A', rpc_interval_seconds=.1) assert keep_alive.is_alive() time.sleep(.5) assert keep_alive.is_alive() keep_alive.shutdown() assert not keep_alive.is_alive() # A second shutdown should also work, even if it is a no-op. keep_alive.shutdown() assert not keep_alive.is_alive()
def test_lease_keep_alive_filled_wallet(): # Ensure that there are expected retain_lease calls when # the LeaseWallet has a resource. lease_wallet = LeaseWallet() lease_A = _create_lease('A', 'epoch', [1]) lease_wallet.add(lease_A) lease_client = MockLeaseClient(lease_wallet) max_loops = MaxKeepAliveLoops(3) keep_alive = LeaseKeepAlive(lease_client, resource='A', rpc_interval_seconds=.1, keep_running_cb=max_loops) keep_alive.wait_until_done() assert 3 == max_loops.cur_loops assert 3 == lease_client.retain_lease_calls assert None != lease_wallet.get_lease('A')
def test_lease_keep_alive_lease_use_result_error(): # Ensure that an exception thrown when calling retain_lease # will not take down the loop. lease_wallet = LeaseWallet() lease_A = _create_lease('A', 'epoch', [1]) lease_wallet.add(lease_A) lease_client = MockLeaseClient(lease_wallet, error_on_call_N=6) max_loops = MaxKeepAliveLoops(10) keep_alive = LeaseKeepAlive(lease_client, resource='A', rpc_interval_seconds=.1, keep_running_cb=max_loops) keep_alive.wait_until_done() assert 10 == max_loops.cur_loops assert 6 == lease_client.retain_lease_calls assert None == lease_wallet.get_lease('A')
def test_lease_wallet_on_lease_result_older(): lease_wallet = LeaseWallet() lease = _create_lease('A', 'epoch', [1]) lease_wallet.add(lease) # Assert that initial state of adding a lease looks fine. lease_state = lease_wallet.get_lease_state(resource='A') assert None != lease_state assert Lease.CompareResult.SAME == lease.compare( lease_state.lease_original) assert Lease.CompareResult.SUPER_LEASE == lease.compare( lease_state.lease_current) assert LeaseState.Status.SELF_OWNER == lease_state.lease_status stale_lease = lease_state.lease_current # Advance the Lease so we can compare newer and older leases recent_lease = lease_wallet.advance(resource='A') # When an "OLDER" result comes in for an attempt which is not the current lease, # do not change the current lease state lease_use_result = _create_lease_use_result( LeaseProto.LeaseUseResult.STATUS_OLDER, stale_lease, None) lease_wallet.on_lease_use_result(lease_use_result, resource='A') new_lease_state = lease_wallet.get_lease_state(resource='A') assert None != new_lease_state assert Lease.CompareResult.SAME == lease.compare( new_lease_state.lease_original) assert Lease.CompareResult.SUPER_LEASE == lease.compare( new_lease_state.lease_current) assert LeaseState.Status.SELF_OWNER == new_lease_state.lease_status # When an "OLDER" result comes in for an attempt which is the current lease, # the lease state should change to other owner. lease_use_result = _create_lease_use_result( LeaseProto.LeaseUseResult.STATUS_OLDER, recent_lease, None) lease_wallet.on_lease_use_result(lease_use_result, resource='A') newer_lease_state = lease_wallet.get_lease_state(resource='A') assert None != newer_lease_state assert None == newer_lease_state.lease_current assert None == newer_lease_state.lease_original assert LeaseState.Status.OTHER_OWNER == newer_lease_state.lease_status with pytest.raises(LeaseNotOwnedByWallet) as excinfo: lease_wallet.get_lease(resource='A') assert 'Lease on "A" has state ({})'.format( LeaseState.Status.OTHER_OWNER) == str(excinfo.value)
def test_lease_keep_alive_lease_use_result_error(): # Ensure that an exception thrown when calling retain_lease # will not take down the loop. lease_wallet = LeaseWallet() lease_A = _create_lease('A', 'epoch', [1]) lease_wallet.add(lease_A) lease_client = MockLeaseClient(lease_wallet, error_on_call_N=6) max_loops = MaxKeepAliveLoops(10) keep_alive = LeaseKeepAlive(lease_client, resource='A', rpc_interval_seconds=.1, keep_running_cb=max_loops) keep_alive.wait_until_done() assert 10 == max_loops.cur_loops assert 6 == lease_client.retain_lease_calls with pytest.raises(NoSuchLease) as excinfo: lease_wallet.get_lease('A') # The exception should be clear when translated to text. assert 'No lease for resource "A"' == str(excinfo.value)
def test_lease_wallet_on_lease_result_wrong_epoch(): lease_wallet = LeaseWallet() lease = _create_lease('A', 'epoch', [1]) lease_wallet.add(lease) # Assert that initial state of adding a lease looks fine. lease_state = lease_wallet.get_lease_state(resource='A') assert None != lease_state assert Lease.CompareResult.SAME == lease.compare( lease_state.lease_original) assert Lease.CompareResult.SUPER_LEASE == lease.compare( lease_state.lease_current) assert LeaseState.STATUS_SELF_OWNER == lease_state.lease_status stale_lease = lease_state.lease_current # Advance the Lease so we can compare newer and older leases recent_lease = lease_wallet.advance(resource='A') # When a "REVOKED" result comes in for an attempt which is not the current lease, # do not change the current lease state lease_use_result = _create_lease_use_result( LeaseProto.LeaseUseResult.STATUS_WRONG_EPOCH, stale_lease, None) lease_wallet.on_lease_use_result(lease_use_result, resource='A') new_lease_state = lease_wallet.get_lease_state(resource='A') assert None != new_lease_state assert Lease.CompareResult.SAME == lease.compare( new_lease_state.lease_original) assert Lease.CompareResult.SUPER_LEASE == lease.compare( new_lease_state.lease_current) assert LeaseState.STATUS_SELF_OWNER == new_lease_state.lease_status # When an "REVOKED" result comes in for an attempt which is the current lease, # the lease state should change to revoked status lease_use_result = _create_lease_use_result( LeaseProto.LeaseUseResult.STATUS_WRONG_EPOCH, recent_lease, None) lease_wallet.on_lease_use_result(lease_use_result, resource='A') newer_lease_state = lease_wallet.get_lease_state(resource='A') assert None != newer_lease_state assert None == newer_lease_state.lease_current assert None == newer_lease_state.lease_original assert LeaseState.STATUS_UNOWNED == newer_lease_state.lease_status
def test_lease_wallet_constructor(): lease_wallet = LeaseWallet()