Program: 85758
Project: 85758
Buggy source:
@Test public void testSimpleChannel() throws Exception {
  final SettableFuture<ListenableFuture<PaymentChannelServerState>> serverCloseFuture=SettableFuture.create();
  final SettableFuture<Sha256Hash> channelOpenFuture=SettableFuture.create();
  final BlockingQueue<BigInteger> q=new LinkedBlockingQueue<BigInteger>();
  final PaymentChannelServerListener server=new PaymentChannelServerListener(mockBroadcaster,serverWallet,1,Utils.COIN,new PaymentChannelServerListener.HandlerFactory(){
    @Nullable @Override public ServerConnectionEventHandler onNewConnection(    SocketAddress clientAddress){
      return new ServerConnectionEventHandler(){
        @Override public void channelOpen(        Sha256Hash channelId){
          channelOpenFuture.set(channelId);
        }
        @Override public void paymentIncrease(        BigInteger by,        BigInteger to){
          q.add(to);
        }
        @Override public void channelClosed(        CloseReason reason){
          serverCloseFuture.set(null);
        }
      }
;
    }
  }
);
  server.bindAndStart(4243);
  PaymentChannelClientConnection client=new PaymentChannelClientConnection(new InetSocketAddress("String_Node_Str",4243),1,wallet,myKey,Utils.COIN,"String_Node_Str");
  broadcastTxPause.release();
  Transaction broadcastMultiSig=broadcasts.take();
  client.getChannelOpenFuture().get();
  assertEquals(broadcastMultiSig.getHash(),channelOpenFuture.get());
  assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE,client.state().getValueSpent());
  final CountDownLatch latch=new CountDownLatch(3);
  File tempFile=File.createTempFile("String_Node_Str","String_Node_Str");
  tempFile.deleteOnExit();
  serverWallet.autosaveToFile(tempFile,0,TimeUnit.SECONDS,new WalletFiles.Listener(){
    @Override public void onBeforeAutoSave(    File tempFile){
      latch.countDown();
    }
    @Override public void onAfterAutoSave(    File newlySavedFile){
    }
  }
);
  Thread.sleep(1250);
  BigInteger amount=client.state().getValueSpent();
  assertEquals(amount,q.take());
  client.incrementPayment(Utils.CENT).get();
  amount=amount.add(Utils.CENT);
  assertEquals(amount,q.take());
  client.incrementPayment(Utils.CENT).get();
  amount=amount.add(Utils.CENT);
  assertEquals(amount,q.take());
  client.incrementPayment(Utils.CENT).get();
  amount=amount.add(Utils.CENT);
  assertEquals(amount,q.take());
  latch.await();
  StoredPaymentChannelServerStates channels=(StoredPaymentChannelServerStates)serverWallet.getExtensions().get(StoredPaymentChannelServerStates.EXTENSION_ID);
  StoredServerChannel storedServerChannel=channels.getChannel(broadcastMultiSig.getHash());
  PaymentChannelServerState serverState=storedServerChannel.getOrCreateState(serverWallet,mockBroadcaster);
  client.settle();
  client.settle();
  broadcastTxPause.release();
  Transaction settleTx=broadcasts.take();
  assertEquals(PaymentChannelServerState.State.CLOSED,serverState.getState());
  if (!serverState.getBestValueToMe().equals(amount) || !serverState.getFeePaid().equals(BigInteger.ZERO))   fail();
  assertTrue(channels.mapChannels.isEmpty());
  sendMoneyToWallet(settleTx,AbstractBlockChain.NewBlockType.BEST_CHAIN);
  assertEquals(PaymentChannelClientState.State.CLOSED,client.state().getState());
  server.close();
  server.close();
  assertEquals(1,StoredPaymentChannelClientStates.getFromWallet(wallet).mapChannels.size());
  wallet.notifyNewBestBlock(createFakeBlock(blockStore).storedBlock);
  assertEquals(1,StoredPaymentChannelClientStates.getFromWallet(wallet).mapChannels.size());
  wallet.notifyNewBestBlock(createFakeBlock(blockStore).storedBlock);
  assertEquals(1,StoredPaymentChannelClientStates.getFromWallet(wallet).mapChannels.size());
  wallet.notifyNewBestBlock(createFakeBlock(blockStore).storedBlock);
  assertEquals(0,StoredPaymentChannelClientStates.getFromWallet(wallet).mapChannels.size());
}

Fixed version (ground truth):
@Test public void testSimpleChannel() throws Exception {
  final SettableFuture<ListenableFuture<PaymentChannelServerState>> serverCloseFuture=SettableFuture.create();
  final SettableFuture<Sha256Hash> channelOpenFuture=SettableFuture.create();
  final BlockingQueue<BigInteger> q=new LinkedBlockingQueue<BigInteger>();
  final PaymentChannelServerListener server=new PaymentChannelServerListener(mockBroadcaster,serverWallet,30,Utils.COIN,new PaymentChannelServerListener.HandlerFactory(){
    @Nullable @Override public ServerConnectionEventHandler onNewConnection(    SocketAddress clientAddress){
      return new ServerConnectionEventHandler(){
        @Override public void channelOpen(        Sha256Hash channelId){
          channelOpenFuture.set(channelId);
        }
        @Override public void paymentIncrease(        BigInteger by,        BigInteger to){
          q.add(to);
        }
        @Override public void channelClosed(        CloseReason reason){
          serverCloseFuture.set(null);
        }
      }
;
    }
  }
);
  server.bindAndStart(4243);
  PaymentChannelClientConnection client=new PaymentChannelClientConnection(new InetSocketAddress("String_Node_Str",4243),30,wallet,myKey,Utils.COIN,"String_Node_Str");
  broadcastTxPause.release();
  Transaction broadcastMultiSig=broadcasts.take();
  client.getChannelOpenFuture().get();
  assertEquals(broadcastMultiSig.getHash(),channelOpenFuture.get());
  assertEquals(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE,client.state().getValueSpent());
  final CountDownLatch latch=new CountDownLatch(3);
  File tempFile=File.createTempFile("String_Node_Str","String_Node_Str");
  tempFile.deleteOnExit();
  serverWallet.autosaveToFile(tempFile,0,TimeUnit.SECONDS,new WalletFiles.Listener(){
    @Override public void onBeforeAutoSave(    File tempFile){
      latch.countDown();
    }
    @Override public void onAfterAutoSave(    File newlySavedFile){
    }
  }
);
  Thread.sleep(1250);
  BigInteger amount=client.state().getValueSpent();
  assertEquals(amount,q.take());
  client.incrementPayment(Utils.CENT).get();
  amount=amount.add(Utils.CENT);
  assertEquals(amount,q.take());
  client.incrementPayment(Utils.CENT).get();
  amount=amount.add(Utils.CENT);
  assertEquals(amount,q.take());
  client.incrementPayment(Utils.CENT).get();
  amount=amount.add(Utils.CENT);
  assertEquals(amount,q.take());
  latch.await();
  StoredPaymentChannelServerStates channels=(StoredPaymentChannelServerStates)serverWallet.getExtensions().get(StoredPaymentChannelServerStates.EXTENSION_ID);
  StoredServerChannel storedServerChannel=channels.getChannel(broadcastMultiSig.getHash());
  PaymentChannelServerState serverState=storedServerChannel.getOrCreateState(serverWallet,mockBroadcaster);
  client.settle();
  client.settle();
  broadcastTxPause.release();
  Transaction settleTx=broadcasts.take();
  assertEquals(PaymentChannelServerState.State.CLOSED,serverState.getState());
  if (!serverState.getBestValueToMe().equals(amount) || !serverState.getFeePaid().equals(BigInteger.ZERO))   fail();
  assertTrue(channels.mapChannels.isEmpty());
  sendMoneyToWallet(settleTx,AbstractBlockChain.NewBlockType.BEST_CHAIN);
  assertEquals(PaymentChannelClientState.State.CLOSED,client.state().getState());
  server.close();
  server.close();
  assertEquals(1,StoredPaymentChannelClientStates.getFromWallet(wallet).mapChannels.size());
  wallet.notifyNewBestBlock(createFakeBlock(blockStore).storedBlock);
  assertEquals(1,StoredPaymentChannelClientStates.getFromWallet(wallet).mapChannels.size());
  wallet.notifyNewBestBlock(createFakeBlock(blockStore).storedBlock);
  assertEquals(0,StoredPaymentChannelClientStates.getFromWallet(wallet).mapChannels.size());
}