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