diff --git a/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/pom.xml b/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/pom.xml index 4c3cd4e58e..9b2bec4b23 100644 --- a/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/pom.xml +++ b/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/pom.xml @@ -58,5 +58,10 @@ true test + + org.awaitility + awaitility + test + diff --git a/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/fixture/InternalController.java b/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/fixture/InternalController.java index 6a289e8761..63d209eef3 100644 --- a/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/fixture/InternalController.java +++ b/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/fixture/InternalController.java @@ -18,14 +18,18 @@ package org.apache.shardingsphere.elasticjob.http.executor.fixture; import lombok.extern.slf4j.Slf4j; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.apache.shardingsphere.elasticjob.restful.Http; import org.apache.shardingsphere.elasticjob.restful.RestfulController; import org.apache.shardingsphere.elasticjob.restful.annotation.Mapping; import org.apache.shardingsphere.elasticjob.restful.annotation.Param; import org.apache.shardingsphere.elasticjob.restful.annotation.ParamSource; +import org.awaitility.Awaitility; import java.util.Objects; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; @Slf4j public final class InternalController implements RestfulController { @@ -71,7 +75,9 @@ public String postName(@Param(name = "updateName", source = ParamSource.PATH) fi */ @Mapping(method = Http.POST, path = "/postWithTimeout") public String postWithTimeout() { - BlockUtils.waitingShortTime(); + Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(1L, TimeUnit.SECONDS).untilAsserted(() -> + assertThat(Boolean.TRUE, is(Boolean.TRUE)) + ); return "ejob"; } } diff --git a/elasticjob-infra/elasticjob-infra-common/pom.xml b/elasticjob-infra/elasticjob-infra-common/pom.xml index 6f7e0f54bc..005d783eb6 100644 --- a/elasticjob-infra/elasticjob-infra-common/pom.xml +++ b/elasticjob-infra/elasticjob-infra-common/pom.xml @@ -87,5 +87,10 @@ logback-classic test + + org.awaitility + awaitility + test + diff --git a/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/concurrent/ElasticJobExecutorServiceTest.java b/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/concurrent/ElasticJobExecutorServiceTest.java index e92c078fa9..b3a4e94319 100644 --- a/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/concurrent/ElasticJobExecutorServiceTest.java +++ b/elasticjob-infra/elasticjob-infra-common/src/test/java/org/apache/shardingsphere/elasticjob/infra/concurrent/ElasticJobExecutorServiceTest.java @@ -17,9 +17,11 @@ package org.apache.shardingsphere.elasticjob.infra.concurrent; +import org.awaitility.Awaitility; import org.junit.Test; import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertFalse; @@ -38,15 +40,17 @@ public void assertCreateExecutorService() { assertFalse(executorServiceObject.isShutdown()); ExecutorService executorService = executorServiceObject.createExecutorService(); executorService.submit(new FooTask()); - BlockUtils.waitingShortTime(); - assertThat(executorServiceObject.getActiveThreadCount(), is(1)); - assertThat(executorServiceObject.getWorkQueueSize(), is(0)); - assertFalse(executorServiceObject.isShutdown()); + Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(5L, TimeUnit.MINUTES).untilAsserted(() -> { + assertThat(executorServiceObject.getActiveThreadCount(), is(1)); + assertThat(executorServiceObject.getWorkQueueSize(), is(0)); + assertFalse(executorServiceObject.isShutdown()); + }); executorService.submit(new FooTask()); - BlockUtils.waitingShortTime(); - assertThat(executorServiceObject.getActiveThreadCount(), is(1)); - assertThat(executorServiceObject.getWorkQueueSize(), is(1)); - assertFalse(executorServiceObject.isShutdown()); + Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(5L, TimeUnit.MINUTES).untilAsserted(() -> { + assertThat(executorServiceObject.getActiveThreadCount(), is(1)); + assertThat(executorServiceObject.getWorkQueueSize(), is(1)); + assertFalse(executorServiceObject.isShutdown()); + }); executorService.shutdownNow(); assertThat(executorServiceObject.getWorkQueueSize(), is(0)); assertTrue(executorServiceObject.isShutdown()); @@ -54,13 +58,11 @@ public void assertCreateExecutorService() { } static class FooTask implements Runnable { - + @Override public void run() { - BlockUtils.sleep(1000L); - while (!hasExecuted) { - Thread.yield(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES) + .untilAsserted(() -> assertThat(hasExecuted, is(true))); } } } diff --git a/elasticjob-lite/elasticjob-lite-core/pom.xml b/elasticjob-lite/elasticjob-lite-core/pom.xml index 32ff0c09a3..a4898adb53 100644 --- a/elasticjob-lite/elasticjob-lite-core/pom.xml +++ b/elasticjob-lite/elasticjob-lite-core/pom.xml @@ -125,5 +125,10 @@ logback-classic test + + org.awaitility + awaitility + test + diff --git a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/DisabledJobIntegrateTest.java b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/DisabledJobIntegrateTest.java index 7124db006a..bca4a25ac7 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/DisabledJobIntegrateTest.java +++ b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/DisabledJobIntegrateTest.java @@ -17,16 +17,19 @@ package org.apache.shardingsphere.elasticjob.lite.integrate.disable; -import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap; import org.apache.shardingsphere.elasticjob.api.JobConfiguration; +import org.apache.shardingsphere.elasticjob.infra.env.IpUtils; +import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO; +import org.apache.shardingsphere.elasticjob.infra.yaml.YamlEngine; +import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap; import org.apache.shardingsphere.elasticjob.lite.fixture.job.DetailedFooJob; import org.apache.shardingsphere.elasticjob.lite.integrate.BaseIntegrateTest; -import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; import org.apache.shardingsphere.elasticjob.lite.internal.server.ServerStatus; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; -import org.apache.shardingsphere.elasticjob.infra.env.IpUtils; -import org.apache.shardingsphere.elasticjob.infra.yaml.YamlEngine; +import org.awaitility.Awaitility; +import org.hamcrest.core.IsNull; + +import java.util.concurrent.TimeUnit; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertNull; @@ -39,8 +42,10 @@ public DisabledJobIntegrateTest(final TestType type) { } protected final void assertDisabledRegCenterInfo() { - assertThat(JobRegistry.getInstance().getCurrentShardingTotalCount(getJobName()), is(3)); - assertThat(JobRegistry.getInstance().getJobInstance(getJobName()).getServerIp(), is(IpUtils.getIp())); + Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> { + assertThat(JobRegistry.getInstance().getCurrentShardingTotalCount(getJobName()), is(3)); + assertThat(JobRegistry.getInstance().getJobInstance(getJobName()).getServerIp(), is(IpUtils.getIp())); + }); JobConfiguration jobConfig = YamlEngine.unmarshal(getREGISTRY_CENTER().get("/" + getJobName() + "/config"), JobConfigurationPOJO.class).toJobConfiguration(); assertThat(jobConfig.getShardingTotalCount(), is(3)); if (getJobBootstrap() instanceof ScheduleJobBootstrap) { @@ -50,8 +55,8 @@ protected final void assertDisabledRegCenterInfo() { } assertThat(jobConfig.getShardingItemParameters(), is("0=A,1=B,2=C")); assertThat(getREGISTRY_CENTER().get("/" + getJobName() + "/servers/" + JobRegistry.getInstance().getJobInstance(getJobName()).getServerIp()), is(ServerStatus.DISABLED.name())); - while (null != getREGISTRY_CENTER().get("/" + getJobName() + "/leader/election/instance")) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(getREGISTRY_CENTER().get("/" + getJobName() + "/leader/election/instance"), is(IsNull.nullValue())) + ); } } diff --git a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/OneOffDisabledJobIntegrateTest.java b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/OneOffDisabledJobIntegrateTest.java index 77665b1b01..c9a04f18c3 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/OneOffDisabledJobIntegrateTest.java +++ b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/OneOffDisabledJobIntegrateTest.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.elasticjob.lite.integrate.disable; import org.apache.shardingsphere.elasticjob.api.JobConfiguration; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.junit.Test; public final class OneOffDisabledJobIntegrateTest extends DisabledJobIntegrateTest { @@ -35,7 +34,6 @@ protected JobConfiguration getJobConfiguration(final String jobName) { @Test public void assertJobRunning() { - BlockUtils.waitingShortTime(); assertDisabledRegCenterInfo(); } } diff --git a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/ScheduleDisabledJobIntegrateTest.java b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/ScheduleDisabledJobIntegrateTest.java index bebc278945..53c90c65e0 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/ScheduleDisabledJobIntegrateTest.java +++ b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/disable/ScheduleDisabledJobIntegrateTest.java @@ -21,9 +21,13 @@ import org.apache.shardingsphere.elasticjob.lite.fixture.job.DetailedFooJob; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; import org.apache.shardingsphere.elasticjob.lite.internal.server.ServerStatus; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; +import org.awaitility.Awaitility; import org.junit.Test; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public final class ScheduleDisabledJobIntegrateTest extends DisabledJobIntegrateTest { @@ -40,12 +44,11 @@ protected JobConfiguration getJobConfiguration(final String jobName) { @Test public void assertJobRunning() { - BlockUtils.waitingShortTime(); assertDisabledRegCenterInfo(); setJobEnable(); - while (!((DetailedFooJob) getElasticJob()).isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(10L, TimeUnit.SECONDS).untilAsserted(() -> + assertThat(((DetailedFooJob) getElasticJob()).isCompleted(), is(true)) + ); assertEnabledRegCenterInfo(); } diff --git a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/enable/OneOffEnabledJobIntegrateTest.java b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/enable/OneOffEnabledJobIntegrateTest.java index 2bcfedc310..eea60ebd7b 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/enable/OneOffEnabledJobIntegrateTest.java +++ b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/enable/OneOffEnabledJobIntegrateTest.java @@ -19,9 +19,13 @@ import org.apache.shardingsphere.elasticjob.api.JobConfiguration; import org.apache.shardingsphere.elasticjob.lite.fixture.job.DetailedFooJob; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; +import org.awaitility.Awaitility; import org.junit.Test; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public final class OneOffEnabledJobIntegrateTest extends EnabledJobIntegrateTest { @@ -38,9 +42,9 @@ protected JobConfiguration getJobConfiguration(final String jobName) { @Test public void assertJobInit() { - while (!((DetailedFooJob) getElasticJob()).isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(((DetailedFooJob) getElasticJob()).isCompleted(), is(true)) + ); assertTrue(getREGISTRY_CENTER().isExisted("/" + getJobName() + "/sharding")); } } diff --git a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/enable/ScheduleEnabledJobIntegrateTest.java b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/enable/ScheduleEnabledJobIntegrateTest.java index 5ce9bf7c7c..de4a7ec691 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/enable/ScheduleEnabledJobIntegrateTest.java +++ b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/integrate/enable/ScheduleEnabledJobIntegrateTest.java @@ -19,9 +19,13 @@ import org.apache.shardingsphere.elasticjob.api.JobConfiguration; import org.apache.shardingsphere.elasticjob.lite.fixture.job.DetailedFooJob; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; +import org.awaitility.Awaitility; import org.junit.Test; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public final class ScheduleEnabledJobIntegrateTest extends EnabledJobIntegrateTest { @@ -35,12 +39,12 @@ protected JobConfiguration getJobConfiguration(final String jobName) { return JobConfiguration.newBuilder(jobName, 3).cron("0/1 * * * * ?").shardingItemParameters("0=A,1=B,2=C") .jobListenerTypes("INTEGRATE-TEST", "INTEGRATE-DISTRIBUTE").overwrite(true).build(); } - + @Test public void assertJobInit() { - while (!((DetailedFooJob) getElasticJob()).isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(10L, TimeUnit.SECONDS).untilAsserted(() -> + assertThat(((DetailedFooJob) getElasticJob()).isCompleted(), is(true)) + ); assertTrue(getREGISTRY_CENTER().isExisted("/" + getJobName() + "/sharding")); } } diff --git a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/annotation/integrate/OneOffEnabledJobTest.java b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/annotation/integrate/OneOffEnabledJobTest.java index 33bf40ef86..9be96c5cb0 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/annotation/integrate/OneOffEnabledJobTest.java +++ b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/annotation/integrate/OneOffEnabledJobTest.java @@ -17,22 +17,24 @@ package org.apache.shardingsphere.elasticjob.lite.internal.annotation.integrate; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - import org.apache.shardingsphere.elasticjob.api.JobConfiguration; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.apache.shardingsphere.elasticjob.infra.env.IpUtils; import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO; import org.apache.shardingsphere.elasticjob.infra.yaml.YamlEngine; import org.apache.shardingsphere.elasticjob.lite.fixture.job.AnnotationUnShardingJob; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; import org.apache.shardingsphere.elasticjob.lite.internal.server.ServerStatus; +import org.awaitility.Awaitility; import org.junit.Before; import org.junit.Test; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + public final class OneOffEnabledJobTest extends BaseAnnotationTest { public OneOffEnabledJobTest() { @@ -55,9 +57,9 @@ public void assertEnabledRegCenterInfo() { @Test public void assertJobInit() { - while (!((AnnotationUnShardingJob) getElasticJob()).isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(((AnnotationUnShardingJob) getElasticJob()).isCompleted(), is(true)) + ); assertTrue(getREGISTRY_CENTER().isExisted("/" + getJobName() + "/sharding")); } diff --git a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/annotation/integrate/ScheduleEnabledJobTest.java b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/annotation/integrate/ScheduleEnabledJobTest.java index d250341e3f..3acfe26049 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/annotation/integrate/ScheduleEnabledJobTest.java +++ b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/annotation/integrate/ScheduleEnabledJobTest.java @@ -17,22 +17,24 @@ package org.apache.shardingsphere.elasticjob.lite.internal.annotation.integrate; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - import org.apache.shardingsphere.elasticjob.api.JobConfiguration; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.apache.shardingsphere.elasticjob.infra.env.IpUtils; import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO; import org.apache.shardingsphere.elasticjob.infra.yaml.YamlEngine; import org.apache.shardingsphere.elasticjob.lite.fixture.job.AnnotationSimpleJob; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; import org.apache.shardingsphere.elasticjob.lite.internal.server.ServerStatus; +import org.awaitility.Awaitility; import org.junit.Before; import org.junit.Test; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + public final class ScheduleEnabledJobTest extends BaseAnnotationTest { public ScheduleEnabledJobTest() { @@ -57,9 +59,9 @@ public void assertEnabledRegCenterInfo() { @Test public void assertJobInit() { - while (!((AnnotationSimpleJob) getElasticJob()).isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(((AnnotationSimpleJob) getElasticJob()).isCompleted(), is(true)) + ); assertTrue(getREGISTRY_CENTER().isExisted("/" + getJobName() + "/sharding")); } diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/pom.xml b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/pom.xml index f73b900060..b672142222 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/pom.xml +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/pom.xml @@ -87,5 +87,10 @@ h2 test + + org.awaitility + awaitility + test + diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootScannerTest.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootScannerTest.java index 25b948701a..6d9db0bcb7 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootScannerTest.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootScannerTest.java @@ -17,18 +17,22 @@ package org.apache.shardingsphere.elasticjob.lite.spring.boot.job; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap; import org.apache.shardingsphere.elasticjob.lite.spring.boot.job.fixture.EmbedTestingServer; import org.apache.shardingsphere.elasticjob.lite.spring.boot.job.fixture.job.impl.AnnotationCustomJob; import org.apache.shardingsphere.elasticjob.lite.spring.core.scanner.ElasticJobScan; +import org.awaitility.Awaitility; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @SpringBootTest @@ -44,9 +48,9 @@ public static void init() { @Test public void assertDefaultBeanNameWithTypeJob() { - while (!AnnotationCustomJob.isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(AnnotationCustomJob.isCompleted(), is(true)) + ); assertTrue(AnnotationCustomJob.isCompleted()); assertNotNull(applicationContext); assertNotNull(applicationContext.getBean("annotationCustomJobSchedule", ScheduleJobBootstrap.class)); diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootTest.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootTest.java index 6ded070407..94e60f1d9c 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootTest.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootTest.java @@ -19,7 +19,6 @@ import org.apache.shardingsphere.elasticjob.api.ElasticJob; import org.apache.shardingsphere.elasticjob.api.JobExtraConfiguration; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.JobBootstrap; import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap; import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap; @@ -30,6 +29,7 @@ import org.apache.shardingsphere.elasticjob.lite.spring.boot.tracing.TracingProperties; import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter; import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration; +import org.awaitility.Awaitility; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -45,6 +45,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.TimeUnit; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; @@ -131,12 +132,13 @@ public void assertElasticJobProperties() { @Test public void assertJobScheduleCreation() { - assertNotNull(applicationContext); - Map elasticJobBeans = applicationContext.getBeansOfType(ElasticJob.class); - assertFalse(elasticJobBeans.isEmpty()); - Map jobBootstrapBeans = applicationContext.getBeansOfType(JobBootstrap.class); - assertFalse(jobBootstrapBeans.isEmpty()); - BlockUtils.waitingShortTime(); + Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> { + assertNotNull(applicationContext); + Map elasticJobBeans = applicationContext.getBeansOfType(ElasticJob.class); + assertFalse(elasticJobBeans.isEmpty()); + Map jobBootstrapBeans = applicationContext.getBeansOfType(JobBootstrap.class); + assertFalse(jobBootstrapBeans.isEmpty()); + }); } @Test diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/EmbedTestingServer.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/EmbedTestingServer.java index aa97974ea2..40f6c48b73 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/EmbedTestingServer.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/EmbedTestingServer.java @@ -19,12 +19,17 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.curator.CuratorZookeeperClient; +import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.apache.shardingsphere.elasticjob.reg.exception.RegExceptionHandler; +import org.awaitility.Awaitility; +import org.hamcrest.Matchers; -import java.io.File; import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertThat; @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class EmbedTestingServer { @@ -46,14 +51,11 @@ public static String getConnectionString() { * Start the server. */ public static void start() { - // sleep some time to avoid testServer intended stop. - long sleepTime = 1000L; - BlockUtils.sleep(sleepTime); if (null != testingServer) { return; } try { - testingServer = new TestingServer(PORT, new File(String.format("target/test_zk_data/%s/", System.nanoTime()))); + testingServer = new TestingServer(PORT, true); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON @@ -61,12 +63,24 @@ public static void start() { } finally { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { - Thread.sleep(sleepTime); testingServer.close(); - } catch (final IOException | InterruptedException ex) { + } catch (final IOException ex) { RegExceptionHandler.handleException(ex); } })); } + try (CuratorZookeeperClient client = new CuratorZookeeperClient(getConnectionString(), + 60 * 1000, 500, null, + new ExponentialBackoffRetry(500, 3, 500 * 3))) { + client.start(); + Awaitility.await() + .atLeast(100L, TimeUnit.MILLISECONDS) + .atMost(500 * 60L, TimeUnit.MILLISECONDS) + .untilAsserted(() -> assertThat(client.isConnected(), Matchers.is(true))); + // CHECKSTYLE:OFF + } catch (Exception e) { + // CHECKSTYLE:ON + throw new RuntimeException(e); + } } } diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/pom.xml b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/pom.xml index de79e7f6e0..9d8ae8adda 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/pom.xml +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/pom.xml @@ -114,5 +114,10 @@ commons-dbcp2 test + + org.awaitility + awaitility + test + diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/AbstractJobSpringIntegrateTest.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/AbstractJobSpringIntegrateTest.java index 9d70f3e5cc..b9227793e5 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/AbstractJobSpringIntegrateTest.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/AbstractJobSpringIntegrateTest.java @@ -19,16 +19,20 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; -import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.fixture.job.DataflowElasticJob; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.fixture.job.FooSimpleElasticJob; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.test.AbstractZookeeperJUnit4SpringContextTests; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; +import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.awaitility.Awaitility; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @RequiredArgsConstructor @@ -61,17 +65,17 @@ public void assertSpringJobBean() { } private void assertSimpleElasticJobBean() { - while (!FooSimpleElasticJob.isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(FooSimpleElasticJob.isCompleted(), is(true)) + ); assertTrue(FooSimpleElasticJob.isCompleted()); assertTrue(regCenter.isExisted("/" + simpleJobName + "/sharding")); } private void assertThroughputDataflowElasticJobBean() { - while (!DataflowElasticJob.isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(DataflowElasticJob.isCompleted(), is(true)) + ); assertTrue(DataflowElasticJob.isCompleted()); assertTrue(regCenter.isExisted("/" + throughputDataflowJobName + "/sharding")); } diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/AbstractOneOffJobSpringIntegrateTest.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/AbstractOneOffJobSpringIntegrateTest.java index f68dea1ff0..45142a5d39 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/AbstractOneOffJobSpringIntegrateTest.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/AbstractOneOffJobSpringIntegrateTest.java @@ -18,18 +18,22 @@ package org.apache.shardingsphere.elasticjob.lite.spring.namespace.job; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.fixture.job.DataflowElasticJob; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.fixture.job.FooSimpleElasticJob; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.test.AbstractZookeeperJUnit4SpringContextTests; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.awaitility.Awaitility; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @RequiredArgsConstructor @@ -64,9 +68,9 @@ public void assertSpringJobBean() { private void assertSimpleElasticJobBean() { OneOffJobBootstrap bootstrap = applicationContext.getBean(simpleJobName, OneOffJobBootstrap.class); bootstrap.execute(); - while (!FooSimpleElasticJob.isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(10L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(FooSimpleElasticJob.isCompleted(), is(true)) + ); assertTrue(FooSimpleElasticJob.isCompleted()); assertTrue(regCenter.isExisted("/" + simpleJobName + "/sharding")); } @@ -74,9 +78,9 @@ private void assertSimpleElasticJobBean() { private void assertThroughputDataflowElasticJobBean() { OneOffJobBootstrap bootstrap = applicationContext.getBean(throughputDataflowJobName, OneOffJobBootstrap.class); bootstrap.execute(); - while (!DataflowElasticJob.isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(10L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(DataflowElasticJob.isCompleted(), is(true)) + ); assertTrue(DataflowElasticJob.isCompleted()); assertTrue(regCenter.isExisted("/" + throughputDataflowJobName + "/sharding")); } diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/JobSpringNamespaceWithRefTest.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/JobSpringNamespaceWithRefTest.java index 834b9771b0..109647dd1c 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/JobSpringNamespaceWithRefTest.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/JobSpringNamespaceWithRefTest.java @@ -18,16 +18,20 @@ package org.apache.shardingsphere.elasticjob.lite.spring.namespace.job; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; -import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.fixture.job.ref.RefFooSimpleElasticJob; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.test.AbstractZookeeperJUnit4SpringContextTests; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; +import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.awaitility.Awaitility; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @ContextConfiguration(locations = "classpath:META-INF/job/withJobRef.xml") @@ -55,9 +59,9 @@ public void assertSpringJobBean() { } private void assertSimpleElasticJobBean() { - while (!RefFooSimpleElasticJob.isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(RefFooSimpleElasticJob.isCompleted(), is(true)) + ); assertTrue(RefFooSimpleElasticJob.isCompleted()); assertTrue(regCenter.isExisted("/" + simpleJobName + "/sharding")); } diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/JobSpringNamespaceWithTypeTest.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/JobSpringNamespaceWithTypeTest.java index 7b9ad4bc8b..7f556446a0 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/JobSpringNamespaceWithTypeTest.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/JobSpringNamespaceWithTypeTest.java @@ -17,11 +17,10 @@ package org.apache.shardingsphere.elasticjob.lite.spring.namespace.job; -import static org.junit.Assert.assertTrue; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.test.AbstractZookeeperJUnit4SpringContextTests; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.awaitility.Awaitility; import org.junit.After; import org.junit.Test; import org.quartz.Scheduler; @@ -30,6 +29,12 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.util.ReflectionTestUtils; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + @ContextConfiguration(locations = "classpath:META-INF/job/withJobType.xml") public final class JobSpringNamespaceWithTypeTest extends AbstractZookeeperJUnit4SpringContextTests { @@ -41,18 +46,18 @@ public final class JobSpringNamespaceWithTypeTest extends AbstractZookeeperJUnit private Scheduler scheduler; @After - public void tearDown() throws SchedulerException { - while (!scheduler.getCurrentlyExecutingJobs().isEmpty()) { - BlockUtils.waitingShortTime(); - } + public void tearDown() { + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(scheduler.getCurrentlyExecutingJobs().isEmpty(), is(true)) + ); JobRegistry.getInstance().getJobScheduleController(scriptJobName).shutdown(); } @Test public void jobScriptWithJobTypeTest() throws SchedulerException { - while (!regCenter.isExisted("/" + scriptJobName + "/sharding")) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(regCenter.isExisted("/" + scriptJobName + "/sharding"), is(true)) + ); scheduler = (Scheduler) ReflectionTestUtils.getField(JobRegistry.getInstance().getJobScheduleController(scriptJobName), "scheduler"); assertTrue(scheduler.isStarted()); } diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/OneOffJobSpringNamespaceWithRefTest.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/OneOffJobSpringNamespaceWithRefTest.java index d014f74c6c..47976d08f4 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/OneOffJobSpringNamespaceWithRefTest.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/OneOffJobSpringNamespaceWithRefTest.java @@ -19,16 +19,20 @@ import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; -import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.fixture.job.ref.RefFooSimpleElasticJob; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.test.AbstractZookeeperJUnit4SpringContextTests; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; +import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.awaitility.Awaitility; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @ContextConfiguration(locations = "classpath:META-INF/job/oneOffWithJobRef.xml") @@ -58,9 +62,9 @@ public void assertSpringJobBean() { } private void assertOneOffSimpleElasticJobBean() { - while (!RefFooSimpleElasticJob.isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(RefFooSimpleElasticJob.isCompleted(), is(true)) + ); assertTrue(RefFooSimpleElasticJob.isCompleted()); assertTrue(regCenter.isExisted("/" + oneOffSimpleJobName + "/sharding")); } diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/OneOffJobSpringNamespaceWithTypeTest.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/OneOffJobSpringNamespaceWithTypeTest.java index fdc03bf695..c6f1f46dbb 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/OneOffJobSpringNamespaceWithTypeTest.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/job/OneOffJobSpringNamespaceWithTypeTest.java @@ -17,16 +17,18 @@ package org.apache.shardingsphere.elasticjob.lite.spring.namespace.job; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.test.AbstractZookeeperJUnit4SpringContextTests; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.awaitility.Awaitility; import org.junit.After; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; +import java.util.concurrent.TimeUnit; + import static org.junit.Assert.assertTrue; @ContextConfiguration(locations = "classpath:META-INF/job/oneOffWithJobType.xml") @@ -46,7 +48,8 @@ public void tearDown() { public void jobScriptWithJobTypeTest() { OneOffJobBootstrap bootstrap = applicationContext.getBean(scriptJobName, OneOffJobBootstrap.class); bootstrap.execute(); - BlockUtils.sleep(1000L); - assertTrue(regCenter.isExisted("/" + scriptJobName + "/sharding")); + Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertTrue(regCenter.isExisted("/" + scriptJobName + "/sharding")) + ); } } diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/scanner/AbstractJobSpringIntegrateTest.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/scanner/AbstractJobSpringIntegrateTest.java index 5cc4ffdf24..76f461b52d 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/scanner/AbstractJobSpringIntegrateTest.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/scanner/AbstractJobSpringIntegrateTest.java @@ -18,16 +18,20 @@ package org.apache.shardingsphere.elasticjob.lite.spring.namespace.scanner; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.fixture.job.annotation.AnnotationSimpleJob; import org.apache.shardingsphere.elasticjob.lite.spring.namespace.test.AbstractZookeeperJUnit4SpringContextTests; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; +import org.awaitility.Awaitility; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @RequiredArgsConstructor @@ -55,9 +59,9 @@ public void assertSpringJobBean() { } private void assertSimpleElasticJobBean() { - while (!AnnotationSimpleJob.isCompleted()) { - BlockUtils.waitingShortTime(); - } + Awaitility.await().atMost(1L, TimeUnit.MINUTES).untilAsserted(() -> + assertThat(AnnotationSimpleJob.isCompleted(), is(true)) + ); assertTrue(AnnotationSimpleJob.isCompleted()); assertTrue(regCenter.isExisted("/" + simpleJobName + "/sharding")); } diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/test/EmbedZookeeperTestExecutionListener.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/test/EmbedZookeeperTestExecutionListener.java index 6bd6b56857..dfc432b280 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/test/EmbedZookeeperTestExecutionListener.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/test/EmbedZookeeperTestExecutionListener.java @@ -17,14 +17,19 @@ package org.apache.shardingsphere.elasticjob.lite.spring.namespace.test; +import org.apache.curator.CuratorZookeeperClient; +import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; import org.apache.shardingsphere.elasticjob.reg.exception.RegExceptionHandler; -import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils; +import org.awaitility.Awaitility; +import org.hamcrest.Matchers; import org.springframework.test.context.TestContext; import org.springframework.test.context.support.AbstractTestExecutionListener; -import java.io.File; import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertThat; public final class EmbedZookeeperTestExecutionListener extends AbstractTestExecutionListener { @@ -34,13 +39,13 @@ public final class EmbedZookeeperTestExecutionListener extends AbstractTestExecu public void beforeTestClass(final TestContext testContext) { startEmbedTestingServer(); } - + private static void startEmbedTestingServer() { if (null != testingServer) { return; } try { - testingServer = new TestingServer(3181, new File(String.format("target/test_zk_data/%s/", System.nanoTime()))); + testingServer = new TestingServer(3181, true); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON @@ -48,12 +53,24 @@ private static void startEmbedTestingServer() { } finally { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { - BlockUtils.sleep(2000L); testingServer.close(); } catch (final IOException ex) { RegExceptionHandler.handleException(ex); } })); } + try (CuratorZookeeperClient client = new CuratorZookeeperClient(testingServer.getConnectString(), + 60 * 1000, 500, null, + new ExponentialBackoffRetry(500, 3, 500 * 3))) { + client.start(); + Awaitility.await() + .atLeast(100L, TimeUnit.MILLISECONDS) + .atMost(500 * 60L, TimeUnit.MILLISECONDS) + .untilAsserted(() -> assertThat(client.isConnected(), Matchers.is(true))); + // CHECKSTYLE:OFF + } catch (Exception e) { + // CHECKSTYLE:ON + throw new RuntimeException(e); + } } } diff --git a/pom.xml b/pom.xml index e56be37d11..9af552d722 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,7 @@ 4.12 2.2 4.8.0 + 4.2.0 0.15 3.8.0 @@ -347,6 +348,12 @@ ${aspectj.version} test + + org.awaitility + awaitility + ${awaitility.version} + test +