Skip to content

Commit

Permalink
Use Awaitility to instead of Thread.sleep (#2333)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 29, 2023
1 parent fbd53d1 commit 4944f53
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
5 changes: 5 additions & 0 deletions ecosystem/tracing/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
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;
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;
Expand All @@ -55,13 +57,11 @@ void assertRegisterFailure() {
}

@Test
void assertPost() throws InterruptedException {
void assertPost() {
jobTracingEventBus = new JobTracingEventBus(new TracingConfiguration<>("TEST", jobEventCaller));
assertIsRegistered(true);
jobTracingEventBus.post(new JobExecutionEvent("localhost", "127.0.0.1", "fake_task_id", "test_event_bus_job", JobExecutionEvent.ExecutionSource.NORMAL_TRIGGER, 0));
while (!TestTracingListener.isExecutionEventCalled()) {
Thread.sleep(100L);
}
Awaitility.await().pollDelay(100L, TimeUnit.MILLISECONDS).until(TestTracingListener::isExecutionEventCalled);
verify(jobEventCaller).call();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -33,6 +34,7 @@
import org.quartz.SchedulerException;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -91,26 +93,23 @@ void assertShutdown() throws SchedulerException {
assertTrue(getScheduler(oneOffJobBootstrap).isShutdown());
}

@SneakyThrows
@SneakyThrows(ReflectiveOperationException.class)
private JobScheduler getJobScheduler(final OneOffJobBootstrap oneOffJobBootstrap) {
Field field = OneOffJobBootstrap.class.getDeclaredField("jobScheduler");
field.setAccessible(true);
return (JobScheduler) field.get(oneOffJobBootstrap);
}

@SneakyThrows
@SneakyThrows(ReflectiveOperationException.class)
private Scheduler getScheduler(final OneOffJobBootstrap oneOffJobBootstrap) {
JobScheduler jobScheduler = getJobScheduler(oneOffJobBootstrap);
Field schedulerField = JobScheduleController.class.getDeclaredField("scheduler");
schedulerField.setAccessible(true);
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());
}
}
4 changes: 4 additions & 0 deletions registry-center/provider/zookeeper-curator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.curator.test.KillSession;
import org.apache.shardingsphere.elasticjob.reg.base.ElectionCandidate;
import org.apache.shardingsphere.elasticjob.test.util.EmbedTestingServer;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -33,6 +34,7 @@

import java.lang.reflect.Field;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import static org.mockito.Mockito.atLeastOnce;
Expand Down Expand Up @@ -79,11 +81,8 @@ void assertContend() throws Exception {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void execute() {
handleConcurrentExecution();
}
try {
Thread.sleep(100);
Thread.sleep(100L);
} catch (final InterruptedException ex) {
waitingThread.interrupt();
}
Expand Down

0 comments on commit 4944f53

Please sign in to comment.