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

CURATOR-603 Use Awaitility to instead of Thread sleep method in recipes module. #392

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions curator-recipes/pom.xml
Expand Up @@ -86,6 +86,12 @@
<artifactId>commons-math</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.apache.curator.test.Timing;
import org.apache.curator.test.compatibility.Timing2;
import org.apache.curator.utils.CloseableUtils;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -721,10 +722,8 @@ public void takeLeadership(CuratorFramework client) throws Exception
leaderSelector1.start();
leaderSelector2.start();

while ( !leaderSelector1.hasLeadership() && !leaderSelector2.hasLeadership() )
{
Thread.sleep(1000);
}
Awaitility.await()
.until(()-> leaderSelector1.hasLeadership() || leaderSelector2.hasLeadership());

assertNotSame(leaderSelector1.hasLeadership(), leaderSelector2.hasLeadership());

Expand Down
Expand Up @@ -37,6 +37,7 @@
import org.apache.curator.utils.CloseableUtils;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.KeeperException;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;

import java.util.List;
Expand Down Expand Up @@ -343,10 +344,8 @@ public Object call() throws Exception
if ( isFirst.compareAndSet(true, false) )
{
semaphore.release(THREAD_QTY - 1);
while ( semaphore.availablePermits() > 0 )
{
Thread.sleep(100);
}
Awaitility.await()
.until(()-> semaphore.availablePermits() <= 0);
}
else
{
Expand Down Expand Up @@ -538,11 +537,8 @@ public Object call() throws Exception
}
);

while ( !mutexForClient1.isAcquiredInThisProcess() && !mutexForClient2.isAcquiredInThisProcess() )
{
Thread.sleep(1000);
assertFalse(future1.isDone() && future2.isDone());
}
Awaitility.await()
.until(()-> mutexForClient1.isAcquiredInThisProcess() || mutexForClient2.isAcquiredInThisProcess());

assertTrue(mutexForClient1.isAcquiredInThisProcess() != mutexForClient2.isAcquiredInThisProcess());
Thread.sleep(1000);
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.curator.retry.RetryOneTime;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.List;
Expand Down Expand Up @@ -106,13 +107,8 @@ public void testOrdering() throws Exception
queue.put(new TestQueueItem(id), id);
}

int iteration = 0;
while ( consumer.size() < ITEM_QTY )
{
assertTrue(++iteration < ITEM_QTY);
Thread.sleep(1000);
}

Awaitility.await()
.until(()-> consumer.size() >= ITEM_QTY);
int i = 0;
for ( TestQueueItem item : consumer.getItems() )
{
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.test.Timing;
import org.apache.zookeeper.CreateMode;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
Expand Down Expand Up @@ -250,19 +251,15 @@ public void putMultiCompleted(MultiItem<TestQueueItem> items)
ExecutorService service = Executors.newCachedThreadPool();
service.submit(producer);

int iteration = 0;
while ( consumer.size() < itemQty )
{
assertTrue(++iteration < 10);
Thread.sleep(1000);
}
Awaitility.await()
.until(()->consumer.size() >= itemQty);

int i = 0;
for ( TestQueueItem item : consumer.getItems() )
{
assertEquals(item.str, Integer.toString(i++));
}

assertEquals(listenerCalls.get(), itemQty);
}
finally
Expand All @@ -271,7 +268,7 @@ public void putMultiCompleted(MultiItem<TestQueueItem> items)
CloseableUtils.closeQuietly(client);
}
}

@Test
public void testErrorMode() throws Exception
{
Expand Down Expand Up @@ -475,7 +472,7 @@ public void consumeMessage(TestQueueItem message) throws Exception
throw new Exception("dummy"); // simulate a crash
}
}

addToTakenItems(message, takenItems, itemQty);
synchronized(takenItemsForConsumer1)
{
Expand Down Expand Up @@ -679,12 +676,8 @@ public void testMultiPutterSingleGetter() throws Exception
service.submit(producer1);
service.submit(producer2);

int iteration = 0;
while ( consumer.size() < itemQty )
{
assertTrue(++iteration < 10);
Thread.sleep(1000);
}
Awaitility.await()
.until(()-> consumer.size() >= itemQty);

List<TestQueueItem> items = consumer.getItems();

Expand Down Expand Up @@ -776,13 +769,8 @@ public void testSimple() throws Exception
ExecutorService service = Executors.newCachedThreadPool();
service.submit(producer);

int iteration = 0;
while ( consumer.size() < itemQty )
{
assertTrue(++iteration < 10);
Thread.sleep(1000);
}

Awaitility.await()
.until(()->consumer.size() >= itemQty);
int i = 0;
for ( TestQueueItem item : consumer.getItems() )
{
Expand Down