use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class TailerTest method drainTailer.
private List<String> drainTailer(ExcerptTailer tailer) {
final List<String> result = new ArrayList<>();
for (; ; ) {
try (DocumentContext dc = tailer.readingDocument()) {
if (!dc.isPresent())
break;
final String text = dc.wire().readText();
System.out.format("Tailed \"%s\" at %d%n", text, dc.index());
result.add(text);
}
}
return result;
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class TailerTest method reproduce.
@Test
public void reproduce() {
IOTools.deleteDirWithFiles(QUEUE_PATH.toFile());
long firstOutputIndex = Long.MAX_VALUE;
long lastOutputIndex = Long.MIN_VALUE;
try (ChronicleQueue q = createQueue();
ExcerptAppender appender = q.acquireAppender();
ExcerptTailer tailer = q.createTailer()) {
for (int i = 0; i < 5; i++) {
final String text = "Hello World " + i;
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().writeText(text);
}
System.out.format("Appended \"%s\" at %d%n", text, appender.lastIndexAppended());
firstOutputIndex = Math.min(firstOutputIndex, appender.lastIndexAppended());
lastOutputIndex = Math.max(lastOutputIndex, appender.lastIndexAppended());
}
System.out.format("firstOutputIndex = %d%n", firstOutputIndex);
System.out.format("lastOutputIndex = %d%n", lastOutputIndex);
System.out.println("Reading initial");
drainTailer(tailer);
}
try (ChronicleQueue q = createQueue();
ExcerptTailer tailer = q.createTailer()) {
initRecovery(tailer, firstOutputIndex + OFFSET);
final List<String> messages = drainTailer(tailer);
assertEquals("Hello World " + OFFSET, messages.get(0));
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class PretoucherTest method cycleRollByPretoucher.
private void cycleRollByPretoucher(int earlyMillis) {
File dir = getTmpDir();
clock.set(100);
try (final SingleChronicleQueue queue = createQueue(dir, clock::get);
final SingleChronicleQueue queue2 = createQueue(dir, clock::get);
final Pretoucher pretoucher = new Pretoucher(queue2, chunkListener, capturedCycles::add, true, true)) {
range(0, 10).forEach(i -> {
try (final DocumentContext ctx = queue.acquireAppender().writingDocument()) {
assertEquals(i == 0 ? 0 : i + 1, capturedCycles.size());
ctx.wire().write().int32(i);
ctx.wire().write().bytes(new byte[1024]);
}
try {
pretoucher.execute();
} catch (InvalidEventHandlerException e) {
throw Jvm.rethrow(e);
}
assertEquals(i + 1, capturedCycles.size());
clock.addAndGet(950 - earlyMillis);
try {
pretoucher.execute();
} catch (InvalidEventHandlerException e) {
throw Jvm.rethrow(e);
}
clock.addAndGet(50 + earlyMillis);
BackgroundResourceReleaser.releasePendingResources();
assertEquals(i + 2, capturedCycles.size());
});
assertEquals(11, capturedCycles.size());
// TODO FIX
// assertFalse(chunkListener.chunkMap.isEmpty());
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RollAtEndOfCycleTest method shouldAppendToExistingQueueFile.
@Test
public void shouldAppendToExistingQueueFile() throws IOException {
try (final SingleChronicleQueue queue = createQueue()) {
final ExcerptAppender appender = queue.acquireAppender();
appender.writeDocument(1, (w, i) -> {
w.int32(i);
});
final ExcerptTailer tailer = queue.createTailer();
try (final DocumentContext context = tailer.readingDocument()) {
assertTrue(context.isPresent());
}
assertQueueFileCount(queue.path.toPath(), 1);
assertFalse(tailer.readingDocument().isPresent());
appender.writeDocument(2, (w, i) -> {
w.int32(i);
});
assertQueueFileCount(queue.path.toPath(), 1);
try (final DocumentContext context = tailer.readingDocument()) {
assertTrue(context.isPresent());
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RollAtEndOfCycleTest method shouldRollAndAppendToNewFile.
@Test
public void shouldRollAndAppendToNewFile() throws IOException {
assumeFalse(Jvm.isArm());
try (final SingleChronicleQueue queue = createQueue()) {
final ExcerptAppender appender = queue.acquireAppender();
appender.writeDocument(1, (w, i) -> {
w.int32(i);
});
final ExcerptTailer tailer = queue.createTailer();
try (final DocumentContext context = tailer.readingDocument()) {
assertTrue(context.isPresent());
}
assertQueueFileCount(queue.path.toPath(), 1);
clock.addAndGet(TimeUnit.SECONDS.toMillis(2));
assertFalse(tailer.readingDocument().isPresent());
appender.writeDocument(2, (w, i) -> {
w.int32(i);
});
assertQueueFileCount(queue.path.toPath(), 2);
try (final DocumentContext context = tailer.readingDocument()) {
assertTrue(context.isPresent());
}
final ExcerptTailer newTailer = queue.createTailer();
int totalCount = 0;
while (true) {
final DocumentContext context = newTailer.readingDocument();
if (context.isPresent() && context.isData()) {
assertNotEquals(0, context.wire().read().int32());
totalCount++;
} else if (!context.isPresent()) {
break;
}
}
assertEquals(2, totalCount);
}
}
Aggregations