use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.
the class InternalAppenderWriteBytesTest method testJumpingAMessageThrowsAIllegalStateException.
@Test(expected = IllegalStateException.class)
public void testJumpingAMessageThrowsAIllegalStateException() {
try (SingleChronicleQueue q = binary(tempDir("q")).rollCycle(MINUTELY).timeProvider(() -> 0).build();
ExcerptAppender appender = q.acquireAppender()) {
appender.writeText("hello");
appender.writeText("hello2");
try (final DocumentContext dc = appender.writingDocument()) {
dc.wire().bytes().writeLong(1);
}
final long l = appender.lastIndexAppended();
final RollCycle rollCycle = q.rollCycle();
final int currentCycle = rollCycle.toCycle(l);
// try to write to next roll cycle and write at seqnum 1 (but miss the 0th seqnum of that roll cycle)
final long index = rollCycle.toIndex(currentCycle + 1, 1);
((InternalAppender) appender).writeBytes(index, Bytes.from("text"));
}
}
use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.
the class NoDataIsSkippedWithInterruptTest method test.
@Test
public void test() {
final SetTimeProvider timeProvider = new SetTimeProvider();
try (SingleChronicleQueue q = SingleChronicleQueueBuilder.single(DirectoryUtils.tempDir(".")).rollCycle(RollCycles.MINUTELY).timeProvider(timeProvider).testBlockSize().build();
final ExcerptAppender excerptAppender = q.acquireAppender();
final ExcerptTailer tailer = q.createTailer()) {
Thread.currentThread().interrupt();
excerptAppender.writeText(EXPECTED);
// TODO: restore the below
Assert.assertTrue(Thread.currentThread().isInterrupted());
timeProvider.advanceMillis(60_000);
excerptAppender.writeText(EXPECTED);
Assert.assertEquals(EXPECTED, tailer.readText());
Assert.assertEquals(EXPECTED, tailer.readText());
}
}
use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.
the class PeekDocumentTest method soakTestPeekDocument.
@Ignore("PeekDocument gets stuck and reports no messages when there are messages - https://github.com/OpenHFT/Chronicle-Queue/issues/797")
@Test
public void soakTestPeekDocument() throws ExecutionException, InterruptedException {
File tempDir = getTmpDir();
try (SingleChronicleQueue q = SingleChronicleQueueBuilder.single(tempDir).rollCycle(RollCycles.MINUTELY).build()) {
final long maxMessagesPerCycle = RollCycles.MINUTELY.maxMessagesPerCycle();
System.out.println("maxMessagesPerCycle = " + DecimalFormat.getInstance().format(maxMessagesPerCycle));
final ScheduledExecutorService es2 = newSingleThreadScheduledExecutor();
es2.submit(() -> {
try (final ExcerptAppender appender = q.acquireAppender()) {
for (int i = 0; i < maxMessagesPerCycle; i++) {
try (final DocumentContext documentContext = appender.writingDocument()) {
documentContext.wire().write("value").int64(i);
}
}
}
});
final ScheduledExecutorService es = newSingleThreadScheduledExecutor();
es.submit(() -> {
try (final ExcerptTailer excerptTailer = q.createTailer()) {
long count = 0;
Thread.yield();
OUTER: for (; ; ) {
while (excerptTailer.peekDocument()) {
try (DocumentContext dc = excerptTailer.readingDocument()) {
if (!dc.isPresent())
continue OUTER;
assertEquals(count, dc.wire().read("value").int64());
count++;
if (count % 1_000_000 == 0)
System.out.println("count = " + DecimalFormat.getInstance().format(count));
if (count == maxMessagesPerCycle)
return null;
}
}
}
}
}).get();
es.shutdownNow();
es2.shutdownNow();
}
}
use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.
the class PeekDocumentTest method testReadWrite10.
@Test
public void testReadWrite10() {
File tempDir = getTmpDir();
try {
try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
ExcerptAppender appender = queue.acquireAppender();
for (int i = 0; i < 1024; i++) {
try (DocumentContext documentContext = appender.writingDocument()) {
documentContext.wire().write("value").text("hello" + i);
}
}
ExcerptTailer tailer = queue.createTailer();
for (int i = 0; i < 1024; i++) {
assertTrue(tailer.peekDocument());
try (DocumentContext documentContext = tailer.readingDocument()) {
assertTrue(documentContext.isPresent());
assertTrue(tailer.peekDocument());
Wire wire = documentContext.wire();
long l = wire.bytes().readPosition();
try {
assertEquals("hello" + i, wire.read("value").text());
} finally {
// simulate if the message was read
if (l % 2 == 0)
wire.bytes().readPosition(l);
}
}
}
assertFalse(tailer.peekDocument());
try (DocumentContext documentContext = appender.writingDocument()) {
documentContext.wire().write("value").text("hello" + 10);
}
assertTrue(tailer.peekDocument());
}
} finally {
tempDir.deleteOnExit();
}
}
use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.
the class PeekDocumentTest method testReadWrite10Backwards.
@Test
public void testReadWrite10Backwards() {
File tempDir = getTmpDir();
try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tempDir).build()) {
ExcerptAppender appender = queue.acquireAppender();
try (DocumentContext documentContext = appender.writingDocument()) {
documentContext.wire().write("value").text("hello");
}
ExcerptTailer tailer = queue.createTailer();
assertTrue(tailer.peekDocument());
try (DocumentContext documentContext = tailer.readingDocument()) {
assertTrue(documentContext.isPresent());
assertTrue(tailer.peekDocument());
Wire wire = documentContext.wire();
String result = wire.read("value").text();
assertEquals("hello", result);
// System.out.println(result);
}
assertFalse(tailer.peekDocument());
tailer.direction(TailerDirection.BACKWARD);
assertTrue(tailer.peekDocument());
try (DocumentContext documentContext = tailer.readingDocument()) {
}
assertFalse(tailer.peekDocument());
} finally {
tempDir.deleteOnExit();
}
}
Aggregations