Skip to content

Commit

Permalink
Use Awaitility instead of BlockUtils in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Sep 12, 2023
1 parent 6c946cd commit 83ea9b3
Show file tree
Hide file tree
Showing 24 changed files with 221 additions and 112 deletions.
5 changes: 5 additions & 0 deletions elasticjob-infra/elasticjob-infra-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,10 @@
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,29 +40,29 @@ 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(1L, 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(1L, 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());
hasExecuted = true;
}

static class FooTask implements Runnable {

@Override
public void run() {
BlockUtils.sleep(1000L);
while (!hasExecuted) {
Thread.yield();
}
Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(1L, TimeUnit.MINUTES)
.untilAsserted(() -> assertThat(hasExecuted, is(true)));
}
}
}
5 changes: 5 additions & 0 deletions elasticjob-lite/elasticjob-lite-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,10 @@
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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().atLeast(100L, TimeUnit.MILLISECONDS).atMost(1L, TimeUnit.MINUTES).untilAsserted(() ->
assertThat(getREGISTRY_CENTER().get("/" + getJobName() + "/leader/election/instance"), is(IsNull.nullValue()))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,7 +34,6 @@ protected JobConfiguration getJobConfiguration(final String jobName) {

@Test
public void assertJobRunning() {
BlockUtils.waitingShortTime();
assertDisabledRegCenterInfo();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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().atLeast(100L, TimeUnit.MILLISECONDS).atMost(10L, TimeUnit.SECONDS).untilAsserted(() ->
assertThat(((DetailedFooJob) getElasticJob()).isCompleted(), is(true))
);
assertEnabledRegCenterInfo();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -38,9 +42,9 @@ protected JobConfiguration getJobConfiguration(final String jobName) {

@Test
public void assertJobInit() {
while (!((DetailedFooJob) getElasticJob()).isCompleted()) {
BlockUtils.waitingShortTime();
}
Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(1L, TimeUnit.MINUTES).untilAsserted(() ->
assertThat(((DetailedFooJob) getElasticJob()).isCompleted(), is(true))
);
assertTrue(getREGISTRY_CENTER().isExisted("/" + getJobName() + "/sharding"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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().atLeast(100L, TimeUnit.MILLISECONDS).atMost(10L, TimeUnit.SECONDS).untilAsserted(() ->
assertThat(((DetailedFooJob) getElasticJob()).isCompleted(), is(true))
);
assertTrue(getREGISTRY_CENTER().isExisted("/" + getJobName() + "/sharding"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -55,9 +57,9 @@ public void assertEnabledRegCenterInfo() {

@Test
public void assertJobInit() {
while (!((AnnotationUnShardingJob) getElasticJob()).isCompleted()) {
BlockUtils.waitingShortTime();
}
Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(1L, TimeUnit.MINUTES).untilAsserted(() ->
assertThat(((AnnotationUnShardingJob) getElasticJob()).isCompleted(), is(true))
);
assertTrue(getREGISTRY_CENTER().isExisted("/" + getJobName() + "/sharding"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -57,9 +59,9 @@ public void assertEnabledRegCenterInfo() {

@Test
public void assertJobInit() {
while (!((AnnotationSimpleJob) getElasticJob()).isCompleted()) {
BlockUtils.waitingShortTime();
}
Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(1L, TimeUnit.MINUTES).untilAsserted(() ->
assertThat(((AnnotationSimpleJob) getElasticJob()).isCompleted(), is(true))
);
assertTrue(getREGISTRY_CENTER().isExisted("/" + getJobName() + "/sharding"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,10 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,9 +48,9 @@ public static void init() {

@Test
public void assertDefaultBeanNameWithTypeJob() {
while (!AnnotationCustomJob.isCompleted()) {
BlockUtils.waitingShortTime();
}
Awaitility.await().atLeast(100L, TimeUnit.MILLISECONDS).atMost(1L, TimeUnit.MINUTES).untilAsserted(() ->
assertThat(AnnotationCustomJob.isCompleted(), is(true))
);
assertTrue(AnnotationCustomJob.isCompleted());
assertNotNull(applicationContext);
assertNotNull(applicationContext.getBean("annotationCustomJobSchedule", ScheduleJobBootstrap.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -136,7 +135,6 @@ public void assertJobScheduleCreation() {
assertFalse(elasticJobBeans.isEmpty());
Map<String, JobBootstrap> jobBootstrapBeans = applicationContext.getBeansOfType(JobBootstrap.class);
assertFalse(jobBootstrapBeans.isEmpty());
BlockUtils.waitingShortTime();
}

@Test
Expand Down
Loading

0 comments on commit 83ea9b3

Please sign in to comment.