Skip to content

Commit

Permalink
inject ports in CausalClusterInProcessRunnerIT
Browse files Browse the repository at this point in the history
  • Loading branch information
lassewesth committed Sep 4, 2017
1 parent ed53a99 commit 9e127fe
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 75 deletions.
33 changes: 33 additions & 0 deletions enterprise/neo4j-harness-enterprise/pom.xml
Expand Up @@ -115,5 +115,38 @@
<scope>test</scope>
<type>test-jar</type>
</dependency>

<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-com</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<!-- Integration tests spend significant time waiting, so we can run more things in parallel here-->
<forkCount>1C</forkCount>
<!-- Misha says it is not safe to run tests with more than one thread per JVM -->
<!-- He also said he'd fix that! -->
<threadCount>1</threadCount>
<!-- slowest test class takes up to 10 minutes, so let's cater for that, with contingency -->
<parallelTestsTimeoutInSeconds>1200</parallelTestsTimeoutInSeconds>
<parallelTestsTimeoutForcedInSeconds>1800</parallelTestsTimeoutForcedInSeconds>
<!-- file system separation between forks -->
<workingDirectory>${project.build.directory}/FORK_DIRECTORY_${surefire.forkNumber}</workingDirectory>
<systemPropertyVariables>
<!-- coordination on ports between forks -->
<port.authority.directory>${user.dir}/target/port-authority-${maven.build.timestamp}</port.authority.directory>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
Expand Up @@ -60,7 +60,7 @@ public static void main( String[] args ) throws IOException, ExecutionException,
Path clusterPath = Files.createTempDirectory( "causal-cluster" );
System.out.println( "clusterPath = " + clusterPath );

CausalCluster cluster = new CausalCluster( 3, 3, clusterPath, toOutputStream( System.out ) );
CausalCluster cluster = new CausalCluster( 3, 3, clusterPath, toOutputStream( System.out ), PortPickingStrategy.DEFAULT );

System.out.println( "Waiting for cluster to boot up..." );
cluster.boot();
Expand All @@ -85,96 +85,83 @@ abstract static class PortPickingStrategy
public static final PortPickingStrategy DEFAULT = new PortPickingStrategy()
{
@Override
int hazelcastPort( int coreId )
int port( int offset, int id )
{
return 55000 + coreId;
return offset + id;
}
};

@Override
int txCorePort( int coreId )
{
return 56000 + coreId;
}
abstract int port( int offset, int id );

@Override
int raftCorePort( int coreId )
{
return 57000 + coreId;
}
int hazelcastPort( int coreId )
{
return port( 55000, coreId );
}

@Override
int boltCorePort( int coreId )
{
return 58000 + coreId;
}
int txCorePort( int coreId )
{
return port( 56000, coreId );
}

@Override
int httpCorePort( int coreId )
{
return 59000 + coreId;
}
int raftCorePort( int coreId )
{
return port( 57000, coreId );
}

@Override
int httpsCorePort( int coreId )
{
return 60000 + coreId;
}
int boltCorePort( int coreId )
{
return port( 58000, coreId );
}

@Override
int txReadReplicaPort( int replicaId )
{
return 56500 + replicaId;
}
int httpCorePort( int coreId )
{
return port( 59000, coreId );
}

@Override
int boltReadReplicaPort( int replicaId )
{
return 58500 + replicaId;
}
int httpsCorePort( int coreId )
{
return port( 60000, coreId );
}

@Override
int httpReadReplicaPort( int replicaId )
{
return 59500 + replicaId;
}
int txReadReplicaPort( int replicaId )
{
return port( 56500, replicaId );
}

@Override
int httpsReadReplicaPort( int replicaId )
{
return 60500 + replicaId;
}
};
int boltReadReplicaPort( int replicaId )
{
return port( 58500, replicaId );
}

abstract int hazelcastPort( int coreId );
abstract int txCorePort( int coreId );
abstract int raftCorePort( int coreId );
abstract int boltCorePort( int coreId );
abstract int httpCorePort( int coreId );
abstract int httpsCorePort( int coreId );
abstract int txReadReplicaPort( int replicaId );
abstract int boltReadReplicaPort( int replicaId );
abstract int httpReadReplicaPort( int replicaId );
abstract int httpsReadReplicaPort( int replicaId );
int httpReadReplicaPort( int replicaId )
{
return port( 59500, replicaId );
}

int httpsReadReplicaPort( int replicaId )
{
return port( 60500, replicaId );
}
}

static class CausalCluster
{
private static final PortPickingStrategy PortPickingStrategy = CausalClusterInProcessRunner.PortPickingStrategy.DEFAULT;

private final int nCores;
private final int nReplicas;
private final Path clusterPath;
private final Log log;
private final PortPickingStrategy portPickingStrategy;

private List<ServerControls> coreControls = synchronizedList( new ArrayList<>() );
private List<ServerControls> replicaControls = synchronizedList( new ArrayList<>() );

CausalCluster( int nCores, int nReplicas, Path clusterPath, LogProvider logProvider )
CausalCluster( int nCores, int nReplicas, Path clusterPath, LogProvider logProvider, PortPickingStrategy portPickingStrategy )
{
this.nCores = nCores;
this.nReplicas = nReplicas;
this.clusterPath = clusterPath;
this.log = logProvider.getLog( getClass() );
this.portPickingStrategy = portPickingStrategy;
}

void boot() throws IOException, InterruptedException
Expand All @@ -183,7 +170,7 @@ void boot() throws IOException, InterruptedException

for ( int coreId = 0; coreId < nCores; coreId++ )
{
int hazelcastPort = PortPickingStrategy.hazelcastPort( coreId );
int hazelcastPort = portPickingStrategy.hazelcastPort( coreId );
initialMembers.add( "localhost:" + hazelcastPort );
}

Expand All @@ -192,12 +179,12 @@ void boot() throws IOException, InterruptedException

for ( int coreId = 0; coreId < nCores; coreId++ )
{
int hazelcastPort = PortPickingStrategy.hazelcastPort( coreId );
int txPort = PortPickingStrategy.txCorePort( coreId );
int raftPort = PortPickingStrategy.raftCorePort( coreId );
int boltPort = PortPickingStrategy.boltCorePort( coreId );
int httpPort = PortPickingStrategy.httpCorePort( coreId );
int httpsPort = PortPickingStrategy.httpsCorePort( coreId );
int hazelcastPort = portPickingStrategy.hazelcastPort( coreId );
int txPort = portPickingStrategy.txCorePort( coreId );
int raftPort = portPickingStrategy.raftCorePort( coreId );
int boltPort = portPickingStrategy.boltCorePort( coreId );
int httpPort = portPickingStrategy.httpCorePort( coreId );
int httpsPort = portPickingStrategy.httpsCorePort( coreId );

String homeDir = "core-" + coreId;
TestServerBuilder builder = new EnterpriseInProcessServerBuilder( clusterPath.toFile(), homeDir );
Expand Down Expand Up @@ -239,10 +226,10 @@ void boot() throws IOException, InterruptedException

for ( int replicaId = 0; replicaId < nReplicas; replicaId++ )
{
int txPort = PortPickingStrategy.txReadReplicaPort( replicaId );
int boltPort = PortPickingStrategy.boltReadReplicaPort( replicaId );
int httpPort = PortPickingStrategy.httpReadReplicaPort( replicaId );
int httpsPort = PortPickingStrategy.httpsReadReplicaPort( replicaId );
int txPort = portPickingStrategy.txReadReplicaPort( replicaId );
int boltPort = portPickingStrategy.boltReadReplicaPort( replicaId );
int httpPort = portPickingStrategy.httpReadReplicaPort( replicaId );
int httpsPort = portPickingStrategy.httpsReadReplicaPort( replicaId );

String homeDir = "replica-" + replicaId;
TestServerBuilder builder = new EnterpriseInProcessServerBuilder( clusterPath.toFile(), homeDir );
Expand Down
Expand Up @@ -19,10 +19,14 @@
*/
package org.neo4j.harness;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

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

import org.neo4j.com.ports.allocation.PortAuthority;
import org.neo4j.harness.CausalClusterInProcessRunner.CausalCluster;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.test.rule.TestDirectory;
Expand All @@ -35,7 +39,24 @@ public class CausalClusterInProcessRunnerIT
@Test
public void shouldBootAndShutdownCluster() throws Exception
{
CausalCluster cluster = new CausalCluster( 3, 3, testDirectory.absolutePath().toPath(), NullLogProvider.getInstance() );
Path clusterPath = testDirectory.absolutePath().toPath();
CausalClusterInProcessRunner.PortPickingStrategy portPickingStrategy = new CausalClusterInProcessRunner.PortPickingStrategy()
{
Map<Integer, Integer> cache = new HashMap<>();

@Override
int port( int offset, int id )
{
int key = offset + id;
if ( ! cache.containsKey( key ) )
{
cache.put( key, PortAuthority.allocatePort() );
}

return cache.get( key );
}
};
CausalCluster cluster = new CausalCluster( 3, 3, clusterPath, NullLogProvider.getInstance(), portPickingStrategy );

cluster.boot();
cluster.shutdown();
Expand Down

0 comments on commit 9e127fe

Please sign in to comment.