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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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())
);
}
}