Search in sources :

Example 1 with StubEvent

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

the class BatchEventProcessorTest method shouldCallMethodsInLifecycleOrderForBatch.

@Test
public void shouldCallMethodsInLifecycleOrderForBatch() throws Exception {
    CountDownLatch eventLatch = new CountDownLatch(3);
    LatchEventHandler eventHandler = new LatchEventHandler(eventLatch);
    final BatchEventProcessor<StubEvent> batchEventProcessor = new BatchEventProcessor<StubEvent>(ringBuffer, sequenceBarrier, eventHandler);
    ringBuffer.addGatingSequences(batchEventProcessor.getSequence());
    ringBuffer.publish(ringBuffer.next());
    ringBuffer.publish(ringBuffer.next());
    ringBuffer.publish(ringBuffer.next());
    Thread thread = new Thread(batchEventProcessor);
    thread.start();
    assertTrue(eventLatch.await(2, TimeUnit.SECONDS));
    batchEventProcessor.halt();
    thread.join();
}
Also used : StubEvent(com.lmax.disruptor.support.StubEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with StubEvent

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

the class BatchEventProcessorTest method shouldCallExceptionHandlerOnUncaughtException.

@Test
public void shouldCallExceptionHandlerOnUncaughtException() throws Exception {
    CountDownLatch exceptionLatch = new CountDownLatch(1);
    LatchExceptionHandler latchExceptionHandler = new LatchExceptionHandler(exceptionLatch);
    final BatchEventProcessor<StubEvent> batchEventProcessor = new BatchEventProcessor<StubEvent>(ringBuffer, sequenceBarrier, new ExceptionEventHandler());
    ringBuffer.addGatingSequences(batchEventProcessor.getSequence());
    batchEventProcessor.setExceptionHandler(latchExceptionHandler);
    Thread thread = new Thread(batchEventProcessor);
    thread.start();
    ringBuffer.publish(ringBuffer.next());
    assertTrue(exceptionLatch.await(2, TimeUnit.SECONDS));
    batchEventProcessor.halt();
    thread.join();
}
Also used : StubEvent(com.lmax.disruptor.support.StubEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with StubEvent

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

the class EventTranslatorTest method shouldTranslateOtherDataIntoAnEvent.

@Test
public void shouldTranslateOtherDataIntoAnEvent() {
    StubEvent event = StubEvent.EVENT_FACTORY.newInstance();
    EventTranslator<StubEvent> eventTranslator = new ExampleEventTranslator(TEST_VALUE);
    eventTranslator.translateTo(event, 0);
    Assert.assertEquals(TEST_VALUE, event.getTestString());
}
Also used : StubEvent(com.lmax.disruptor.support.StubEvent) Test(org.junit.Test)

Example 4 with StubEvent

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

the class SequenceBarrierTest method fillRingBuffer.

private void fillRingBuffer(long expectedNumberMessages) throws InterruptedException {
    for (long i = 0; i < expectedNumberMessages; i++) {
        long sequence = ringBuffer.next();
        StubEvent event = ringBuffer.get(sequence);
        event.setValue((int) i);
        ringBuffer.publish(sequence);
    }
}
Also used : StubEvent(com.lmax.disruptor.support.StubEvent)

Example 5 with StubEvent

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

the class SequenceBarrierTest method shouldWaitForWorkCompleteWhereAllWorkersAreBlockedOnRingBuffer.

@Test
public void shouldWaitForWorkCompleteWhereAllWorkersAreBlockedOnRingBuffer() throws Exception {
    long expectedNumberMessages = 10;
    fillRingBuffer(expectedNumberMessages);
    final DummyEventProcessor[] workers = new DummyEventProcessor[3];
    for (int i = 0, size = workers.length; i < size; i++) {
        workers[i] = new DummyEventProcessor();
        workers[i].setSequence(expectedNumberMessages - 1);
    }
    final SequenceBarrier sequenceBarrier = ringBuffer.newBarrier(Util.getSequencesFor(workers));
    Runnable runnable = new Runnable() {

        public void run() {
            long sequence = ringBuffer.next();
            StubEvent event = ringBuffer.get(sequence);
            event.setValue((int) sequence);
            ringBuffer.publish(sequence);
            for (DummyEventProcessor stubWorker : workers) {
                stubWorker.setSequence(sequence);
            }
        }
    };
    new Thread(runnable).start();
    long expectedWorkSequence = expectedNumberMessages;
    long completedWorkSequence = sequenceBarrier.waitFor(expectedNumberMessages);
    assertTrue(completedWorkSequence >= expectedWorkSequence);
}
Also used : DummyEventProcessor(com.lmax.disruptor.support.DummyEventProcessor) StubEvent(com.lmax.disruptor.support.StubEvent) Test(org.junit.Test)

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