From ac7abcf08f5d75897662832b3d63cccc95774dbe Mon Sep 17 00:00:00 2001 From: Kyle Aure Date: Wed, 4 Oct 2023 14:54:56 -0500 Subject: [PATCH] Invalid assertion in LastExecutionTests --- .../api/LastExecution/LastExecutionTests.java | 39 ++++++------------- .../concurrent/api/Trigger/TriggerTests.java | 2 +- .../common/fixed/counter/StaticCounter.java | 6 ++- .../framework/junit/extensions/Wait.java | 19 --------- .../inheritedapi/InheritedAPITests.java | 13 +++---- .../InheritedAPIServletTests.java | 19 ++++----- .../inheritedapi/TestEjb.java | 19 ++++----- 7 files changed, 37 insertions(+), 80 deletions(-) diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/api/LastExecution/LastExecutionTests.java b/tck/src/main/java/ee/jakarta/tck/concurrent/api/LastExecution/LastExecutionTests.java index e0973d96..d7f3326a 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/api/LastExecution/LastExecutionTests.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/api/LastExecution/LastExecutionTests.java @@ -16,11 +16,8 @@ package ee.jakarta.tck.concurrent.api.LastExecution; -import static org.junit.jupiter.api.Assertions.assertEquals; - import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ScheduledFuture; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.shrinkwrap.api.ShrinkWrap; @@ -36,7 +33,6 @@ import ee.jakarta.tck.concurrent.framework.junit.anno.Common.PACKAGE; import ee.jakarta.tck.concurrent.framework.junit.anno.TestName; import ee.jakarta.tck.concurrent.framework.junit.anno.Web; -import ee.jakarta.tck.concurrent.framework.junit.extensions.Wait; import jakarta.annotation.Resource; import jakarta.enterprise.concurrent.ManagedExecutors; import jakarta.enterprise.concurrent.ManagedScheduledExecutorService; @@ -72,51 +68,40 @@ public void lastExecutionGetIdentityNameTest() { Map executionProperties = new HashMap(); executionProperties.put(ManagedTask.IDENTITY_NAME, IDENTITY_NAME_TEST_ID); - ScheduledFuture sf = scheduledExecutor.schedule( + scheduledExecutor.schedule( ManagedExecutors.managedTask(new CounterRunnableTask(), executionProperties, null), new LogicDrivenTrigger(TestConstants.pollInterval.toMillis(), testname)); - Wait.waitTillFutureIsDone(sf); - - assertEquals(LogicDrivenTrigger.RIGHT_COUNT, // expected - StaticCounter.getCount(), // actual - "Got wrong identity name. See server log for more details."); + + StaticCounter.waitTill(LogicDrivenTrigger.RIGHT_COUNT, "Got wrong identity name. See server log for more details."); } @Assertion(id = "JAVADOC:16", strategy = "Result of the last execution.") public void lastExecutionGetResultRunnableTest() { // test with runnable, LastExecution should return null - ScheduledFuture sf = scheduledExecutor.schedule( + scheduledExecutor.schedule( ManagedExecutors.managedTask(new CounterRunnableTask(), null, null), new LogicDrivenTrigger(TestConstants.pollInterval.toMillis(), testname)); - Wait.waitTillFutureIsDone(sf); - - assertEquals(LogicDrivenTrigger.RIGHT_COUNT, // expected - StaticCounter.getCount(), // actual - "Got wrong last execution result. See server log for more details."); + + StaticCounter.waitTill(LogicDrivenTrigger.RIGHT_COUNT, "Got wrong last execution result. See server log for more details."); } @Assertion(id = "JAVADOC:16", strategy = "Result of the last execution.") public void lastExecutionGetResultCallableTest() { // test with callable, LastExecution should return 1 - ScheduledFuture sf = scheduledExecutor.schedule( + scheduledExecutor.schedule( ManagedExecutors.managedTask(new CounterCallableTask(), null, null), new LogicDrivenTrigger(TestConstants.pollInterval.toMillis(), testname)); - Wait.waitTillFutureIsDone(sf); - - assertEquals(LogicDrivenTrigger.RIGHT_COUNT, // expected - StaticCounter.getCount(), // actual - "Got wrong last execution result. See server log for more details."); + + StaticCounter.waitTill(LogicDrivenTrigger.RIGHT_COUNT, "Got wrong last execution result. See server log for more details."); } @Assertion(id = "JAVADOC:17 JAVADOC:18 JAVADOC:19", strategy = "The last time in which the task was completed.") public void lastExecutionGetRunningTimeTest() { - ScheduledFuture sf = scheduledExecutor.schedule( + scheduledExecutor.schedule( ManagedExecutors.managedTask(new CounterRunnableTask(TestConstants.pollInterval), null, null), new LogicDrivenTrigger(TestConstants.pollInterval.toMillis(), testname)); - Wait.waitTillFutureIsDone(sf); - assertEquals(LogicDrivenTrigger.RIGHT_COUNT, // expected - StaticCounter.getCount(), // actual - "Got wrong last execution result."); + + StaticCounter.waitTill(LogicDrivenTrigger.RIGHT_COUNT, "Got wrong last execution result. See server log for more details."); } } diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/api/Trigger/TriggerTests.java b/tck/src/main/java/ee/jakarta/tck/concurrent/api/Trigger/TriggerTests.java index 2c41f5d8..baf2908d 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/api/Trigger/TriggerTests.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/api/Trigger/TriggerTests.java @@ -72,7 +72,7 @@ public void triggerGetNextRunTimeTest() throws Exception { Assertions.assertBetween(StaticCounter.getCount(), TestConstants.pollsPerTimeout - 2, TestConstants.pollsPerTimeout + 2); } finally { - Wait.waitTillFutureIsDone(result); + Wait.waitForTaskComplete(result); } } diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/common/fixed/counter/StaticCounter.java b/tck/src/main/java/ee/jakarta/tck/concurrent/common/fixed/counter/StaticCounter.java index 546059e6..74751ef7 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/common/fixed/counter/StaticCounter.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/common/fixed/counter/StaticCounter.java @@ -44,11 +44,15 @@ public static void reset() { } public static void waitTill(final int expected) { + waitTill(expected, "Expected count " + expected + " within timeout."); + } + + public static void waitTill(final int expected, final String message) { assertTimeoutPreemptively(TestConstants.waitTimeout, () -> { for (; expected != StaticCounter.getCount(); Wait.sleep(TestConstants.pollInterval)) { //empty } - }); + }, message); } public static void waitTillSurpassed(final int expected) { diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/framework/junit/extensions/Wait.java b/tck/src/main/java/ee/jakarta/tck/concurrent/framework/junit/extensions/Wait.java index 35d68a17..8b75c943 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/framework/junit/extensions/Wait.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/framework/junit/extensions/Wait.java @@ -72,25 +72,6 @@ public static void waitForListenerComplete(final ManagedTaskListenerImpl managed }); } - /** - * Waits for future to complete, but will timeout after - * {@link TestConstants#waitTimeout}, and will be polled every - * {@link TestConstants#pollInterval} - * - * The difference between this method and waitForTaskComplete is that some - * scheduled task will return values for multiple times, in this situation - * waitForTaskComplete does not work. - * - * @param future - the future to wait for - */ - public static void waitTillFutureIsDone(final Future future) { - assertTimeoutPreemptively(TestConstants.waitTimeout, () -> { - for (; !future.isDone(); sleep(TestConstants.pollInterval)) { - //empty - } - }); - } - /** * Waits for future to throw an error, but will timeout after * {@link TestConstants#waitTimeout}, and will be polled every diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedExecutorService/inheritedapi/InheritedAPITests.java b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedExecutorService/inheritedapi/InheritedAPITests.java index cb15fb0e..ea7fa30b 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedExecutorService/inheritedapi/InheritedAPITests.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedExecutorService/inheritedapi/InheritedAPITests.java @@ -72,16 +72,13 @@ public void testExecute() { strategy = "Test basic function for ManagedExecutorService: submit") public void testSubmit() throws Exception { Future result = executor.submit(new CommonTasks.SimpleCallable()); - Wait.waitTillFutureIsDone(result); - assertEquals(result.get(), TestConstants.simpleReturnValue); + assertEquals(Wait.waitForTaskComplete(result), TestConstants.simpleReturnValue); result = executor.submit(new CommonTasks.SimpleRunnable()); - Wait.waitTillFutureIsDone(result); - result.get(); + Wait.waitForTaskComplete(result); result = executor.submit(new CommonTasks.SimpleRunnable(), TestConstants.simpleReturnValue); - Wait.waitTillFutureIsDone(result); - assertEquals(result.get(), TestConstants.simpleReturnValue); + assertEquals(Wait.waitForTaskComplete(result), TestConstants.simpleReturnValue); } @Assertion(id = "SPEC:14.4; SPEC:6.1; SPEC:6.2; SPEC:8", @@ -119,7 +116,7 @@ public void testInvokeAll() { taskList.add(new CommonTasks.SimpleArgCallable(3)); List> resultList = executor.invokeAll(taskList); for (Future each : resultList) { - Wait.waitTillFutureIsDone(each); + Wait.waitForTaskComplete(each); } assertEquals(resultList.get(0).get(), 1); assertEquals(resultList.get(1).get(), 2); @@ -127,7 +124,7 @@ public void testInvokeAll() { resultList = executor.invokeAll(taskList, TestConstants.waitTimeout.getSeconds(), TimeUnit.SECONDS); for (Future each : resultList) { - Wait.waitTillFutureIsDone(each); + Wait.waitForTaskComplete(each); } assertEquals(resultList.get(0).get(), 1); assertEquals(resultList.get(1).get(), 2); diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/inheritedapi/InheritedAPIServletTests.java b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/inheritedapi/InheritedAPIServletTests.java index 6662b7a2..bbc98575 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/inheritedapi/InheritedAPIServletTests.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/inheritedapi/InheritedAPIServletTests.java @@ -67,16 +67,13 @@ public void before() { @Assertion(id = "SPEC:44.1", strategy = "Test basic function for ManagedScheduledExecutorService: submit") public void testApiSubmit() throws Exception { Future result = scheduledExecutor.submit(new CommonTasks.SimpleCallable()); - Wait.waitTillFutureIsDone(result); - assertEquals(result.get(), TestConstants.simpleReturnValue); + assertEquals(Wait.waitForTaskComplete(result), TestConstants.simpleReturnValue); result = scheduledExecutor.submit(new CommonTasks.SimpleRunnable()); - Wait.waitTillFutureIsDone(result); - result.get(); + Wait.waitForTaskComplete(result); result = scheduledExecutor.submit(new CommonTasks.SimpleRunnable(), TestConstants.simpleReturnValue); - Wait.waitTillFutureIsDone(result); - assertEquals(result.get(), TestConstants.simpleReturnValue); + assertEquals(Wait.waitForTaskComplete(result), TestConstants.simpleReturnValue); } @Assertion(id = "SPEC:44.2", strategy = "Test basic function for ManagedScheduledExecutorService: execute") @@ -98,7 +95,7 @@ public void testApiInvokeAll() throws Exception { taskList.add(new CommonTasks.SimpleArgCallable(3)); List> resultList = scheduledExecutor.invokeAll(taskList); for (Future each : resultList) { - Wait.waitTillFutureIsDone(each); + Wait.waitForTaskComplete(each); } assertEquals(resultList.get(0).get(), 1); assertEquals(resultList.get(1).get(), 2); @@ -106,7 +103,7 @@ public void testApiInvokeAll() throws Exception { resultList = scheduledExecutor.invokeAll(taskList, TestConstants.waitTimeout.getSeconds(), TimeUnit.SECONDS); for (Future each : resultList) { - Wait.waitTillFutureIsDone(each); + Wait.waitForTaskComplete(each); } assertEquals(resultList.get(0).get(), 1); assertEquals(resultList.get(1).get(), 2); @@ -154,13 +151,11 @@ public void testApiInvokeAny() throws Exception { public void testApiSchedule() throws Exception { Future result = scheduledExecutor.schedule(new CommonTasks.SimpleCallable(), TestConstants.pollInterval.getSeconds(), TimeUnit.SECONDS); - Wait.waitTillFutureIsDone(result); - assertEquals(result.get(), TestConstants.simpleReturnValue); + assertEquals(Wait.waitForTaskComplete(result), TestConstants.simpleReturnValue); result = scheduledExecutor.schedule(new CommonTasks.SimpleRunnable(), TestConstants.pollInterval.getSeconds(), TimeUnit.SECONDS); - Wait.waitTillFutureIsDone(result); - assertEquals(result.get(), null); + assertEquals(Wait.waitForTaskComplete(result), null); } @Assertion(id = "SPEC:44.6", strategy = "Test basic function for ManagedScheduledExecutorService: scheduleAtFixedRate") diff --git a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/inheritedapi/TestEjb.java b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/inheritedapi/TestEjb.java index 46ac97d8..b4a30949 100644 --- a/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/inheritedapi/TestEjb.java +++ b/tck/src/main/java/ee/jakarta/tck/concurrent/spec/ManagedScheduledExecutorService/inheritedapi/TestEjb.java @@ -54,16 +54,13 @@ public class TestEjb implements TestEjbInterface { public void testApiSubmit() { try { Future result = scheduledExecutor.submit(new CommonTasks.SimpleCallable()); - Wait.waitTillFutureIsDone(result); - assertEquals(result.get(), TestConstants.simpleReturnValue); + assertEquals(Wait.waitForTaskComplete(result), TestConstants.simpleReturnValue); result = scheduledExecutor.submit(new CommonTasks.SimpleRunnable()); - Wait.waitTillFutureIsDone(result); - result.get(); + Wait.waitForTaskComplete(result); result = scheduledExecutor.submit(new CommonTasks.SimpleRunnable(), TestConstants.simpleReturnValue); - Wait.waitTillFutureIsDone(result); - assertEquals(result.get(), TestConstants.simpleReturnValue); + assertEquals(Wait.waitForTaskComplete(result), TestConstants.simpleReturnValue); } catch (Exception e) { throw new RuntimeException(e); } @@ -87,7 +84,7 @@ public void testApiInvokeAll() { taskList.add(new CommonTasks.SimpleArgCallable(3)); List> resultList = scheduledExecutor.invokeAll(taskList); for (Future each : resultList) { - Wait.waitTillFutureIsDone(each); + Wait.waitForTaskComplete(each); } assertEquals(resultList.get(0).get(), 1); assertEquals(resultList.get(1).get(), 2); @@ -96,7 +93,7 @@ public void testApiInvokeAll() { resultList = scheduledExecutor.invokeAll(taskList, TestConstants.waitTimeout.getSeconds(), TimeUnit.SECONDS); for (Future each : resultList) { - Wait.waitTillFutureIsDone(each); + Wait.waitForTaskComplete(each); } assertEquals(resultList.get(0).get(), 1); assertEquals(resultList.get(1).get(), 2); @@ -151,13 +148,11 @@ public void testApiSchedule() { try { Future result = scheduledExecutor.schedule(new CommonTasks.SimpleCallable(), TestConstants.pollInterval.getSeconds(), TimeUnit.SECONDS); - Wait.waitTillFutureIsDone(result); - assertEquals(TestConstants.simpleReturnValue, result.get()); + assertEquals(TestConstants.simpleReturnValue, Wait.waitForTaskComplete(result)); result = scheduledExecutor.schedule(new CommonTasks.SimpleRunnable(), TestConstants.pollInterval.getSeconds(), TimeUnit.SECONDS); - Wait.waitTillFutureIsDone(result); - assertEquals(result.get(), null); + assertEquals(null, Wait.waitForTaskComplete(result)); } catch (Exception e) { fail(e.getMessage()); }