3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 23:03:04 +00:00

Simplify the MockTransactionBroadcaster a bit and update the docs.

This commit is contained in:
Mike Hearn 2014-04-23 00:24:48 +02:00
parent bcc8055b7e
commit 6e999c6054
2 changed files with 6 additions and 5 deletions

View File

@ -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 * 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 * broadcast through it and makes them available via the {@link #waitForTransaction()} method. Using that will cause
* {@link LinkedBlockingQueue} will block the thread until a transaction is available. * 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 { public class MockTransactionBroadcaster implements TransactionBroadcaster {
private final ReentrantLock lock = Threading.lock("mock tx broadcaster"); private final ReentrantLock lock = Threading.lock("mock tx broadcaster");
@ -49,12 +50,14 @@ public class MockTransactionBroadcaster implements TransactionBroadcaster {
private final LinkedBlockingQueue<TxFuturePair> broadcasts = new LinkedBlockingQueue<TxFuturePair>(); private final LinkedBlockingQueue<TxFuturePair> broadcasts = new LinkedBlockingQueue<TxFuturePair>();
/** Sets this mock broadcaster on the given wallet. */
public MockTransactionBroadcaster(Wallet wallet) { public MockTransactionBroadcaster(Wallet wallet) {
// This code achieves nothing directly, but it sets up the broadcaster/peergroup > wallet lock ordering // This code achieves nothing directly, but it sets up the broadcaster/peergroup > wallet lock ordering
// so inversions can be caught. // so inversions can be caught.
lock.lock(); lock.lock();
try { try {
this.wallet = wallet; this.wallet = wallet;
wallet.setTransactionBroadcaster(this);
wallet.getPendingTransactions(); wallet.getPendingTransactions();
} finally { } finally {
lock.unlock(); lock.unlock();

View File

@ -2187,7 +2187,6 @@ public class WalletTest extends TestWithWallet {
Utils.setMockClock(); Utils.setMockClock();
// Watch out for wallet-initiated broadcasts. // Watch out for wallet-initiated broadcasts.
MockTransactionBroadcaster broadcaster = new MockTransactionBroadcaster(wallet); MockTransactionBroadcaster broadcaster = new MockTransactionBroadcaster(wallet);
wallet.setTransactionBroadcaster(broadcaster);
wallet.setKeyRotationEnabled(true); wallet.setKeyRotationEnabled(true);
// Send three cents to two different keys, then add a key and mark the initial keys as compromised. // Send three cents to two different keys, then add a key and mark the initial keys as compromised.
ECKey key1 = new ECKey(); ECKey key1 = new ECKey();
@ -2251,7 +2250,7 @@ public class WalletTest extends TestWithWallet {
assertArrayEquals(address.getHash160(), tx.getOutput(0).getScriptPubKey().getPubKeyHash()); 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 { public void fragmentedReKeying() throws Exception {
// Send lots of small coins and check the fee is correct. // Send lots of small coins and check the fee is correct.
ECKey key = new ECKey(); ECKey key = new ECKey();
@ -2264,7 +2263,6 @@ public class WalletTest extends TestWithWallet {
} }
MockTransactionBroadcaster broadcaster = new MockTransactionBroadcaster(wallet); MockTransactionBroadcaster broadcaster = new MockTransactionBroadcaster(wallet);
wallet.setTransactionBroadcaster(broadcaster);
wallet.setKeyRotationEnabled(true); wallet.setKeyRotationEnabled(true);
Date compromise = Utils.now(); Date compromise = Utils.now();