use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class CheckIndicesTest method appendToQueue.
private void appendToQueue() {
ExcerptAppender appender = queue0.acquireAppender();
try {
for (int i = 0; i < BATCH_SIZE; i++) {
try (DocumentContext dc = appender.writingDocument()) {
long seq = appender.queue().rollCycle().toSequenceNumber(dc.index());
// System.out.println("write=" + Long.toHexString(dc.index()));
dc.wire().write("value").writeLong(seq);
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class DocumentOrderingTest method codeWithinPriorDocumentMustExecuteBeforeSubsequentDocumentWhenQueueIsEmpty.
@Test
public void codeWithinPriorDocumentMustExecuteBeforeSubsequentDocumentWhenQueueIsEmpty() throws InterruptedException, TimeoutException, ExecutionException {
try (final ChronicleQueue queue = builder(getTmpDir(), 3_000L).build()) {
final ExcerptAppender excerptAppender = queue.acquireAppender();
final Future<RecordInfo> otherDocumentWriter;
try (final DocumentContext documentContext = excerptAppender.writingDocument()) {
// move time to beyond the next cycle
clock.addAndGet(TimeUnit.SECONDS.toMillis(2L));
otherDocumentWriter = attemptToWriteDocument(queue);
// stall this thread, other thread should not be able to advance,
// since this DocumentContext is still open
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2L));
documentContext.wire().getValueOut().int32(counter.getAndIncrement());
}
expectCounterVaueOne(otherDocumentWriter);
final ExcerptTailer tailer = queue.createTailer();
expectValue(0, tailer);
expectValue(1, tailer);
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class DocumentOrderingTest method shouldRecoverFromUnfinishedFirstMessageInPreviousQueue.
@Test
public void shouldRecoverFromUnfinishedFirstMessageInPreviousQueue() throws InterruptedException, TimeoutException, ExecutionException {
expectException("Couldn't acquire write lock");
expectException("Forced unlock for the lock");
// as below, but don't actually close the initial context
try (final ChronicleQueue queue = builder(getTmpDir(), 1_000L).build()) {
final ExcerptAppender excerptAppender = queue.acquireAppender();
final Future<RecordInfo> otherDocumentWriter;
// begin a record in the first cycle file
final DocumentContext documentContext = excerptAppender.writingDocument();
documentContext.wire().getValueOut().int32(counter.getAndIncrement());
// move time to beyond the next cycle
clock.addAndGet(TimeUnit.SECONDS.toMillis(2L));
otherDocumentWriter = attemptToWriteDocument(queue);
expectCounterVaueOne(otherDocumentWriter);
final ExcerptTailer tailer = queue.createTailer();
expectValue(1, tailer);
assertFalse(tailer.readingDocument().isPresent());
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class QueueAppendAfterRollReplayedIssueTest method test.
@Test
public void test() {
int messages = 10;
String path = OS.getTarget() + "/" + getClass().getSimpleName() + "-" + Time.uniqueId();
SetTimeProvider timeProvider = new SetTimeProvider();
try (final ChronicleQueue writeQueue = ChronicleQueue.singleBuilder(path).testBlockSize().timeProvider(timeProvider).rollCycle(RollCycles.TEST_SECONDLY).build()) {
for (int i = 0; i < messages; i++) {
timeProvider.advanceMillis(i * 100);
ExcerptAppender appender = writeQueue.acquireAppender();
Map<String, Object> map = new HashMap<>();
map.put("key", i);
appender.writeMap(map);
}
}
timeProvider.advanceMillis(1000);
try (final ChronicleQueue queue = ChronicleQueue.singleBuilder(path).testBlockSize().timeProvider(timeProvider).rollCycle(RollCycles.TEST_SECONDLY).build()) {
final ExcerptAppender excerptAppender = queue.acquireAppender();
try (final DocumentContext documentContext = excerptAppender.acquireWritingDocument(false)) {
assertNotNull(documentContext.wire());
}
} finally {
try {
IOTools.deleteDirWithFiles(path, 2);
} catch (IORuntimeException todoFixOnWindows) {
}
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RawAccessJavaTest method Tailer.
@Test
public void Tailer() {
if (!assert_from_cpp())
return;
String tmp = "/dev/shm/RawAccessCtoJ";
// so C++ knows this ran rather than skipped
System.out.println(tmp);
try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {
ExcerptTailer tailer = cq.createTailer();
for (int i = 0; i < COUNT; ++i) {
try (DocumentContext dc = tailer.readingDocument()) {
Bytes bytes = dc.wire().bytes();
bytes.readSkip(-QUEUE_HEADER_SIZE);
int header = bytes.readInt();
// document length, inc 4-byte length
int length = Wires.lengthOf(header);
// actual length of data
int data_length = bytes.readInt();
assertEquals(bytes.readByte(), (byte) 0xab);
assertEquals(bytes.readShort(), (short) 12);
assertEquals(bytes.readInt(), 123);
assertEquals(bytes.readLong(), 123456789L);
assertEquals(bytes.readFloat(), 1.234f, 1.0e-7);
assertEquals(bytes.readDouble(), 123.456, 1.0e-7);
assertEquals(bytes.readChar(), 'a');
StringBuilder sb = new StringBuilder();
bytes.read8bit(sb);
assertEquals(sb.toString(), "Hello World");
}
}
}
}
Aggregations