use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RawAccessJavaTest method Appender.
@Test
public void Appender() {
if (!assert_from_cpp())
return;
String tmp = "/dev/shm/RawAccessJtoC";
// so C++ knows this ran rather than skipped
System.out.println(tmp);
try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {
ExcerptAppender appender = cq.acquireAppender();
for (int i = 0; i < COUNT; ++i) {
try (DocumentContext dc = appender.writingDocument()) {
Bytes bytes = dc.wire().bytes();
// will contain the size of the blob
long start = bytes.writePosition();
bytes.writeSkip(RAW_SIZE_PREFIX);
{
bytes.writeByte((byte) 0xab);
bytes.writeShort((short) 12);
bytes.writeInt(123);
bytes.writeLong(123456789L);
bytes.writeFloat(1.234f);
bytes.writeDouble(123.456);
bytes.writeChar('a');
bytes.write8bit("Hello World");
}
long end = bytes.writePosition();
bytes.writeInt(start, (int) (end - start - RAW_SIZE_PREFIX));
}
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class EntryCountNotBehindReadTest method runReader.
private void runReader(SingleChronicleQueue queue, CyclicBarrier startBarrier, LongConsumer onRead) {
try (final ExcerptTailer tailer = queue.createTailer()) {
waitOn(startBarrier);
int count = 0;
while (count < TOTAL_EVENTS) {
try (DocumentContext entry = tailer.readingDocument()) {
if (entry.isData() && !entry.isNotComplete()) {
onRead.accept(entry.index());
++count;
}
}
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class QueueMultiThreadedJLBHBenchmark method run.
@Override
public void run(long startTimeNS) {
datum.ts = startTimeNS;
try (DocumentContext dc = appender.writingDocument()) {
datum.writeMarshallable(dc.wire().bytes());
}
writeProbe.sampleNanos(System.nanoTime() - startTimeNS);
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class PretoucherDontWriteTest method dontWrite.
@Test
public void dontWrite() {
File dir = getTmpDir();
try (final SingleChronicleQueue queue = PretoucherTest.createQueue(dir, clock::get);
final SingleChronicleQueue pretoucherQueue = PretoucherTest.createQueue(dir, clock::get);
final Pretoucher pretoucher = new Pretoucher(pretoucherQueue, chunkListener, capturedCycles::add, false, false)) {
range(0, 10).forEach(i -> {
try (final DocumentContext ctx = queue.acquireAppender().writingDocument()) {
assertEquals(i + 0.5, capturedCycles.size(), 0.5);
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(TimeUnit.SECONDS.toMillis(5L));
try {
pretoucher.execute();
} catch (InvalidEventHandlerException e) {
throw Jvm.rethrow(e);
}
assertEquals(i + 1.5, capturedCycles.size(), 0.5);
});
assertEquals(10.5, capturedCycles.size(), 0.5);
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class AppenderLockOnlyAppliesToFileTest method concurrentLockItUp.
/*
Failed tests:
AppenderLockOnlyAppliesToFileTest.concurrentLockItUp:59 Writer thread completed before timeout
*/
@Ignore("fails too often")
@Test
public void concurrentLockItUp() throws InterruptedException {
final AtomicReference<String> writerQueueFile = new AtomicReference<>();
final File path = DirectoryUtils.tempDir(this.getClass().getSimpleName());
final SingleChronicleQueueBuilder builder = ChronicleQueueBuilder.single(path).sourceId(1).rollCycle(ROLL_CYCLE).timeoutMS(TIMEOUT_MS);
final String initialFile;
final DocumentContext initialContext = builder.build().acquireAppender().writingDocument();
initialContext.wire().writeText("abcd");
initialFile = getFilename(initialContext);
// don't close context
final long afterInitialWrite = System.currentTimeMillis();
final CountDownLatch writerStarted = new CountDownLatch(1);
final CountDownLatch writerFinished = new CountDownLatch(1);
Thread writerThread = new Thread(() -> {
ExcerptAppender appender = builder.build().acquireAppender();
writerStarted.countDown();
// wait for less than timeout and more than roll
Jvm.pause(WAIT_FOR_ROLL_MS);
try (@NotNull DocumentContext context = appender.writingDocument()) {
// should not have been held up by locking
writerQueueFile.set(getFilename(context));
Wire wire = context.wire();
wire.writeText("hello");
writerFinished.countDown();
}
});
writerThread.start();
assertTrue("Writer thread not started", writerStarted.await(1, TimeUnit.SECONDS));
assertTrue("Writer thread completed before timeout", writerFinished.await(WAIT_FOR_ROLL_MS + 50, TimeUnit.MILLISECONDS));
assertFalse("Threads wrote to different queue cycles, so no locking occurred", initialFile.equals(writerQueueFile.get()));
assertTrue("We are within timeout", System.currentTimeMillis() < afterInitialWrite + TIMEOUT_MS);
ExcerptTailer tailer = builder.build().createTailer();
// this call to readingDocument waits for timeout and logs out ".... resetting header after timeout ...."
try (DocumentContext rd = tailer.readingDocument()) {
assertFalse("We are outside timeout", System.currentTimeMillis() < afterInitialWrite + TIMEOUT_MS);
assertTrue("Something was written", rd.isPresent());
String value = rd.wire().readText();
assertEquals("the first (locked) write is lost", "hello", value);
}
try (DocumentContext rd = tailer.readingDocument()) {
assertFalse("Should be only one message in the queue", rd.isPresent());
}
try {
initialContext.close();
fail("close should have thrown");
} catch (IllegalStateException e) {
// expected for close to throw
}
}
Aggregations