Skip to content

Commit

Permalink
Fixes issue 587: disables connecting to local bitcoin node during tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wlk authored and schildbach committed Nov 4, 2014
1 parent 604b013 commit 30089c9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ protected void initPeerGroup() {
peerGroup = new PeerGroup(unitTestParams, blockChain, new BlockingClientManager());
peerGroup.setPingIntervalMsec(0); // Disable the pings as they just get in the way of most tests.
peerGroup.addWallet(wallet);
peerGroup.setUseLocalhostPeerWhenPossible(false); // Prevents from connecting to bitcoin nodes on localhost.
}

protected InboundMessageQueuer connectPeerWithoutVersionExchange(int id) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public void serializeDownloadBlockWithWallet() throws Exception {
super.setUp(blockStore);

peerGroup.addWallet(wallet);
peerGroup.setUseLocalhostPeerWhenPossible(false); // Prevents from connecting to bitcoin nodes on localhost.

blockChain.addWallet(wallet);

peerGroup.startAsync();
Expand Down
20 changes: 18 additions & 2 deletions core/src/test/java/org/bitcoinj/core/PeerGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.*;
Expand Down Expand Up @@ -686,9 +687,24 @@ public void waitForPeersOfVersion() throws Exception {

@Test
public void preferLocalPeer() throws IOException {
// Check that if we have a localhost port 8333 then it's used instead of the p2p network.
ServerSocket local = new ServerSocket(params.getPort(), 100, InetAddresses.forString("127.0.0.1"));
// Because we are using the same port (8333 or 18333) that is used by Satoshi client
// We have to consider 2 cases:
// 1. Test are executed on the same machine that is running full node / Satoshi client
// 2. Test are executed without any full node running locally
// We have to avoid to connecting to real and external services in unit tests
// So we skip this test in case we have already something running on port params.getPort()

// Check that if we have a localhost port 8333 or 18333 then it's used instead of the p2p network.
ServerSocket local = null;
try {
local = new ServerSocket(params.getPort(), 100, InetAddresses.forString("127.0.0.1"));
}
catch(BindException e) { // Port already in use, skipping this test.
return;
}

try {
peerGroup.setUseLocalhostPeerWhenPossible(true);
peerGroup.startAsync();
peerGroup.awaitRunning();
local.accept().close(); // Probe connect
Expand Down

0 comments on commit 30089c9

Please sign in to comment.