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());
}
}
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);
}
}
}
}
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());
}
}
}
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]));
}
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());
}
}
Aggregations