Search in sources :

Example 41 with ExcerptTailer

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

the class ToEndTest method missingCyclesToEndTest.

@Test
public void missingCyclesToEndTest() throws InterruptedException {
    String path = OS.TARGET + "/missingCyclesToEndTest-" + System.nanoTime();
    IOTools.shallowDeleteDirWithFiles(path);
    final SetTimeProvider timeProvider = new SetTimeProvider();
    long now = 1470757797000L;
    long timeIncMs = 1001;
    timeProvider.currentTimeMillis(now);
    final RollingChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).timeProvider(timeProvider).build();
    final ExcerptAppender appender = queue.acquireAppender();
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(1));
    // roll
    timeProvider.currentTimeMillis(now += timeIncMs);
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(2));
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(3));
    final ExcerptTailer tailer = queue.createTailer().toEnd();
    try (DocumentContext dc = tailer.readingDocument()) {
        if (dc.isPresent()) {
            fail("Should be at the end of the queue but dc.isPresent and we read: " + String.valueOf(dc.wire().read(() -> "msg").int32()));
        }
    }
    // append same cycle.
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(4));
    try (DocumentContext dc = tailer.readingDocument()) {
        assertTrue("Should be able to read entry in this cycle. Got NoDocumentContext.", dc.isPresent());
        int i = dc.wire().read(() -> "msg").int32();
        assertEquals("Should've read 4, instead we read: " + i, 4, i);
    }
    // read from the beginning
    tailer.toStart();
    for (int j = 1; j <= 4; j++) {
        try (DocumentContext dc = tailer.readingDocument()) {
            assertTrue(dc.isPresent());
            int i = dc.wire().read(() -> "msg").int32();
            assertEquals(j, i);
        }
    }
    try (DocumentContext dc = tailer.readingDocument()) {
        if (dc.isPresent()) {
            fail("Should be at the end of the queue but dc.isPresent and we read: " + String.valueOf(dc.wire().read(() -> "msg").int32()));
        }
    }
    // write another
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(5));
    // roll 5 cycles
    timeProvider.currentTimeMillis(now += timeIncMs * 5);
    try (DocumentContext dc = tailer.readingDocument()) {
        assertTrue(dc.isPresent());
        assertEquals(5, dc.wire().read(() -> "msg").int32());
    }
    try (DocumentContext dc = tailer.readingDocument()) {
        assertFalse(dc.isPresent());
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 42 with ExcerptTailer

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

the class ToEndTest method toEndBeforeWriteTest.

@Test
public void toEndBeforeWriteTest() {
    File baseDir = DirectoryUtils.tempDir("toEndBeforeWriteTest");
    IOTools.shallowDeleteDirWithFiles(baseDir);
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(baseDir).testBlockSize().build()) {
        checkOneFile(baseDir);
        // if this appender isn't created, the tailer toEnd doesn't cause a roll.
        ExcerptAppender appender = queue.acquireAppender();
        checkOneFile(baseDir);
        ExcerptTailer tailer = queue.createTailer();
        checkOneFile(baseDir);
        ExcerptTailer tailer2 = queue.createTailer();
        checkOneFile(baseDir);
        tailer.toEnd();
        checkOneFile(baseDir);
        tailer2.toEnd();
        checkOneFile(baseDir);
    }
    System.gc();
    /*for (int i = 0; i < 10; i++) {
            final int j = i;
            appender.writeDocument(wire -> wire.write(() -> "msg").int32(j));
        }*/
    IOTools.shallowDeleteDirWithFiles(baseDir);
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 43 with ExcerptTailer

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

the class Queue36Test method testTail.

@Test
public void testTail() throws IOException {
    File basePath = getTmpDir();
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(basePath).testBlockSize().build()) {
        checkNoFiles(basePath);
        ExcerptTailer tailer = queue.createTailer();
        checkNoFiles(basePath);
        tailer.toStart();
        checkNoFiles(basePath);
        assertFalse(tailer.readDocument(d -> {
        }));
        checkNoFiles(basePath);
    }
}
Also used : SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) ChronicleQueueTestBase(net.openhft.chronicle.queue.ChronicleQueueTestBase) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) File(java.io.File) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 44 with ExcerptTailer

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

the class SingleChronicleQueueTest method testReadAtIndexSingle.

@Test
public void testReadAtIndexSingle() {
    final File file = createTempFile("testReadAtIndexSingle");
    try {
        final DirectChronicleQueue chronicle = createQueue(file);
        final ExcerptAppender appender = chronicle.acquireAppender();
        // create 100 documents
        for (int i = 0; i < 100; i++) {
            final int j = i;
            appender.writeDocument(wire -> wire.write(() -> "key").text("value=" + j));
        }
        final ExcerptTailer tailer = chronicle.createTailer();
        tailer.index(5);
        StringBuilder sb = new StringBuilder();
        tailer.readDocument(wire -> wire.read(() -> "key").text(sb));
        Assert.assertEquals("value=5", sb.toString());
    } finally {
        file.delete();
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 45 with ExcerptTailer

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

the class SingleChronicleQueueTest method testSingleWire.

// *************************************************************************
// 
// *************************************************************************
@Test
public void testSingleWire() {
    final File file = createTempFile("testSingleWire");
    try {
        final ChronicleQueue chronicle = createQueue(file);
        final ExcerptAppender appender = chronicle.acquireAppender();
        appender.writeDocument(wire -> wire.write(() -> "FirstName").text("Steve"));
        appender.writeDocument(wire -> wire.write(() -> "Surname").text("Jobs"));
        StringBuilder first = new StringBuilder();
        StringBuilder surname = new StringBuilder();
        final ExcerptTailer tailer = chronicle.createTailer();
        tailer.readDocument(wire -> wire.read(() -> "FirstName").text(first));
        tailer.readDocument(wire -> wire.read(() -> "Surname").text(surname));
        Assert.assertEquals("Steve Jobs", first + " " + surname);
    } finally {
        file.delete();
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Aggregations

ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)45 Test (org.junit.Test)39 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)34 File (java.io.File)24 DocumentContext (net.openhft.chronicle.wire.DocumentContext)22 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)15 MappedFile (net.openhft.chronicle.bytes.MappedFile)11 ArrayList (java.util.ArrayList)5 MethodReader (net.openhft.chronicle.bytes.MethodReader)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 IOException (java.io.IOException)4 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)4 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 Bytes (net.openhft.chronicle.bytes.Bytes)3 ChronicleQueueTestBase (net.openhft.chronicle.queue.ChronicleQueueTestBase)3 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)3 Wire (net.openhft.chronicle.wire.Wire)3 ByteBuffer (java.nio.ByteBuffer)2 Future (java.util.concurrent.Future)2