private void ensureTwoEventsProcessedAccordingToDependencies( final CountDownLatch countDownLatch, final DelayedEventHandler... dependencies) throws InterruptedException, BrokenBarrierException { publishEvent(); publishEvent(); for (DelayedEventHandler dependency : dependencies) { assertThatCountDownLatchEquals(countDownLatch, 2L); dependency.processEvent(); dependency.processEvent(); } assertThatCountDownLatchIsZero(countDownLatch); }
@Test public void shouldProvideEventsToWorkHandlers() throws Exception { final TestWorkHandler workHandler1 = createTestWorkHandler(); final TestWorkHandler workHandler2 = createTestWorkHandler(); disruptor.handleEventsWithWorkerPool(workHandler1, workHandler2); publishEvent(); publishEvent(); workHandler1.processEvent(); workHandler2.processEvent(); }
@Test(expected = TimeoutException.class, timeout = 2000) public void shouldThrowTimeoutExceptionIfShutdownDoesNotCompleteNormally() throws Exception { //Given final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); disruptor.handleEventsWith(delayedEventHandler); publishEvent(); //When disruptor.shutdown(1, SECONDS); //Then }
@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 shouldApplyDefaultExceptionHandlerToExistingEventProcessors() throws Exception { AtomicReference<Throwable> eventHandled = new AtomicReference<Throwable>(); ExceptionHandler exceptionHandler = new StubExceptionHandler(eventHandled); RuntimeException testException = new RuntimeException(); ExceptionThrowingEventHandler handler = new ExceptionThrowingEventHandler(testException); disruptor.handleEventsWith(handler); disruptor.setDefaultExceptionHandler(exceptionHandler); publishEvent(); final Throwable actualException = waitFor(eventHandled); assertSame(testException, actualException); }
@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.setDefaultExceptionHandler(exceptionHandler); disruptor.handleEventsWith(handler); publishEvent(); final Throwable actualException = waitFor(eventHandled); assertSame(testException, actualException); }
@SuppressWarnings("deprecation") @Test public void shouldSupportSpecifyingAExceptionHandlerForEventProcessors() 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 shouldSupportUsingWorkerPoolWithADependency() throws Exception { final TestWorkHandler workHandler1 = createTestWorkHandler(); final TestWorkHandler workHandler2 = createTestWorkHandler(); final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); disruptor.handleEventsWith(delayedEventHandler).thenHandleEventsWithWorkerPool(workHandler1, workHandler2); publishEvent(); publishEvent(); delayedEventHandler.processEvent(); delayedEventHandler.processEvent(); workHandler1.processEvent(); workHandler2.processEvent(); }
@Test(timeout = 1000) public void shouldTrackRemainingCapacity() throws Exception { final long[] remainingCapacity = {-1}; //Given final EventHandler<TestEvent> eventHandler = new EventHandler<TestEvent>() { @Override public void onEvent(final TestEvent event, final long sequence, final boolean endOfBatch) throws Exception { remainingCapacity[0] = disruptor.getRingBuffer().remainingCapacity(); } }; disruptor.handleEventsWith(eventHandler); //When publishEvent(); //Then while (remainingCapacity[0] == -1) { Thread.sleep(100); } assertThat(remainingCapacity[0], is(ringBuffer.getBufferSize() - 1L)); assertThat(disruptor.getRingBuffer().remainingCapacity(), is(ringBuffer.getBufferSize() - 0L)); }
@Test public void shouldSupportUsingWorkerPoolAsDependency() throws Exception { final TestWorkHandler workHandler1 = createTestWorkHandler(); final TestWorkHandler workHandler2 = createTestWorkHandler(); final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); disruptor.handleEventsWithWorkerPool(workHandler1, workHandler2).then(delayedEventHandler); publishEvent(); publishEvent(); assertThat(disruptor.getBarrierFor(delayedEventHandler).getCursor(), equalTo(-1L)); workHandler2.processEvent(); workHandler1.processEvent(); delayedEventHandler.processEvent(); }
@Test public void shouldSupportUsingWorkerPoolAsDependencyAndProcessFirstEventAsSoonAsItIsAvailable() throws Exception { final TestWorkHandler workHandler1 = createTestWorkHandler(); final TestWorkHandler workHandler2 = createTestWorkHandler(); final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); disruptor.handleEventsWithWorkerPool(workHandler1, workHandler2).then(delayedEventHandler); publishEvent(); publishEvent(); workHandler1.processEvent(); delayedEventHandler.processEvent(); workHandler2.processEvent(); delayedEventHandler.processEvent(); }
@SuppressWarnings("deprecation") @Test public void shouldOnlyApplyExceptionsHandlersSpecifiedViaHandleExceptionsWithOnNewEventProcessors() 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); disruptor.handleExceptionsWith(new FatalExceptionHandler()); publishEvent(); final Throwable actualException = waitFor(eventHandled); assertSame(testException, actualException); }
@Test public void shouldSupportCombiningWorkerPoolWithEventHandlerAsDependencyWhenNotPreviouslyRegistered() throws Exception { final TestWorkHandler workHandler1 = createTestWorkHandler(); final DelayedEventHandler delayedEventHandler1 = createDelayedEventHandler(); final DelayedEventHandler delayedEventHandler2 = createDelayedEventHandler(); disruptor .handleEventsWith(delayedEventHandler1) .and(disruptor.handleEventsWithWorkerPool(workHandler1)) .then(delayedEventHandler2); publishEvent(); publishEvent(); delayedEventHandler1.processEvent(); delayedEventHandler1.processEvent(); workHandler1.processEvent(); delayedEventHandler2.processEvent(); workHandler1.processEvent(); delayedEventHandler2.processEvent(); }