SweetCountDownLatch extends CountDownLatch { @Override public void await() { boolean wait = true; while (wait) { try { super.await(); wait = false; } catch (InterruptedException e) { logger.debug("Interrupted in SweetCountDownLatch", e); } } } SweetCountDownLatch(int count, Logger logger); @Override void await(); @Override boolean await(long timeout, TimeUnit unit);  }
@Test public void test() throws InterruptedException { final CountDownLatch latch = new SweetCountDownLatch(1, logger); Thread latchThread = new Thread(new Runnable() { @Override public void run() { try { latch.await(); } catch (InterruptedException e) { } } }); T t = new T(latch, latchThread, 0); new Thread(t).start(); latchThread.start(); latchThread.join(); Assert.assertEquals(0, latch.getCount()); }
@Test public void testTimeoutExpired() throws InterruptedException { final SweetCountDownLatch latch = new SweetCountDownLatch(1, logger); final boolean[] b = new boolean[1]; Thread latchThread = new Thread(new Runnable() { @Override public void run() { b[0] = latch.await(50, TimeUnit.MILLISECONDS); } }); T t = new T(latch, latchThread, 10000); new Thread(t).start(); latchThread.start(); latchThread.join(); Assert.assertFalse(b[0]); }
@Test public void testTimeout() throws InterruptedException { final SweetCountDownLatch latch = new SweetCountDownLatch(1, logger); final boolean[] b = new boolean[1]; Thread latchThread = new Thread(new Runnable() { @Override public void run() { b[0] = latch.await(10000, TimeUnit.MILLISECONDS); } }); T t = new T(latch, latchThread, 0); new Thread(t).start(); latchThread.start(); latchThread.join(); Assert.assertTrue(b[0]); }