Search in sources :

Example 16 with SetTimeProvider

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

the class SingleChronicleQueueTest method testReadingWritingWhenCycleIsSkipped.

@Test
public void testReadingWritingWhenCycleIsSkipped() {
    SetTimeProvider timeProvider = new SetTimeProvider();
    final File dir = DirectoryUtils.tempDir(testName.getMethodName());
    final RollCycles rollCycle = RollCycles.TEST_SECONDLY;
    // write first message
    try (ChronicleQueue queue = binary(dir).rollCycle(rollCycle).timeProvider(timeProvider).build()) {
        queue.acquireAppender().writeText("first message");
    }
    timeProvider.advanceMillis(2100);
    // write second message
    try (ChronicleQueue queue = binary(dir).rollCycle(rollCycle).timeProvider(timeProvider).build()) {
        queue.acquireAppender().writeText("second message");
    }
    // read both messages
    try (ChronicleQueue queue = binary(dir).rollCycle(rollCycle).timeProvider(timeProvider).build()) {
        ExcerptTailer tailer = queue.createTailer();
        Assert.assertEquals("first message", tailer.readText());
        Assert.assertEquals("second message", tailer.readText());
    }
}
Also used : RollCycles(net.openhft.chronicle.queue.RollCycles) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider)

Example 17 with SetTimeProvider

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

the class SingleChronicleQueueTest method testTailerWhenCyclesWhereSkippedOnWrite.

@Test
public void testTailerWhenCyclesWhereSkippedOnWrite() {
    SetTimeProvider timeProvider = new SetTimeProvider();
    try (final RollingChronicleQueue queue = binary(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(timeProvider).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        final ExcerptTailer tailer = queue.createTailer();
        final List<String> stringsToPut = Arrays.asList("one", "two", "three");
        // writes two strings immediately and one string with 2 seconds delay
        {
            try (DocumentContext writingContext = appender.writingDocument()) {
                writingContext.wire().write().bytes(stringsToPut.get(0).getBytes());
            }
            try (DocumentContext writingContext = appender.writingDocument()) {
                writingContext.wire().write().bytes(stringsToPut.get(1).getBytes());
            }
            timeProvider.advanceMillis(2100);
            try (DocumentContext writingContext = appender.writingDocument()) {
                writingContext.wire().write().bytes(stringsToPut.get(2).getBytes());
            }
        }
        System.out.println(queue.dump());
        for (String expected : stringsToPut) {
            try (DocumentContext readingContext = tailer.readingDocument()) {
                if (!readingContext.isPresent())
                    Assert.fail();
                String text = readingContext.wire().read().text();
                System.out.println("read=" + text);
                Assert.assertEquals(expected, text);
            }
        }
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider)

Example 18 with SetTimeProvider

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

the class SingleChronicleQueueTest method testAppendAndReadWithRollingB.

@Test
public void testAppendAndReadWithRollingB() {
    SetTimeProvider stp = new SetTimeProvider();
    stp.currentTimeMillis(System.currentTimeMillis() - 3 * 86400_000L);
    try (final ChronicleQueue queue = builder(getTmpDir(), this.wireType).rollCycle(TEST_DAILY).timeProvider(stp).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        appender.writeDocument(w -> w.write(TestKey.test).int32(0));
        appender.writeDocument(w -> w.write(TestKey.test2).int32(1000));
        int cycle = appender.cycle();
        for (int i = 1; i <= 5; i++) {
            stp.currentTimeMillis(stp.currentTimeMillis() + 86400_000L);
            final int n = i;
            appender.writeDocument(w -> w.write(TestKey.test).int32(n));
            assertEquals(cycle + i, appender.cycle());
            appender.writeDocument(w -> w.write(TestKey.test2).int32(n + 1000));
            assertEquals(cycle + i, appender.cycle());
        }
        /* Note this means the file has rolled
            --- !!not-ready-meta-data! #binary
            ...
             */
        assertEquals(expectedAppendAndReadWithRolling(), queue.dump());
        assumeFalse(encryption);
        assumeFalse(wireType == WireType.DEFAULT_ZERO_BINARY);
        final ExcerptTailer tailer = queue.createTailer().toStart();
        for (int i = 0; i < 6; i++) {
            final int n = i;
            boolean condition = tailer.readDocument(r -> assertEquals(n, r.read(TestKey.test).int32()));
            assertTrue("i : " + i, condition);
            assertEquals(cycle + i, tailer.cycle());
            boolean condition2 = tailer.readDocument(r -> assertEquals(n + 1000, r.read(TestKey.test2).int32()));
            assertTrue("i2 : " + i, condition2);
            assertEquals(cycle + i, tailer.cycle());
        }
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider)

Example 19 with SetTimeProvider

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

the class SingleChronicleQueueTest method testCountExceptsBetweenCycles.

@Test
public void testCountExceptsBetweenCycles() {
    SetTimeProvider timeProvider = new SetTimeProvider();
    final SingleChronicleQueueBuilder builder = binary(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(timeProvider);
    final RollingChronicleQueue queue = builder.build();
    final ExcerptAppender appender = queue.createAppender();
    long[] indexs = new long[10];
    for (int i = 0; i < indexs.length; i++) {
        System.out.println(".");
        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++) {
            System.out.println("lower=" + lower + ",upper=" + upper);
            Assert.assertEquals(upper - lower, queue.countExcerpts(indexs[lower], indexs[upper]));
        }
    }
    // check the base line of the test below
    Assert.assertEquals(6, queue.countExcerpts(indexs[0], indexs[6]));
    // / check for the case when the last index has a sequence number of -1
    Assert.assertEquals(queue.rollCycle().toSequenceNumber(indexs[6]), 0);
    Assert.assertEquals(5, queue.countExcerpts(indexs[0], indexs[6] - 1));
    // / check for the case when the first index has a sequence number of -1
    Assert.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)

Example 20 with SetTimeProvider

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

the class ToEndTest method missingCyclesToEndTest.

@Test
public void missingCyclesToEndTest() throws InterruptedException {
    String path = OS.TARGET + "/missingCyclesToEndTest-" + System.nanoTime();
    IOTools.shallowDeleteDirWithFiles(path);
    final SetTimeProvider timeProvider = new SetTimeProvider();
    long now = 1470757797000L;
    long timeIncMs = 1001;
    timeProvider.currentTimeMillis(now);
    final RollingChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).timeProvider(timeProvider).build();
    final ExcerptAppender appender = queue.acquireAppender();
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(1));
    // roll
    timeProvider.currentTimeMillis(now += timeIncMs);
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(2));
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(3));
    final ExcerptTailer tailer = queue.createTailer().toEnd();
    try (DocumentContext dc = tailer.readingDocument()) {
        if (dc.isPresent()) {
            fail("Should be at the end of the queue but dc.isPresent and we read: " + String.valueOf(dc.wire().read(() -> "msg").int32()));
        }
    }
    // append same cycle.
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(4));
    try (DocumentContext dc = tailer.readingDocument()) {
        assertTrue("Should be able to read entry in this cycle. Got NoDocumentContext.", dc.isPresent());
        int i = dc.wire().read(() -> "msg").int32();
        assertEquals("Should've read 4, instead we read: " + i, 4, i);
    }
    // read from the beginning
    tailer.toStart();
    for (int j = 1; j <= 4; j++) {
        try (DocumentContext dc = tailer.readingDocument()) {
            assertTrue(dc.isPresent());
            int i = dc.wire().read(() -> "msg").int32();
            assertEquals(j, i);
        }
    }
    try (DocumentContext dc = tailer.readingDocument()) {
        if (dc.isPresent()) {
            fail("Should be at the end of the queue but dc.isPresent and we read: " + String.valueOf(dc.wire().read(() -> "msg").int32()));
        }
    }
    // write another
    appender.writeDocument(wire -> wire.write(() -> "msg").int32(5));
    // roll 5 cycles
    timeProvider.currentTimeMillis(now += timeIncMs * 5);
    try (DocumentContext dc = tailer.readingDocument()) {
        assertTrue(dc.isPresent());
        assertEquals(5, dc.wire().read(() -> "msg").int32());
    }
    try (DocumentContext dc = tailer.readingDocument()) {
        assertFalse(dc.isPresent());
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) 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