|
| 1 | +package com.baeldung.disruptor; |
| 2 | + |
| 3 | +import java.util.concurrent.ThreadFactory; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import com.lmax.disruptor.BusySpinWaitStrategy; |
| 7 | +import com.lmax.disruptor.RingBuffer; |
| 8 | +import com.lmax.disruptor.WaitStrategy; |
| 9 | +import com.lmax.disruptor.dsl.Disruptor; |
| 10 | +import com.lmax.disruptor.dsl.ProducerType; |
| 11 | +import com.lmax.disruptor.util.DaemonThreadFactory; |
| 12 | + |
| 13 | +public class DisruptorTest { |
| 14 | + private Disruptor<ValueEvent> disruptor; |
| 15 | + private WaitStrategy waitStrategy; |
| 16 | + |
| 17 | + @Before |
| 18 | + public void setUp() throws Exception { |
| 19 | + waitStrategy = new BusySpinWaitStrategy(); |
| 20 | + } |
| 21 | + |
| 22 | + private void createDisruptor(final ProducerType producerType, final EventConsumer eventConsumer) { |
| 23 | + final ThreadFactory threadFactory = DaemonThreadFactory.INSTANCE; |
| 24 | + disruptor = new Disruptor<ValueEvent>(ValueEvent.EVENT_FACTORY, 16, threadFactory, producerType, waitStrategy); |
| 25 | + disruptor.handleEventsWith(eventConsumer.getEventHandler()); |
| 26 | + } |
| 27 | + |
| 28 | + private void startProducing(final RingBuffer<ValueEvent> ringBuffer, final int count, final EventProducer eventProducer) { |
| 29 | + eventProducer.startProducing(ringBuffer, count); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void whenMultipleProducerSingleConsumer_thenOutputInFifoOrder() { |
| 34 | + final EventConsumer eventConsumer = new SingleEventPrintConsumer(); |
| 35 | + final EventProducer eventProducer = new DelayedMultiEventProducer(); |
| 36 | + createDisruptor(ProducerType.MULTI, eventConsumer); |
| 37 | + final RingBuffer<ValueEvent> ringBuffer = disruptor.start(); |
| 38 | + |
| 39 | + startProducing(ringBuffer, 32, eventProducer); |
| 40 | + |
| 41 | + disruptor.halt(); |
| 42 | + disruptor.shutdown(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void whenSingleProducerSingleConsumer_thenOutputInFifoOrder() { |
| 47 | + final EventConsumer eventConsumer = new SingleEventConsumer(); |
| 48 | + final EventProducer eventProducer = new SingleEventProducer(); |
| 49 | + createDisruptor(ProducerType.SINGLE, eventConsumer); |
| 50 | + final RingBuffer<ValueEvent> ringBuffer = disruptor.start(); |
| 51 | + |
| 52 | + startProducing(ringBuffer, 32, eventProducer); |
| 53 | + |
| 54 | + disruptor.halt(); |
| 55 | + disruptor.shutdown(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void whenSingleProducerMultipleConsumer_thenOutputInFifoOrder() { |
| 60 | + final EventConsumer eventConsumer = new MultiEventConsumer(); |
| 61 | + final EventProducer eventProducer = new SingleEventProducer(); |
| 62 | + createDisruptor(ProducerType.SINGLE, eventConsumer); |
| 63 | + final RingBuffer<ValueEvent> ringBuffer = disruptor.start(); |
| 64 | + |
| 65 | + startProducing(ringBuffer, 32, eventProducer); |
| 66 | + |
| 67 | + disruptor.halt(); |
| 68 | + disruptor.shutdown(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void whenMultipleProducerMultipleConsumer_thenOutputInFifoOrder() { |
| 73 | + final EventConsumer eventConsumer = new MultiEventPrintConsumer(); |
| 74 | + final EventProducer eventProducer = new DelayedMultiEventProducer(); |
| 75 | + createDisruptor(ProducerType.MULTI, eventConsumer); |
| 76 | + final RingBuffer<ValueEvent> ringBuffer = disruptor.start(); |
| 77 | + |
| 78 | + startProducing(ringBuffer, 32, eventProducer); |
| 79 | + |
| 80 | + disruptor.halt(); |
| 81 | + disruptor.shutdown(); |
| 82 | + } |
| 83 | +} |
0 commit comments