Skip to content

Commit

Permalink
GEODE-5630: fixup usage of Awaitility
Browse files Browse the repository at this point in the history
  • Loading branch information
kirklund committed Aug 27, 2018
1 parent 85953f0 commit 45138b4
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.apache.geode.test.junit.rules;

import static org.assertj.core.api.Assertions.assertThat;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Expand All @@ -37,7 +39,8 @@ protected MySqlConnectionRule(DockerComposeRule dockerRule, String serviceName,
@Override
public Connection getConnection() throws SQLException {
Awaitility.await().ignoreExceptions().atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> DriverManager.getConnection(getCreateDbConnectionUrl()));
.untilAsserted(
() -> assertThat(DriverManager.getConnection(getCreateDbConnectionUrl())).isNotNull());
String dbName = getDbName();
if (dbName != null) {
Connection connection = DriverManager.getConnection(getCreateDbConnectionUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.apache.geode.test.junit.rules;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
Expand Down Expand Up @@ -64,7 +66,7 @@ protected String getDbName() {
public Connection getConnection() throws SQLException {
String connectionUrl = getConnectionUrl();
Awaitility.await().ignoreExceptions().atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> DriverManager.getConnection(connectionUrl));
.untilAsserted(() -> assertThat(DriverManager.getConnection(connectionUrl)).isNotNull());
Connection connection = DriverManager.getConnection(connectionUrl);
return connection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.geode.test.dunit.NetworkUtils.getServerHostName;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -436,7 +437,7 @@ private Object getInVM(VM vm, final String regionName, final Serializable key) {
private void putAndWaitForSuccess(VM vm, final String regionName, final Serializable key,
final Serializable value) {
Awaitility.await().atMost(MAX_WAIT, MILLISECONDS).untilAsserted(() -> {
putInVM(vm, regionName, key, value);
assertThatCode(() -> putInVM(vm, regionName, key, value)).doesNotThrowAnyException();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.awaitility.Awaitility;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -518,7 +517,7 @@ public void testCreateAsyncIndexWhileDoingGIIAndCompareQueryResults() throws Exc
}

public void validateIndexSize() {
Awaitility.await().atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
await().atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
boolean indexSizeCheck_NAME = validateIndexSizeForRegion(NAME);
boolean indexSizeCheck_REP_REG_NAME = validateIndexSizeForRegion(REP_REG_NAME);
boolean indexSizeCheck_PERSISTENT_REG_NAME = validateIndexSizeForRegion(PERSISTENT_REG_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void entriesShouldNotExpireDuringGII() throws Exception {

doRegionOps.await();

await().untilAsserted(() -> region.values().isEmpty());
await().until(() -> region.values().isEmpty());

assertThat(region.values()).hasSize(0);
assertThat(region.keySet()).hasSize(ENTRY_COUNT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ private void waitForParallelAsyncEventQueueSize(int expectedRegionQueueSize) {
}

private void waitForPrimaryToMove() {
await().atMost(TWO_MINUTES).untilAsserted(() -> getPrimaryMovingAsyncEventListener().isMoved());
await().atMost(TWO_MINUTES).until(() -> getPrimaryMovingAsyncEventListener().isMoved());
}

private InternalGatewaySender getInternalGatewaySender() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.awaitility.Awaitility;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -224,13 +223,8 @@ public void testRemoteBeanKnowledge_MaintainLocatorAndCrashServer()

private void waitForMBeanFederationFrom(int numMemberMBeans, MemberVM member) {
String memberName = "server-" + member.getVM().getId();
Awaitility.waitAtMost(10, SECONDS).untilAsserted(() -> {
List<ObjectName> beans = null;
try {
beans = getFederatedGemfireBeansFrom(locator1);
} catch (IOException e) {
e.printStackTrace();
}
waitAtMost(10, SECONDS).untilAsserted(() -> {
List<ObjectName> beans = getFederatedGemfireBeansFrom(locator1);
List<ObjectName> beanList =
beans.stream().filter(b -> b.toString().contains(memberName)).sorted().collect(toList());
assertThat(beanList.size()).isEqualTo(numMemberMBeans);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,7 @@ private void createClientCache(final String host, final int serverPort) {
private void verifyClientIds(final DistributedMember serverMember, final int serverPort)
throws Exception {
CacheServerMXBean cacheServerMXBean = awaitCacheServerMXBean(serverMember, serverPort);
await().untilAsserted(() -> {
try {
assertThat(cacheServerMXBean.getClientIds()).hasSize(2);
} catch (Exception e) {
throw new Error(e);
}
});
await().untilAsserted(() -> assertThat(cacheServerMXBean.getClientIds()).hasSize(2));
assertThat(cacheServerMXBean.getClientIds()).hasSize(2); // TODO: write better assertions
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package org.apache.geode.internal.cache.tier.sockets;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
Expand All @@ -36,11 +38,8 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.apache.shiro.subject.Subject;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand Down Expand Up @@ -152,21 +151,12 @@ public void testCacheClientNotifier_NotifyClients_QRMCausesPrematureRemovalFromH
}

// Verify that we do not hang in peek() for the second proxy due to the wrapper
Awaitility.waitAtMost(new Duration(30, TimeUnit.SECONDS)).untilAsserted(() -> {
try {
Object eventPeeked = null;
while (eventPeeked == null) {
// Simulating message dispatching. We peek() and remove() but aren't testing
// the actual message delivery for this test.
eventPeeked = cacheClientProxyTwo.getHARegionQueue().peek();
if (eventPeeked != null) {
cacheClientProxyTwo.getHARegionQueue().remove();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
throw (new RuntimeException(e));
await().atMost(30, SECONDS).until(() -> {
if (cacheClientProxyTwo.getHARegionQueue().peek() != null) {
cacheClientProxyTwo.getHARegionQueue().remove();
return true;
}
return false;
});

Assert.assertEquals("Expected the HAContainer to be empty", 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.apache.geode.cache.client.internal;

import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.MINUTES;

import org.awaitility.Awaitility;
import org.junit.Rule;
Expand Down Expand Up @@ -50,12 +50,11 @@ public void run() {
}
});

/**
/*
* Sometimes need to wait for more than sec as thread execution takes time.
*/
Awaitility.await("Waiting for exception").atMost(60l, TimeUnit.SECONDS).untilAsserted(() -> {
systemErrRule.getLog().contains(erroMsg);
});
Awaitility.await("Waiting for exception").atMost(1, MINUTES)
.until(() -> systemErrRule.getLog().contains(erroMsg));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.apache.geode.cache.query.internal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -91,7 +92,9 @@ public void cqQueryIsNotMonitored() {
private Thread createQueryExecutionThread(int i) {
Thread thread = new Thread(() -> {
// make sure the threadlocal variable is updated
Awaitility.await().untilAsserted(() -> QueryMonitor.isQueryExecutionCanceled());
Awaitility.await()
.untilAsserted(() -> assertThatCode(() -> QueryMonitor.isQueryExecutionCanceled())
.doesNotThrowAnyException());
});
thread.setName("query" + i);
return thread;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,12 @@ private void checkClientHealthStatus(int expectedNumPuts,
SystemManagementService service =
(SystemManagementService) ManagementService.getExistingManagementService(cache);
CacheServerMXBean serviceMBean = service.getJMXAdapter().getClientServiceMXBean(serverPort);
try {
String clientId = serviceMBean.getClientIds()[0];
ClientHealthStatus status = serviceMBean.showClientStats(clientId);
assertThat(status.getNumOfPuts()).isEqualTo(expectedNumPuts);
assertThat(status.getPoolStats().keySet())
.containsExactlyInAnyOrder(expectedPoolStatKeys);
} catch (Exception e) {
throw new RuntimeException(e);
}
String clientId = serviceMBean.getClientIds()[0];
ClientHealthStatus status = serviceMBean.showClientStats(clientId);

assertThat(status.getNumOfPuts()).isEqualTo(expectedNumPuts);
assertThat(status.getPoolStats().keySet())
.containsExactlyInAnyOrder(expectedPoolStatKeys);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,8 @@ private void doTestSubscriptionWithMixedServersAndPeerFeed(String version,
server2.invoke("wait for failover queue to drain", () -> {
CacheClientProxy proxy =
CacheClientNotifier.getInstance().getClientProxies().iterator().next();
Awaitility.await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> {
proxy.getHARegionQueue().isEmpty();
});
Awaitility.await().atMost(30, TimeUnit.SECONDS)
.until(() -> proxy.getHARegionQueue().isEmpty());
});

// the client should now get duplicate events from the current-version server
Expand All @@ -204,9 +203,8 @@ private void doTestSubscriptionWithMixedServersAndPeerFeed(String version,
server3.invoke("wait for failover queue to drain", () -> {
CacheClientProxy proxy =
CacheClientNotifier.getInstance().getClientProxies().iterator().next();
Awaitility.await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> {
proxy.getHARegionQueue().isEmpty();
});
Awaitility.await().atMost(30, TimeUnit.SECONDS)
.until(() -> proxy.getHARegionQueue().isEmpty());
});

// the client should now get duplicate events from the current-version server
Expand Down Expand Up @@ -290,9 +288,8 @@ public void giiEventQueueShouldSucceedWithMixedVersions(String server1Version,
server2.invoke("wait for failover queue to drain", () -> {
CacheClientProxy proxy =
CacheClientNotifier.getInstance().getClientProxies().iterator().next();
Awaitility.await().atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
proxy.getHARegionQueue().isEmpty();
});
Awaitility.await().atMost(60, TimeUnit.SECONDS)
.until(() -> proxy.getHARegionQueue().isEmpty());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.REGION_NAME;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Collection;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -76,13 +75,7 @@ public void regionWithExpirationSetMustAlsoRemoveLuceneIndexEntries(
LuceneQuery<Integer, TestObject> luceneQuery = luceneService.createLuceneQueryFactory()
.setLimit(100).create(INDEX_NAME, REGION_NAME, "world", "text");

Collection luceneResultList = null;
try {
luceneResultList = luceneQuery.findKeys();
} catch (LuceneQueryException e) {
e.printStackTrace();
fail();
}
Collection luceneResultList = luceneQuery.findKeys();
assertEquals(0, luceneResultList.size());
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.apache.geode.cache.lucene;

import static java.util.concurrent.TimeUnit.SECONDS;
import static junitparams.JUnitParamsRunner.$;
import static org.apache.geode.cache.RegionShortcut.PARTITION;
import static org.apache.geode.cache.RegionShortcut.PARTITION_OVERFLOW;
Expand All @@ -30,18 +31,18 @@
import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.getIndexQueue;
import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.pauseSender;
import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.verifyIndexFinishFlushing;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.awaitility.Awaitility;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand Down Expand Up @@ -228,13 +229,7 @@ private void verifyQueryResultSize(String indexName, String regionName, String q
String defaultField, int size) throws Exception {
LuceneQuery query = luceneService.createLuceneQueryFactory().create(indexName, regionName,
queryString, defaultField);
Awaitility.await().atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
try {
assertEquals(size, query.findPages().size());
} catch (LuceneQueryException e) {
throw new RuntimeException(e);
}
});
await().atMost(60, SECONDS).untilAsserted(() -> assertThat(query.findPages()).hasSize(size));
}

private void verifyInternalRegions(Consumer<LocalRegion> verify) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,11 @@ void putSerializableObject(VM putter, String regionName, int start, int end)

private void waitForRegionToHaveExpectedSize(String regionName, int expectedRegionSize) {
Awaitility.await().atMost(60, TimeUnit.SECONDS).untilAsserted(() -> {
try {
Object region =
cache.getClass().getMethod("getRegion", String.class).invoke(cache, regionName);
int regionSize = (int) region.getClass().getMethod("size").invoke(region);
assertEquals("Region size not as expected after 60 seconds", expectedRegionSize,
regionSize);
} catch (Exception e) {
throw new RuntimeException();
}

Object region =
cache.getClass().getMethod("getRegion", String.class).invoke(cache, regionName);
int regionSize = (int) region.getClass().getMethod("size").invoke(region);
assertEquals("Region size not as expected after 60 seconds", expectedRegionSize,
regionSize);
});
}

Expand Down

0 comments on commit 45138b4

Please sign in to comment.