From c11c4d126d04c9d7ab85546d1284717c004636a5 Mon Sep 17 00:00:00 2001 From: Wojciech Langiewicz Date: Sat, 25 Oct 2014 17:30:04 +0200 Subject: [PATCH] Fixes issue 587: disables connecting to local bitcoin node during tests --- .../bitcoinj/testing/TestWithPeerGroup.java | 1 + ...ilteredBlockAndPartialMerkleTreeTests.java | 2 ++ .../java/org/bitcoinj/core/PeerGroupTest.java | 20 +++++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/testing/TestWithPeerGroup.java b/core/src/main/java/org/bitcoinj/testing/TestWithPeerGroup.java index acbc8451..0d7b5691 100644 --- a/core/src/main/java/org/bitcoinj/testing/TestWithPeerGroup.java +++ b/core/src/main/java/org/bitcoinj/testing/TestWithPeerGroup.java @@ -80,6 +80,7 @@ public class TestWithPeerGroup extends TestWithNetworkConnections { 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 { diff --git a/core/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTests.java b/core/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTests.java index c62c16e4..f994d8e9 100644 --- a/core/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTests.java +++ b/core/src/test/java/org/bitcoinj/core/FilteredBlockAndPartialMerkleTreeTests.java @@ -141,6 +141,8 @@ public class FilteredBlockAndPartialMerkleTreeTests extends TestWithPeerGroup { super.setUp(blockStore); peerGroup.addWallet(wallet); + peerGroup.setUseLocalhostPeerWhenPossible(false); // Prevents from connecting to bitcoin nodes on localhost. + blockChain.addWallet(wallet); peerGroup.startAsync(); diff --git a/core/src/test/java/org/bitcoinj/core/PeerGroupTest.java b/core/src/test/java/org/bitcoinj/core/PeerGroupTest.java index d894d53f..d826947f 100644 --- a/core/src/test/java/org/bitcoinj/core/PeerGroupTest.java +++ b/core/src/test/java/org/bitcoinj/core/PeerGroupTest.java @@ -36,6 +36,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.io.IOException; +import java.net.BindException; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.util.*; @@ -720,9 +721,24 @@ public class PeerGroupTest extends TestWithPeerGroup { @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