Back to Repositories

Testing Consumer Repository Management in LMAX-Exchange/disruptor

This test suite examines the ConsumerRepository class functionality in the LMAX Disruptor framework, focusing on handler-processor-barrier relationships. It validates the management and retrieval of event processors, handlers, and sequence barriers within the consumer repository system.

Test Coverage Overview

The test suite provides comprehensive coverage of ConsumerRepository operations including:

  • Barrier retrieval for registered handlers
  • Null handling for unregistered handlers
  • Event processor lookup functionality
  • Exception handling for invalid requests

Implementation Analysis

The testing approach utilizes JUnit Jupiter with a clear setup-execute-verify pattern. Each test case isolates specific repository operations using dummy implementations and sleeping event handlers to ensure reliable behavior verification.

The implementation leverages mock objects (DummyEventProcessor, DummySequenceBarrier) to isolate the repository logic from actual event processing.

Technical Details

Testing tools and configuration:

  • JUnit Jupiter framework
  • Hamcrest matchers for assertions
  • Mock implementations: DummyEventProcessor, DummySequenceBarrier
  • SleepingEventHandler for handler simulation
  • BeforeEach setup for test isolation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming reflecting purpose
  • Proper test isolation through @BeforeEach
  • Comprehensive error case coverage
  • Use of appropriate assertion methods
  • Clean separation of setup and verification code

lmax-exchange/disruptor

src/test/java/com/lmax/disruptor/dsl/ConsumerRepositoryTest.java

            
/*
 * Copyright 2011 LMAX Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmax.disruptor.dsl;

import com.lmax.disruptor.EventProcessor;
import com.lmax.disruptor.Sequence;
import com.lmax.disruptor.SequenceBarrier;
import com.lmax.disruptor.dsl.stubs.SleepingEventHandler;
import com.lmax.disruptor.support.DummyEventProcessor;
import com.lmax.disruptor.support.DummySequenceBarrier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ConsumerRepositoryTest
{
    private ConsumerRepository consumerRepository;
    private EventProcessor eventProcessor1;
    private EventProcessor eventProcessor2;
    private SleepingEventHandler handler1;
    private SleepingEventHandler handler2;
    private SequenceBarrier barrier1;
    private SequenceBarrier barrier2;

    @BeforeEach
    public void setUp()
    {
        consumerRepository = new ConsumerRepository();
        eventProcessor1 = new DummyEventProcessor(new Sequence());
        eventProcessor2 = new DummyEventProcessor(new Sequence());

        eventProcessor1.run();
        eventProcessor2.run();

        handler1 = new SleepingEventHandler();
        handler2 = new SleepingEventHandler();

        barrier1 = new DummySequenceBarrier();
        barrier2 = new DummySequenceBarrier();
    }

    @Test
    public void shouldGetBarrierByHandler()
    {
        consumerRepository.add(eventProcessor1, handler1, barrier1);

        assertThat(consumerRepository.getBarrierFor(handler1), sameInstance(barrier1));
    }

    @Test
    public void shouldReturnNullForBarrierWhenHandlerIsNotRegistered()
    {
        assertThat(consumerRepository.getBarrierFor(handler1), is(nullValue()));
    }

    @Test
    public void shouldRetrieveEventProcessorForHandler()
    {
        consumerRepository.add(eventProcessor1, handler1, barrier1);

        assertThat(consumerRepository.getEventProcessorFor(handler1), sameInstance(eventProcessor1));
    }

    @Test
    public void shouldThrowExceptionWhenHandlerIsNotRegistered()
    {
        assertThrows(IllegalArgumentException.class, () ->
                consumerRepository.getEventProcessorFor(new SleepingEventHandler())
        );
    }
}