Search in sources :

Example 6 with TestEvent

use of com.lmax.disruptor.support.TestEvent in project disruptor by LMAX-Exchange.

the class DisruptorTest method shouldMakeEntriesAvailableToFirstHandlersImmediately.

@Test
public void shouldMakeEntriesAvailableToFirstHandlersImmediately() throws Exception {
    CountDownLatch countDownLatch = new CountDownLatch(2);
    EventHandler<TestEvent> eventHandler = new EventHandlerStub<TestEvent>(countDownLatch);
    disruptor.handleEventsWith(createDelayedEventHandler(), eventHandler);
    ensureTwoEventsProcessedAccordingToDependencies(countDownLatch);
}
Also used : EventHandlerStub(com.lmax.disruptor.dsl.stubs.EventHandlerStub) TestEvent(com.lmax.disruptor.support.TestEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 7 with TestEvent

use of com.lmax.disruptor.support.TestEvent in project disruptor by LMAX-Exchange.

the class DisruptorTest method shouldMakeEntriesAvailableToFirstCustomProcessorsImmediately.

@Test
public void shouldMakeEntriesAvailableToFirstCustomProcessorsImmediately() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    final EventHandler<TestEvent> eventHandler = new EventHandlerStub<TestEvent>(countDownLatch);
    disruptor.handleEventsWith(new EventProcessorFactory<TestEvent>() {

        @Override
        public EventProcessor createEventProcessor(final RingBuffer<TestEvent> ringBuffer, final Sequence[] barrierSequences) {
            assertEquals("Should not have had any barrier sequences", 0, barrierSequences.length);
            return new BatchEventProcessor<TestEvent>(disruptor.getRingBuffer(), ringBuffer.newBarrier(barrierSequences), eventHandler);
        }
    });
    ensureTwoEventsProcessedAccordingToDependencies(countDownLatch);
}
Also used : EventHandlerStub(com.lmax.disruptor.dsl.stubs.EventHandlerStub) TestEvent(com.lmax.disruptor.support.TestEvent) EventProcessor(com.lmax.disruptor.EventProcessor) BatchEventProcessor(com.lmax.disruptor.BatchEventProcessor) Sequence(com.lmax.disruptor.Sequence) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 8 with TestEvent

use of com.lmax.disruptor.support.TestEvent in project disruptor by LMAX-Exchange.

the class DisruptorTest method shouldAddEventProcessorsAfterPublishing.

@Test
public void shouldAddEventProcessorsAfterPublishing() throws Exception {
    RingBuffer<TestEvent> rb = disruptor.getRingBuffer();
    BatchEventProcessor<TestEvent> b1 = new BatchEventProcessor<TestEvent>(rb, rb.newBarrier(), new SleepingEventHandler());
    BatchEventProcessor<TestEvent> b2 = new BatchEventProcessor<TestEvent>(rb, rb.newBarrier(b1.getSequence()), new SleepingEventHandler());
    BatchEventProcessor<TestEvent> b3 = new BatchEventProcessor<TestEvent>(rb, rb.newBarrier(b2.getSequence()), new SleepingEventHandler());
    assertThat(b1.getSequence().get(), is(-1L));
    assertThat(b2.getSequence().get(), is(-1L));
    assertThat(b3.getSequence().get(), is(-1L));
    rb.publish(rb.next());
    rb.publish(rb.next());
    rb.publish(rb.next());
    rb.publish(rb.next());
    rb.publish(rb.next());
    rb.publish(rb.next());
    disruptor.handleEventsWith(b1, b2, b3);
    assertThat(b1.getSequence().get(), is(5L));
    assertThat(b2.getSequence().get(), is(5L));
    assertThat(b3.getSequence().get(), is(5L));
}
Also used : SleepingEventHandler(com.lmax.disruptor.dsl.stubs.SleepingEventHandler) TestEvent(com.lmax.disruptor.support.TestEvent) BatchEventProcessor(com.lmax.disruptor.BatchEventProcessor) Test(org.junit.Test)

Example 9 with TestEvent

use of com.lmax.disruptor.support.TestEvent in project disruptor by LMAX-Exchange.

the class DisruptorTest method shouldProcessMessagesPublishedBeforeStartIsCalled.

@Test
public void shouldProcessMessagesPublishedBeforeStartIsCalled() throws Exception {
    final CountDownLatch eventCounter = new CountDownLatch(0);
    disruptor.handleEventsWith(new EventHandler<TestEvent>() {

        @Override
        public void onEvent(final TestEvent event, final long sequence, final boolean endOfBatch) throws Exception {
            eventCounter.countDown();
        }
    });
    disruptor.publishEvent(new EventTranslator<TestEvent>() {

        @Override
        public void translateTo(final TestEvent event, final long sequence) {
            lastPublishedEvent = event;
        }
    });
    disruptor.start();
    disruptor.publishEvent(new EventTranslator<TestEvent>() {

        @Override
        public void translateTo(final TestEvent event, final long sequence) {
            lastPublishedEvent = event;
        }
    });
    if (!eventCounter.await(5, TimeUnit.SECONDS)) {
        fail("Did not process event published before start was called. Missed events: " + eventCounter.getCount());
    }
}
Also used : TestEvent(com.lmax.disruptor.support.TestEvent) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TimeoutException(com.lmax.disruptor.TimeoutException) Test(org.junit.Test)

Example 10 with TestEvent

use of com.lmax.disruptor.support.TestEvent in project disruptor by LMAX-Exchange.

the class DisruptorTest method should.

@Test
public void should() throws Exception {
    RingBuffer<TestEvent> rb = disruptor.getRingBuffer();
    BatchEventProcessor<TestEvent> b1 = new BatchEventProcessor<TestEvent>(rb, rb.newBarrier(), new SleepingEventHandler());
    EventProcessorFactory<TestEvent> b2 = new EventProcessorFactory<TestEvent>() {

        @Override
        public EventProcessor createEventProcessor(RingBuffer<TestEvent> ringBuffer, Sequence[] barrierSequences) {
            return new BatchEventProcessor<TestEvent>(ringBuffer, ringBuffer.newBarrier(barrierSequences), new SleepingEventHandler());
        }
    };
    disruptor.handleEventsWith(b1).then(b2);
    disruptor.start();
}
Also used : SleepingEventHandler(com.lmax.disruptor.dsl.stubs.SleepingEventHandler) TestEvent(com.lmax.disruptor.support.TestEvent) BatchEventProcessor(com.lmax.disruptor.BatchEventProcessor) RingBuffer(com.lmax.disruptor.RingBuffer) Test(org.junit.Test)

Aggregations

TestEvent (com.lmax.disruptor.support.TestEvent)17 Test (org.junit.Test)16 CountDownLatch (java.util.concurrent.CountDownLatch)10 EventHandlerStub (com.lmax.disruptor.dsl.stubs.EventHandlerStub)9 DelayedEventHandler (com.lmax.disruptor.dsl.stubs.DelayedEventHandler)8 BatchEventProcessor (com.lmax.disruptor.BatchEventProcessor)7 Sequence (com.lmax.disruptor.Sequence)3 SleepingEventHandler (com.lmax.disruptor.dsl.stubs.SleepingEventHandler)3 EventProcessor (com.lmax.disruptor.EventProcessor)2 SequenceBarrier (com.lmax.disruptor.SequenceBarrier)2 RingBuffer (com.lmax.disruptor.RingBuffer)1 TimeoutException (com.lmax.disruptor.TimeoutException)1 StubPublisher (com.lmax.disruptor.dsl.stubs.StubPublisher)1 TestWorkHandler (com.lmax.disruptor.dsl.stubs.TestWorkHandler)1 DummyEventProcessor (com.lmax.disruptor.support.DummyEventProcessor)1 DummySequenceBarrier (com.lmax.disruptor.support.DummySequenceBarrier)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 Before (org.junit.Before)1