Search in sources :

Example 51 with ExcerptAppender

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

the class RollCycleTest method testWriteToCorruptedFile.

@Test
public void testWriteToCorruptedFile() {
    File dir = DirectoryUtils.tempDir("testWriteToCorruptedFile");
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
        ExcerptAppender appender = queue.acquireAppender();
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("hello world");
        }
        Bytes bytes;
        long pos;
        try (DocumentContext dc = appender.writingDocument()) {
            bytes = dc.wire().bytes();
            pos = bytes.writePosition() - 4;
        }
        // write as not complete.
        bytes.writeInt(pos, Wires.NOT_COMPLETE_UNKNOWN_LENGTH);
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("hello world 2");
        }
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write().text("hello world 3");
        }
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Test(org.junit.Test)

Example 52 with ExcerptAppender

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

the class SingleChronicleQueueCloseTest method testTailAfterClose.

@Test
public void testTailAfterClose() {
    final ChronicleQueue queue = SingleChronicleQueueBuilder.builder(getTmpDir(), WireType.BINARY).build();
    final ExcerptAppender appender = queue.acquireAppender();
    appender.writeDocument(w -> w.write(TestKey.test).int32(1));
    queue.close();
    try {
        appender.writeDocument(w -> w.write(TestKey.test).int32(2));
        Assert.fail();
    } catch (IllegalStateException e) {
    // ok
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Test(org.junit.Test)

Example 53 with ExcerptAppender

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

the class SingleChronicleQueueStoreTest method shouldNotPerformIndexingOnAppendWhenLazyIndexingIsEnabled.

@Test
public void shouldNotPerformIndexingOnAppendWhenLazyIndexingIsEnabled() throws Exception {
    runTest(queue -> {
        final ExcerptAppender appender = queue.acquireAppender();
        appender.lazyIndexing(true);
        final long[] indices = writeMessagesStoreIndices(appender, queue.createTailer());
        assertExcerptsAreIndexed(queue, indices, i -> false, ScanResult.NOT_REACHED);
    });
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Test(org.junit.Test)

Example 54 with ExcerptAppender

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

the class TestWriteWhenCurrentCycleGotEOF method shouldBeAbleToWriteIfCurrentCycleGotEOF.

@Test
public void shouldBeAbleToWriteIfCurrentCycleGotEOF() throws TimeoutException {
    File tmpDir = getTmpDir();
    createQueueWithOnlyHeaderFile(tmpDir);
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().build();
    ExcerptAppender appender = queue.acquireAppender();
    try (DocumentContext dc = appender.writingDocument()) {
        dc.wire().write("test").text("Hello world");
    }
    try (DocumentContext dc = queue.acquireTailer().readingDocument()) {
        assertEquals("Hello world", dc.wire().read("test").text());
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 55 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender 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)

Aggregations

ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)63 Test (org.junit.Test)55 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)34 File (java.io.File)33 DocumentContext (net.openhft.chronicle.wire.DocumentContext)28 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)21 MappedFile (net.openhft.chronicle.bytes.MappedFile)15 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)6 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 Ignore (org.junit.Ignore)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ValueOut (net.openhft.chronicle.wire.ValueOut)4 Wire (net.openhft.chronicle.wire.Wire)4 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 ExecutorService (java.util.concurrent.ExecutorService)3 Bytes (net.openhft.chronicle.bytes.Bytes)3 ChronicleQueueTestBase (net.openhft.chronicle.queue.ChronicleQueueTestBase)3