예제 #1
0
 @Test(expected = IllegalStateException.class)
 public void shouldThrowExceptionWhenAddingEventProcessorsAfterTheProducerBarrierHasBeenCreated()
     throws Exception {
   executor.ignoreExecutions();
   disruptor.handleEventsWith(new SleepingEventHandler());
   disruptor.start();
   disruptor.handleEventsWith(new SleepingEventHandler());
 }
예제 #2
0
 @Test(expected = IllegalStateException.class)
 public void shouldThrowExceptionIfStartIsCalledTwice() throws Exception {
   executor.ignoreExecutions();
   disruptor.handleEventsWith(new SleepingEventHandler());
   disruptor.start();
   disruptor.start();
 }
예제 #3
0
 @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);
 }
예제 #4
0
  @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);
  }
예제 #5
0
  @Test
  public void shouldMakeEntriesAvailableToFirstHandlersImmediately() throws Exception {
    CountDownLatch countDownLatch = new CountDownLatch(2);
    EventHandler<TestEvent> eventHandler = new EventHandlerStub(countDownLatch);

    disruptor.handleEventsWith(createDelayedEventHandler(), eventHandler);

    ensureTwoEventsProcessedAccordingToDependencies(countDownLatch);
  }
예제 #6
0
  @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);
  }
예제 #7
0
  @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);
  }
예제 #8
0
  @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);
  }
예제 #9
0
  @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)));
  }
예제 #10
0
  @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);
  }
예제 #11
0
  @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);
  }
예제 #12
0
  @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);
  }
예제 #13
0
  @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();
    }
  }