use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class ToEndPaddingTest method toEndWorksWithDifferentlyPaddedMessages.
@Test
public void toEndWorksWithDifferentlyPaddedMessages() {
try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(getTmpDir()).testBlockSize().rollCycle(RollCycles.TEST8_DAILY).build()) {
final ExcerptAppender appender = queue.acquireAppender();
final ExcerptTailer tailer = queue.createTailer();
try (final DocumentContext documentContext = appender.acquireWritingDocument(false)) {
documentContext.wire().write("start").text("start");
}
DocumentContext dc;
try (final DocumentContext documentContext = tailer.readingDocument(false)) {
assertTrue(documentContext.isPresent());
final String text = documentContext.wire().read().text();
assertEquals("start", text);
// cache for later
dc = documentContext;
}
for (int i = 0; i < 2; i++) {
try (final DocumentContext documentContext = appender.acquireWritingDocument(true)) {
documentContext.wire().write("metakey" + i).text(Bytes.wrapForRead(new byte[i + 1]));
}
}
// System.out.println(queue.dump());
// toEnd just before adding one more entry
assertEquals(2328, dc.wire().bytes().readPosition());
tailer.toEnd();
assertEquals(2360, dc.wire().bytes().readPosition());
try (final DocumentContext documentContext = appender.acquireWritingDocument(false)) {
documentContext.wire().write("key").text("value");
}
try (final DocumentContext documentContext = tailer.readingDocument(false)) {
assertTrue(documentContext.isPresent());
final String text = documentContext.wire().read().text();
assertEquals("value", text);
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class ValueStringArrayTest method test.
@Test
public void test() {
// No explicit support of putting a Value into Wire.
expectException("BytesMarshallable found in field which is not matching exactly");
ValueStringArray value = new ValueStringArray();
value.setCsArrItem(1, EXPECTED);
// this is the directory the queue is written to
final File dataDir = getTmpDir();
try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(dataDir).build()) {
try (DocumentContext dc = queue.acquireAppender().writingDocument()) {
dc.wire().write("data").marshallable(value);
}
try (DocumentContext dc = queue.createTailer().readingDocument()) {
dc.wire().read("data").marshallable(using);
CharSequence actual = using.getCsArr().getCharSequenceWrapperAt(1).getCharSequence();
// System.out.println(actual);
Assert.assertEquals(EXPECTED, actual.toString());
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class DtoBytesMarshallableTest method testDtoBytesMarshallable.
@Test
public void testDtoBytesMarshallable() {
File tmp = getTmpDir();
DtoBytesMarshallable dto = new DtoBytesMarshallable();
dto.age = 45;
dto.name.append("rob");
try (ChronicleQueue q = ChronicleQueue.singleBuilder(tmp).build()) {
try (DocumentContext dc = q.acquireAppender().writingDocument()) {
dc.wire().write("who").object(dto);
}
try (DocumentContext dc = q.createTailer().readingDocument()) {
DtoBytesMarshallable who = (DtoBytesMarshallable) dc.wire().read("who").object();
Assert.assertEquals("!net.openhft.chronicle.queue.DtoBytesMarshallableTest$DtoBytesMarshallable {\n" + " name: rob,\n" + " age: 45\n" + "}\n", who.toString());
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class DtoBytesMarshallableTest method testDtoAbstractMarshallable.
@Test
public void testDtoAbstractMarshallable() {
File tmp = getTmpDir();
DtoAbstractMarshallable dto = new DtoAbstractMarshallable();
dto.age = 45;
dto.name.append("rob");
try (ChronicleQueue q = ChronicleQueue.singleBuilder(tmp).build()) {
try (DocumentContext dc = q.acquireAppender().writingDocument()) {
dc.wire().write("who").object(dto);
}
try (DocumentContext dc = q.createTailer().readingDocument()) {
String yaml = dc.toString();
// System.out.println(yaml);
DtoAbstractMarshallable who = (DtoAbstractMarshallable) dc.wire().read("who").object();
// System.out.println(who);
Assert.assertTrue(yaml.contains(who.toString()));
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class CheckHalfWrittenMsgNotSeenByTailerTest method checkTailerOnlyReadsTwoMessageTwoProcesses.
@Test
public void checkTailerOnlyReadsTwoMessageTwoProcesses() throws IOException, InterruptedException {
Assume.assumeTrue(!OS.isWindows());
Assume.assumeTrue(!OS.isMacOSX());
final File queueDirectory = DirectoryUtils.tempDir("halfWritten");
final String command = String.format("mvn compile exec:java -Dexec.classpathScope=test " + "-Dexec.mainClass=%s -Dexec.args=\"%s\"", HalfWriteAMessage.class.getName(), queueDirectory.getAbsoluteFile());
runCommand(command);
try (final ChronicleQueue single = ChronicleQueue.single(queueDirectory.getPath());
final ExcerptTailer tailer = single.createTailer()) {
try (final DocumentContext dc = tailer.readingDocument()) {
Assert.assertTrue(dc.isPresent());
Assert.assertEquals("hello world 1", dc.wire().read("key1").text());
Assert.assertEquals("hello world 2", dc.wire().read("key2").text());
}
try (final DocumentContext dc = tailer.readingDocument()) {
Assert.assertTrue(dc.isPresent());
Assert.assertEquals("hello world 3", dc.wire().read("key1").text());
Assert.assertEquals("hello world 4", dc.wire().read("key2").text());
}
try (final DocumentContext dc = tailer.readingDocument()) {
final boolean present = dc.isPresent();
if (present) {
Jvm.error().on(getClass(), "Found an excerpt " + dc.wire().bytes().toHexString());
String key = dc.wire().readEvent(String.class);
String value = dc.wire().getValueIn().text();
fail("key: " + key + ", value: " + value);
}
}
}
}
Aggregations