@Test
public void peerDiscoveryPolling() throws InterruptedException {

    // Check that if peer discovery fails, we keep trying until we have some nodes to talk with.
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean result = new AtomicBoolean();
    peerGroup.addPeerDiscovery(new PeerDiscovery() {
        public InetSocketAddress[] getPeers(long unused, TimeUnit unused2) throws PeerDiscoveryException {
            if (!result.getAndSet(true)) {
                // Pretend we are not connected to the internet.
                throw new PeerDiscoveryException("test failure");
            } else {
                // Return a bogus address.
                latch.countDown();
                return new InetSocketAddress[]{new InetSocketAddress("localhost", 1)};
            }
        }
        public void shutdown() {
        }
    });
    peerGroup.startAndWait();
    latch.await();
    // Check that we did indeed throw an exception. If we got here it means we threw and then PeerGroup tried
    // again a bit later.
    assertTrue(result.get());
}