From 6e999c6054685864da3600ff19f438bb43a36125 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Wed, 23 Apr 2014 00:24:48 +0200 Subject: [PATCH] Simplify the MockTransactionBroadcaster a bit and update the docs. --- .../google/bitcoin/testing/MockTransactionBroadcaster.java | 7 +++++-- core/src/test/java/com/google/bitcoin/core/WalletTest.java | 4 +--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/testing/MockTransactionBroadcaster.java b/core/src/main/java/com/google/bitcoin/testing/MockTransactionBroadcaster.java index 7f2a4790..ec2e6d6c 100644 --- a/core/src/main/java/com/google/bitcoin/testing/MockTransactionBroadcaster.java +++ b/core/src/main/java/com/google/bitcoin/testing/MockTransactionBroadcaster.java @@ -30,8 +30,9 @@ import java.util.concurrent.locks.ReentrantLock; /** * A mock transaction broadcaster can be used in unit tests as a stand-in for a PeerGroup. It catches any transactions - * broadcast through it and makes them available via the {@link #broadcasts} member. Reading from that - * {@link LinkedBlockingQueue} will block the thread until a transaction is available. + * broadcast through it and makes them available via the {@link #waitForTransaction()} method. Using that will cause + * the broadcast to be seen as if it never propagated though, so you may instead use {@link #waitForTxFuture()} and then + * set the returned future when you want the "broadcast" to be completed. */ public class MockTransactionBroadcaster implements TransactionBroadcaster { private final ReentrantLock lock = Threading.lock("mock tx broadcaster"); @@ -49,12 +50,14 @@ public class MockTransactionBroadcaster implements TransactionBroadcaster { private final LinkedBlockingQueue broadcasts = new LinkedBlockingQueue(); + /** Sets this mock broadcaster on the given wallet. */ public MockTransactionBroadcaster(Wallet wallet) { // This code achieves nothing directly, but it sets up the broadcaster/peergroup > wallet lock ordering // so inversions can be caught. lock.lock(); try { this.wallet = wallet; + wallet.setTransactionBroadcaster(this); wallet.getPendingTransactions(); } finally { lock.unlock(); diff --git a/core/src/test/java/com/google/bitcoin/core/WalletTest.java b/core/src/test/java/com/google/bitcoin/core/WalletTest.java index 65740eb5..9fd14edb 100644 --- a/core/src/test/java/com/google/bitcoin/core/WalletTest.java +++ b/core/src/test/java/com/google/bitcoin/core/WalletTest.java @@ -2187,7 +2187,6 @@ public class WalletTest extends TestWithWallet { Utils.setMockClock(); // Watch out for wallet-initiated broadcasts. MockTransactionBroadcaster broadcaster = new MockTransactionBroadcaster(wallet); - wallet.setTransactionBroadcaster(broadcaster); wallet.setKeyRotationEnabled(true); // Send three cents to two different keys, then add a key and mark the initial keys as compromised. ECKey key1 = new ECKey(); @@ -2251,7 +2250,7 @@ public class WalletTest extends TestWithWallet { assertArrayEquals(address.getHash160(), tx.getOutput(0).getScriptPubKey().getPubKeyHash()); } - //@Test - this test is slow, disable for now. + //@Test //- this test is slow, disable for now. public void fragmentedReKeying() throws Exception { // Send lots of small coins and check the fee is correct. ECKey key = new ECKey(); @@ -2264,7 +2263,6 @@ public class WalletTest extends TestWithWallet { } MockTransactionBroadcaster broadcaster = new MockTransactionBroadcaster(wallet); - wallet.setTransactionBroadcaster(broadcaster); wallet.setKeyRotationEnabled(true); Date compromise = Utils.now();