Skip to content

Commit 4dffb5e

Browse files
authored
Use Awaitility to instead of Thread sleep method. (#389)
1 parent e42a07a commit 4dffb5e

File tree

6 files changed

+44
-87
lines changed

6 files changed

+44
-87
lines changed

curator-client/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<artifactId>slf4j-log4j12</artifactId>
8383
<scope>test</scope>
8484
</dependency>
85+
<dependency>
86+
<groupId>org.awaitility</groupId>
87+
<artifactId>awaitility</artifactId>
88+
<scope>test</scope>
89+
</dependency>
8590
</dependencies>
8691

8792
<build>

curator-client/src/test/java/org/apache/curator/BasicTests.java

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@
2323
import static org.junit.jupiter.api.Assertions.assertFalse;
2424
import static org.junit.jupiter.api.Assertions.assertNotNull;
2525
import static org.junit.jupiter.api.Assertions.assertTrue;
26-
import static org.junit.jupiter.api.Assertions.fail;
26+
import java.time.Duration;
2727
import org.apache.curator.ensemble.fixed.FixedEnsembleProvider;
2828
import org.apache.curator.retry.RetryOneTime;
2929
import org.apache.curator.test.BaseClassForTests;
3030
import org.apache.curator.test.Timing;
3131
import org.apache.curator.utils.ZookeeperFactory;
3232
import org.apache.zookeeper.CreateMode;
3333
import org.apache.zookeeper.KeeperException;
34-
import org.apache.zookeeper.WatchedEvent;
3534
import org.apache.zookeeper.Watcher;
3635
import org.apache.zookeeper.ZooDefs;
3736
import org.apache.zookeeper.ZooKeeper;
37+
import org.awaitility.Awaitility;
38+
import org.junit.jupiter.api.Assertions;
3839
import org.junit.jupiter.api.Test;
3940
import org.mockito.Mockito;
40-
import java.util.concurrent.Callable;
4141
import java.util.concurrent.CountDownLatch;
4242
import java.util.concurrent.atomic.AtomicBoolean;
4343

@@ -47,14 +47,8 @@ public class BasicTests extends BaseClassForTests
4747
public void testFactory() throws Exception
4848
{
4949
final ZooKeeper mockZookeeper = Mockito.mock(ZooKeeper.class);
50-
ZookeeperFactory zookeeperFactory = new ZookeeperFactory()
51-
{
52-
@Override
53-
public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws Exception
54-
{
55-
return mockZookeeper;
56-
}
57-
};
50+
ZookeeperFactory zookeeperFactory =
51+
(connectString, sessionTimeout, watcher, canBeReadOnly) -> mockZookeeper;
5852
CuratorZookeeperClient client = new CuratorZookeeperClient(zookeeperFactory, new FixedEnsembleProvider(server.getConnectString()), 10000, 10000, null, new RetryOneTime(1), false);
5953
client.start();
6054
assertEquals(client.getZooKeeper(), mockZookeeper);
@@ -68,15 +62,10 @@ public void testExpiredSession() throws Exception
6862
final Timing timing = new Timing();
6963

7064
final CountDownLatch latch = new CountDownLatch(1);
71-
Watcher watcher = new Watcher()
72-
{
73-
@Override
74-
public void process(WatchedEvent event)
65+
Watcher watcher = event -> {
66+
if ( event.getState() == Watcher.Event.KeeperState.Expired )
7567
{
76-
if ( event.getState() == Event.KeeperState.Expired )
77-
{
78-
latch.countDown();
79-
}
68+
latch.countDown();
8069
}
8170
};
8271

@@ -88,11 +77,7 @@ public void process(WatchedEvent event)
8877
RetryLoop.callWithRetry
8978
(
9079
client,
91-
new Callable<Object>()
92-
{
93-
@Override
94-
public Object call() throws Exception
95-
{
80+
() -> {
9681
if ( firstTime.compareAndSet(true, false) )
9782
{
9883
try
@@ -113,7 +98,6 @@ public Object call() throws Exception
11398
assertNotNull(zooKeeper.exists("/foo", false));
11499
return null;
115100
}
116-
}
117101
);
118102
}
119103
finally
@@ -166,34 +150,16 @@ public void testSimple() throws Exception
166150
}
167151

168152
@Test
169-
public void testBackgroundConnect() throws Exception
153+
public void testBackgroundConnect() throws Exception
170154
{
171155
final int CONNECTION_TIMEOUT_MS = 4000;
172-
173-
CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), 10000, CONNECTION_TIMEOUT_MS, null, new RetryOneTime(1));
174-
try
175-
{
156+
try (CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), 10000,
157+
CONNECTION_TIMEOUT_MS, null, new RetryOneTime(1))) {
176158
assertFalse(client.isConnected());
177159
client.start();
178-
179-
outer: do
180-
{
181-
for ( int i = 0; i < (CONNECTION_TIMEOUT_MS / 1000); ++i )
182-
{
183-
if ( client.isConnected() )
184-
{
185-
break outer;
186-
}
187-
188-
Thread.sleep(CONNECTION_TIMEOUT_MS);
189-
}
190-
191-
fail();
192-
} while ( false );
193-
}
194-
finally
195-
{
196-
client.close();
160+
Awaitility.await()
161+
.atMost(Duration.ofMillis(CONNECTION_TIMEOUT_MS))
162+
.untilAsserted(() -> Assertions.assertTrue(client.isConnected()));
197163
}
198164
}
199165
}

curator-client/src/test/java/org/apache/curator/utils/TestCloseableExecutorService.java

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.jupiter.api.Assertions.assertEquals;
2323
import static org.junit.jupiter.api.Assertions.assertTrue;
2424
import com.google.common.collect.Lists;
25+
import org.awaitility.Awaitility;
2526
import org.junit.jupiter.api.AfterEach;
2627
import org.junit.jupiter.api.BeforeEach;
2728
import org.junit.jupiter.api.Test;
@@ -89,11 +90,7 @@ public void testBasicCallable() throws InterruptedException
8990
{
9091
service.submit
9192
(
92-
new Callable<Void>()
93-
{
94-
@Override
95-
public Void call() throws Exception
96-
{
93+
(Callable<Void>) () -> {
9794
try
9895
{
9996
startLatch.countDown();
@@ -109,7 +106,6 @@ public Void call() throws Exception
109106
}
110107
return null;
111108
}
112-
}
113109
);
114110
}
115111

@@ -128,11 +124,7 @@ public void testListeningRunnable() throws InterruptedException
128124
{
129125
Future<?> future = service.submit
130126
(
131-
new Runnable()
132-
{
133-
@Override
134-
public void run()
135-
{
127+
() -> {
136128
try
137129
{
138130
startLatch.countDown();
@@ -143,7 +135,6 @@ public void run()
143135
Thread.currentThread().interrupt();
144136
}
145137
}
146-
}
147138
);
148139
futures.add(future);
149140
}
@@ -168,11 +159,7 @@ public void testListeningCallable() throws InterruptedException
168159
{
169160
Future<?> future = service.submit
170161
(
171-
new Callable<Void>()
172-
{
173-
@Override
174-
public Void call() throws Exception
175-
{
162+
(Callable<Void>) () -> {
176163
try
177164
{
178165
startLatch.countDown();
@@ -184,7 +171,6 @@ public Void call() throws Exception
184171
}
185172
return null;
186173
}
187-
}
188174
);
189175
futures.add(future);
190176
}
@@ -204,11 +190,7 @@ public void testPartialRunnable() throws InterruptedException
204190
final CountDownLatch outsideLatch = new CountDownLatch(1);
205191
executorService.submit
206192
(
207-
new Runnable()
208-
{
209-
@Override
210-
public void run()
211-
{
193+
() -> {
212194
try
213195
{
214196
Thread.currentThread().join();
@@ -222,7 +204,6 @@ public void run()
222204
outsideLatch.countDown();
223205
}
224206
}
225-
}
226207
);
227208

228209
CloseableExecutorService service = new CloseableExecutorService(executorService);
@@ -233,10 +214,7 @@ public void run()
233214
submitRunnable(service, startLatch, latch);
234215
}
235216

236-
while ( service.size() < QTY )
237-
{
238-
Thread.sleep(100);
239-
}
217+
Awaitility.await().until(()-> service.size() >= QTY);
240218

241219
assertTrue(startLatch.await(3, TimeUnit.SECONDS));
242220
service.close();
@@ -248,11 +226,7 @@ private void submitRunnable(CloseableExecutorService service, final CountDownLat
248226
{
249227
service.submit
250228
(
251-
new Runnable()
252-
{
253-
@Override
254-
public void run()
255-
{
229+
() -> {
256230
try
257231
{
258232
startLatch.countDown();
@@ -267,7 +241,6 @@ public void run()
267241
latch.countDown();
268242
}
269243
}
270-
}
271244
);
272245
}
273246
}

curator-framework/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
<artifactId>slf4j-log4j12</artifactId>
8787
<scope>test</scope>
8888
</dependency>
89+
<dependency>
90+
<groupId>org.awaitility</groupId>
91+
<artifactId>awaitility</artifactId>
92+
<scope>test</scope>
93+
</dependency>
8994
</dependencies>
9095

9196
<build>

curator-framework/src/test/java/org/apache/curator/framework/imps/TestCleanState.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.curator.utils.CloseableUtils;
2525
import org.apache.zookeeper.ZooKeeper;
2626
import java.util.concurrent.Callable;
27+
import org.awaitility.Awaitility;
2728

2829
public class TestCleanState
2930
{
@@ -43,10 +44,8 @@ public static void closeAndTestClean(CuratorFramework client)
4344
EnsembleTracker ensembleTracker = internalClient.getEnsembleTracker();
4445
if ( ensembleTracker != null )
4546
{
46-
while ( ensembleTracker.hasOutstanding() )
47-
{
48-
Thread.sleep(100);
49-
}
47+
Awaitility.await()
48+
.until(() -> !ensembleTracker.hasOutstanding());
5049
ensembleTracker.close();
5150
}
5251
ZooKeeper zooKeeper = internalClient.getZooKeeper();

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<dropwizard-version>3.2.5</dropwizard-version>
101101
<snappy-version>1.1.7</snappy-version>
102102
<build-helper-maven-plugin-version>3.1.0</build-helper-maven-plugin-version>
103+
<awaitility-version>4.1.0</awaitility-version>
103104

104105
<!-- OSGi Properties -->
105106
<osgi.export.package />
@@ -627,6 +628,14 @@
627628
<artifactId>snappy-java</artifactId>
628629
<version>${snappy-version}</version>
629630
</dependency>
631+
632+
<dependency>
633+
<groupId>org.awaitility</groupId>
634+
<artifactId>awaitility</artifactId>
635+
<version>${awaitility-version}</version>
636+
<scope>test</scope>
637+
</dependency>
638+
630639
</dependencies>
631640
</dependencyManagement>
632641

0 commit comments

Comments
 (0)