@Test(expected = IllegalStateException.class) public void shouldThrowExceptionWhenAddingEventProcessorsAfterTheProducerBarrierHasBeenCreated() throws Exception { executor.ignoreExecutions(); disruptor.handleEventsWith(new SleepingEventHandler()); disruptor.start(); disruptor.handleEventsWith(new SleepingEventHandler()); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionIfStartIsCalledTwice() throws Exception { executor.ignoreExecutions(); disruptor.handleEventsWith(new SleepingEventHandler()); disruptor.start(); disruptor.start(); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionIfHandlerUsedWithAndIsNotAlreadyConsuming() throws Exception { final DelayedEventHandler handler1 = createDelayedEventHandler(); final DelayedEventHandler handler2 = createDelayedEventHandler(); disruptor.handleEventsWith(handler1); disruptor.after(handler1).and(handler2); }
@Test public void shouldSupportHandlersAsDependenciesToCustomProcessors() throws Exception { final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); disruptor.handleEventsWith(delayedEventHandler); RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer(); CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub(countDownLatch); final SequenceBarrier sequenceBarrier = disruptor.after(delayedEventHandler).asSequenceBarrier(); final BatchEventProcessor<TestEvent> processor = new BatchEventProcessor<TestEvent>(ringBuffer, sequenceBarrier, handlerWithBarrier); disruptor.handleEventsWith(processor); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, delayedEventHandler); }
@Test public void shouldMakeEntriesAvailableToFirstHandlersImmediately() throws Exception { CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> eventHandler = new EventHandlerStub(countDownLatch); disruptor.handleEventsWith(createDelayedEventHandler(), eventHandler); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch); }
@Test(expected = IllegalArgumentException.class) public void shouldTrackEventHandlersByIdentityNotEquality() throws Exception { EvilEqualsEventHandler handler1 = new EvilEqualsEventHandler(); EvilEqualsEventHandler handler2 = new EvilEqualsEventHandler(); disruptor.handleEventsWith(handler1); // handler2.equals(handler1) but it hasn't yet been registered so should throw exception. disruptor.after(handler2); }
@Test public void shouldAllowSpecifyingSpecificEventProcessorsToWaitFor() throws Exception { DelayedEventHandler handler1 = createDelayedEventHandler(); DelayedEventHandler handler2 = createDelayedEventHandler(); CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub(countDownLatch); disruptor.handleEventsWith(handler1, handler2); disruptor.after(handler1, handler2).handleEventsWith(handlerWithBarrier); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, handler1, handler2); }
@Test public void shouldWaitUntilAllFirstEventProcessorsProcessEventBeforeMakingItAvailableToDependentEventProcessors() throws Exception { DelayedEventHandler eventHandler1 = createDelayedEventHandler(); CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> eventHandler2 = new EventHandlerStub(countDownLatch); disruptor.handleEventsWith(eventHandler1).then(eventHandler2); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, eventHandler1); }
@Test public void shouldCreateEventProcessorGroupForFirstEventProcessors() throws Exception { executor.ignoreExecutions(); final EventHandler<TestEvent> eventHandler1 = new SleepingEventHandler(); EventHandler<TestEvent> eventHandler2 = new SleepingEventHandler(); final EventHandlerGroup<TestEvent> eventHandlerGroup = disruptor.handleEventsWith(eventHandler1, eventHandler2); disruptor.start(); assertNotNull(eventHandlerGroup); assertThat(Integer.valueOf(executor.getExecutionCount()), equalTo(Integer.valueOf(2))); }
@Test public void shouldWaitOnAllProducersJoinedByAnd() throws Exception { DelayedEventHandler handler1 = createDelayedEventHandler(); DelayedEventHandler handler2 = createDelayedEventHandler(); CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub(countDownLatch); disruptor.handleEventsWith(handler1, handler2); disruptor.after(handler1).and(handler2).handleEventsWith(handlerWithBarrier); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, handler1, handler2); }
@Test public void shouldBeAbleToOverrideTheExceptionHandlerForAEventProcessor() throws Exception { final RuntimeException testException = new RuntimeException(); final ExceptionThrowingEventHandler eventHandler = new ExceptionThrowingEventHandler(testException); disruptor.handleEventsWith(eventHandler); AtomicReference<Throwable> reference = new AtomicReference<Throwable>(); StubExceptionHandler exceptionHandler = new StubExceptionHandler(reference); disruptor.handleExceptionsFor(eventHandler).with(exceptionHandler); publishEvent(); waitFor(reference); }
@Test public void shouldSupportSpecifyingADefaultExceptionHandlerForEventProcessors() throws Exception { AtomicReference<Throwable> eventHandled = new AtomicReference<Throwable>(); ExceptionHandler exceptionHandler = new StubExceptionHandler(eventHandled); RuntimeException testException = new RuntimeException(); ExceptionThrowingEventHandler handler = new ExceptionThrowingEventHandler(testException); disruptor.handleExceptionsWith(exceptionHandler); disruptor.handleEventsWith(handler); publishEvent(); final Throwable actualException = waitFor(eventHandled); assertSame(testException, actualException); }
@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(); } }