Skip to content

Commit

Permalink
TestWithNetworkConnections: pick random TCP bind ports for each test …
Browse files Browse the repository at this point in the history
…instance

This will hopefully reduce spurious test failures due to already bound ports.

This is a backport of 006c00a.
  • Loading branch information
schildbach committed Jun 24, 2024
1 parent 300b6aa commit 9b8660f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -49,7 +50,9 @@
* Utility class that makes it easy to work with mock NetworkConnections.
*/
public class TestWithNetworkConnections {
protected static final int TCP_PORT_BASE = 10000 + new Random().nextInt(40000);
public static final int PEER_SERVERS = 5;

protected static final NetworkParameters UNITTEST = UnitTestParams.get();
protected Context context;
protected BlockStore blockStore;
Expand Down Expand Up @@ -130,7 +133,7 @@ public void connectionOpened() {
}
};
}
}, new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000 + i));
}, new InetSocketAddress(InetAddress.getLoopbackAddress(), TCP_PORT_BASE + i));
peerServers[i].startAsync();
peerServers[i].awaitRunning();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void run() {

protected InboundMessageQueuer connectPeerWithoutVersionExchange(int id) throws Exception {
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();
InboundMessageQueuer writeTarget = newPeerWriteTargetQueue.take();
writeTarget.peer = peer;
Expand Down

0 comments on commit 9b8660f

Please sign in to comment.