use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class LastIndexAppendedTest method testLastIndexAppendedAcrossRestarts.
@Test
public void testLastIndexAppendedAcrossRestarts() {
String path = OS.getTarget() + "/" + getClass().getSimpleName() + "-" + Time.uniqueId();
for (int i = 0; i < 5; i++) {
try (ChronicleQueue queue = single(path).testBlockSize().rollCycle(TEST_DAILY).build()) {
ExcerptAppender appender = queue.acquireAppender();
try (DocumentContext documentContext = appender.writingDocument()) {
int index = (int) documentContext.index();
assertEquals(i, index);
documentContext.wire().write().text("hello world");
}
assertEquals(i, (int) appender.lastIndexAppended());
}
}
try {
IOTools.deleteDirWithFiles(path, 2);
} catch (Exception index) {
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class CycleNotFoundTest method tailerCycleNotFoundTest.
// reduced so that it runs quicker for the continuous integration (CI)
@Test(timeout = 50_000L)
public void tailerCycleNotFoundTest() throws InterruptedException, ExecutionException {
// added nano time just to make
File path = getTmpDir();
ExecutorService executorService = Executors.newFixedThreadPool((int) NUMBER_OF_MSG, new NamedThreadFactory("tailerCycleNotFoundTest"));
AtomicLong counter = new AtomicLong();
Runnable reader = () -> {
long count = 0;
try (ChronicleQueue rqueue = SingleChronicleQueueBuilder.binary(path).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).build()) {
final ExcerptTailer tailer = rqueue.createTailer();
long last = -1;
while (count < NUMBER_OF_MSG) {
try (DocumentContext dc = tailer.readingDocument()) {
if (!dc.isPresent())
continue;
Assert.assertTrue(dc.isData());
Assert.assertEquals(last + 1, last = dc.wire().read().int64());
count++;
counter.incrementAndGet();
}
if (executorService.isShutdown())
Assert.fail();
}
// check nothing after the NUMBER_OF_MSG
try (DocumentContext dc = tailer.readingDocument()) {
Assert.assertFalse(dc.isPresent());
}
} finally {
// System.out.printf("Read %,d messages, thread=" + Thread.currentThread().getName() + "\n", count);
}
};
List<Future> tailers = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_TAILERS; i++) {
tailers.add(executorService.submit(reader));
}
// Appender run in a different thread
ExecutorService executorService1 = Executors.newSingleThreadExecutor(new NamedThreadFactory("appender"));
Future<?> submit = executorService1.submit(() -> {
ChronicleQueue wqueue = SingleChronicleQueueBuilder.binary(path).testBlockSize().rollCycle(TEST_SECONDLY).build();
final ExcerptAppender appender = wqueue.acquireAppender();
long next = System.nanoTime() + INTERVAL_US * 1000;
for (int i = 0; i < NUMBER_OF_MSG; i++) {
while (System.nanoTime() < next) /* busy wait*/
;
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().int64(i);
}
next += INTERVAL_US * 1000;
if (executorService1.isShutdown())
return;
}
wqueue.close();
});
submit.get();
// wait for all the tailer to finish
for (Future f : tailers) {
f.get();
}
executorService.shutdownNow();
executorService1.shutdownNow();
executorService.awaitTermination(5, TimeUnit.SECONDS);
executorService1.awaitTermination(5, TimeUnit.SECONDS);
assertEquals(NUMBER_OF_MSG * NUMBER_OF_TAILERS, counter.get());
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class ChronicleQueueIndexTest method readKeyed.
@NotNull
private List<String> readKeyed(ChronicleQueue queue, boolean includeMetaData) {
try (ExcerptTailer tailer = queue.createTailer()) {
List<String> allReads = new ArrayList<>();
for (; ; ) {
try (DocumentContext dc = tailer.readingDocument(includeMetaData)) {
if (!dc.isPresent())
return allReads;
final Wire wire = dc.wire();
final String key = wire.readEvent(String.class);
if (!key.equals("key"))
continue;
String str = wire.getValueIn().text();
allReads.add(str);
}
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class ChronicleQueueIndexTest method writeReadMetadata.
// https://github.com/OpenHFT/Chronicle-Queue/issues/822
@Test
public void writeReadMetadata() {
try (final ChronicleQueue queue = ChronicleQueue.singleBuilder(getTmpDir()).rollCycle(RollCycles.TEST4_SECONDLY).testBlockSize().build()) {
final ExcerptAppender appender = queue.acquireAppender();
final ExcerptTailer tailer = queue.createTailer();
boolean metadata = true;
try (DocumentContext dc = appender.writingDocument(metadata)) {
dc.wire().write("a").text("hello");
}
try (DocumentContext dc = tailer.readingDocument(metadata)) {
Assert.assertTrue(dc.isPresent());
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class Stackoveflow52274284Test method fails.
@Test
public void fails() throws IOException {
String basePath = OS.getTarget();
String path = Files.createTempDirectory(Paths.get(basePath), "chronicle-").toAbsolutePath().toString();
try (ChronicleQueue chronicleQueue = ChronicleQueue.singleBuilder(path).testBlockSize().build()) {
// Create Appender
ExcerptAppender appender = chronicleQueue.acquireAppender();
// Create Tailer
ExcerptTailer tailer = chronicleQueue.createTailer();
tailer.toStart();
int numberOfRecords = 10;
// Write
for (int i = 0; i <= numberOfRecords; i++) {
// System.out.println("Writing " + i);
try (final DocumentContext dc = appender.writingDocument()) {
dc.wire().write("msg").text("Hello World!");
// System.out.println("your data was store to index=" + dc.index());
} catch (Exception e) {
System.err.println("Unable to store value to chronicle");
e.printStackTrace();
}
}
// Read
for (int i = 0; i <= numberOfRecords; i++) {
// System.out.println("Reading " + i);
try (DocumentContext documentContext = tailer.readingDocument()) {
long currentOffset = documentContext.index();
// System.out.println("Current offset: " + currentOffset);
Wire wire = documentContext.wire();
if (wire != null) {
String msg = wire.read("msg").text();
// System.out.println(msg);
}
}
}
}
}
Aggregations