Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Awaitility to instead of Thread sheep method. #389

Merged
merged 2 commits into from Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions curator-client/pom.xml
Expand Up @@ -82,6 +82,11 @@
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
64 changes: 15 additions & 49 deletions curator-client/src/test/java/org/apache/curator/BasicTests.java
Expand Up @@ -23,21 +23,21 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.time.Duration;
import org.apache.curator.ensemble.fixed.FixedEnsembleProvider;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.test.BaseClassForTests;
import org.apache.curator.test.Timing;
import org.apache.curator.utils.ZookeeperFactory;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -47,14 +47,8 @@ public class BasicTests extends BaseClassForTests
public void testFactory() throws Exception
{
final ZooKeeper mockZookeeper = Mockito.mock(ZooKeeper.class);
ZookeeperFactory zookeeperFactory = new ZookeeperFactory()
{
@Override
public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws Exception
{
return mockZookeeper;
}
};
ZookeeperFactory zookeeperFactory =
(connectString, sessionTimeout, watcher, canBeReadOnly) -> mockZookeeper;
CuratorZookeeperClient client = new CuratorZookeeperClient(zookeeperFactory, new FixedEnsembleProvider(server.getConnectString()), 10000, 10000, null, new RetryOneTime(1), false);
client.start();
assertEquals(client.getZooKeeper(), mockZookeeper);
Expand All @@ -68,15 +62,10 @@ public void testExpiredSession() throws Exception
final Timing timing = new Timing();

final CountDownLatch latch = new CountDownLatch(1);
Watcher watcher = new Watcher()
{
@Override
public void process(WatchedEvent event)
Watcher watcher = event -> {
if ( event.getState() == Watcher.Event.KeeperState.Expired )
{
if ( event.getState() == Event.KeeperState.Expired )
{
latch.countDown();
}
latch.countDown();
}
};

Expand All @@ -88,11 +77,7 @@ public void process(WatchedEvent event)
RetryLoop.callWithRetry
(
client,
new Callable<Object>()
{
@Override
public Object call() throws Exception
{
() -> {
if ( firstTime.compareAndSet(true, false) )
{
try
Expand All @@ -113,7 +98,6 @@ public Object call() throws Exception
assertNotNull(zooKeeper.exists("/foo", false));
return null;
}
}
);
}
finally
Expand Down Expand Up @@ -166,34 +150,16 @@ public void testSimple() throws Exception
}

@Test
public void testBackgroundConnect() throws Exception
public void testBackgroundConnect() throws Exception
{
final int CONNECTION_TIMEOUT_MS = 4000;

CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), 10000, CONNECTION_TIMEOUT_MS, null, new RetryOneTime(1));
try
{
try (CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), 10000,
CONNECTION_TIMEOUT_MS, null, new RetryOneTime(1))) {
assertFalse(client.isConnected());
client.start();

outer: do
{
for ( int i = 0; i < (CONNECTION_TIMEOUT_MS / 1000); ++i )
{
if ( client.isConnected() )
{
break outer;
}

Thread.sleep(CONNECTION_TIMEOUT_MS);
}

fail();
} while ( false );
}
finally
{
client.close();
Awaitility.await()
.atMost(Duration.ofMillis(CONNECTION_TIMEOUT_MS))
.untilAsserted(() -> Assertions.assertTrue(client.isConnected()));
}
}
}
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.common.collect.Lists;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -89,11 +90,7 @@ public void testBasicCallable() throws InterruptedException
{
service.submit
(
new Callable<Void>()
{
@Override
public Void call() throws Exception
{
(Callable<Void>) () -> {
try
{
startLatch.countDown();
Expand All @@ -109,7 +106,6 @@ public Void call() throws Exception
}
return null;
}
}
);
}

Expand All @@ -128,11 +124,7 @@ public void testListeningRunnable() throws InterruptedException
{
Future<?> future = service.submit
(
new Runnable()
{
@Override
public void run()
{
() -> {
try
{
startLatch.countDown();
Expand All @@ -143,7 +135,6 @@ public void run()
Thread.currentThread().interrupt();
}
}
}
);
futures.add(future);
}
Expand All @@ -168,11 +159,7 @@ public void testListeningCallable() throws InterruptedException
{
Future<?> future = service.submit
(
new Callable<Void>()
{
@Override
public Void call() throws Exception
{
(Callable<Void>) () -> {
try
{
startLatch.countDown();
Expand All @@ -184,7 +171,6 @@ public Void call() throws Exception
}
return null;
}
}
);
futures.add(future);
}
Expand All @@ -204,11 +190,7 @@ public void testPartialRunnable() throws InterruptedException
final CountDownLatch outsideLatch = new CountDownLatch(1);
executorService.submit
(
new Runnable()
{
@Override
public void run()
{
() -> {
try
{
Thread.currentThread().join();
Expand All @@ -222,7 +204,6 @@ public void run()
outsideLatch.countDown();
}
}
}
);

CloseableExecutorService service = new CloseableExecutorService(executorService);
Expand All @@ -233,10 +214,7 @@ public void run()
submitRunnable(service, startLatch, latch);
}

while ( service.size() < QTY )
{
Thread.sleep(100);
}
Awaitility.await().until(()-> service.size() >= QTY);

assertTrue(startLatch.await(3, TimeUnit.SECONDS));
service.close();
Expand All @@ -248,11 +226,7 @@ private void submitRunnable(CloseableExecutorService service, final CountDownLat
{
service.submit
(
new Runnable()
{
@Override
public void run()
{
() -> {
try
{
startLatch.countDown();
Expand All @@ -267,7 +241,6 @@ public void run()
latch.countDown();
}
}
}
);
}
}
9 changes: 9 additions & 0 deletions pom.xml
Expand Up @@ -98,6 +98,7 @@
<dropwizard-version>3.2.5</dropwizard-version>
<snappy-version>1.1.7</snappy-version>
<build-helper-maven-plugin-version>3.1.0</build-helper-maven-plugin-version>
<awaitility-version>4.1.0</awaitility-version>

<!-- OSGi Properties -->
<osgi.export.package />
Expand Down Expand Up @@ -625,6 +626,14 @@
<artifactId>snappy-java</artifactId>
<version>${snappy-version}</version>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility-version}</version>
<scope>test</scope>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down