Skip to content

Commit 2b81fb1

Browse files
authored
Fixing Unexpected method calls: HttpSession.invalidate (#2201)
## Summary 1. Why: The test failed sometimes with unexpected method calls. 2. What: The fix is preparing the test to accept invalidate method call too ## Expected Behavior Tests are running without failure ## Actual Behavior Tests are failing sometimes with unexpected method call. ## Steps to Reproduce 1. setup repeated run on e.g. `testCreateUserTask` in IDE 2. observe failure after multiple successful runs (for me it was failing after around 250 successful runs) ## Additional evidence ``` java.lang.AssertionError: On mock #2 (zero indexed): Unexpected method calls: HttpSession.invalidate() at org.easymock.EasyMock.getAssertionError(EasyMock.java:2230) at org.easymock.EasyMock.verify(EasyMock.java:2058) at com.linkedin.kafka.cruisecontrol.servlet.UserTaskManagerTest.testCreateUserTask(UserTaskManagerTest.java:59) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:112) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:40) at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:60) at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:52) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33) at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94) at com.sun.proxy.$Proxy5.processTestClass(Unknown Source) at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:176) at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129) at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100) at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60) at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56) at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:113) at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:65) at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69) at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74) ``` ## Categorization - [x] bugfix - [ ] new feature - [ ] refactor - [ ] CVE - [ ] other
1 parent 6e45231 commit 2b81fb1

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

cruise-control/src/test/java/com/linkedin/kafka/cruisecontrol/servlet/UserTaskManagerTest.java

+15-18
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,22 @@
2525

2626
public class UserTaskManagerTest {
2727

28+
private static HttpSession getMockHttpSession(long lastAccessedTimeReturn) {
29+
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
30+
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(lastAccessedTimeReturn).anyTimes();
31+
mockHttpSession.invalidate();
32+
EasyMock.expectLastCall().andVoid().anyTimes();
33+
return mockHttpSession;
34+
}
35+
2836
@Test
2937
public void testCreateUserTask() throws Exception {
3038
UUID testUserTaskId = UUID.randomUUID();
3139

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

35-
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
36-
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
43+
HttpSession mockHttpSession = getMockHttpSession(100L);
3744

3845
HttpServletRequest mockHttpServletRequest1 = prepareServletRequest(mockHttpSession, null);
3946

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

112-
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
113-
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
119+
HttpSession mockHttpSession = getMockHttpSession(System.currentTimeMillis());
114120

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

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

164168
HttpServletRequest mockHttpServletRequest = prepareServletRequest(mockHttpSession, null);
165169

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

193197
@Test
194198
public void testCompletedTasks() throws Exception {
195-
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
196-
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
197-
mockHttpSession.invalidate();
199+
HttpSession mockHttpSession = getMockHttpSession(100L);
198200

199201
HttpServletRequest mockHttpServletRequest = prepareServletRequest(mockHttpSession, null);
200202
UserTaskManager.UuidGenerator mockUuidGenerator = EasyMock.mock(UserTaskManager.UuidGenerator.class);
@@ -234,9 +236,7 @@ public void testExpireSession() throws Exception {
234236
EasyMock.expect(mockUuidGenerator.randomUUID()).andReturn(testUserTaskId).anyTimes();
235237

236238
Time mockTime = new MockTime();
237-
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
238-
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(mockTime.milliseconds()).anyTimes();
239-
mockHttpSession.invalidate();
239+
HttpSession mockHttpSession = getMockHttpSession(100L);
240240
HttpServletRequest mockHttpServletRequest = prepareServletRequest(mockHttpSession, null);
241241

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

266266
@Test
267267
public void testMaximumActiveTasks() throws Exception {
268-
HttpSession mockHttpSession1 = EasyMock.mock(HttpSession.class);
269-
EasyMock.expect(mockHttpSession1.getLastAccessedTime()).andReturn(100L).anyTimes();
268+
HttpSession mockHttpSession1 = getMockHttpSession(100L);
270269

271270
HttpServletRequest mockHttpServletRequest1 = prepareServletRequest(mockHttpSession1, null);
272271

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

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

0 commit comments

Comments
 (0)