Back to Repositories

Testing Aggregate Event Handler Sequence Management in LMAX-Disruptor

This test suite validates the AggregateEventHandler functionality in the LMAX Disruptor library, focusing on event handling sequence, lifecycle management, and handler aggregation. The tests ensure proper event propagation and lifecycle method execution across multiple event handlers.

Test Coverage Overview

The test suite provides comprehensive coverage of the AggregateEventHandler class functionality.

Key areas tested include:
  • Sequential event processing across multiple handlers
  • Lifecycle method execution (onStart, onShutdown)
  • Empty handler list scenarios
  • Event sequence validation

Implementation Analysis

The testing approach utilizes JUnit Jupiter framework with a focus on behavioral verification. The implementation employs DummyEventHandler instances to simulate real event handlers, allowing precise verification of event propagation and method invocation order.

Technical patterns include:
  • Handler aggregation pattern testing
  • Sequence validation assertions
  • Lifecycle method verification

Technical Details

Testing stack includes:
  • JUnit Jupiter for test execution
  • Hamcrest for assertions
  • DummyEventHandler for test doubles
  • Custom assertion helpers for verification
Configuration leverages @Test annotations with exception handling for lifecycle methods.

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through clear separation of concerns and thorough verification.

Notable practices include:
  • Isolated test cases for each functionality
  • Comprehensive edge case coverage
  • Clear test method naming
  • Reusable assertion helpers
  • Proper test setup and teardown

lmax-exchange/disruptor

src/test/java/com/lmax/disruptor/AggregateEventHandlerTest.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;

import com.lmax.disruptor.support.DummyEventHandler;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

@SuppressWarnings("unchecked")
public final class AggregateEventHandlerTest
{
    private final DummyEventHandler<int[]> eh1 = new DummyEventHandler<>();
    private final DummyEventHandler<int[]> eh2 = new DummyEventHandler<>();
    private final DummyEventHandler<int[]> eh3 = new DummyEventHandler<>();

    @Test
    public void shouldCallOnEventInSequence()
        throws Exception
    {
        final int[] event = {7};
        final long sequence = 3L;
        final boolean endOfBatch = true;

        final AggregateEventHandler<int[]> aggregateEventHandler = new AggregateEventHandler<>(eh1, eh2, eh3);

        aggregateEventHandler.onEvent(event, sequence, endOfBatch);
        assertLastEvent(event, sequence, eh1, eh2, eh3);
    }

    @Test
    public void shouldCallOnStartInSequence()
        throws Exception
    {
        final AggregateEventHandler<int[]> aggregateEventHandler = new AggregateEventHandler<>(eh1, eh2, eh3);

        aggregateEventHandler.onStart();

        assertStartCalls(1, eh1, eh2, eh3);
    }

    @Test
    public void shouldCallOnShutdownInSequence()
        throws Exception
    {
        final AggregateEventHandler<int[]> aggregateEventHandler = new AggregateEventHandler<>(eh1, eh2, eh3);

        aggregateEventHandler.onShutdown();

        assertShutdownCalls(1, eh1, eh2, eh3);
    }

    @Test
    public void shouldHandleEmptyListOfEventHandlers() throws Exception
    {
        final AggregateEventHandler<int[]> aggregateEventHandler = new AggregateEventHandler<>();

        aggregateEventHandler.onEvent(new int[]{7}, 0L, true);
        aggregateEventHandler.onStart();
        aggregateEventHandler.onShutdown();
    }

    private static void assertLastEvent(final int[] event, final long sequence, final DummyEventHandler<int[]>... eh1)
    {
        for (DummyEventHandler<int[]> eh : eh1)
        {
            assertThat(eh.lastEvent, is(event));
            assertThat(eh.lastSequence, is(sequence));
        }
    }

    private static void assertStartCalls(final int startCalls, final DummyEventHandler<int[]>... handlers)
    {
        for (DummyEventHandler<int[]> handler : handlers)
        {
            assertThat(handler.startCalls, is(startCalls));
        }
    }

    private static void assertShutdownCalls(final int startCalls, final DummyEventHandler<int[]>... handlers)
    {
        for (DummyEventHandler<int[]> handler : handlers)
        {
            assertThat(handler.shutdownCalls, is(startCalls));
        }
    }
}