Back to Repositories

Testing Event Translation Mechanisms in LMAX-Disruptor

This test suite validates the EventTranslator functionality in the LMAX Disruptor library, focusing on the translation of data into events. The tests ensure proper event transformation and value assignment within the high-performance inter-thread messaging library.

Test Coverage Overview

The test coverage focuses on the core event translation functionality of the LMAX Disruptor.

Key areas tested include:
  • Event instantiation and factory usage
  • Data translation into event objects
  • Value assignment verification
  • Sequence handling during translation

Implementation Analysis

The testing approach utilizes JUnit Jupiter for test execution and assertions. The implementation demonstrates a clean separation of concerns through the ExampleEventTranslator class that implements the EventTranslator interface.

Key patterns include:
  • Factory pattern for event creation
  • Interface implementation for translation logic
  • Immutable test value handling

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • StubEvent support class for test scenarios
  • Custom EventTranslator implementation
  • Assertion utilities for verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Single responsibility principle in test methods
  • Clear test naming conventions
  • Proper encapsulation of test data
  • Focused test scenarios with specific assertions
  • Clean separation between test setup and verification

lmax-exchange/disruptor

src/test/java/com/lmax/disruptor/EventTranslatorTest.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.StubEvent;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public final class EventTranslatorTest
{
    private static final String TEST_VALUE = "Wibble";

    @Test
    public void shouldTranslateOtherDataIntoAnEvent()
    {
        StubEvent event = StubEvent.EVENT_FACTORY.newInstance();
        EventTranslator<StubEvent> eventTranslator = new ExampleEventTranslator(TEST_VALUE);

        eventTranslator.translateTo(event, 0);

        assertEquals(TEST_VALUE, event.getTestString());
    }

    public static final class ExampleEventTranslator
        implements EventTranslator<StubEvent>
    {
        private final String testValue;

        public ExampleEventTranslator(final String testValue)
        {
            this.testValue = testValue;
        }

        @Override
        public void translateTo(final StubEvent event, final long sequence)
        {
            event.setTestString(testValue);
        }
    }
}