TimeoutBlockingWaitStrategy implements WaitStrategy { @Override public long waitFor(final long sequence, final Sequence cursorSequence, final Sequence dependentSequence, final SequenceBarrier barrier) throws AlertException, InterruptedException, TimeoutException { long nanos = timeoutInNanos; long availableSequence; if (cursorSequence.get() < sequence) { lock.lock(); try { while (cursorSequence.get() < sequence) { barrier.checkAlert(); nanos = processorNotifyCondition.awaitNanos(nanos); if (nanos <= 0) { throw TimeoutException.INSTANCE; } } } finally { lock.unlock(); } } while ((availableSequence = dependentSequence.get()) < sequence) { barrier.checkAlert(); } return availableSequence; } TimeoutBlockingWaitStrategy(final long timeout, final TimeUnit units); @Override long waitFor(final long sequence, final Sequence cursorSequence,
                        final Sequence dependentSequence, final SequenceBarrier barrier); @Override void signalAllWhenBlocking(); @Override String toString();  }
@Test public void shouldTimeoutWaitFor() throws Exception { final SequenceBarrier sequenceBarrier = new DummySequenceBarrier(); long theTimeout = 500; TimeoutBlockingWaitStrategy waitStrategy = new TimeoutBlockingWaitStrategy(theTimeout, TimeUnit.MILLISECONDS); Sequence cursor = new Sequence(5); Sequence dependent = cursor; long t0 = System.currentTimeMillis(); try { waitStrategy.waitFor(6, cursor, dependent, sequenceBarrier); fail("TimeoutException should have been thrown"); } catch (TimeoutException e) { } long t1 = System.currentTimeMillis(); long timeWaiting = t1 - t0; assertTrue(timeWaiting >= theTimeout); }