diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortAuthority.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortAuthority.java index 26a6061c7398c..e90f5a36fdb44 100644 --- a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortAuthority.java +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortAuthority.java @@ -49,7 +49,7 @@ public class PortAuthority { Path directory = Paths.get( portAuthorityDirectory ); Files.createDirectories( directory ); - PortRepository portRepository = new PortRepository( directory, EphemeralPortMinimum ); + PortRepository portRepository = new PortRepository( directory, 20000 ); PortProbe portProbe = new DefaultPortProbe(); portProvider = new CoordinatingPortProvider( portRepository, portProbe ); } diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepository.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepository.java index 49ee721254fe7..aefcd82c40e87 100644 --- a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepository.java +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepository.java @@ -19,36 +19,33 @@ */ package org.neo4j.causalclustering; +import static org.neo4j.causalclustering.PortConstants.EphemeralPortMaximum; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Random; public class PortRepository { - private static final Random RANDOM = new Random(); - private final Path directory; - private final int lowestPort; - public PortRepository( Path directory, int lowestPort ) + private int currentPort; + + public PortRepository( Path directory, int initialPort ) { this.directory = directory; - this.lowestPort = lowestPort; + + this.currentPort = initialPort; } // synchronize between threads in this JVM public synchronized int reserveNextPort( String trace ) { - // if we do not find one after trying 100(!) times, bail out - for ( int i = 0; i < 100; i++ ) + while ( currentPort <= EphemeralPortMaximum ) { - int port = lowestPort + RANDOM.nextInt( PortConstants.EphemeralPortMaximum - lowestPort ); - - Path portFilePath = directory.resolve( "port" + port ); + Path portFilePath = directory.resolve( "port" + currentPort ); try { @@ -60,11 +57,11 @@ public synchronized int reserveNextPort( String trace ) fileOutputStream.flush(); } - return port; + return currentPort++; } catch ( FileAlreadyExistsException e ) { - // try again + currentPort++; } catch ( IOException e ) { diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepositoryIT.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepositoryIT.java index a67f0a5fb80f9..6ae49cf154810 100644 --- a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepositoryIT.java +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/PortRepositoryIT.java @@ -88,7 +88,6 @@ public void shouldNotInterfereWithOtherRepositories() throws Exception } @Test - @Ignore( "This was for when ports were allocated sequentially" ) public void shouldNotOverrun() throws Exception { Path directory = testDirectory.cleanDirectory( "port-repository" ).toPath();