### Question:
BarrierImplementation implements Barrier, BarrierGroup.Listener { @Override public void await() throws CancelledBarrierException, CommunicationException { new ForeverWaiter().await(); } BarrierImplementation(BarrierGroup group,
                               BarrierIdentity.Factory identityFactory); @Override void await(); @Override boolean await(long timeout, TimeUnit unit); @Override boolean await(long timeout); @Override void awaken(Set<BarrierIdentity> waiters); @Override void cancel(); @Override String getName();  }### Answer:
@Test public void testAwaitTimeout() throws Exception { final BarrierImplementation b = new BarrierImplementation(m_barrierGroup, m_identityFactory); reset(m_barrierGroup); final boolean result = b.await(1, TimeUnit.MICROSECONDS); verify(m_barrierGroup).addWaiter(ID2); verify(m_barrierGroup).cancelWaiter(ID2); verify(m_barrierGroup).removeBarriers(1); verify(m_barrierGroup).removeListener(b); verifyNoMoreInteractions(m_barrierGroup); assertFalse(result); }
@Test public void testAwaitTimeout2() throws Exception { final BarrierImplementation b = new BarrierImplementation(m_barrierGroup, m_identityFactory); reset(m_barrierGroup); final boolean result = b.await(1); verify(m_barrierGroup).addWaiter(ID2); verify(m_barrierGroup).addWaiter(ID2); verify(m_barrierGroup).cancelWaiter(ID2); verify(m_barrierGroup).removeBarriers(1); verify(m_barrierGroup).removeListener(b); verifyNoMoreInteractions(m_barrierGroup); assertFalse(result); }
@Test public void testAwaitInterrupted() throws Exception { final BarrierImplementation b = new BarrierImplementation(m_barrierGroup, m_identityFactory); final CyclicBarrier sync = new CyclicBarrier(2); final Thread mainThread = Thread.currentThread(); final Future<Void> future = m_executor.submit( new Callable<Void>() { public Void call() throws Exception { sync.await(); mainThread.interrupt(); return null; } }); sync.await(); try { b.await(); fail("Expected UncheckedInterruptedException"); } catch (UncheckedInterruptedException e) { } future.get(); }
@Test public void testAwaitTimeoutInterrupted() throws Exception { final BarrierImplementation b = new BarrierImplementation(m_barrierGroup, m_identityFactory); reset(m_barrierGroup); Thread.currentThread().interrupt(); try { b.await(123456); fail("Expected UncheckedInterruptedException"); } catch (UncheckedInterruptedException e) { } verify(m_barrierGroup).addWaiter(ID2); verify(m_barrierGroup).cancelWaiter(ID2); verify(m_barrierGroup).removeBarriers(1); verify(m_barrierGroup).removeListener(b); verifyNoMoreInteractions(m_barrierGroup); }