/** * Verify the create session call in the Leader.FinalRequestProcessor after * the session expiration. */ @Test(timeout = 20000) public void testAddSessionAfterSessionExpiry() throws Exception { ZooKeeperServer zks = setupSessionTracker(); latch = new CountDownLatch(1); zks.sessionTracker.addSession(sessionId, sessionTimeout); SessionTrackerImpl sessionTrackerImpl = (SessionTrackerImpl) zks.sessionTracker; SessionImpl sessionImpl = sessionTrackerImpl.sessionsById .get(sessionId); Assert.assertNotNull("Sessionid:" + sessionId + " doesn't exists in sessiontracker", sessionImpl); // verify the session existence Object sessionOwner = new Object(); sessionTrackerImpl.checkSession(sessionId, sessionOwner); // waiting for the session expiry latch.await(sessionTimeout * 2, TimeUnit.MILLISECONDS); // Simulating FinalRequestProcessor logic: create session request has // delayed and now reaches FinalRequestProcessor. Here the leader zk // will do sessionTracker.addSession(id, timeout) sessionTrackerImpl.addSession(sessionId, sessionTimeout); try { sessionTrackerImpl.checkSession(sessionId, sessionOwner); Assert.fail("Should throw session expiry exception " + "as the session has expired and closed"); } catch (KeeperException.SessionExpiredException e) { // expected behaviour } Assert.assertTrue("Session didn't expired", sessionImpl.isClosing()); Assert.assertFalse("Session didn't expired", sessionTrackerImpl .touchSession(sessionId, sessionTimeout)); Assert.assertEquals( "Duplicate session expiry request has been generated", 1, firstProcessor.getCountOfCloseSessionReq()); }
/** * Verify the session closure request has reached PrepRequestProcessor soon * after session expiration by the session tracker */ @Test(timeout = 20000) public void testCloseSessionRequestAfterSessionExpiry() throws Exception { ZooKeeperServer zks = setupSessionTracker(); latch = new CountDownLatch(1); zks.sessionTracker.addSession(sessionId, sessionTimeout); SessionTrackerImpl sessionTrackerImpl = (SessionTrackerImpl) zks.sessionTracker; SessionImpl sessionImpl = sessionTrackerImpl.sessionsById .get(sessionId); Assert.assertNotNull("Sessionid:" + sessionId + " doesn't exists in sessiontracker", sessionImpl); // verify the session existence Object sessionOwner = new Object(); sessionTrackerImpl.checkSession(sessionId, sessionOwner); // waiting for the session expiry latch.await(sessionTimeout * 2, TimeUnit.MILLISECONDS); // Simulating close session request: removeSession() will be executed // while OpCode.closeSession sessionTrackerImpl.removeSession(sessionId); SessionImpl actualSession = sessionTrackerImpl.sessionsById .get(sessionId); Assert.assertNull("Session:" + sessionId + " still exists after removal", actualSession); }