This is an automated email from the ASF dual-hosted git repository.
zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git
The following commit(s) were added to refs/heads/master by this push:
new 4944f53a7 Use Awaitility to instead of Thread.sleep (#2333)
4944f53a7 is described below
commit 4944f53a73a241643de8f6118efa10f1ff638d44
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Oct 29 12:22:34 2023 +0800
Use Awaitility to instead of Thread.sleep (#2333)
---
ecosystem/tracing/api/pom.xml | 5 +++++
.../elasticjob/tracing/JobTracingEventBusTest.java | 8 ++++----
.../kernel/api/bootstrap/impl/OneOffJobBootstrapTest.java | 11 +++++------
registry-center/provider/zookeeper-curator/pom.xml | 4 ++++
.../reg/zookeeper/ZookeeperElectionServiceTest.java | 7 +++----
.../zookeeper/ZookeeperRegistryCenterExecuteInLeaderTest.java | 2 +-
6 files changed, 22 insertions(+), 15 deletions(-)
diff --git a/ecosystem/tracing/api/pom.xml b/ecosystem/tracing/api/pom.xml
index db999d072..2d1c9eedf 100644
--- a/ecosystem/tracing/api/pom.xml
+++ b/ecosystem/tracing/api/pom.xml
@@ -42,5 +42,10 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>org.awaitility</groupId>
+ <artifactId>awaitility</artifactId>
+ </dependency>
</dependencies>
</project>
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 d1ba01987..239950f66 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.JobEvent;
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.Mock;
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 @@ class JobTracingEventBusTest {
}
@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 91e7c4d67..908a0ee81 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.ZookeeperConfiguration
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.Scheduler;
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 @@ class OneOffJobBootstrapTest {
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 @@ class OneOffJobBootstrapTest {
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 539026923..97609432a 100644
--- a/registry-center/provider/zookeeper-curator/pom.xml
+++ b/registry-center/provider/zookeeper-curator/pom.xml
@@ -52,5 +52,9 @@
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.awaitility</groupId>
+ <artifactId>awaitility</artifactId>
+ </dependency>
</dependencies>
</project>
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 9256db60f..131479fd0 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.retry.RetryOneTime;
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 org.mockito.junit.jupiter.MockitoExtension;
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 @@ class ZookeeperElectionServiceTest {
verify(anotherElectionCandidate, atLeastOnce()).stopLeadership();
}
- @SneakyThrows
private void blockUntilCondition(final Supplier<Boolean> 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 2bead8dae..f44fb68d2 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 @@ class ZookeeperRegistryCenterExecuteInLeaderTest {
handleConcurrentExecution();
}
try {
- Thread.sleep(100);
+ Thread.sleep(100L);
} catch (final InterruptedException ex) {
waitingThread.interrupt();
}