use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.
the class ReadWriteTest method testToEndOnReadOnly.
@Test
public void testToEndOnReadOnly() {
try (SingleChronicleQueue out = SingleChronicleQueueBuilder.binary(chroniclePath).testBlockSize().readOnly(true).build()) {
ExcerptTailer tailer = out.createTailer();
tailer.toEnd();
long index = tailer.index();
assertTrue(index != 0);
}
}
use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.
the class ReadWriteTest method setup.
@Before
public void setup() {
chroniclePath = new File(OS.TARGET, "read_only");
try (SingleChronicleQueue readWrite = SingleChronicleQueueBuilder.binary(chroniclePath).readOnly(false).testBlockSize().build()) {
final ExcerptAppender appender = readWrite.acquireAppender();
appender.writeText(STR1);
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().bytes().writeUtf8(STR2);
}
}
}
use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.
the class VisibilityOfMessagesBetweenTailorsAndAppenderTest method test.
/**
* check if a message is written with an appender its visible to the tailor, without locks etc.
*
* @throws InterruptedException
* @throws ExecutionException
*/
@Test
public void test() throws InterruptedException, ExecutionException {
SingleChronicleQueue x = SingleChronicleQueueBuilder.binary(getTmpDir()).build();
ExecutorService e1 = Executors.newSingleThreadExecutor();
e1.submit(() -> {
ExcerptAppender excerptAppender = x.acquireAppender();
for (long i = 0; i < 1_000_000; i++) {
try (DocumentContext dc = excerptAppender.writingDocument()) {
dc.wire().getValueOut().int64(i);
}
lastWrittenIndex = excerptAppender.lastIndexAppended();
}
});
ExecutorService e2 = Executors.newSingleThreadExecutor();
Future f2 = e2.submit(() -> {
ExcerptTailer tailer = x.createTailer();
for (; ; ) {
long i = lastWrittenIndex;
if (i != Long.MIN_VALUE)
if (!tailer.moveToIndex(i))
throw new ExecutionException("non atomic, index=" + Long.toHexString(i), null);
}
});
try {
f2.get(5, TimeUnit.SECONDS);
} catch (TimeoutException ignore) {
}
e2.shutdownNow();
e1.shutdownNow();
}
use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.
the class WriteReadTextTest method doTest.
private void doTest(@NotNull String... problematic) {
String myPath = OS.TARGET + "/writeReadText-" + System.nanoTime();
try (SingleChronicleQueue theQueue = ChronicleQueueBuilder.single(myPath).blockSize(Maths.nextPower2(EXTREMELY_LARGE.length() * 4, 256 << 10)).build()) {
ExcerptAppender appender = theQueue.acquireAppender();
ExcerptTailer tailer = theQueue.createTailer();
StringBuilder tmpReadback = new StringBuilder();
// If the tests don't fail, try increasing the number of iterations
// Setting it very high may give you a JVM crash
final int tmpNumberOfIterations = 5;
for (int l = 0; l < tmpNumberOfIterations; l++) {
for (int p = 0; p < problematic.length; p++) {
appender.writeText(problematic[p]);
}
for (int p = 0; p < problematic.length; p++) {
tailer.readText(tmpReadback);
Assert.assertEquals("write/readText", problematic[p], tmpReadback.toString());
}
}
for (int l = 0; l < tmpNumberOfIterations; l++) {
for (int p = 0; p < problematic.length; p++) {
final String tmpText = problematic[p];
appender.writeDocument(writer -> writer.getValueOut().text(tmpText));
tailer.readDocument(reader -> reader.getValueIn().textTo(tmpReadback));
String actual = tmpReadback.toString();
Assert.assertEquals(problematic[p].length(), actual.length());
for (int i = 0; i < actual.length(); i += 1024) Assert.assertEquals("i: " + i, problematic[p].substring(i, Math.min(actual.length(), i + 1024)), actual.substring(i, Math.min(actual.length(), i + 1024)));
Assert.assertEquals(problematic[p], actual);
}
}
}
}
use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.
the class RollingChronicleQueueTest method testTailing.
private void testTailing(Function<Pretoucher, Integer> createGap) {
final SetTimeProvider tp = new SetTimeProvider(0);
final File tmpDir = getTmpDir();
try (SingleChronicleQueue queue = builder(tmpDir, WireType.BINARY).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(tp).build()) {
int cyclesAdded = 0;
final Pretoucher pretoucher = new Pretoucher(queue);
ExcerptAppender appender = queue.acquireAppender();
// to file ...000000
appender.writeText("0");
assertEquals(1, tmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
tp.advanceMillis(1000);
// to file ...000001
appender.writeText("1");
assertEquals(2, tmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
tp.advanceMillis(2000);
cyclesAdded += createGap.apply(pretoucher);
assertEquals(2 + cyclesAdded, tmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
tp.advanceMillis(1000);
// to file ...000004
appender.writeText("2");
assertEquals(3 + cyclesAdded, tmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
tp.advanceMillis(2000);
cyclesAdded += createGap.apply(pretoucher);
assertEquals(3 + cyclesAdded, tmpDir.listFiles(file -> file.getName().endsWith("cq4")).length);
// now tail them all back
int count = 0;
ExcerptTailer tailer = queue.createTailer();
long[] indexes = new long[3];
while (true) {
String text = tailer.readText();
if (text == null)
break;
indexes[count] = tailer.index() - 1;
assertEquals(count++, Integer.parseInt(text));
}
assertEquals(indexes.length, count);
// now make sure we can go direct to each index (like afterLastWritten)
tailer.toStart();
for (int i = 0; i < indexes.length; i++) {
assertTrue(tailer.moveToIndex(indexes[i]));
String text = tailer.readText();
assertEquals(i, Integer.parseInt(text));
}
}
}
Aggregations