Skip to content

Commit

Permalink
Fixing Unexpected method calls: HttpSession.invalidate
Browse files Browse the repository at this point in the history
Signed-off-by: Andras Katona <akatona@cloudera.com>
  • Loading branch information
akatona84 committed Sep 17, 2024
1 parent 15ad631 commit 20c79e7
Showing 1 changed file with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@

public class UserTaskManagerTest {

private static HttpSession getMockHttpSession(long lastAccessedTimeReturn) {
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(lastAccessedTimeReturn).anyTimes();
mockHttpSession.invalidate();
EasyMock.expectLastCall().andVoid().anyTimes();
return mockHttpSession;
}

@Test
public void testCreateUserTask() throws Exception {
UUID testUserTaskId = UUID.randomUUID();

UserTaskManager.UuidGenerator mockUuidGenerator = EasyMock.mock(UserTaskManager.UuidGenerator.class);
EasyMock.expect(mockUuidGenerator.randomUUID()).andReturn(testUserTaskId).anyTimes();

HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
HttpSession mockHttpSession = getMockHttpSession(100L);

HttpServletRequest mockHttpServletRequest1 = prepareServletRequest(mockHttpSession, null);

Expand Down Expand Up @@ -109,8 +116,7 @@ public void testSessionsShareUserTask() throws Exception {
UserTaskManager.UuidGenerator mockUuidGenerator = EasyMock.mock(UserTaskManager.UuidGenerator.class);
EasyMock.expect(mockUuidGenerator.randomUUID()).andReturn(testUserTaskId).anyTimes();

HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
HttpSession mockHttpSession = getMockHttpSession(System.currentTimeMillis());

Map<String, String []> requestParams1 = new HashMap<>();
requestParams1.put("param", new String[]{"true"});
Expand Down Expand Up @@ -157,9 +163,7 @@ public void testAddStepsFutures() throws Exception {
UserTaskManager.UuidGenerator mockUuidGenerator = EasyMock.mock(UserTaskManager.UuidGenerator.class);
EasyMock.expect(mockUuidGenerator.randomUUID()).andReturn(testUserTaskId).anyTimes();

HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
// Change mock session's last access time to always return current time to avoid unintended recycling of session.
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(System.currentTimeMillis()).anyTimes();
HttpSession mockHttpSession = getMockHttpSession(System.currentTimeMillis());

HttpServletRequest mockHttpServletRequest = prepareServletRequest(mockHttpSession, null);

Expand Down Expand Up @@ -192,9 +196,7 @@ public void testAddStepsFutures() throws Exception {

@Test
public void testCompletedTasks() throws Exception {
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
mockHttpSession.invalidate();
HttpSession mockHttpSession = getMockHttpSession(100L);

HttpServletRequest mockHttpServletRequest = prepareServletRequest(mockHttpSession, null);
UserTaskManager.UuidGenerator mockUuidGenerator = EasyMock.mock(UserTaskManager.UuidGenerator.class);
Expand Down Expand Up @@ -234,9 +236,7 @@ public void testExpireSession() throws Exception {
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();
HttpSession mockHttpSession = getMockHttpSession(100L);
HttpServletRequest mockHttpServletRequest = prepareServletRequest(mockHttpSession, null);

OperationFuture future = new OperationFuture("future");
Expand Down Expand Up @@ -265,8 +265,7 @@ public void testExpireSession() throws Exception {

@Test
public void testMaximumActiveTasks() throws Exception {
HttpSession mockHttpSession1 = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession1.getLastAccessedTime()).andReturn(100L).anyTimes();
HttpSession mockHttpSession1 = getMockHttpSession(100L);

HttpServletRequest mockHttpServletRequest1 = prepareServletRequest(mockHttpSession1, null);

Expand All @@ -285,9 +284,7 @@ public void testMaximumActiveTasks() throws Exception {
userTaskManager.getOrCreateUserTask(requestContext, uuid -> future, 0, true, null).get(0);
Assert.assertEquals(future, future1);
EasyMock.verify(mockHttpSession1, mockHttpServletRequest1, mockHttpServletResponse);
HttpSession mockHttpSession2 = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession2.getLastAccessedTime()).andReturn(100L).anyTimes();
EasyMock.replay(mockHttpSession2);
HttpSession mockHttpSession2 = getMockHttpSession(100L);
EasyMock.reset(mockHttpServletResponse);

HttpServletRequest mockHttpServletRequest2 = prepareServletRequest(mockHttpSession2, null, "/test2", Collections.emptyMap());
Expand Down

0 comments on commit 20c79e7

Please sign in to comment.