use of net.openhft.chronicle.queue.ExcerptTailer 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());
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class ToEndTest method toEndBeforeWriteTest.
@Test
public void toEndBeforeWriteTest() {
File baseDir = DirectoryUtils.tempDir("toEndBeforeWriteTest");
IOTools.shallowDeleteDirWithFiles(baseDir);
try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(baseDir).testBlockSize().build()) {
checkOneFile(baseDir);
// if this appender isn't created, the tailer toEnd doesn't cause a roll.
ExcerptAppender appender = queue.acquireAppender();
checkOneFile(baseDir);
ExcerptTailer tailer = queue.createTailer();
checkOneFile(baseDir);
ExcerptTailer tailer2 = queue.createTailer();
checkOneFile(baseDir);
tailer.toEnd();
checkOneFile(baseDir);
tailer2.toEnd();
checkOneFile(baseDir);
}
System.gc();
/*for (int i = 0; i < 10; i++) {
final int j = i;
appender.writeDocument(wire -> wire.write(() -> "msg").int32(j));
}*/
IOTools.shallowDeleteDirWithFiles(baseDir);
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class Queue36Test method testTail.
@Test
public void testTail() throws IOException {
File basePath = getTmpDir();
try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(basePath).testBlockSize().build()) {
checkNoFiles(basePath);
ExcerptTailer tailer = queue.createTailer();
checkNoFiles(basePath);
tailer.toStart();
checkNoFiles(basePath);
assertFalse(tailer.readDocument(d -> {
}));
checkNoFiles(basePath);
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueTest method testReadAtIndexSingle.
@Test
public void testReadAtIndexSingle() {
final File file = createTempFile("testReadAtIndexSingle");
try {
final DirectChronicleQueue chronicle = createQueue(file);
final ExcerptAppender appender = chronicle.acquireAppender();
// create 100 documents
for (int i = 0; i < 100; i++) {
final int j = i;
appender.writeDocument(wire -> wire.write(() -> "key").text("value=" + j));
}
final ExcerptTailer tailer = chronicle.createTailer();
tailer.index(5);
StringBuilder sb = new StringBuilder();
tailer.readDocument(wire -> wire.read(() -> "key").text(sb));
Assert.assertEquals("value=5", sb.toString());
} finally {
file.delete();
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueTest method testSingleWire.
// *************************************************************************
//
// *************************************************************************
@Test
public void testSingleWire() {
final File file = createTempFile("testSingleWire");
try {
final ChronicleQueue chronicle = createQueue(file);
final ExcerptAppender appender = chronicle.acquireAppender();
appender.writeDocument(wire -> wire.write(() -> "FirstName").text("Steve"));
appender.writeDocument(wire -> wire.write(() -> "Surname").text("Jobs"));
StringBuilder first = new StringBuilder();
StringBuilder surname = new StringBuilder();
final ExcerptTailer tailer = chronicle.createTailer();
tailer.readDocument(wire -> wire.read(() -> "FirstName").text(first));
tailer.readDocument(wire -> wire.read(() -> "Surname").text(surname));
Assert.assertEquals("Steve Jobs", first + " " + surname);
} finally {
file.delete();
}
}
Aggregations