Search in sources :

Example 6 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class CreateAtIndexTest method testWrittenAndReadIndexesAreTheSameOfTheFirstExcerpt.

// TODO: 2 or more threads soak test
@Test
public void testWrittenAndReadIndexesAreTheSameOfTheFirstExcerpt() throws Exception {
    String tmp = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    long expected;
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
        ExcerptAppender appender = queue.acquireAppender();
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("some-data");
            expected = dc.index();
            Assert.assertTrue(expected > 0);
        }
        appender.lastIndexAppended();
        ExcerptTailer tailer = queue.createTailer();
        try (DocumentContext dc = tailer.readingDocument()) {
            String text = dc.wire().read().text();
            {
                long actualIndex = dc.index();
                Assert.assertTrue(actualIndex > 0);
                Assert.assertEquals(expected, actualIndex);
            }
            {
                long actualIndex = tailer.index();
                Assert.assertTrue(actualIndex > 0);
                Assert.assertEquals(expected, actualIndex);
            }
        }
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 7 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class FsFullReadTest method testFullReadFs.

@Ignore("broken test")
@Test
public void testFullReadFs() throws Exception {
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(basePath).blockSize(256 << 1000).rollCycle(RollCycles.DAILY).build();
    ExcerptTailer tailer = queue.createTailer();
    DocumentContext dc = tailer.readingDocument();
    boolean doExit = false;
    int entries = 0;
    while (!doExit) {
        try {
            if (dc.isPresent()) {
                entries++;
                Wire w = dc.wire();
                LocalDateTime dt = w.read().dateTime();
                assertNotNull(dt);
                byte[] b = w.read().bytes();
                assertEquals(1024, b.length);
            } else {
                System.out.println("Exiting");
                doExit = true;
            }
        } finally {
            dc.close();
        }
    }
    System.out.println(String.format("Read %d entries.", entries));
    CommonStore commonStore = queue.storeForCycle(queue.cycle(), 0, false);
    File file = commonStore.file();
    queue.close();
    int dumpEntries = 0;
    try {
        MappedBytes bytes = MappedBytes.mappedBytes(file, 4 << 20);
        bytes.readLimit(bytes.realCapacity());
        WireDumper dumper = WireDumper.of(bytes);
        Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer();
        while (bytes.readRemaining() >= 4) {
            StringBuilder sb = new StringBuilder();
            boolean last = dumper.dumpOne(sb, buffer);
            assertTrue(sb.length() > 0);
            if (last)
                break;
            dumpEntries++;
        }
    } catch (IOException ioe) {
        err.println("Failed to read " + file + " " + ioe);
    }
    assertEquals(dumpEntries, entries);
}
Also used : LocalDateTime(java.time.LocalDateTime) WireDumper(net.openhft.chronicle.wire.WireDumper) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) ByteBuffer(java.nio.ByteBuffer) CommonStore(net.openhft.chronicle.queue.impl.CommonStore) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class FsFullWriteTest method testAppenderFullFs.

@Ignore("flaky test")
@Test
public void testAppenderFullFs() throws Exception {
    ChronicleQueue queue = SingleChronicleQueueBuilder.binary(basePath).blockSize(256 << 1000).rollCycle(RollCycles.DAILY).build();
    ExcerptAppender appender = queue.acquireAppender();
    byte[] payload = new byte[1024];
    Random r = new Random();
    r.nextBytes(payload);
    final LocalDateTime now = LocalDateTime.now(Clock.systemUTC());
    for (int i = 0; i < 1024 * 200; i++) {
        DocumentContext dc = appender.writingDocument();
        try {
            Wire w = dc.wire();
            w.write().dateTime(now);
            w.write().bytes(payload);
        } finally {
            dc.close();
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Random(java.util.Random) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class LastIndexAppendedTest method doRead.

private long doRead(@NotNull ExcerptTailer tailer, int expected) {
    int[] i = { 0 };
    long t_index = 0;
    while (true) {
        try (DocumentContext dc = tailer.readingDocument()) {
            if (!dc.isPresent())
                break;
            t_index = tailer.index();
            dc.wire().read("log").marshallable(m -> {
                String msg = m.read("msg").text();
                assertNotNull(msg);
                System.out.println("msg:" + msg);
                i[0]++;
            });
        }
    }
    assertEquals(expected, i[0]);
    return t_index;
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Example 10 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class OvertakeTest method doReadBad.

private static long doReadBad(@NotNull ExcerptTailer tailer, int expected, boolean additionalClose) {
    int[] i = { 0 };
    long t_index = 0;
    while (true) {
        try (DocumentContext dc = tailer.readingDocument()) {
            if (!dc.isPresent())
                break;
            t_index = tailer.index();
            dc.wire().read("log").marshallable(m -> {
                String msg = m.read("msg").text();
                assertNotNull(msg);
                i[0]++;
            });
            if (additionalClose) {
                dc.close();
            }
        }
    }
    assertEquals(expected, i[0]);
    return t_index;
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Aggregations

DocumentContext (net.openhft.chronicle.wire.DocumentContext)54 Test (org.junit.Test)41 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)28 File (java.io.File)22 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)22 MappedFile (net.openhft.chronicle.bytes.MappedFile)12 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)9 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)8 Wire (net.openhft.chronicle.wire.Wire)8 NotNull (org.jetbrains.annotations.NotNull)6 Ignore (org.junit.Ignore)6 Future (java.util.concurrent.Future)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Bytes (net.openhft.chronicle.bytes.Bytes)4 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)4 ValueOut (net.openhft.chronicle.wire.ValueOut)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)3