From 0907a3852b63435383926b397a323d87d157b094 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 21 Jun 2013 12:37:27 +0200 Subject: [PATCH] Add a test to ensure that wallet listeners that throw exceptions don't prevent the others from running. --- .../com/google/bitcoin/core/WalletTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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 6bdce6f0..40ea4793 100644 --- a/core/src/test/java/com/google/bitcoin/core/WalletTest.java +++ b/core/src/test/java/com/google/bitcoin/core/WalletTest.java @@ -42,6 +42,7 @@ import java.security.SecureRandom; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import static com.google.bitcoin.core.TestUtils.*; import static com.google.bitcoin.core.Utils.bitcoinValueToFriendlyString; @@ -1806,4 +1807,26 @@ public class WalletTest extends TestWithWallet { // There is a test for spending a coinbase transaction as it matures in BlockChainTest#coinbaseTransactionAvailability // Support for offline spending is tested in PeerGroupTest + + @Test + public void exceptionsDoNotBlockAllListeners() throws Exception { + // Check that if a wallet listener throws an exception, the others still run. + wallet.addEventListener(new AbstractWalletEventListener() { + @Override + public void onCoinsReceived(Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { + throw new RuntimeException("barf"); + } + }); + final AtomicInteger flag = new AtomicInteger(); + wallet.addEventListener(new AbstractWalletEventListener() { + @Override + public void onCoinsReceived(Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { + flag.incrementAndGet(); + } + }); + + sendMoneyToWallet(Utils.toNanoCoins(1, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN); + Threading.waitForUserCode(); + assertEquals(1, flag.get()); + } }