Search in sources :

Example 21 with OperationFuture

use of com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture in project cruise-control by linkedin.

the class UserTaskManagerTest method testExpireSession.

@Test
public void testExpireSession() throws Exception {
    UUID testUserTaskId = UUID.randomUUID();
    UserTaskManager.UuidGenerator mockUuidGenerator = EasyMock.mock(UserTaskManager.UuidGenerator.class);
    EasyMock.expect(mockUuidGenerator.randomUUID()).andReturn(testUserTaskId).anyTimes();
    Time mockTime = new MockTime();
    HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
    EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(mockTime.milliseconds()).anyTimes();
    mockHttpSession.invalidate();
    HttpServletRequest mockHttpServletRequest = prepareRequest(mockHttpSession, null);
    OperationFuture future = new OperationFuture("future");
    UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6), 100, mockTime, mockUuidGenerator);
    HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
    mockHttpServletResponse.setHeader(EasyMock.anyString(), EasyMock.anyString());
    EasyMock.replay(mockUuidGenerator, mockHttpSession, mockHttpServletResponse);
    // test-case: test if the sessions are removed on expiration
    OperationFuture future1 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
    Assert.assertEquals(future, future1);
    mockTime.sleep(1001);
    Thread.sleep(TimeUnit.SECONDS.toMillis(UserTaskManager.USER_TASK_SCANNER_PERIOD_SECONDS + 1));
    OperationFuture future2 = userTaskManager.getFuture(mockHttpServletRequest);
    Assert.assertNull(future2);
    EasyMock.verify(mockUuidGenerator, mockHttpSession, mockHttpServletResponse);
    userTaskManager.close();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) Capture(org.easymock.Capture) Time(org.apache.kafka.common.utils.Time) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) Test(org.junit.Test) UUID(java.util.UUID) EasyMock(org.easymock.EasyMock) TimeUnit(java.util.concurrent.TimeUnit) MockTime(kafka.utils.MockTime) HttpServletRequest(javax.servlet.http.HttpServletRequest) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) Map(java.util.Map) GET_METHOD(com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServletUtils.GET_METHOD) Assert(org.junit.Assert) Collections(java.util.Collections) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) Time(org.apache.kafka.common.utils.Time) MockTime(kafka.utils.MockTime) UUID(java.util.UUID) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) MockTime(kafka.utils.MockTime) Test(org.junit.Test)

Aggregations

OperationFuture (com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 HashMap (java.util.HashMap)7 UUID (java.util.UUID)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 HttpSession (javax.servlet.http.HttpSession)6 MockTime (kafka.utils.MockTime)6 Test (org.junit.Test)6 GET_METHOD (com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServletUtils.GET_METHOD)5 Collections (java.util.Collections)5 Time (org.apache.kafka.common.utils.Time)5 Capture (org.easymock.Capture)5 EasyMock (org.easymock.EasyMock)5 Assert (org.junit.Assert)5 CruiseControlResponse (com.linkedin.cruisecontrol.servlet.response.CruiseControlResponse)2 ArrayList (java.util.ArrayList)2 Timer (com.codahale.metrics.Timer)1 Gson (com.google.gson.Gson)1