Skip to content

Commit

Permalink
Move some testing utilities to a new testing subpackage and rename Te…
Browse files Browse the repository at this point in the history
…xtUtils to reflect what it actually does.
  • Loading branch information
mikehearn committed Apr 22, 2014
1 parent 98fc582 commit e1d6707
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract class PeerSocketHandler extends AbstractTimeoutHandler implement
// If we close() before we know our writeTarget, set this to true to call writeTarget.closeConnection() right away.
private boolean closePending = false;
// writeTarget will be thread-safe, and may call into PeerGroup, which calls us, so we should call it unlocked
@VisibleForTesting MessageWriteTarget writeTarget = null;
@VisibleForTesting protected MessageWriteTarget writeTarget = null;

// The ByteBuffers passed to us from the writeTarget are static in size, and usually smaller than some messages we
// will receive. For SPV clients, this should be rare (ie we're mostly dealing with small transactions), but for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
* limitations under the License.
*/

package com.google.bitcoin.utils;
package com.google.bitcoin.testing;

import com.google.bitcoin.core.*;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.store.BlockStoreException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;

public class TestUtils {
public class FakeTxBuilder {
public static Transaction createFakeTxWithChangeAddress(NetworkParameters params, BigInteger nanocoins, Address to, Address changeOutput)
throws IOException, ProtocolException {
// Create a fake TX of sufficient realism to exercise the unit tests. Two outputs, one to us, one to somewhere
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.google.bitcoin.core;
package com.google.bitcoin.testing;

import com.google.bitcoin.core.*;
import com.google.common.util.concurrent.SettableFuture;

import java.net.InetSocketAddress;
Expand All @@ -9,11 +10,11 @@
import java.util.concurrent.BlockingQueue;

/**
* An extension of {@link PeerSocketHandler} that keeps inbound messages in a queue for later processing
* An extension of {@link com.google.bitcoin.core.PeerSocketHandler} that keeps inbound messages in a queue for later processing
*/
public abstract class InboundMessageQueuer extends PeerSocketHandler {
final BlockingQueue<Message> inboundMessages = new ArrayBlockingQueue<Message>(1000);
final Map<Long, SettableFuture<Void>> mapPingFutures = new HashMap<Long, SettableFuture<Void>>();
public final BlockingQueue<Message> inboundMessages = new ArrayBlockingQueue<Message>(1000);
public final Map<Long, SettableFuture<Void>> mapPingFutures = new HashMap<Long, SettableFuture<Void>>();

public Peer peer;
public BloomFilter lastReceivedFilter;
Expand All @@ -33,7 +34,7 @@ public Message nextMessageBlocking() throws InterruptedException {
@Override
protected void processMessage(Message m) throws Exception {
if (m instanceof Ping) {
SettableFuture<Void> future = mapPingFutures.get(((Ping)m).getNonce());
SettableFuture<Void> future = mapPingFutures.get(((Ping) m).getNonce());
if (future != null) {
future.set(null);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

package com.google.bitcoin.utils;
package com.google.bitcoin.testing;

import com.google.bitcoin.core.Transaction;
import com.google.bitcoin.core.TransactionBroadcaster;
import com.google.bitcoin.core.VerificationException;
import com.google.bitcoin.core.Wallet;
import com.google.bitcoin.utils.Threading;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.SettableFuture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* limitations under the License.
*/

package com.google.bitcoin.core;
package com.google.bitcoin.testing;

import com.google.bitcoin.core.*;
import com.google.bitcoin.net.*;
import com.google.bitcoin.params.UnitTestParams;
import com.google.bitcoin.store.BlockStore;
Expand All @@ -37,7 +38,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import static com.google.common.base.Preconditions.checkArgument;
import static org.junit.Assert.assertTrue;
import static com.google.common.base.Preconditions.checkState;

/**
* Utility class that makes it easy to work with mock NetworkConnections.
Expand All @@ -56,7 +57,7 @@ public class TestWithNetworkConnections {
private final ClientConnectionManager channels;
protected final BlockingQueue<InboundMessageQueuer> newPeerWriteTargetQueue = new LinkedBlockingQueue<InboundMessageQueuer>();

enum ClientType {
public enum ClientType {
NIO_CLIENT_MANAGER,
BLOCKING_CLIENT_MANAGER,
NIO_CLIENT,
Expand Down Expand Up @@ -168,8 +169,8 @@ else if (clientType == ClientType.BLOCKING_CLIENT)
writeTarget.sendMessage(versionMessage);
writeTarget.sendMessage(new VersionAck());
try {
assertTrue(writeTarget.nextMessageBlocking() instanceof VersionMessage);
assertTrue(writeTarget.nextMessageBlocking() instanceof VersionAck);
checkState(writeTarget.nextMessageBlocking() instanceof VersionMessage);
checkState(writeTarget.nextMessageBlocking() instanceof VersionAck);
synchronized (doneConnecting) {
doneConnecting.set(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@
* limitations under the License.
*/

package com.google.bitcoin.utils;
package com.google.bitcoin.testing;

import com.google.bitcoin.core.*;
import com.google.bitcoin.params.UnitTestParams;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.store.MemoryBlockStore;
import com.google.bitcoin.testing.FakeTxBuilder;
import com.google.bitcoin.utils.BriefLogFormatter;

import javax.annotation.Nullable;
import java.io.IOException;
import java.math.BigInteger;

import static com.google.bitcoin.utils.TestUtils.createFakeBlock;
import static com.google.bitcoin.utils.TestUtils.createFakeTx;
import static com.google.bitcoin.testing.FakeTxBuilder.createFakeBlock;
import static com.google.bitcoin.testing.FakeTxBuilder.createFakeTx;

// TODO: This needs to be somewhat rewritten - the "sendMoneyToWallet" methods aren't sending via the block chain object

Expand Down Expand Up @@ -69,7 +71,7 @@ protected Transaction sendMoneyToWallet(Wallet wallet, Transaction tx, AbstractB
if (wallet.isPendingTransactionRelevant(tx))
wallet.receivePending(tx, null);
} else {
TestUtils.BlockPair bp = createFakeBlock(blockStore, tx);
FakeTxBuilder.BlockPair bp = createFakeBlock(blockStore, tx);
wallet.receiveFromBlock(tx, bp.storedBlock, type, 0);
if (type == AbstractBlockChain.NewBlockType.BEST_CHAIN)
wallet.notifyNewBestBlock(bp.storedBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import com.google.bitcoin.params.UnitTestParams;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.store.MemoryBlockStore;
import com.google.bitcoin.testing.FakeTxBuilder;
import com.google.bitcoin.utils.BriefLogFormatter;
import com.google.bitcoin.utils.TestUtils;
import com.google.common.util.concurrent.ListenableFuture;
import org.junit.After;
import org.junit.Before;
Expand All @@ -33,8 +33,8 @@
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.google.bitcoin.utils.TestUtils.createFakeBlock;
import static com.google.bitcoin.utils.TestUtils.createFakeTx;
import static com.google.bitcoin.testing.FakeTxBuilder.createFakeBlock;
import static com.google.bitcoin.testing.FakeTxBuilder.createFakeTx;
import static org.junit.Assert.*;

// Handling of chain splits/reorgs are in ChainSplitTests.
Expand Down Expand Up @@ -272,7 +272,7 @@ public void intraBlockDependencies() throws Exception {
wallet.addKey(key);
Address addr = key.toAddress(unitTestParams);
// Create a tx that gives us some coins, and another that spends it to someone else in the same block.
Transaction t1 = TestUtils.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), addr);
Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), addr);
Transaction t2 = new Transaction(unitTestParams);
t2.addInput(t1.getOutputs().get(0));
t2.addOutput(Utils.toNanoCoins(2, 0), somebodyElse);
Expand Down
12 changes: 6 additions & 6 deletions core/src/test/java/com/google/bitcoin/core/ChainSplitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import com.google.bitcoin.core.TransactionConfidence.ConfidenceType;
import com.google.bitcoin.params.UnitTestParams;
import com.google.bitcoin.store.MemoryBlockStore;
import com.google.bitcoin.testing.FakeTxBuilder;
import com.google.bitcoin.utils.BriefLogFormatter;
import com.google.bitcoin.utils.TestUtils;
import com.google.bitcoin.utils.Threading;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -540,8 +540,8 @@ public void orderingInsideBlock() throws Exception {
// This covers issue 468.

// Receive some money to the wallet.
Transaction t1 = TestUtils.createFakeTx(unitTestParams, Utils.COIN, coinsTo);
final Block b1 = TestUtils.makeSolvedTestBlock(unitTestParams.genesisBlock, t1);
Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.COIN, coinsTo);
final Block b1 = FakeTxBuilder.makeSolvedTestBlock(unitTestParams.genesisBlock, t1);
chain.add(b1);

// Send a couple of payments one after the other (so the second depends on the change output of the first).
Expand All @@ -550,7 +550,7 @@ public void orderingInsideBlock() throws Exception {
wallet.commitTx(t2);
Transaction t3 = checkNotNull(wallet.createSend(new ECKey().toAddress(unitTestParams), Utils.CENT));
wallet.commitTx(t3);
chain.add(TestUtils.makeSolvedTestBlock(b1, t2, t3));
chain.add(FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3));

final BigInteger coins0point98 = Utils.COIN.subtract(Utils.CENT).subtract(Utils.CENT);
assertEquals(coins0point98, wallet.getBalance());
Expand All @@ -559,8 +559,8 @@ public void orderingInsideBlock() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
wallet.saveToFileStream(bos);
wallet = Wallet.loadFromFileStream(new ByteArrayInputStream(bos.toByteArray()));
final Block b2 = TestUtils.makeSolvedTestBlock(b1, t2, t3);
final Block b3 = TestUtils.makeSolvedTestBlock(b2);
final Block b2 = FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3);
final Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2);
chain.add(b2);
chain.add(b3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.bitcoin.core.TransactionConfidence.ConfidenceType;
import com.google.bitcoin.params.UnitTestParams;
import com.google.bitcoin.store.MemoryBlockStore;
import com.google.bitcoin.testing.InboundMessageQueuer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import java.nio.ByteBuffer;
import java.util.Arrays;

import static com.google.bitcoin.utils.TestUtils.createFakeBlock;
import static com.google.bitcoin.utils.TestUtils.createFakeTx;
import static com.google.bitcoin.testing.FakeTxBuilder.createFakeBlock;
import static com.google.bitcoin.testing.FakeTxBuilder.createFakeTx;
import static org.junit.Assert.*;

public class LazyParseByteCacheTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.google.bitcoin.core;

import com.google.bitcoin.params.UnitTestParams;
import com.google.bitcoin.testing.FakeTxBuilder;
import com.google.bitcoin.utils.BriefLogFormatter;
import com.google.bitcoin.utils.TestUtils;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -35,7 +35,7 @@ public class MemoryPoolTest {
@Before
public void setup() throws Exception {
BriefLogFormatter.init();
tx1 = TestUtils.createFakeTx(params, Utils.toNanoCoins(1, 0), new ECKey().toAddress(params));
tx1 = FakeTxBuilder.createFakeTx(params, Utils.toNanoCoins(1, 0), new ECKey().toAddress(params));
tx2 = new Transaction(params, tx1.bitcoinSerialize());

address1 = new PeerAddress(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));
Expand Down
21 changes: 11 additions & 10 deletions core/src/test/java/com/google/bitcoin/core/PeerGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import com.google.bitcoin.net.discovery.PeerDiscoveryException;
import com.google.bitcoin.params.UnitTestParams;
import com.google.bitcoin.store.MemoryBlockStore;
import com.google.bitcoin.utils.TestUtils;
import com.google.bitcoin.testing.FakeTxBuilder;
import com.google.bitcoin.testing.InboundMessageQueuer;
import com.google.bitcoin.utils.Threading;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -177,7 +178,7 @@ public void receiveTxBroadcast() throws Exception {
assertEquals(tmp, expectedPeers);

BigInteger value = Utils.toNanoCoins(1, 0);
Transaction t1 = TestUtils.createFakeTx(unitTestParams, value, address);
Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, value, address);
InventoryMessage inv = new InventoryMessage(unitTestParams);
inv.addTransaction(t1);

Expand Down Expand Up @@ -212,10 +213,10 @@ public void singleDownloadPeer1() throws Exception {

// Set up a little block chain. We heard about b1 but not b2 (it is pending download). b3 is solved whilst we
// are downloading the chain.
Block b1 = TestUtils.createFakeBlock(blockStore).block;
Block b1 = FakeTxBuilder.createFakeBlock(blockStore).block;
blockChain.add(b1);
Block b2 = TestUtils.makeSolvedTestBlock(b1);
Block b3 = TestUtils.makeSolvedTestBlock(b2);
Block b2 = FakeTxBuilder.makeSolvedTestBlock(b1);
Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2);

// Peer 1 and 2 receives an inv advertising a newly solved block.
InventoryMessage inv = new InventoryMessage(params);
Expand Down Expand Up @@ -254,9 +255,9 @@ public void singleDownloadPeer2() throws Exception {
InboundMessageQueuer p1 = connectPeer(1);

// Set up a little block chain.
Block b1 = TestUtils.createFakeBlock(blockStore).block;
Block b2 = TestUtils.makeSolvedTestBlock(b1);
Block b3 = TestUtils.makeSolvedTestBlock(b2);
Block b1 = FakeTxBuilder.createFakeBlock(blockStore).block;
Block b2 = FakeTxBuilder.makeSolvedTestBlock(b1);
Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2);

// Expect a zero hash getblocks on p1. This is how the process starts.
peerGroup.startBlockChainDownload(new AbstractPeerEventListener() {
Expand Down Expand Up @@ -299,7 +300,7 @@ public void onTransaction(Peer peer, Transaction t) {
InboundMessageQueuer p2 = connectPeer(2);
InboundMessageQueuer p3 = connectPeer(3);

Transaction tx = TestUtils.createFakeTx(params, Utils.toNanoCoins(20, 0), address);
Transaction tx = FakeTxBuilder.createFakeTx(params, Utils.toNanoCoins(20, 0), address);
InventoryMessage inv = new InventoryMessage(params);
inv.addTransaction(tx);

Expand Down Expand Up @@ -534,7 +535,7 @@ public void testBloomOnP2Pubkey() throws Exception {
InboundMessageQueuer p1 = connectPeer(1);
InboundMessageQueuer p2 = connectPeer(2);
// Create a pay to pubkey tx.
Transaction tx = TestUtils.createFakeTx(params, Utils.COIN, key);
Transaction tx = FakeTxBuilder.createFakeTx(params, Utils.COIN, key);
Transaction tx2 = new Transaction(params);
tx2.addInput(tx.getOutput(0));
TransactionOutPoint outpoint = tx2.getInput(0).getOutpoint();
Expand Down
22 changes: 12 additions & 10 deletions core/src/test/java/com/google/bitcoin/core/PeerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.google.bitcoin.core;

import com.google.bitcoin.params.TestNet3Params;
import com.google.bitcoin.utils.TestUtils;
import com.google.bitcoin.testing.FakeTxBuilder;
import com.google.bitcoin.testing.InboundMessageQueuer;
import com.google.bitcoin.testing.TestWithNetworkConnections;
import com.google.bitcoin.utils.Threading;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListenableFuture;
Expand Down Expand Up @@ -46,7 +48,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import static com.google.bitcoin.utils.TestUtils.*;
import static com.google.bitcoin.testing.FakeTxBuilder.*;
import static org.junit.Assert.*;

@RunWith(value = Parameterized.class)
Expand Down Expand Up @@ -541,9 +543,9 @@ public void onTransaction(Peer peer1, Transaction t) {
// -> [t7]
// -> [t8]
// The ones in brackets are assumed to be in the chain and are represented only by hashes.
Transaction t2 = TestUtils.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), to);
Transaction t2 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), to);
Sha256Hash t5 = t2.getInput(0).getOutpoint().getHash();
Transaction t4 = TestUtils.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), new ECKey());
Transaction t4 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), new ECKey());
Sha256Hash t6 = t4.getInput(0).getOutpoint().getHash();
t4.addOutput(Utils.toNanoCoins(1, 0), new ECKey());
Transaction t3 = new Transaction(unitTestParams);
Expand All @@ -557,10 +559,10 @@ public void onTransaction(Peer peer1, Transaction t) {
Sha256Hash anotherHash = new Sha256Hash("3b801dd82f01d17bbde881687bf72bc62e2faa8ab8133d36fcb8c3abe7459da6");
t1.addInput(new TransactionInput(unitTestParams, t1, new byte[]{}, new TransactionOutPoint(unitTestParams, 1, anotherHash)));
t1.addOutput(Utils.toNanoCoins(1, 0), to);
t1 = TestUtils.roundTripTransaction(unitTestParams, t1);
t2 = TestUtils.roundTripTransaction(unitTestParams, t2);
t3 = TestUtils.roundTripTransaction(unitTestParams, t3);
t4 = TestUtils.roundTripTransaction(unitTestParams, t4);
t1 = FakeTxBuilder.roundTripTransaction(unitTestParams, t1);
t2 = FakeTxBuilder.roundTripTransaction(unitTestParams, t2);
t3 = FakeTxBuilder.roundTripTransaction(unitTestParams, t3);
t4 = FakeTxBuilder.roundTripTransaction(unitTestParams, t4);

// Announce the first one. Wait for it to be downloaded.
InventoryMessage inv = new InventoryMessage(unitTestParams);
Expand Down Expand Up @@ -663,7 +665,7 @@ public void onCoinsReceived(Wallet wallet, Transaction tx, BigInteger prevBalanc
}
});
// Send a normal relevant transaction, it's received correctly.
Transaction t1 = TestUtils.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), key);
Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), key);
inbound(writeTarget, t1);
GetDataMessage getdata = (GetDataMessage) outbound(writeTarget);
if (useNotFound) {
Expand All @@ -676,7 +678,7 @@ public void onCoinsReceived(Wallet wallet, Transaction tx, BigInteger prevBalanc
assertNotNull(vtx[0]);
vtx[0] = null;
// Send a timelocked transaction, nothing happens.
Transaction t2 = TestUtils.createFakeTx(unitTestParams, Utils.toNanoCoins(2, 0), key);
Transaction t2 = FakeTxBuilder.createFakeTx(unitTestParams, Utils.toNanoCoins(2, 0), key);
t2.setLockTime(999999);
inbound(writeTarget, t2);
Threading.waitForUserCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.google.bitcoin.net.NioClientManager;
import com.google.bitcoin.params.UnitTestParams;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.testing.InboundMessageQueuer;
import com.google.bitcoin.testing.TestWithNetworkConnections;
import com.google.common.base.Preconditions;

import java.net.InetSocketAddress;
Expand Down

0 comments on commit e1d6707

Please sign in to comment.