private void assertProducerReaches( final StubPublisher stubPublisher, final int expectedPublicationCount, boolean strict) { long loopStart = System.currentTimeMillis(); while (stubPublisher.getPublicationCount() < expectedPublicationCount && System.currentTimeMillis() - loopStart < 5000) { yield(); } if (strict) { assertThat( Integer.valueOf(stubPublisher.getPublicationCount()), equalTo(Integer.valueOf(expectedPublicationCount))); } else { final int actualPublicationCount = stubPublisher.getPublicationCount(); assertTrue( "Producer reached unexpected count. Expected at least " + expectedPublicationCount + " but only reached " + actualPublicationCount, actualPublicationCount >= expectedPublicationCount); } }
@Test public void shouldBlockProducerUntilAllEventProcessorsHaveAdvanced() throws Exception { final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); disruptor.handleEventsWith(delayedEventHandler); final RingBuffer<TestEvent> ringBuffer = disruptor.start(); final StubPublisher stubPublisher = new StubPublisher(ringBuffer); try { executor.execute(stubPublisher); assertProducerReaches(stubPublisher, 4, true); delayedEventHandler.processEvent(); delayedEventHandler.processEvent(); delayedEventHandler.processEvent(); delayedEventHandler.processEvent(); delayedEventHandler.processEvent(); assertProducerReaches(stubPublisher, 5, false); } finally { stubPublisher.halt(); } }