use of net.openhft.chronicle.core.time.SetTimeProvider in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueTest method checkReferenceCountingWhenRollingAndCheckFileDeletion.
@Test
public void checkReferenceCountingWhenRollingAndCheckFileDeletion() {
SetTimeProvider timeProvider = new SetTimeProvider();
MappedFile mappedFile1, mappedFile2, mappedFile3, mappedFile4;
{
try (RollingChronicleQueue queue = binary(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(timeProvider).build()) {
ExcerptAppender appender = queue.acquireAppender();
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("some text");
mappedFile1 = toMappedFile(dc);
}
timeProvider.advanceMillis(1100);
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("some more text");
mappedFile2 = toMappedFile(dc);
}
ExcerptTailer tailer = queue.createTailer();
try (DocumentContext documentContext = tailer.readingDocument()) {
mappedFile3 = toMappedFile(documentContext);
Assert.assertEquals("some text", documentContext.wire().read().text());
}
try (DocumentContext documentContext = tailer.readingDocument()) {
mappedFile4 = toMappedFile(documentContext);
Assert.assertEquals("some more text", documentContext.wire().read().text());
}
}
waitFor(mappedFile1::isClosed, "mappedFile1 is not closed");
waitFor(mappedFile2::isClosed, "mappedFile2 is not closed");
// this used to fail on windows
Assert.assertTrue(mappedFile1.file().delete());
Assert.assertTrue(mappedFile2.file().delete());
}
}
use of net.openhft.chronicle.core.time.SetTimeProvider in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueTest method testReadingWritingWhenNextCycleIsInSequence.
@Test
public void testReadingWritingWhenNextCycleIsInSequence() {
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(1100);
// 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 testLongLivingTailerAppenderReAcquiredEachSecond.
@Test
public void testLongLivingTailerAppenderReAcquiredEachSecond() {
SetTimeProvider timeProvider = new SetTimeProvider();
final File dir = DirectoryUtils.tempDir(testName.getMethodName());
final RollCycles rollCycle = RollCycles.TEST_SECONDLY;
try (ChronicleQueue queuet = binary(dir).rollCycle(rollCycle).timeProvider(timeProvider).build()) {
final ExcerptTailer tailer = queuet.createTailer();
// write first message
try (ChronicleQueue queue = binary(dir).rollCycle(rollCycle).timeProvider(timeProvider).build()) {
for (int i = 0; i < 5; i++) {
final ExcerptAppender appender = queue.acquireAppender();
timeProvider.advanceMillis(1100);
try (final DocumentContext dc = appender.writingDocument()) {
dc.wire().write("some").int32(i);
}
try (final DocumentContext dc = tailer.readingDocument()) {
Assert.assertEquals(i, dc.wire().read("some").int32());
}
}
}
}
}
use of net.openhft.chronicle.core.time.SetTimeProvider in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueTest method testLastWritten.
@Test
public void testLastWritten() throws InterruptedException {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
try {
final SetTimeProvider tp = new SetTimeProvider();
try (SingleChronicleQueue outQueue = builder(getTmpDir(), wireType).rollCycle(RollCycles.TEST_SECONDLY).sourceId(1).timeProvider(tp).build()) {
File inQueueTmpDir = getTmpDir();
try (SingleChronicleQueue inQueue = builder(inQueueTmpDir, wireType).rollCycle(RollCycles.TEST_SECONDLY).sourceId(2).timeProvider(tp).build()) {
// write some initial data to the inqueue
final Msg msg = inQueue.acquireAppender().methodWriterBuilder(Msg.class).recordHistory(true).build();
msg.msg("somedata-0");
assertEquals(1, inQueueTmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
tp.advanceMillis(1000);
// write data into the inQueue
msg.msg("somedata-1");
assertEquals(2, inQueueTmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
// read a message on the in queue and write it to the out queue
{
Msg out = outQueue.acquireAppender().methodWriterBuilder(Msg.class).recordHistory(true).build();
MethodReader methodReader = inQueue.createTailer().methodReader((Msg) out::msg);
// reads the somedata-0
methodReader.readOne();
// reads the somedata-1
methodReader.readOne();
Assert.assertFalse(methodReader.readOne());
tp.advanceMillis(1000);
// this writes the EOF marker but doesn't create a new file
Assert.assertFalse(methodReader.readOne());
}
assertEquals("trying to read should not create a file", 2, inQueueTmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
// write data into the inQueue
msg.msg("somedata-2");
assertEquals(3, inQueueTmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
// advance 2 cycles - we will end up with a missing file
tp.advanceMillis(2000);
msg.msg("somedata-3");
msg.msg("somedata-4");
assertEquals("Should be a missing cycle file", 4, inQueueTmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
AtomicReference<String> actualValue = new AtomicReference<>();
// check that we are able to pick up from where we left off, in other words the next read should be somedata-2
{
ExcerptTailer excerptTailer = inQueue.createTailer().afterLastWritten(outQueue);
MethodReader methodReader = excerptTailer.methodReader((Msg) actualValue::set);
methodReader.readOne();
Assert.assertEquals("somedata-2", actualValue.get());
methodReader.readOne();
Assert.assertEquals("somedata-3", actualValue.get());
methodReader.readOne();
Assert.assertEquals("somedata-4", actualValue.get());
Assert.assertFalse(methodReader.readOne());
}
}
}
} finally {
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.SECONDS);
executorService.shutdownNow();
}
}
use of net.openhft.chronicle.core.time.SetTimeProvider in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueTest method testTailerSnappingRollWithNewAppender.
@Test
public void testTailerSnappingRollWithNewAppender() throws Exception {
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()) {
ExcerptAppender excerptAppender = queue.acquireAppender();
excerptAppender.writeText("someText");
ExecutorService executorService = Executors.newFixedThreadPool(2);
Future f1 = executorService.submit(() -> {
try (ChronicleQueue queue2 = binary(dir).rollCycle(rollCycle).timeProvider(timeProvider).build()) {
queue2.acquireAppender().writeText("someText more");
}
timeProvider.advanceMillis(1100);
try (ChronicleQueue queue2 = binary(dir).rollCycle(rollCycle).build()) {
queue2.acquireAppender().writeText("someText more");
}
});
Future f2 = executorService.submit(() -> {
// write second message
try (ChronicleQueue queue2 = binary(dir).rollCycle(rollCycle).timeProvider(timeProvider).build()) {
for (int i = 0; i < 5; i++) {
queue2.acquireAppender().writeText("someText more");
timeProvider.advanceMillis(400);
}
}
});
f1.get();
f2.get();
executorService.shutdownNow();
}
}
Aggregations