Skip to content

Commit

Permalink
Merge pull request #2257 from linghengqian/new-awaitility
Browse files Browse the repository at this point in the history
Use Awaitility instead of BlockUtils in unit tests
  • Loading branch information
strongduanmu authored Sep 13, 2023
2 parents 6c946cd + 1a0db64 commit 5e466de
Show file tree
Hide file tree
Showing 26 changed files with 247 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,10 @@
<optional>true</optional>
<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 @@ -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 {
Expand Down Expand Up @@ -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";
}
}
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(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());
hasExecuted = true;
}

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)));
}
}
}
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().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().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().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().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().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().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>
Loading

0 comments on commit 5e466de

Please sign in to comment.