Search in sources :

Example 1 with Pretoucher

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

the class RollingChronicleQueueTest method testTailing.

private void testTailing(Function<Pretoucher, Integer> createGap) {
    final SetTimeProvider tp = new SetTimeProvider(0);
    final File tmpDir = getTmpDir();
    try (SingleChronicleQueue queue = builder(tmpDir, WireType.BINARY).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(tp).build()) {
        int cyclesAdded = 0;
        final Pretoucher pretoucher = new Pretoucher(queue);
        ExcerptAppender appender = queue.acquireAppender();
        // to file ...000000
        appender.writeText("0");
        assertEquals(1, tmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
        tp.advanceMillis(1000);
        // to file ...000001
        appender.writeText("1");
        assertEquals(2, tmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
        tp.advanceMillis(2000);
        cyclesAdded += createGap.apply(pretoucher);
        assertEquals(2 + cyclesAdded, tmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
        tp.advanceMillis(1000);
        // to file ...000004
        appender.writeText("2");
        assertEquals(3 + cyclesAdded, tmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
        tp.advanceMillis(2000);
        cyclesAdded += createGap.apply(pretoucher);
        assertEquals(3 + cyclesAdded, tmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
        // now tail them all back
        int count = 0;
        ExcerptTailer tailer = queue.createTailer();
        long[] indexes = new long[3];
        while (true) {
            String text = tailer.readText();
            if (text == null)
                break;
            indexes[count] = tailer.index() - 1;
            assertEquals(count++, Integer.parseInt(text));
        }
        assertEquals(indexes.length, count);
        // now make sure we can go direct to each index (like afterLastWritten)
        tailer.toStart();
        for (int i = 0; i < indexes.length; i++) {
            assertTrue(tailer.moveToIndex(indexes[i]));
            String text = tailer.readText();
            assertEquals(i, Integer.parseInt(text));
        }
    }
}
Also used : Pretoucher(net.openhft.chronicle.queue.impl.single.Pretoucher) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) File(java.io.File)

Aggregations

File (java.io.File)1 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)1 Pretoucher (net.openhft.chronicle.queue.impl.single.Pretoucher)1 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)1