Search in sources :

Example 6 with SetTimeProvider

use of net.openhft.chronicle.core.time.SetTimeProvider in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueTest method doTestCheckIndex.

void doTestCheckIndex(@NotNull BiConsumer<ExcerptAppender, Integer> writeTo) {
    SetTimeProvider stp = new SetTimeProvider();
    stp.currentTimeMillis(System.currentTimeMillis() - 3 * 86400_000L);
    try (final ChronicleQueue queue = builder(getTmpDir(), wireType).timeProvider(stp).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        ExcerptTailer tailer = queue.createTailer();
        int cycle = appender.cycle();
        for (int i = 0; i <= 5; i++) {
            final int n = i;
            writeTo.accept(appender, n);
            assertEquals(cycle + i, appender.cycle());
            try (DocumentContext dc = tailer.readingDocument()) {
                long index = tailer.index();
                System.out.println(i + " index: " + Long.toHexString(index));
                assertEquals(appender.cycle(), tailer.cycle());
                assertEquals(cycle + i, DAILY.toCycle(index));
            }
            stp.currentTimeMillis(stp.currentTimeMillis() + 86400_000L);
        }
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider)

Example 7 with SetTimeProvider

use of net.openhft.chronicle.core.time.SetTimeProvider in project Chronicle-Queue by OpenHFT.

the class TestBinarySearch method testBinarySearch.

@Test
public void testBinarySearch() throws ParseException {
    final SetTimeProvider stp = new SetTimeProvider();
    long time = 0;
    stp.currentTimeMillis(time);
    final File tmpDir = getTmpDir();
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        for (int i = 0; i < MAX_NUMBER_OF_TESTED_MESSAGES; i++) {
            try (final DocumentContext dc = appender.writingDocument()) {
                final MyData myData = new MyData();
                myData.key = i;
                myData.value = "some value where the key=" + String.valueOf(i);
                dc.wire().getValueOut().typedMarshallable(myData);
                time += 300;
                stp.currentTimeMillis(time);
            }
        }
        for (int j = 0; j < MAX_NUMBER_OF_TESTED_MESSAGES; j++) {
            Wire key = toWire(j);
            final Comparator<Wire> comparator = (o1, o2) -> {
                final long readPositionO1 = o1.bytes().readPosition();
                final long readPositionO2 = o2.bytes().readPosition();
                try {
                    MyData myDataO1 = null;
                    MyData myDataO2 = null;
                    try (final DocumentContext dc = o1.readingDocument()) {
                        myDataO1 = dc.wire().getValueIn().typedMarshallable();
                        assert myDataO1.value != null;
                    }
                    try (final DocumentContext dc = o2.readingDocument()) {
                        myDataO2 = dc.wire().getValueIn().typedMarshallable();
                        assert myDataO2.value != null;
                    }
                    final int compare = Integer.compare(myDataO1.key, myDataO2.key);
                    return compare;
                } finally {
                    o1.bytes().readPosition(readPositionO1);
                    o2.bytes().readPosition(readPositionO2);
                }
            };
            long index = BinarySearch.search(queue, key, comparator);
            // assert index != -1 : "i=" + j;
            final ExcerptTailer tailer = queue.createTailer();
            tailer.moveToIndex(index);
            try (final DocumentContext documentContext = tailer.readingDocument()) {
                Assert.assertTrue(documentContext.toString().contains("some value where the key=" + j));
            }
            key.bytes().release();
        }
    } finally {
        System.gc();
        deleteDir(tmpDir);
    }
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) DocumentContext(net.openhft.chronicle.wire.DocumentContext) AbstractMarshallable(net.openhft.chronicle.wire.AbstractMarshallable) Test(org.junit.Test) Wire(net.openhft.chronicle.wire.Wire) WireType(net.openhft.chronicle.wire.WireType) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Bytes(net.openhft.chronicle.bytes.Bytes) ChronicleQueueTestBase(net.openhft.chronicle.queue.ChronicleQueueTestBase) RollCycles(net.openhft.chronicle.queue.RollCycles) After(org.junit.After) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) ParseException(java.text.ParseException) Comparator(java.util.Comparator) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 8 with SetTimeProvider

use of net.openhft.chronicle.core.time.SetTimeProvider in project Chronicle-Queue by OpenHFT.

the class ToEndTest method toEndAfterWriteTest.

@Test
public void toEndAfterWriteTest() {
    File file = DirectoryUtils.tempDir("toEndAfterWriteTest");
    IOTools.shallowDeleteDirWithFiles(file);
    final SetTimeProvider stp = new SetTimeProvider();
    stp.currentTimeMillis(1470757797000L);
    try (RollingChronicleQueue wqueue = SingleChronicleQueueBuilder.binary(file).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
        ExcerptAppender appender = wqueue.acquireAppender();
        for (int i = 0; i < 10; i++) {
            try (DocumentContext dc = appender.writingDocument()) {
                dc.wire().write().text("hi-" + i);
                lastCycle = wqueue.rollCycle().toCycle(dc.index());
            }
            stp.currentTimeMillis(stp.currentTimeMillis() + 1000);
        }
    }
    try (ChronicleQueue rqueue = SingleChronicleQueueBuilder.binary(file).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).timeProvider(stp).build()) {
        ExcerptTailer tailer = rqueue.createTailer();
        stp.currentTimeMillis(stp.currentTimeMillis() + 1000);
        // noinspection StatementWithEmptyBody
        while (tailer.readText() != null) ;
        assertNull(tailer.readText());
        stp.currentTimeMillis(stp.currentTimeMillis() + 1000);
        ExcerptTailer tailer1 = rqueue.createTailer();
        ExcerptTailer excerptTailer = tailer1.toEnd();
        assertNull(excerptTailer.readText());
    }
    System.gc();
    IOTools.shallowDeleteDirWithFiles(file);
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 9 with SetTimeProvider

use of net.openhft.chronicle.core.time.SetTimeProvider in project Chronicle-Queue by OpenHFT.

the class RollCycleTest method newRollCycleIgnored.

@Test
public void newRollCycleIgnored() throws Exception {
    File path = DirectoryUtils.tempDir("newRollCycleIgnored");
    SetTimeProvider timeProvider = new SetTimeProvider();
    ParallelQueueObserver observer = new ParallelQueueObserver(timeProvider, path.toPath());
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(RollCycles.DAILY).timeProvider(timeProvider).build()) {
        ExcerptAppender appender = queue.acquireAppender();
        Thread thread = new Thread(observer);
        thread.start();
        observer.await();
        // two days pass
        timeProvider.advanceMillis(TimeUnit.DAYS.toMillis(2));
        appender.writeText("0");
        // allow parallel tailer to finish iteration
        for (int i = 0; i < 5_000 && observer.documentsRead != 1; i++) {
            Thread.sleep(1);
        }
        thread.interrupt();
    }
    assertEquals(1, observer.documentsRead);
    observer.queue.close();
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) Test(org.junit.Test)

Example 10 with SetTimeProvider

use of net.openhft.chronicle.core.time.SetTimeProvider in project Chronicle-Queue by OpenHFT.

the class RollCycleTest method newRollCycleIgnored2.

@Test
public void newRollCycleIgnored2() throws Exception {
    File path = DirectoryUtils.tempDir("newRollCycleIgnored2");
    SetTimeProvider timeProvider = new SetTimeProvider();
    ParallelQueueObserver observer = new ParallelQueueObserver(timeProvider, path.toPath());
    int cyclesToWrite = 100;
    try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(RollCycles.DAILY).timeProvider(timeProvider).build()) {
        ExcerptAppender appender = queue.acquireAppender();
        appender.writeText("0");
        Thread thread = new Thread(observer);
        thread.start();
        observer.await();
        for (int i = 1; i <= cyclesToWrite; i++) {
            // two days pass
            timeProvider.advanceMillis(TimeUnit.DAYS.toMillis(2));
            appender.writeText(Integer.toString(i));
        }
        // allow parallel tailer to finish iteration
        for (int i = 0; i < 5_000 && observer.documentsRead != 1 + cyclesToWrite; i++) {
            Thread.sleep(1);
        }
        thread.interrupt();
    }
    assertEquals(1 + cyclesToWrite, observer.documentsRead);
    observer.queue.close();
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) Test(org.junit.Test)

Aggregations

SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)21 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)13 Test (org.junit.Test)10 File (java.io.File)7 MappedFile (net.openhft.chronicle.bytes.MappedFile)5 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)5 RollCycles (net.openhft.chronicle.queue.RollCycles)5 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)4 DocumentContext (net.openhft.chronicle.wire.DocumentContext)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)2 Field (java.lang.reflect.Field)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Future (java.util.concurrent.Future)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Bytes (net.openhft.chronicle.bytes.Bytes)1 MethodReader (net.openhft.chronicle.bytes.MethodReader)1 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1