def test_lock_on_session_expired_error(self): # Session expired error comes from the server on lock request, retries and gets valid fence self.prepare_acquire_session(1) self.mock_request_lock(2, SessionExpiredError()) self.assertEqual(2, self.proxy.lock()) self.assert_call_counts(2, 0, 1) self.assert_lock_session_id(1)
def test_try_lock_on_session_expired_error_when_not_timed_out(self): # Session expired error comes from the server on lock request, # client retries due to not reaching timeout and succeeds self.prepare_acquire_session(1) self.mock_request_try_lock(2, SessionExpiredError()) self.assertEqual(2, self.proxy.try_lock(100)) self.assert_call_counts(2, 0, 1) self.assert_lock_session_id(1)
def test_try_lock_on_session_expired_error(self): # Session expired error comes from the server on lock request, # client determines the timeout and returns invalid fence self.prepare_acquire_session(1) self.mock_request_try_lock(2, SessionExpiredError()) self.assertEqual(FencedLock.INVALID_FENCE, self.proxy.try_lock()) self.assert_call_counts(1, 0, 1) self.assert_no_lock_session_id()
def test_try_acquire_on_session_expired_error_when_not_timed_out(self): # Session expired error comes from the server on acquire request, # retries and succeeds self.prepare_acquire_session(123) self.mock_request_acquire(True, SessionExpiredError()) self.assertTrue(self.semaphore.try_acquire(15, 3)) self.assert_call_counts(2, 0, 1) self.assert_acquire_count(15)
def test_try_acquire_on_session_expired_error(self): # Session expired error comes from the server on acquire request, # determines the timeout self.prepare_acquire_session(1) self.mock_request_acquire(True, SessionExpiredError()) self.assertFalse(self.semaphore.try_acquire()) self.assert_call_counts(1, 0, 1) self.assert_acquire_count(1)
def test_acquire_on_session_expired_error(self): # Session expired error comes from the server on acquire request, # retries and succeeds self.prepare_acquire_session(1) self.mock_request_acquire(True, SessionExpiredError()) self.assertIsNone(self.semaphore.acquire()) self.assert_call_counts(2, 0, 1) self.assert_acquire_count(1)
def test_drain_on_session_expired_error(self): # Session expired error comes from the server on drain request, # retries and succeeds self.prepare_acquire_session(99) self.mock_request_drain(101, SessionExpiredError()) self.assertEqual(101, self.semaphore.drain_permits()) self.assert_call_counts(2, 1, 1) self.assert_acquire_count(DRAIN_SESSION_ACQ_COUNT) self.assert_release_count(99, DRAIN_SESSION_ACQ_COUNT - 101)
def test_unlock_on_session_expired_error(self): # Server sends session expired error self.prepare_get_session(1) self.mock_request_unlock(None, SessionExpiredError()) with self.assertRaises(LockOwnershipLostError): self.proxy.unlock() self.assert_call_counts(0, 0, 1) self.assert_no_lock_session_id()
def test_release_on_session_expired_error(self): # Session expired error comes from the server on release request, self.prepare_get_session(99) self.mock_request_release(SessionExpiredError()) with self.assertRaises(IllegalStateError): self.semaphore.release(123) self.assert_call_counts(0, 1, 1) # Session will be invalidated before released, so release call is actually a no-op self.assert_release_count(99, 123)
def test_try_lock_on_session_expired_error_on_reentrant_lock_request(self): # Session expired error comes from the server on second lock request, # while holding a lock, should not retry self.prepare_acquire_session(1) self.prepare_lock_session_ids(1) self.mock_request_try_lock(3, SessionExpiredError()) with self.assertRaises(LockOwnershipLostError): self.proxy.try_lock() self.assert_call_counts(1, 0, 1) self.assert_no_lock_session_id()
def test_increase_permits_on_session_expired_error(self): # Session expired error comes from the server on change request self.prepare_acquire_session(99) self.mock_request_change(SessionExpiredError()) with self.assertRaises(IllegalStateError): self.semaphore.increase_permits(123) self.assert_call_counts(1, 1, 1) # Session will be invalidated before released, so release call is actually a no-op self.assert_acquire_count(1) self.assert_release_count(99, 1)
def test_heartbeat_on_failure(self): reactor = self.mock_reactor() self.mock_request_new_session() self.manager._request_heartbeat = MagicMock( return_value=ImmediateExceptionFuture(SessionExpiredError())) m = MagicMock(side_effect=self.manager.invalidate_session) self.manager.invalidate_session = m self.manager.acquire_session(self.raft_group_id, 1).result() time.sleep(2) self.manager.shutdown() reactor.shutdown() self.assertGreater(self.context.reactor.add_timer.call_count, 1) # assert that the heartbeat task is executed m.assert_called_once_with(self.raft_group_id, self.session_id) self.assertEqual(0, len(self.manager._sessions))