Search in sources :

Example 11 with RollingChronicleQueue

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

the class StuckQueueTest method test.

@Test
public void test() throws IOException {
    // todo remove see https://github.com/OpenHFT/Chronicle-Queue/issues/837
    assumeFalse(Jvm.isMacArm());
    // java.nio.file.InvalidPathException: Illegal char <:> at index 2: /D:/BuildAgent/work/1e5875c1db7235db/target/test-classes/stuck.queue.test/20180508-1249.cq4
    assumeFalse(OS.isWindows());
    Path tmpDir = getTmpDir().toPath();
    expectException("Failback to readonly tablestore");
    expectException("reading control code as text");
    // expectException("Unable to copy TimedStoreRecovery safely will try anyway");
    // expectException("Unable to copy SCQStore safely will try anyway");
    // expectException("Unable to copy SCQSRoll safely");
    // expectException("Unable to copy SCQSIndexing safely");
    tmpDir.toFile().mkdirs();
    Path templatePath = Paths.get(StuckQueueTest.class.getResource("/stuck.queue.test/20180508-1249.cq4").getFile());
    Path to = tmpDir.resolve(templatePath.getFileName());
    Files.copy(templatePath, to, StandardCopyOption.REPLACE_EXISTING);
    try (RollingChronicleQueue q = ChronicleQueue.singleBuilder(tmpDir).rollCycle(RollCycles.MINUTELY).readOnly(true).build();
        ExcerptTailer tailer = q.createTailer()) {
        // System.out.println(q.dump());
        int cycle = q.rollCycle().toCycle(0x18406e100000000L);
        try (SingleChronicleQueueStore wireStore = q.storeForCycle(cycle, q.epoch(), false, null)) {
            String absolutePath = wireStore.file().getAbsolutePath();
            // System.out.println(absolutePath);
            Assert.assertTrue(absolutePath.endsWith("20180508-1249.cq4"));
        }
        try (DocumentContext dc = tailer.readingDocument()) {
        // Assert.assertTrue(!dc.isPresent());
        // System.out.println(Long.toHexString(dc.index()));
        }
        // Assert.assertTrue(tailer.moveToIndex(0x183efe300000000L));
        try (final SingleChronicleQueue q2 = ChronicleQueue.singleBuilder(tmpDir).rollCycle(RollCycles.MINUTELY).build()) {
            try (DocumentContext dc = q2.acquireAppender().writingDocument()) {
                dc.wire().write("hello").text("world");
            }
        }
        ExcerptTailer tailer2 = q.createTailer();
        try (DocumentContext dc = tailer2.readingDocument()) {
            Assert.assertTrue(dc.isPresent());
            String actual = dc.wire().read("hello").text();
            Assert.assertEquals("world", actual);
        // System.out.println(Long.toHexString(dc.index()));
        }
    }
}
Also used : Path(java.nio.file.Path) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 12 with RollingChronicleQueue

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

the class SingleChronicleQueueTest method testCountExceptsBetweenCycles.

@Test
public void testCountExceptsBetweenCycles() {
    SetTimeProvider timeProvider = new SetTimeProvider();
    try (final RollingChronicleQueue queue = binary(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(timeProvider).build();
        final ExcerptAppender appender = queue.acquireAppender()) {
        long[] indexs = new long[10];
        for (int i = 0; i < indexs.length; i++) {
            try (DocumentContext writingContext = appender.writingDocument()) {
                writingContext.wire().write().text("some-text-" + i);
                indexs[i] = writingContext.index();
            }
            // skipped
            if ((i + 1) % 5 == 0)
                timeProvider.advanceMillis(2000);
            else if ((i + 1) % 3 == 0)
                timeProvider.advanceMillis(1000);
        }
        for (int lower = 0; lower < indexs.length; lower++) {
            for (int upper = lower; upper < indexs.length; upper++) {
                assertEquals(upper - lower, queue.countExcerpts(indexs[lower], indexs[upper]));
            }
        }
        // check the base line of the test below
        assertEquals(6, queue.countExcerpts(indexs[0], indexs[6]));
        // / check for the case when the last index has a sequence number of -1
        assertEquals(0, queue.rollCycle().toSequenceNumber(indexs[6]));
        assertEquals(5, queue.countExcerpts(indexs[0], indexs[6] - 1));
        // / check for the case when the first index has a sequence number of -1
        assertEquals(7, queue.countExcerpts(indexs[0] - 1, indexs[6]));
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) Test(org.junit.Test)

Example 13 with RollingChronicleQueue

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

the class SingleChronicleQueueTest method testReadAtIndex.

@Test
public void testReadAtIndex() {
    try (final RollingChronicleQueue queue = builder(getTmpDir(), wireType).indexCount(8).indexSpacing(8).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        // create 100 documents
        for (int i = 0; i < 100; i++) {
            final int j = i;
            try (final DocumentContext context = appender.writingDocument()) {
                context.wire().write("key").text("value=" + j);
            }
        }
        long lastIndex = appender.lastIndexAppended();
        final int cycle = queue.rollCycle().toCycle(lastIndex);
        assertEquals(queue.firstCycle(), cycle);
        assertEquals(queue.lastCycle(), cycle);
        final ExcerptTailer tailer = queue.createTailer(named ? "named" : null);
        StringBuilder sb = new StringBuilder();
        for (int i : new int[] { 0, 8, 7, 9, 64, 65, 66 }) {
            final long index = queue.rollCycle().toIndex(cycle, i);
            assertTrue("i: " + i, tailer.moveToIndex(index));
            final DocumentContext context = tailer.readingDocument();
            assertEquals(index, context.index());
            context.wire().read("key").text(sb);
            assertEquals("value=" + i, sb.toString());
        }
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) Test(org.junit.Test)

Aggregations

RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)13 Test (org.junit.Test)12 File (java.io.File)8 DocumentContext (net.openhft.chronicle.wire.DocumentContext)4 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)2 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)2 NotNull (org.jetbrains.annotations.NotNull)2 Path (java.nio.file.Path)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MappedFile (net.openhft.chronicle.bytes.MappedFile)1 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)1 NamedThreadFactory (net.openhft.chronicle.threads.NamedThreadFactory)1 UnrecoverableTimeoutException (net.openhft.chronicle.wire.UnrecoverableTimeoutException)1 ValueIn (net.openhft.chronicle.wire.ValueIn)1