use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class TestBinarySearch method toWire.
@NotNull
private Wire toWire(int key) {
final MyData myData = new MyData();
myData.key = key;
myData.value = Integer.toString(key);
Wire result = WireType.BINARY.apply(Bytes.elasticByteBuffer());
try (final DocumentContext dc = result.writingDocument()) {
dc.wire().getValueOut().typedMarshallable(myData);
}
return result;
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class TestWriteWhenCurrentCycleGotEOF method shouldBeAbleToWriteIfCurrentCycleGotEOF.
@Test
public void shouldBeAbleToWriteIfCurrentCycleGotEOF() throws TimeoutException {
File tmpDir = getTmpDir();
createQueueWithOnlyHeaderFile(tmpDir);
SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().build();
ExcerptAppender appender = queue.acquireAppender();
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write("test").text("Hello world");
}
try (DocumentContext dc = queue.acquireTailer().readingDocument()) {
assertEquals("Hello world", dc.wire().read("test").text());
}
}
use of net.openhft.chronicle.wire.DocumentContext 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.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RollEOFTest method createQueueAndWriteData.
private void createQueueAndWriteData(MutableTimeProvider timeProvider) {
final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).testBlockSize().rollCycle(RollCycles.TEST_DAILY).timeProvider(timeProvider).build();
ExcerptAppender excerptAppender = queue.acquireAppender();
try (DocumentContext dc = excerptAppender.writingDocument(false)) {
dc.wire().write(() -> "test").int64(0);
}
}
Aggregations