Search in sources :

Example 6 with StubEvent

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

the class RingBufferTest method shouldClaimAndGet.

@Test
public void shouldClaimAndGet() throws Exception {
    assertEquals(SingleProducerSequencer.INITIAL_CURSOR_VALUE, ringBuffer.getCursor());
    StubEvent expectedEvent = new StubEvent(2701);
    ringBuffer.publishEvent(StubEvent.TRANSLATOR, expectedEvent.getValue(), expectedEvent.getTestString());
    long sequence = sequenceBarrier.waitFor(0);
    assertEquals(0, sequence);
    StubEvent event = ringBuffer.get(sequence);
    assertEquals(expectedEvent, event);
    assertEquals(0L, ringBuffer.getCursor());
}
Also used : StubEvent(com.lmax.disruptor.support.StubEvent) Test(org.junit.Test)

Example 7 with StubEvent

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

the class RingBufferTest method shouldClaimAndGetInSeparateThread.

@Test
public void shouldClaimAndGetInSeparateThread() throws Exception {
    Future<List<StubEvent>> messages = getMessages(0, 0);
    StubEvent expectedEvent = new StubEvent(2701);
    ringBuffer.publishEvent(StubEvent.TRANSLATOR, expectedEvent.getValue(), expectedEvent.getTestString());
    assertEquals(expectedEvent, messages.get().get(0));
}
Also used : StubEvent(com.lmax.disruptor.support.StubEvent) List(java.util.List) Test(org.junit.Test)

Example 8 with StubEvent

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

the class RingBufferTest method shouldPreventPublishersOvertakingEventProcessorWrapPoint.

@Test
public void shouldPreventPublishersOvertakingEventProcessorWrapPoint() throws InterruptedException {
    final int ringBufferSize = 16;
    final CountDownLatch latch = new CountDownLatch(ringBufferSize);
    final AtomicBoolean publisherComplete = new AtomicBoolean(false);
    final RingBuffer<StubEvent> buffer2 = createMultiProducer(StubEvent.EVENT_FACTORY, ringBufferSize);
    final TestEventProcessor processor = new TestEventProcessor(buffer2.newBarrier());
    buffer2.addGatingSequences(processor.getSequence());
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i <= ringBufferSize; i++) {
                long sequence = buffer2.next();
                StubEvent event = buffer2.get(sequence);
                event.setValue(i);
                buffer2.publish(sequence);
                latch.countDown();
            }
            publisherComplete.set(true);
        }
    });
    thread.start();
    latch.await();
    assertThat(Long.valueOf(buffer2.getCursor()), is(Long.valueOf(ringBufferSize - 1)));
    assertFalse(publisherComplete.get());
    processor.run();
    thread.join();
    assertTrue(publisherComplete.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StubEvent(com.lmax.disruptor.support.StubEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with StubEvent

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

the class DynamiclyAddHandler method main.

public static void main(String[] args) throws InterruptedException {
    ExecutorService executor = Executors.newCachedThreadPool(DaemonThreadFactory.INSTANCE);
    // Build a disruptor and start it.
    Disruptor<StubEvent> disruptor = new Disruptor<StubEvent>(StubEvent.EVENT_FACTORY, 1024, DaemonThreadFactory.INSTANCE);
    RingBuffer<StubEvent> ringBuffer = disruptor.start();
    // Construct 2 batch event processors.
    DynamicHandler handler1 = new DynamicHandler();
    BatchEventProcessor<StubEvent> processor1 = new BatchEventProcessor<StubEvent>(ringBuffer, ringBuffer.newBarrier(), handler1);
    DynamicHandler handler2 = new DynamicHandler();
    BatchEventProcessor<StubEvent> processor2 = new BatchEventProcessor<StubEvent>(ringBuffer, ringBuffer.newBarrier(processor1.getSequence()), handler2);
    // Dynamically add both sequences to the ring buffer
    ringBuffer.addGatingSequences(processor1.getSequence(), processor2.getSequence());
    // Start the new batch processors.
    executor.execute(processor1);
    executor.execute(processor2);
    // Remove a processor.
    // Stop the processor
    processor2.halt();
    // Wait for shutdown the complete
    handler2.awaitShutdown();
    // Remove the gating sequence from the ring buffer
    ringBuffer.removeGatingSequence(processor2.getSequence());
}
Also used : StubEvent(com.lmax.disruptor.support.StubEvent) ExecutorService(java.util.concurrent.ExecutorService) Disruptor(com.lmax.disruptor.dsl.Disruptor)

Aggregations

StubEvent (com.lmax.disruptor.support.StubEvent)9 Test (org.junit.Test)7 CountDownLatch (java.util.concurrent.CountDownLatch)3 Disruptor (com.lmax.disruptor.dsl.Disruptor)1 DummyEventProcessor (com.lmax.disruptor.support.DummyEventProcessor)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1