Search in sources :

Example 41 with DocumentContext

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

the class QueueInspectorTest method shouldIndicateNoProcessIdWhenDocumentIsComplete.

@Test
public void shouldIndicateNoProcessIdWhenDocumentIsComplete() 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(not(OS.getProcessId())));
        assertThat(QueueInspector.isValidThreadId(writingThreadId), is(false));
    }
}
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 42 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 43 with DocumentContext

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

the class RollAtEndOfCycleTest method shouldAppendToExistingQueueFile.

@Test
public void shouldAppendToExistingQueueFile() throws Exception {
    try (final SingleChronicleQueue queue = createQueue()) {
        final ExcerptAppender appender = queue.acquireAppender();
        appender.writeDocument(1, (w, i) -> {
            w.int32(i);
        });
        final ExcerptTailer tailer = queue.createTailer();
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
        assertQueueFileCount(queue.path.toPath(), 1);
        assertFalse(tailer.readingDocument().isPresent());
        appender.writeDocument(2, (w, i) -> {
            w.int32(i);
        });
        assertQueueFileCount(queue.path.toPath(), 1);
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 44 with DocumentContext

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

the class RollAtEndOfCycleTest method shouldRollAndAppendToNewFile.

@Test
public void shouldRollAndAppendToNewFile() throws Exception {
    try (final SingleChronicleQueue queue = createQueue()) {
        final ExcerptAppender appender = queue.acquireAppender();
        appender.writeDocument(1, (w, i) -> {
            w.int32(i);
        });
        final ExcerptTailer tailer = queue.createTailer();
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
        assertQueueFileCount(queue.path.toPath(), 1);
        clock.addAndGet(TimeUnit.SECONDS.toMillis(2));
        assertFalse(tailer.readingDocument().isPresent());
        appender.writeDocument(2, (w, i) -> {
            w.int32(i);
        });
        assertQueueFileCount(queue.path.toPath(), 2);
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
        final ExcerptTailer newTailer = queue.createTailer();
        int totalCount = 0;
        while (true) {
            final DocumentContext context = newTailer.readingDocument();
            if (context.isPresent() && context.isData()) {
                assertTrue(context.wire().read().int32() != 0);
                totalCount++;
            } else if (!context.isPresent()) {
                break;
            }
        }
        assertThat(totalCount, is(2));
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 45 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)

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