Search in sources :

Example 1 with RollCycle

use of net.openhft.chronicle.queue.RollCycle in project Chronicle-Queue by OpenHFT.

the class ExcerptsSkippedWhenTailerDirectionNoneTest method shouldNotSkipMessageAtStartOfQueue.

@Test
public void shouldNotSkipMessageAtStartOfQueue() throws Exception {
    final File tmpDir = DirectoryUtils.tempDir(ExcerptsSkippedWhenTailerDirectionNoneTest.class.getSimpleName());
    try (final ChronicleQueue writeQueue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().rollCycle(TEST_DAILY).build()) {
        final ExcerptAppender excerptAppender = writeQueue.acquireAppender();
        try (final DocumentContext ctx = excerptAppender.writingDocument()) {
            ctx.wire().getValueOut().object("first");
        }
        try (final DocumentContext ctx = excerptAppender.writingDocument()) {
            ctx.wire().getValueOut().object("second");
        }
    }
    try (final SingleChronicleQueue readQueue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().rollCycle(TEST_DAILY).build()) {
        final ExcerptTailer tailer = readQueue.createTailer();
        final RollCycle rollCycle = readQueue.rollCycle();
        assertThat(rollCycle.toSequenceNumber(tailer.index()), is(0L));
        try (final DocumentContext ctx = tailer.direction(TailerDirection.NONE).readingDocument()) {
        // access the first document without incrementing sequence number
        }
        assertThat(rollCycle.toSequenceNumber(tailer.index()), is(0L));
        String value;
        try (DocumentContext dc = tailer.direction(TailerDirection.FORWARD).readingDocument()) {
            ValueIn valueIn = dc.wire().getValueIn();
            value = (String) valueIn.object();
        }
        assertThat(rollCycle.toSequenceNumber(tailer.index()), is(1L));
        assertThat(value, is("first"));
        try (DocumentContext dc = tailer.direction(TailerDirection.NONE).readingDocument()) {
            ValueIn valueIn = dc.wire().getValueIn();
            value = (String) valueIn.object();
        }
        assertThat(rollCycle.toSequenceNumber(tailer.index()), is(1L));
        assertThat(value, is("second"));
        try (DocumentContext dc = tailer.direction(TailerDirection.NONE).readingDocument()) {
            ValueIn valueIn = dc.wire().getValueIn();
            value = (String) valueIn.object();
        }
        assertThat(rollCycle.toSequenceNumber(tailer.index()), is(1L));
        assertThat(value, is("second"));
    }
}
Also used : ValueIn(net.openhft.chronicle.wire.ValueIn) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) RollCycle(net.openhft.chronicle.queue.RollCycle) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Aggregations

File (java.io.File)1 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)1 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)1 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)1 RollCycle (net.openhft.chronicle.queue.RollCycle)1 DocumentContext (net.openhft.chronicle.wire.DocumentContext)1 ValueIn (net.openhft.chronicle.wire.ValueIn)1 Test (org.junit.Test)1