Skip to content

Commit

Permalink
Replace while loop with assertEventually().
Browse files Browse the repository at this point in the history
  • Loading branch information
apcj committed Jul 8, 2016
1 parent a1fece3 commit 82187ff
Showing 1 changed file with 19 additions and 16 deletions.
Expand Up @@ -19,9 +19,6 @@
*/
package org.neo4j.coreedge.scenarios;

import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
Expand All @@ -30,9 +27,11 @@
import java.util.Set;
import java.util.SortedMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BinaryOperator;

import org.junit.Rule;
import org.junit.Test;

import org.neo4j.coreedge.discovery.Cluster;
import org.neo4j.coreedge.discovery.CoreServer;
import org.neo4j.coreedge.discovery.EdgeServer;
Expand All @@ -58,13 +57,15 @@
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore;
import org.neo4j.kernel.lifecycle.LifecycleException;
import org.neo4j.logging.Log;
import org.neo4j.storageengine.api.lock.AcquireLockTimeoutException;
import org.neo4j.test.DbRepresentation;
import org.neo4j.test.coreedge.ClusterRule;

import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
Expand All @@ -74,6 +75,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

import static org.neo4j.coreedge.raft.log.segmented.SegmentedRaftLog.SEGMENTED_LOG_DIRECTORY_NAME;
import static org.neo4j.coreedge.server.core.EnterpriseCoreEditionModule.CLUSTER_STATE_DIRECTORY_NAME;
import static org.neo4j.function.Predicates.awaitEx;
Expand Down Expand Up @@ -339,25 +341,26 @@ private long versionBy( File storeDir, BinaryOperator<Long> operator )
return logs.keySet().stream().reduce( operator ).orElseThrow( IllegalStateException::new );
}

private GraphDatabaseService executeOnLeaderWithRetry( Workload workload, Cluster cluster ) throws TimeoutException
private void executeOnLeaderWithRetry( Workload workload, Cluster cluster ) throws Exception
{
CoreGraphDatabase coreDB;
while ( true )
{
coreDB = cluster.awaitLeader( 5000 ).database();
try ( Transaction tx = coreDB.beginTx() )
assertEventually( "Executed on leader", () -> {
try
{
workload.doWork( coreDB );
tx.success();
break;
CoreGraphDatabase coreDB = cluster.awaitLeader( 5000 ).database();
try ( Transaction tx = coreDB.beginTx() )
{
workload.doWork( coreDB );
tx.success();
return true;
}
}
catch ( TransactionFailureException e )
catch ( AcquireLockTimeoutException | TransactionFailureException e )
{
// print the stack trace for diagnostic purposes, but retry as this is most likely a transient failure
e.printStackTrace();
return false;
}
}
return coreDB;
}, is( true ), 30, SECONDS );
}

private interface Workload
Expand Down

0 comments on commit 82187ff

Please sign in to comment.