use of net.openhft.chronicle.core.time.TimeProvider in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueTest method testOverreadForwardFromFutureCycleThenReadBackwardTailer.
@Test
public void testOverreadForwardFromFutureCycleThenReadBackwardTailer() {
RollCycle cycle = TEST2_DAILY;
// when "forwardToFuture" flag is set, go one cycle to the future
AtomicBoolean forwardToFuture = new AtomicBoolean(false);
TimeProvider timeProvider = () -> forwardToFuture.get() ? System.currentTimeMillis() + TimeUnit.MILLISECONDS.toDays(1) : System.currentTimeMillis();
try (RollingChronicleQueue chronicle = builder(getTmpDir(), this.wireType).rollCycle(cycle).timeProvider(timeProvider).build()) {
ExcerptAppender appender = chronicle.acquireAppender();
appender.writeDocument(w -> w.writeEventName("hello").text("world"));
// go to the cycle next to the one the write was made on
forwardToFuture.set(true);
ExcerptTailer forwardTailer = chronicle.createTailer().direction(TailerDirection.FORWARD).toStart();
try (DocumentContext context = forwardTailer.readingDocument()) {
assertTrue(context.isPresent());
}
try (DocumentContext context = forwardTailer.readingDocument()) {
assertFalse(context.isPresent());
}
ExcerptTailer backwardTailer = chronicle.createTailer().direction(TailerDirection.BACKWARD).toEnd();
try (DocumentContext context = backwardTailer.readingDocument()) {
assertTrue(context.isPresent());
}
}
}
use of net.openhft.chronicle.core.time.TimeProvider in project Chronicle-Queue by OpenHFT.
the class AcquireReleaseTest method testAccquireAndRelease.
@Test
public void testAccquireAndRelease() throws Exception {
File dir = DirectoryUtils.tempDir("AcquireReleaseTest");
try {
AtomicInteger acount = new AtomicInteger();
AtomicInteger qcount = new AtomicInteger();
StoreFileListener sfl = new StoreFileListener() {
@Override
public void onAcquired(int cycle, File file) {
System.out.println("onAcquired(): " + file);
acount.incrementAndGet();
}
@Override
public void onReleased(int cycle, File file) {
System.out.println("onReleased(): " + file);
// TODO Auto-generated method stub
qcount.incrementAndGet();
}
};
AtomicLong time = new AtomicLong(1000l);
TimeProvider tp = () -> time.getAndAccumulate(1000, (x, y) -> x + y);
ChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).storeFileListener(sfl).timeProvider(tp).build();
for (int i = 0; i < 10; i++) {
queue.acquireAppender().writeDocument(w -> {
w.write("a").marshallable(m -> {
m.write("b").text("c");
});
});
}
Assert.assertEquals(10, acount.get());
Assert.assertEquals(9, qcount.get());
queue.close();
} finally {
try {
IOTools.deleteDirWithFiles(dir, 2);
} catch (IORuntimeException e) {
// ignored
}
}
}
use of net.openhft.chronicle.core.time.TimeProvider in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueTest method mappedSegmentsShouldBeUnmappedAsCycleRolls.
@Test
public void mappedSegmentsShouldBeUnmappedAsCycleRolls() throws Exception {
final Random random = new Random(0xDEADBEEF);
final File queueFolder = DirectoryUtils.tempDir("mappedSegmentsShouldBeUnmappedAsCycleRolls");
final AtomicLong clock = new AtomicLong(System.currentTimeMillis());
try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(queueFolder).timeProvider(clock::get).testBlockSize().rollCycle(RollCycles.HOURLY).build()) {
final ExcerptAppender appender = queue.acquireAppender();
for (int i = 0; i < 20_000; i++) {
final int batchSize = random.nextInt(10);
appender.writeDocument(batchSize, ValueOut::int64);
final byte payload = (byte) random.nextInt();
for (int j = 0; j < batchSize; j++) {
appender.writeDocument(payload, ValueOut::int8);
}
if (random.nextDouble() > 0.995) {
clock.addAndGet(TimeUnit.MINUTES.toMillis(37L));
// this give the reference processor a chance to run
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(15L));
}
}
if (OS.isLinux())
assertTrue("Too many mapped files: " + getMappedQueueFileCount(), getMappedQueueFileCount() < 40);
assertTrue(Files.list(queueFolder.toPath()).filter(p -> p.toString().endsWith(SUFFIX)).count() > 10L);
}
}
Aggregations