Back to Repositories

Testing Event Publisher Implementation in LMAX-Disruptor

This test suite validates the event publishing functionality in the LMAX Disruptor’s RingBuffer implementation. It focuses on testing both standard and conditional event publishing mechanisms while verifying sequence management and buffer capacity handling.

Test Coverage Overview

The test suite provides comprehensive coverage of the EventPublisher component, focusing on two critical publishing scenarios:

  • Standard event publishing with sequence verification
  • Conditional publishing with buffer capacity constraints
  • Sequence management and gating sequence interaction
  • Buffer overflow handling and boundary conditions

Implementation Analysis

The testing approach utilizes JUnit Jupiter framework with a combination of direct RingBuffer interaction and event translation. The implementation demonstrates proper sequence management through NoOpEventProcessor and custom event translation logic for Long-based events.

The tests employ both synchronous and conditional publishing patterns, validating the core functionality of the Disruptor’s event publishing mechanism.

Technical Details

  • JUnit Jupiter test framework
  • RingBuffer configuration with 32-element capacity
  • Custom EventTranslator implementation
  • Hamcrest matchers for assertions
  • Multi-producer RingBuffer setup

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper setup of test dependencies, clear separation of test cases, and comprehensive assertion coverage. It demonstrates effective use of:

  • Clear test method naming conventions
  • Proper buffer initialization and cleanup
  • Comprehensive boundary testing
  • Explicit sequence management

lmax-exchange/disruptor

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

import static com.lmax.disruptor.RingBuffer.createMultiProducer;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class EventPublisherTest implements EventTranslator<LongEvent>
{
    private static final int BUFFER_SIZE = 32;
    private RingBuffer<LongEvent> ringBuffer = createMultiProducer(LongEvent.FACTORY, BUFFER_SIZE);

    @Test
    public void shouldPublishEvent()
    {
        ringBuffer.addGatingSequences(new NoOpEventProcessor(ringBuffer).getSequence());

        ringBuffer.publishEvent(this);
        ringBuffer.publishEvent(this);

        assertThat(Long.valueOf(ringBuffer.get(0).get()), is(Long.valueOf(0 + 29L)));
        assertThat(Long.valueOf(ringBuffer.get(1).get()), is(Long.valueOf(1 + 29L)));
    }

    @Test
    public void shouldTryPublishEvent() throws Exception
    {
        ringBuffer.addGatingSequences(new Sequence());

        for (int i = 0; i < BUFFER_SIZE; i++)
        {
            assertThat(ringBuffer.tryPublishEvent(this), is(true));
        }

        for (int i = 0; i < BUFFER_SIZE; i++)
        {
            assertThat(Long.valueOf(ringBuffer.get(i).get()), is(Long.valueOf(i + 29L)));
        }

        assertThat(ringBuffer.tryPublishEvent(this), is(false));
    }

    @Override
    public void translateTo(final LongEvent event, final long sequence)
    {
        event.set(sequence + 29);
    }
}