diff --git a/ecosystem/tracing/api/pom.xml b/ecosystem/tracing/api/pom.xml index db999d0723..2d1c9eedfd 100644 --- a/ecosystem/tracing/api/pom.xml +++ b/ecosystem/tracing/api/pom.xml @@ -42,5 +42,10 @@ org.apache.commons commons-lang3 + + + org.awaitility + awaitility + diff --git a/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/JobTracingEventBusTest.java b/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/JobTracingEventBusTest.java index d1ba019871..239950f667 100644 --- a/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/JobTracingEventBusTest.java +++ b/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/JobTracingEventBusTest.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.elasticjob.tracing.event.JobExecutionEvent; import org.apache.shardingsphere.elasticjob.tracing.fixture.JobEventCaller; import org.apache.shardingsphere.elasticjob.tracing.fixture.TestTracingListener; +import org.awaitility.Awaitility; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentMatchers; @@ -31,6 +32,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.lang.reflect.Field; +import java.util.concurrent.TimeUnit; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -55,13 +57,11 @@ void assertRegisterFailure() { } @Test - void assertPost() throws InterruptedException { + void assertPost() { jobTracingEventBus = new JobTracingEventBus(new TracingConfiguration<>("TEST", jobEventCaller)); assertIsRegistered(true); jobTracingEventBus.post(new JobExecutionEvent("localhost", "127.0.0.1", "fake_task_id", "test_event_bus_job", JobExecutionEvent.ExecutionSource.NORMAL_TRIGGER, 0)); - while (!TestTracingListener.isExecutionEventCalled()) { - Thread.sleep(100L); - } + Awaitility.await().pollDelay(100L, TimeUnit.MILLISECONDS).until(TestTracingListener::isExecutionEventCalled); verify(jobEventCaller).call(); } diff --git a/kernel/src/test/java/org/apache/shardingsphere/elasticjob/kernel/api/bootstrap/impl/OneOffJobBootstrapTest.java b/kernel/src/test/java/org/apache/shardingsphere/elasticjob/kernel/api/bootstrap/impl/OneOffJobBootstrapTest.java index 91e7c4d678..908a0ee81d 100644 --- a/kernel/src/test/java/org/apache/shardingsphere/elasticjob/kernel/api/bootstrap/impl/OneOffJobBootstrapTest.java +++ b/kernel/src/test/java/org/apache/shardingsphere/elasticjob/kernel/api/bootstrap/impl/OneOffJobBootstrapTest.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter; import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob; import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; +import org.awaitility.Awaitility; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -33,6 +34,7 @@ import org.quartz.SchedulerException; import java.lang.reflect.Field; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import static org.hamcrest.CoreMatchers.is; @@ -91,14 +93,14 @@ void assertShutdown() throws SchedulerException { assertTrue(getScheduler(oneOffJobBootstrap).isShutdown()); } - @SneakyThrows + @SneakyThrows(ReflectiveOperationException.class) private JobScheduler getJobScheduler(final OneOffJobBootstrap oneOffJobBootstrap) { Field field = OneOffJobBootstrap.class.getDeclaredField("jobScheduler"); field.setAccessible(true); return (JobScheduler) field.get(oneOffJobBootstrap); } - @SneakyThrows + @SneakyThrows(ReflectiveOperationException.class) private Scheduler getScheduler(final OneOffJobBootstrap oneOffJobBootstrap) { JobScheduler jobScheduler = getJobScheduler(oneOffJobBootstrap); Field schedulerField = JobScheduleController.class.getDeclaredField("scheduler"); @@ -106,11 +108,8 @@ private Scheduler getScheduler(final OneOffJobBootstrap oneOffJobBootstrap) { return (Scheduler) schedulerField.get(jobScheduler.getJobScheduleController()); } - @SneakyThrows private void blockUtilFinish(final OneOffJobBootstrap oneOffJobBootstrap, final AtomicInteger counter) { Scheduler scheduler = getScheduler(oneOffJobBootstrap); - while (0 == counter.get() || !scheduler.getCurrentlyExecutingJobs().isEmpty()) { - Thread.sleep(100L); - } + Awaitility.await().pollDelay(100L, TimeUnit.MILLISECONDS).until(() -> 0 != counter.get() && scheduler.getCurrentlyExecutingJobs().isEmpty()); } } diff --git a/registry-center/provider/zookeeper-curator/pom.xml b/registry-center/provider/zookeeper-curator/pom.xml index 5390269236..97609432a2 100644 --- a/registry-center/provider/zookeeper-curator/pom.xml +++ b/registry-center/provider/zookeeper-curator/pom.xml @@ -52,5 +52,9 @@ org.apache.curator curator-recipes + + org.awaitility + awaitility + diff --git a/registry-center/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperElectionServiceTest.java b/registry-center/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperElectionServiceTest.java index 9256db60f0..131479fd03 100644 --- a/registry-center/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperElectionServiceTest.java +++ b/registry-center/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperElectionServiceTest.java @@ -25,6 +25,7 @@ import org.apache.curator.test.KillSession; import org.apache.shardingsphere.elasticjob.reg.base.ElectionCandidate; import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer; +import org.awaitility.Awaitility; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -33,6 +34,7 @@ import java.lang.reflect.Field; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import static org.mockito.Mockito.atLeastOnce; @@ -79,11 +81,8 @@ void assertContend() throws Exception { verify(anotherElectionCandidate, atLeastOnce()).stopLeadership(); } - @SneakyThrows private void blockUntilCondition(final Supplier condition) { - while (!condition.get()) { - Thread.sleep(100L); - } + Awaitility.await().pollDelay(100L, TimeUnit.MILLISECONDS).until(condition::get); } @SneakyThrows diff --git a/registry-center/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperRegistryCenterExecuteInLeaderTest.java b/registry-center/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperRegistryCenterExecuteInLeaderTest.java index 2bead8dae4..f44fb68d2d 100644 --- a/registry-center/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperRegistryCenterExecuteInLeaderTest.java +++ b/registry-center/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/ZookeeperRegistryCenterExecuteInLeaderTest.java @@ -82,7 +82,7 @@ public void execute() { handleConcurrentExecution(); } try { - Thread.sleep(100); + Thread.sleep(100L); } catch (final InterruptedException ex) { waitingThread.interrupt(); }