Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Unexpected method calls: HttpSession.invalidate #2201

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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