Exemplo n.º 1
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);
 }
Exemplo n.º 2
0
 @Test(expected = IllegalStateException.class)
 public void shouldThrowExceptionIfStartIsCalledTwice() throws Exception {
   executor.ignoreExecutions();
   disruptor.handleEventsWith(new SleepingEventHandler());
   disruptor.start();
   disruptor.start();
 }
Exemplo n.º 3
0
 @Test(expected = IllegalStateException.class)
 public void shouldThrowExceptionWhenAddingEventProcessorsAfterTheProducerBarrierHasBeenCreated()
     throws Exception {
   executor.ignoreExecutions();
   disruptor.handleEventsWith(new SleepingEventHandler());
   disruptor.start();
   disruptor.handleEventsWith(new SleepingEventHandler());
 }
Exemplo n.º 4
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);
  }
Exemplo n.º 5
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);
  }
Exemplo n.º 6
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)));
  }
Exemplo n.º 7
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);
  }
Exemplo n.º 8
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);
  }
Exemplo n.º 9
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);
  }
Exemplo n.º 10
0
  private TestEvent publishEvent() {
    if (ringBuffer == null) {
      ringBuffer = disruptor.start();
    }

    disruptor.publishEvent(
        new EventTranslator<TestEvent>() {
          @Override
          public TestEvent translateTo(final TestEvent event, final long sequence) {
            lastPublishedEvent = event;
            return event;
          }
        });

    return lastPublishedEvent;
  }
Exemplo n.º 11
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);
  }
Exemplo n.º 12
0
  @Test
  public void shouldMakeEntriesAvailableToFirstHandlersImmediately() throws Exception {
    CountDownLatch countDownLatch = new CountDownLatch(2);
    EventHandler<TestEvent> eventHandler = new EventHandlerStub(countDownLatch);

    disruptor.handleEventsWith(createDelayedEventHandler(), eventHandler);

    ensureTwoEventsProcessedAccordingToDependencies(countDownLatch);
  }
Exemplo n.º 13
0
  @After
  public void tearDown() throws Exception {
    for (DelayedEventHandler delayedEventHandler : delayedEventHandlers) {
      delayedEventHandler.stopWaiting();
    }

    disruptor.halt();
    executor.joinAllThreads();
  }
Exemplo n.º 14
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);
  }
Exemplo n.º 15
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();
    }
  }
Exemplo n.º 16
0
 @Test(expected = IllegalArgumentException.class)
 public void shouldThrowExceptionIfHandlerIsNotAlreadyConsuming() throws Exception {
   disruptor.after(createDelayedEventHandler()).handleEventsWith(createDelayedEventHandler());
 }