TestWithNetworkConnections: pick random TCP bind ports for each test instance

This will hopefully reduce spurious test failures due to already bound ports.
This commit is contained in:
Andreas Schildbach 2023-03-10 23:42:25 +01:00
parent d25c77cf24
commit 006c00ac35
2 changed files with 5 additions and 2 deletions

View file

@ -54,6 +54,7 @@ import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.time.Duration; import java.time.Duration;
import java.util.Random;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
@ -66,7 +67,9 @@ import static com.google.common.base.Preconditions.checkState;
* Utility class that makes it easy to work with mock NetworkConnections. * Utility class that makes it easy to work with mock NetworkConnections.
*/ */
public class TestWithNetworkConnections { public class TestWithNetworkConnections {
protected static final int TCP_PORT_BASE = 10000 + new Random().nextInt(40000);
public static final int PEER_SERVERS = 5; public static final int PEER_SERVERS = 5;
protected static final NetworkParameters UNITTEST = UnitTestParams.get(); protected static final NetworkParameters UNITTEST = UnitTestParams.get();
protected static final NetworkParameters TESTNET = TestNet3Params.get(); protected static final NetworkParameters TESTNET = TestNet3Params.get();
protected BlockStore blockStore; protected BlockStore blockStore;
@ -145,7 +148,7 @@ public class TestWithNetworkConnections {
} }
}; };
} }
}, new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000 + i)); }, new InetSocketAddress(InetAddress.getLoopbackAddress(), TCP_PORT_BASE + i));
peerServers[i].startAsync(); peerServers[i].startAsync();
peerServers[i].awaitRunning(); peerServers[i].awaitRunning();
} }

View file

@ -134,7 +134,7 @@ public class TestWithPeerGroup extends TestWithNetworkConnections {
protected InboundMessageQueuer connectPeerWithoutVersionExchange(int id) throws Exception { protected InboundMessageQueuer connectPeerWithoutVersionExchange(int id) throws Exception {
Preconditions.checkArgument(id < PEER_SERVERS); Preconditions.checkArgument(id < PEER_SERVERS);
InetSocketAddress remoteAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000 + id); InetSocketAddress remoteAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), TCP_PORT_BASE + id);
Peer peer = peerGroup.connectTo(remoteAddress).getConnectionOpenFuture().get(); Peer peer = peerGroup.connectTo(remoteAddress).getConnectionOpenFuture().get();
InboundMessageQueuer writeTarget = newPeerWriteTargetQueue.take(); InboundMessageQueuer writeTarget = newPeerWriteTargetQueue.take();
writeTarget.peer = peer; writeTarget.peer = peer;