Search in sources :

Example 61 with SingleChronicleQueue

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

the class InternalAppenderWriteBytesTest method testJumpingAMessageThrowsAIllegalStateException.

@Test(expected = IllegalStateException.class)
public void testJumpingAMessageThrowsAIllegalStateException() {
    try (SingleChronicleQueue q = binary(tempDir("q")).rollCycle(MINUTELY).timeProvider(() -> 0).build();
        ExcerptAppender appender = q.acquireAppender()) {
        appender.writeText("hello");
        appender.writeText("hello2");
        try (final DocumentContext dc = appender.writingDocument()) {
            dc.wire().bytes().writeLong(1);
        }
        final long l = appender.lastIndexAppended();
        final RollCycle rollCycle = q.rollCycle();
        final int currentCycle = rollCycle.toCycle(l);
        // try to write to next roll cycle and write at seqnum 1 (but miss the 0th seqnum of that roll cycle)
        final long index = rollCycle.toIndex(currentCycle + 1, 1);
        ((InternalAppender) appender).writeBytes(index, Bytes.from("text"));
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender) Test(org.junit.Test)

Example 62 with SingleChronicleQueue

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

the class NoDataIsSkippedWithInterruptTest method test.

@Test
public void test() {
    final SetTimeProvider timeProvider = new SetTimeProvider();
    try (SingleChronicleQueue q = SingleChronicleQueueBuilder.single(DirectoryUtils.tempDir(".")).rollCycle(RollCycles.MINUTELY).timeProvider(timeProvider).testBlockSize().build();
        final ExcerptAppender excerptAppender = q.acquireAppender();
        final ExcerptTailer tailer = q.createTailer()) {
        Thread.currentThread().interrupt();
        excerptAppender.writeText(EXPECTED);
        // TODO: restore the below
        Assert.assertTrue(Thread.currentThread().isInterrupted());
        timeProvider.advanceMillis(60_000);
        excerptAppender.writeText(EXPECTED);
        Assert.assertEquals(EXPECTED, tailer.readText());
        Assert.assertEquals(EXPECTED, tailer.readText());
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) Test(org.junit.Test)

Example 63 with SingleChronicleQueue

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

the class PeekDocumentTest method soakTestPeekDocument.

@Ignore("PeekDocument gets stuck and reports no messages when there are messages - https://github.com/OpenHFT/Chronicle-Queue/issues/797")
@Test
public void soakTestPeekDocument() throws ExecutionException, InterruptedException {
    File tempDir = getTmpDir();
    try (SingleChronicleQueue q = SingleChronicleQueueBuilder.single(tempDir).rollCycle(RollCycles.MINUTELY).build()) {
        final long maxMessagesPerCycle = RollCycles.MINUTELY.maxMessagesPerCycle();
        System.out.println("maxMessagesPerCycle = " + DecimalFormat.getInstance().format(maxMessagesPerCycle));
        final ScheduledExecutorService es2 = newSingleThreadScheduledExecutor();
        es2.submit(() -> {
            try (final ExcerptAppender appender = q.acquireAppender()) {
                for (int i = 0; i < maxMessagesPerCycle; i++) {
                    try (final DocumentContext documentContext = appender.writingDocument()) {
                        documentContext.wire().write("value").int64(i);
                    }
                }
            }
        });
        final ScheduledExecutorService es = newSingleThreadScheduledExecutor();
        es.submit(() -> {
            try (final ExcerptTailer excerptTailer = q.createTailer()) {
                long count = 0;
                Thread.yield();
                OUTER: for (; ; ) {
                    while (excerptTailer.peekDocument()) {
                        try (DocumentContext dc = excerptTailer.readingDocument()) {
                            if (!dc.isPresent())
                                continue OUTER;
                            assertEquals(count, dc.wire().read("value").int64());
                            count++;
                            if (count % 1_000_000 == 0)
                                System.out.println("count = " + DecimalFormat.getInstance().format(count));
                            if (count == maxMessagesPerCycle)
                                return null;
                        }
                    }
                }
            }
        }).get();
        es.shutdownNow();
        es2.shutdownNow();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 64 with SingleChronicleQueue

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

the class PeekDocumentTest method testReadWrite10.

@Test
public void testReadWrite10() {
    File tempDir = getTmpDir();
    try {
        try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
            ExcerptAppender appender = queue.acquireAppender();
            for (int i = 0; i < 1024; i++) {
                try (DocumentContext documentContext = appender.writingDocument()) {
                    documentContext.wire().write("value").text("hello" + i);
                }
            }
            ExcerptTailer tailer = queue.createTailer();
            for (int i = 0; i < 1024; i++) {
                assertTrue(tailer.peekDocument());
                try (DocumentContext documentContext = tailer.readingDocument()) {
                    assertTrue(documentContext.isPresent());
                    assertTrue(tailer.peekDocument());
                    Wire wire = documentContext.wire();
                    long l = wire.bytes().readPosition();
                    try {
                        assertEquals("hello" + i, wire.read("value").text());
                    } finally {
                        // simulate if the message was read
                        if (l % 2 == 0)
                            wire.bytes().readPosition(l);
                    }
                }
            }
            assertFalse(tailer.peekDocument());
            try (DocumentContext documentContext = appender.writingDocument()) {
                documentContext.wire().write("value").text("hello" + 10);
            }
            assertTrue(tailer.peekDocument());
        }
    } finally {
        tempDir.deleteOnExit();
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) File(java.io.File) Test(org.junit.Test)

Example 65 with SingleChronicleQueue

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

the class PeekDocumentTest method testReadWrite10Backwards.

@Test
public void testReadWrite10Backwards() {
    File tempDir = getTmpDir();
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
        ExcerptAppender appender = queue.acquireAppender();
        try (DocumentContext documentContext = appender.writingDocument()) {
            documentContext.wire().write("value").text("hello");
        }
        ExcerptTailer tailer = queue.createTailer();
        assertTrue(tailer.peekDocument());
        try (DocumentContext documentContext = tailer.readingDocument()) {
            assertTrue(documentContext.isPresent());
            assertTrue(tailer.peekDocument());
            Wire wire = documentContext.wire();
            String result = wire.read("value").text();
            assertEquals("hello", result);
        // System.out.println(result);
        }
        assertFalse(tailer.peekDocument());
        tailer.direction(TailerDirection.BACKWARD);
        assertTrue(tailer.peekDocument());
        try (DocumentContext documentContext = tailer.readingDocument()) {
        }
        assertFalse(tailer.peekDocument());
    } finally {
        tempDir.deleteOnExit();
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) File(java.io.File) 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