Search in sources :

Example 66 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class PeekDocumentTest method test2.

@Test
public void test2() {
    File tempDir = getTmpDir();
    try {
        try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
            ExcerptAppender appender = queue.acquireAppender();
            appender.writeText("hello1");
            appender.writeText("hello2");
            ExcerptTailer tailer = queue.createTailer();
            long address1a = Jvm.getValue(tailer, "address");
            assertTrue(tailer.moveToIndex(tailer.index() + 1));
            long address1b = Jvm.getValue(tailer, "address");
            assertNotEquals(address1a, address1b);
            assertFalse(tailer.moveToIndex(tailer.index() + 1));
            long address1c = Jvm.getValue(tailer, "address");
            assertEquals(address1c, NoBytesStore.NO_PAGE);
        }
    } finally {
        tempDir.deleteOnExit();
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) File(java.io.File) Test(org.junit.Test)

Example 67 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class PeekDocumentTest method testReadWrite.

@Test
public void testReadWrite() {
    File tempDir = getTmpDir();
    try {
        Bytes<byte[]> bytes = from(EXPECTED_MESSAGE);
        try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
            ExcerptAppender appender = queue.acquireAppender();
            appender.writeText("hello");
            ExcerptTailer tailer = queue.createTailer();
            assertTrue(tailer.peekDocument());
        }
    } finally {
        tempDir.deleteOnExit();
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) File(java.io.File) Test(org.junit.Test)

Example 68 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class PeekDocumentTest method testWhenNoDocument.

@Test
public void testWhenNoDocument() {
    File tempDir = getTmpDir();
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.single(tempDir).build()) {
        ExcerptTailer tailer = queue.createTailer();
        ExcerptAppender appender = queue.acquireAppender();
        // peekDocumentBeforeWrite   should be false.but returns true
        boolean peekDocumentBeforeWrite = tailer.peekDocument();
        assertFalse(peekDocumentBeforeWrite);
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().writeText("testString");
        }
        boolean peekDocumentAfterWrite = tailer.peekDocument();
        assertTrue(peekDocumentAfterWrite);
        String text = null;
        try (DocumentContext dc = tailer.readingDocument()) {
            text = dc.wire().readText();
        }
        assertEquals("testString", text);
        boolean peekDocumentAfterRead = tailer.peekDocument();
        assertFalse(peekDocumentAfterRead);
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 69 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class IgnoreMethodBasedOnFirstArgTest method testIgnoreMethodBasedOnFirstArg.

@Test
public void testIgnoreMethodBasedOnFirstArg() {
    try (SingleChronicleQueue build = SingleChronicleQueueBuilder.binary(DirectoryUtils.tempDir("q")).build()) {
        Printer printer = build.acquireAppender().methodWriter(Printer.class);
        printer.print(EXPECTED_ENVELOPE, MSG);
        MethodReader mr = build.createTailer().methodReaderBuilder().build(new Printer() {

            @Override
            public boolean ignoreMethodBasedOnFirstArg(final String methodName, final Object firstArg) {
                assertEquals(EXPECTED_ENVELOPE, firstArg);
                return false;
            }

            @Override
            public void print(String envelope, final String msg) {
                assertEquals(EXPECTED_ENVELOPE, envelope);
                assertEquals(MSG, msg);
            }
        });
        mr.readOne();
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) MethodReader(net.openhft.chronicle.bytes.MethodReader) Test(org.junit.Test)

Example 70 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class StridingAQueueTest method testStriding.

@Test
public void testStriding() {
    SetTimeProvider timeProvider = new SetTimeProvider();
    timeProvider.currentTimeMillis(1_567_498_753_000L);
    File tmpDir = getTmpDir();
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().timeProvider(timeProvider).rollCycle(RollCycles.TEST4_SECONDLY).build()) {
        SAQMessage writer = queue.acquireAppender().methodWriter(SAQMessage.class);
        for (int j = 1; j <= 4; j++) {
            for (int i = 0; i < 6 + j; i++) writer.hi(j, i);
            timeProvider.advanceMillis(j * 500);
        // System.out.println(timeProvider.currentTimeMillis());
        }
    }
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().timeProvider(timeProvider).rollCycle(RollCycles.TEST4_SECONDLY).build()) {
        assertEquals(getExpected(), queue.dump().replaceAll("(?m)^#.+$\\n", ""));
        StringWriter sw = new StringWriter();
        ExcerptTailer tailer = queue.createTailer().direction(TailerDirection.BACKWARD).toEnd().striding(true);
        MethodReader reader = tailer.methodReader(Mocker.logging(SAQMessage.class, "", sw));
        while (reader.readOne()) ;
        assertEquals("hi[4, 9]\n" + "hi[4, 8]\n" + "hi[4, 4]\n" + "hi[4, 0]\n" + "hi[3, 8]\n" + "hi[3, 4]\n" + "hi[3, 0]\n" + "hi[2, 7]\n" + "hi[2, 5]\n" + "hi[2, 1]\n" + "hi[1, 4]\n" + "hi[1, 0]\n", sw.toString().replace("\r", ""));
    }
}
Also used : StringWriter(java.io.StringWriter) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader) Test(org.junit.Test)

Aggregations

SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)70 Test (org.junit.Test)54 File (java.io.File)31 DocumentContext (net.openhft.chronicle.wire.DocumentContext)15 RandomAccessFile (java.io.RandomAccessFile)14 ChronicleReader (net.openhft.chronicle.queue.reader.ChronicleReader)13 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)12 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)10 Ignore (org.junit.Ignore)10 NotNull (org.jetbrains.annotations.NotNull)9 MethodReader (net.openhft.chronicle.bytes.MethodReader)8 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)8 InternalAppender (net.openhft.chronicle.queue.impl.single.InternalAppender)7 Bytes (net.openhft.chronicle.bytes.Bytes)5 Path (java.nio.file.Path)4 OS (net.openhft.chronicle.core.OS)4 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)4 Assert (org.junit.Assert)4 IOException (java.io.IOException)3 TimeUnit (java.util.concurrent.TimeUnit)3