Search in sources :

Example 71 with DocumentContext

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

the class QueueInspectorTest method shouldDetermineWritingProcessIdWhenDocumentIsNotComplete.

@Test
public void shouldDetermineWritingProcessIdWhenDocumentIsNotComplete() throws IOException {
    try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpDir.newFolder()).testBlockSize().build()) {
        final QueueInspector inspector = new QueueInspector(queue);
        final ExcerptAppender appender = queue.acquireAppender();
        appender.writeDocument(37L, ValueOut::int64);
        try (final DocumentContext ctx = appender.writingDocument()) {
            ctx.wire().write("foo").int32(17L);
            final int writingThreadId = inspector.getWritingThreadId();
            assertThat(writingThreadId, is(Affinity.getThreadId()));
            assertThat(QueueInspector.isValidThreadId(writingThreadId), is(true));
        }
    }
}
Also used : ValueOut(net.openhft.chronicle.wire.ValueOut) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 72 with DocumentContext

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

the class RollCycleRetrieverTest method shouldRetrieveCorrectRollCycleFromExistingQueueFile.

@Test
public void shouldRetrieveCorrectRollCycleFromExistingQueueFile() throws Exception {
    final File queuePath = tempDir(RollCycleRetrieverTest.class.getSimpleName() + System.nanoTime());
    final SingleChronicleQueueBuilder builder = builder(queuePath, WIRE_TYPE).testBlockSize().rollCycle(ROLL_CYCLE);
    try (final SingleChronicleQueue queue = builder.build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        try (final DocumentContext context = appender.writingDocument()) {
            context.wire().write("foo").text("bar");
        }
    }
    assertThat(getRollCycle(queuePath.toPath(), WIRE_TYPE, builder.blockSize()).isPresent(), is(true));
    assertThat(getRollCycle(queuePath.toPath(), WIRE_TYPE, builder.blockSize()).get(), is(ROLL_CYCLE));
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 73 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext 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 74 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext 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 75 with DocumentContext

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

the class RollEOFTest method createQueueAndWriteData.

private void createQueueAndWriteData(MutableTimeProvider timeProvider) {
    final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).testBlockSize().rollCycle(RollCycles.TEST_DAILY).timeProvider(timeProvider).build();
    ExcerptAppender excerptAppender = queue.acquireAppender();
    try (DocumentContext dc = excerptAppender.writingDocument(false)) {
        dc.wire().write(() -> "test").int64(0);
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Aggregations

DocumentContext (net.openhft.chronicle.wire.DocumentContext)127 Test (org.junit.Test)76 File (java.io.File)46 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)32 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)27 Wire (net.openhft.chronicle.wire.Wire)23 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)22 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)13 NotNull (org.jetbrains.annotations.NotNull)12 Bytes (net.openhft.chronicle.bytes.Bytes)11 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)11 MappedFile (net.openhft.chronicle.bytes.MappedFile)10 Ignore (org.junit.Ignore)7 ArrayList (java.util.ArrayList)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ValueOut (net.openhft.chronicle.wire.ValueOut)6 Histogram (net.openhft.chronicle.core.util.Histogram)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 NamedThreadFactory (net.openhft.chronicle.threads.NamedThreadFactory)5 IOException (java.io.IOException)4