Skip to content

Commit

Permalink
Use ClusterRule in IT
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegrohmann committed Jun 20, 2016
1 parent 14c0bbd commit 9ad3736
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 431 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
*/
package org.neo4j.coreedge.backup;

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

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
Expand All @@ -28,16 +32,11 @@
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;

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

import org.neo4j.backup.OnlineBackupSettings;
import org.neo4j.coreedge.TestStoreId;
import org.neo4j.coreedge.convert.ConversionVerifier;
import org.neo4j.coreedge.convert.ConvertClassicStoreCommand;
import org.neo4j.coreedge.convert.GenerateClusterSeedCommand;
import org.neo4j.coreedge.convert.SourceMetadata;
import org.neo4j.coreedge.discovery.Cluster;
import org.neo4j.coreedge.server.core.CoreGraphDatabase;
import org.neo4j.dbms.DatabaseManagementSystemSettings;
Expand All @@ -52,21 +51,20 @@
import org.neo4j.restore.RestoreDatabaseCommand;
import org.neo4j.test.DbRepresentation;
import org.neo4j.test.coreedge.ClusterRule;
import org.neo4j.test.rule.SuppressOutput;

import static java.util.stream.Collectors.toList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import static org.neo4j.backup.BackupEmbeddedIT.runBackupToolFromOtherJvmToGetExitCode;
import static org.neo4j.coreedge.TestStoreId.assertAllStoresHaveTheSameStoreId;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.helpers.collection.MapUtil.stringMap;

public class BackupCoreIT
{
// @Rule
// public SuppressOutput suppressOutput = suppress( SuppressOutput.System.out, SuppressOutput.System.err );
@Rule
public SuppressOutput suppressOutput = SuppressOutput.suppressAll();

@Rule
public ClusterRule clusterRule = new ClusterRule( BackupCoreIT.class )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.neo4j.kernel.GraphDatabaseDependencies;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade;
import org.neo4j.kernel.impl.store.format.RecordFormat;
import org.neo4j.kernel.impl.store.format.standard.StandardV3_0;
import org.neo4j.kernel.internal.DatabaseHealth;
import org.neo4j.logging.Level;
Expand All @@ -65,7 +66,7 @@
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.kernel.api.exceptions.Status.Transaction.LockSessionExpired;

public class Cluster implements AutoCloseable
public class Cluster
{
private static final String CLUSTER_NAME = "core-neo4j";
private static final int DEFAULT_TIMEOUT_MS = 15_000;
Expand Down Expand Up @@ -116,61 +117,12 @@ public void start() throws InterruptedException, ExecutionException
}
}

public static Cluster start( File parentDir, int noOfCoreServers, int noOfEdgeServers,
DiscoveryServiceFactory discoveryServiceFactory, Map<String, String> coreParams,
Map<String, IntFunction<String>> instanceCoreParams, String recordFormat )
throws ExecutionException, InterruptedException
{
Cluster cluster = new Cluster( parentDir, noOfCoreServers, noOfEdgeServers, discoveryServiceFactory,
coreParams, instanceCoreParams, stringMap(), emptyMap(), recordFormat );
cluster.start();
return cluster;
}

public static Cluster start( File parentDir, int noOfCoreServers, int noOfEdgeServers,
DiscoveryServiceFactory discoveryServiceFactory, String recordFormat )
throws ExecutionException, InterruptedException
{
return start( parentDir, noOfCoreServers, noOfEdgeServers, discoveryServiceFactory, stringMap(), emptyMap(),
recordFormat );
}

public static Cluster start( File parentDir, int noOfCoreServers, int noOfEdgeServers,
DiscoveryServiceFactory discoveryServiceFactory )
throws ExecutionException, InterruptedException
{
return start( parentDir, noOfCoreServers, noOfEdgeServers, discoveryServiceFactory, stringMap(), emptyMap(),
StandardV3_0.NAME );
}

public static Cluster start( File parentDir, int noOfCoreServers, int noOfEdgeServers )
throws ExecutionException, InterruptedException
{
return start( parentDir, noOfCoreServers, noOfEdgeServers, new HazelcastDiscoveryServiceFactory(), stringMap(),
emptyMap(), StandardV3_0.NAME );
}

public static Cluster start( File parentDir, int noOfCoreServers, int noOfEdgeServers,
Map<String, String> coreParams ) throws ExecutionException, InterruptedException
{
return start( parentDir, noOfCoreServers, noOfEdgeServers, new HazelcastDiscoveryServiceFactory(), coreParams,
emptyMap(), StandardV3_0.NAME );
}

public static Cluster start( File parentDir, int noOfCoreServers, int noOfEdgeServers,
Map<String, String> coreParams, DiscoveryServiceFactory discoveryServiceFactory )
throws ExecutionException, InterruptedException
{
return start( parentDir, noOfCoreServers, noOfEdgeServers, discoveryServiceFactory, coreParams, emptyMap(),
StandardV3_0.NAME );
}

public File coreServerStoreDirectory( int serverId )
{
return coreServerStoreDirectory( parentDir, serverId );
}

private static File coreServerStoreDirectory( File parentDir, int serverId )
public static File coreServerStoreDirectory( File parentDir, int serverId )
{
return new File( parentDir, "server-core-" + serverId );
}
Expand Down Expand Up @@ -598,12 +550,6 @@ private boolean isLockExpired( Throwable e )
LockSessionExpired;
}

@Override
public void close() throws Exception
{
shutdown();
}

public Set<CoreGraphDatabase> healthyCoreMembers()
{
return coreServers.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
*/
package org.neo4j.coreedge.scenarios;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutionException;

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

import java.util.List;

import org.neo4j.coreedge.discovery.Cluster;
import org.neo4j.coreedge.server.core.AcquireEndpointsProcedure;
import org.neo4j.coreedge.server.core.DiscoverMembersProcedure;
Expand All @@ -37,43 +34,28 @@
import org.neo4j.kernel.api.exceptions.ProcedureException;
import org.neo4j.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade;
import org.neo4j.test.rule.TargetDirectory;
import org.neo4j.test.coreedge.ClusterRule;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import static org.neo4j.helpers.collection.Iterators.asList;
import static org.neo4j.kernel.api.proc.ProcedureSignature.procedureName;
import static org.neo4j.kernel.api.security.AccessMode.Static.READ;

public class ClusterDiscoveryIT
{
@Rule
public final TargetDirectory.TestDirectory dir = TargetDirectory.testDirForTest( getClass() );

private Cluster cluster;

@After
public void shutdown() throws ExecutionException, InterruptedException
{
if ( cluster != null )
{
cluster.shutdown();
cluster = null;
}
}
public final ClusterRule clusterRule = new ClusterRule( getClass() )
.withNumberOfCoreServers( 3 );

@Test
public void shouldDiscoverCoreClusterMembers() throws Exception
{
// given
File dbDir = dir.directory();

// when
cluster = Cluster.start( dbDir, 3, 0 );
Cluster cluster = clusterRule.withNumberOfEdgeServers( 0 ).startCluster();

// then

Expand All @@ -91,11 +73,8 @@ public void shouldDiscoverCoreClusterMembers() throws Exception
@Test
public void shouldFindReadAndWriteServers() throws Exception
{
// given
File dbDir = dir.directory();

// when
cluster = Cluster.start( dbDir, 3, 1 );
Cluster cluster = clusterRule.withNumberOfEdgeServers( 1 ).startCluster();

// then

Expand All @@ -107,15 +86,13 @@ public void shouldFindReadAndWriteServers() throws Exception
assertEquals(1, currentMembers.stream().filter( x -> x[1].equals( "write" ) ).count());
assertEquals(1, currentMembers.stream().filter( x -> x[1].equals( "read" ) ).count());
}

}

@Test
public void shouldNotBeAbleToDiscoverFromEdgeMembers() throws Exception
{
// given
File dbDir = dir.directory();
cluster = Cluster.start( dbDir, 3, 2 );
Cluster cluster = clusterRule.withNumberOfEdgeServers( 2 ).startCluster();

try
{
Expand Down Expand Up @@ -155,5 +132,4 @@ private List<Object[]> endPoints( GraphDatabaseFacade db ) throws TransactionFai
procedureName( "dbms", "cluster", AcquireEndpointsProcedure.NAME ),
new Object[0] ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,39 @@
*/
package org.neo4j.coreedge.scenarios;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.neo4j.coreedge.discovery.Cluster;
import org.neo4j.coreedge.discovery.DiscoveryServiceFactory;
import org.neo4j.coreedge.discovery.SharedDiscoveryService;
import org.neo4j.coreedge.discovery.SharedDiscoveryService;
import org.neo4j.coreedge.raft.roles.Role;
import org.neo4j.coreedge.server.core.CoreGraphDatabase;
import org.neo4j.graphdb.Transaction;
import org.neo4j.test.rule.TargetDirectory;
import org.neo4j.test.coreedge.ClusterRule;

import static org.junit.Assert.assertEquals;

public class ClusterFormationIT
{
@Rule
public final TargetDirectory.TestDirectory dir = TargetDirectory.testDirForTest( getClass() );
public final ClusterRule clusterRule = new ClusterRule( getClass() )
.withNumberOfCoreServers( 3 )
.withNumberOfEdgeServers( 0 );

private Cluster cluster;

@After
public void shutdown() throws ExecutionException, InterruptedException
@Before
public void setup() throws Exception
{
if ( cluster != null )
{
cluster.shutdown();
}
cluster = clusterRule.startCluster();
}

@Test
public void shouldBeAbleToAddAndRemoveCoreServers() throws Exception
{
// given
cluster = Cluster.start( dir.directory(), 3, 0, new SharedDiscoveryService() );

// when
cluster.removeCoreServerWithServerId( 0 );
cluster.addCoreServerWithServerId( 0, 3 );
Expand All @@ -84,10 +76,8 @@ public void shouldBeAbleToAddAndRemoveCoreServers() throws Exception
public void shouldBeAbleToAddAndRemoveCoreServersUnderModestLoad() throws Exception
{
// given
cluster = Cluster.start( dir.directory(), 3, 0, new SharedDiscoveryService() );

ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit( (Runnable) () -> {
executorService.submit( () -> {
CoreGraphDatabase leader = cluster.getDbWithRole( Role.LEADER );
try ( Transaction tx = leader.beginTx() )
{
Expand Down Expand Up @@ -121,16 +111,12 @@ public void shouldBeAbleToAddAndRemoveCoreServersUnderModestLoad() throws Except
@Test
public void shouldBeAbleToRestartTheCluster() throws Exception
{
// when
final DiscoveryServiceFactory discoveryServiceFactory = new SharedDiscoveryService();
cluster = Cluster.start( dir.directory(), 3, 0, discoveryServiceFactory );

// then
// when started then
assertEquals( 3, cluster.numberOfCoreServers() );

// when
cluster.shutdown();
cluster = Cluster.start( dir.directory(), 3, 0, discoveryServiceFactory );
cluster.start();

// then
assertEquals( 3, cluster.numberOfCoreServers() );
Expand All @@ -139,8 +125,10 @@ public void shouldBeAbleToRestartTheCluster() throws Exception
cluster.removeCoreServerWithServerId( 1 );
cluster.addCoreServerWithServerId( 3, 3 );
cluster.shutdown();
cluster = Cluster.start( dir.directory(), 3, 0, discoveryServiceFactory );

cluster.start();

// then
assertEquals( 3, cluster.numberOfCoreServers() );
}
}
Loading

0 comments on commit 9ad3736

Please sign in to comment.