@Test public void testRandomMove() throws Exception { final Map<Long, String> messageByIndex = new HashMap<>(); try (ChronicleQueue queue = SingleChronicleQueueBuilder. binary(tmpFolder.newFolder()).build()) { // create a queue and add some excerpts final ExcerptAppender appender = queue.acquireAppender(); for (int i = 0; i < 10; i++) { final String message = "msg" + i; appender.writeDocument(w -> w.write("message").object(message)); final long appendIndex = appender.lastIndexAppended(); messageByIndex.put(appendIndex, message); } final Random random = new Random(1510298038000L); final List<Long> indices = new ArrayList<>(messageByIndex.keySet()); final ExcerptTailer tailer = queue.createTailer(); final AtomicReference<String> capturedMessage = new AtomicReference<>(); for (int i = 0; i < 100; i++) { final long randomIndex = indices.get(random.nextInt(messageByIndex.keySet().size())); tailer.moveToIndex(randomIndex); tailer.readDocument(w -> capturedMessage.set((String) w.read("message").object())); assertEquals(messageByIndex.get(randomIndex), capturedMessage.get()); tailer.readDocument(w -> w.read("message").object()); } } }
@Test(expected = IllegalStateException.class) //: no messages written public void testNoMessagesWritten() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); appender.lastIndexAppended(); } }
@Test(expected = IllegalStateException.class) public void testLastWrittenIndexPerAppenderNoData() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); appender.lastIndexAppended(); Assert.fail(); } }
@Test public void testLastIndexAppended() { try (ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { ExcerptAppender appender = chronicle.acquireAppender(); appender.writeDocument(w -> w.writeEventName("hello").text("world0")); final long nextIndexToWrite = appender.lastIndexAppended() + 1; appender.writeDocument(w -> w.getValueOut().bytes(new byte[0])); // System.out.println(chronicle.dump()); Assert.assertEquals(nextIndexToWrite, appender.lastIndexAppended()); } }
@Before public void before() { path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime(); try (ChronicleQueue appender_queue = ChronicleQueue.singleBuilder(path) .testBlockSize() .buffered(false) .build()) { ExcerptAppender appender = appender_queue.acquireAppender(); for (int i = 0; i < messages; i++) { final long l = i; appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m -> { m.write("msg").text("hello world ola multi-verse"); m.write("ts").int64(l); } )); } a_index = appender.lastIndexAppended(); } }
@Test public void testLastIndexAppendedAcrossRestarts() { String path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime(); for (int i = 0; i < 5; i++) { try (ChronicleQueue queue = single(path) .testBlockSize() .rollCycle(TEST_DAILY) .build()) { ExcerptAppender appender = queue.acquireAppender(); try (DocumentContext documentContext = appender.writingDocument()) { int index = (int) documentContext.index(); assertEquals(i, index); documentContext.wire().write().text("hello world"); } assertEquals(i, (int) appender.lastIndexAppended()); } } try { IOTools.deleteDirWithFiles(path, 2); } catch (Exception index) { } }
@Ignore("long running test") @Test public void testReadAtIndex4MB() { try (final ChronicleQueue queue = SingleChronicleQueueBuilder.builder(getTmpDir(), this.wireType).rollCycle(SMALL_DAILY) .build()) { final ExcerptAppender appender = queue.acquireAppender(); System.out.print("Percent written="); for (long i = 0; i < TIMES; i++) { final long j = i; appender.writeDocument(wire -> wire.write(() -> "key").text("value=" + j)); if (i % (TIMES / 20) == 0) { System.out.println("" + (i * 100 / TIMES) + "%, "); } } long lastIndex = appender.lastIndexAppended(); final int cycle = queue.rollCycle().toCycle(lastIndex); final ExcerptTailer tailer = queue.createTailer(); // QueueDumpMain.dump(file, new PrintWriter(System.out)); StringBuilder sb = new StringBuilder(); for (long i = 0; i < (4L << 20L); i++) { assertTrue(tailer.moveToIndex(queue.rollCycle().toIndex(cycle, i))); tailer.readDocument(wire -> wire.read(() -> "key").text(sb)); Assert.assertEquals("value=" + i, sb.toString()); if (i % (TIMES / 20) == 0) { System.out.println("Percent read= " + (i * 100 / TIMES) + "%"); } } } }
@Test public void testAppend() { try (final ChronicleQueue queue = builder(getTmpDir(), wireType) .build()) { final ExcerptAppender appender = queue.acquireAppender(); for (int i = 0; i < 10; i++) { final int n = i; appender.writeDocument(w -> w.write(TestKey.test).int32(n)); assertEquals(n, queue.rollCycle().toSequenceNumber(appender.lastIndexAppended())); } assertThat(countEntries(queue), is(10L)); } }
@Override public Long call() throws Exception { queue = ChronicleQueue.singleBuilder(path) //.testBlockSize() //.rollCycle(TEST_DAILY) .buffered(false) .build(); appender = queue.acquireAppender(); for (int i = 0; i < 50; i++) { appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m -> m.write("msg").text("hello world2 "))); } long index = appender.lastIndexAppended(); sync.put(index); Long fromReader = sync.take(); if (index != fromReader) { System.out.println("Writer:Not the same:" + index + " vs. " + fromReader); } for (int i = 0; i < 50; i++) { appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m -> m.write("msg").text("hello world2 "))); } index = appender.lastIndexAppended(); sync.put(index); return index; } }
public void appendMessage(@NotNull ChronicleQueue queue, long expectedIndex, String msg) { @NotNull ExcerptAppender appender = queue.acquireAppender(); switch (appendMode) { case 1: appender.writeDocument(w -> w.write(() -> "msg").text(msg)); break; case 2: Bytes bytes = Bytes.elasticByteBuffer(); new BinaryWire(bytes).write(() -> "msg").text(msg); appender.writeBytes(bytes); bytes.release(); break; default: try (DocumentContext dc = appender.writingDocument()) { Wire wire = dc.wire(); wire.write(() -> "msg").text(msg); } break; } long index = appender.lastIndexAppended(); assertHexEquals(expectedIndex, index); }
@Test public void testLastWrittenIndexPerAppender() { try (final ChronicleQueue queue = builder(getTmpDir(), this.wireType) .build()) { final ExcerptAppender appender = queue.acquireAppender(); appender.writeDocument(wire -> wire.write(() -> "key").text("test")); Assert.assertEquals(0, queue.rollCycle().toSequenceNumber(appender.lastIndexAppended())); } }
@Test public void testMultipleAppenders() { try (ChronicleQueue syncQ = builder(getTmpDir(), this.wireType) .rollCycle(TEST_DAILY) .build()) { ExcerptAppender syncA = syncQ.acquireAppender(); assumeFalse(syncA.padToCacheAlignMode() == WORD); ExcerptAppender syncB = syncQ.acquireAppender(); ExcerptAppender syncC = syncQ.acquireAppender(); int count = 0; for (int i = 0; i < 3; i++) { syncA.writeText("hello A" + i); assertEquals(count++, (int) syncA.lastIndexAppended()); syncB.writeText("hello B" + i); assertEquals(count++, (int) syncB.lastIndexAppended()); try (DocumentContext dc = syncC.writingDocument(true)) { dc.wire().getValueOut().text("some meta " + i); } } String expected = expectedMultipleAppenders(); assertEquals(expected, syncQ.dump()); } }
@Test public void testIndex() { try (final ChronicleQueue queue = builder(getTmpDir(), this.wireType) .rollCycle(RollCycles.HOURLY) .build()) { final ExcerptAppender appender = queue.acquireAppender(); int cycle = appender.cycle(); // create 100 documents for (int i = 0; i < 5; i++) { final int j = i; appender.writeDocument(wire -> wire.write(() -> "key").text("value=" + j)); if (i == 2) { final long cycle1 = queue.rollCycle().toCycle(appender.lastIndexAppended()); Assert.assertEquals(cycle1, cycle); } } final ExcerptTailer tailer = queue.createTailer(); assertTrue(tailer.moveToIndex(queue.rollCycle().toIndex(cycle, 2))); StringBuilder sb = new StringBuilder(); tailer.readDocument(wire -> wire.read(() -> "key").text(sb)); Assert.assertEquals("value=2", sb.toString()); tailer.readDocument(wire -> wire.read(() -> "key").text(sb)); Assert.assertEquals("value=3", sb.toString()); tailer.readDocument(wire -> wire.read(() -> "key").text(sb)); Assert.assertEquals("value=4", sb.toString()); } }
@Test public void test() throws IOException { try (final RollingChronicleQueue queue = SingleChronicleQueueBuilder .binary(getTmpDir()) .testBlockSize() .wireType(this.wireType) .build()) { final ExcerptAppender appender = queue.acquireAppender(); for (int i = 0; i < 5; i++) { final int n = i; appender.writeDocument( w -> w.write(TestKey.test).int32(n)); final int cycle = queue.lastCycle(); long index0 = queue.rollCycle().toIndex(cycle, n); long indexA = appender.lastIndexAppended(); accessHexEquals(index0, indexA); } } }
@Test public void testIndexWritingDocument() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); long index; try (DocumentContext dc = appender.writingDocument()) { dc.wire().write(() -> "FirstName").text("Quartilla"); index = dc.index(); } try (DocumentContext dc = appender.writingDocument(true)) { dc.wire().write(() -> "FirstName").text("Quartilla"); } Assert.assertEquals(index, appender.lastIndexAppended()); } }
long lastIndex = appender.lastIndexAppended();
@Test public void testSomeMessages() { try (ChronicleQueue chronicle = builder(getTmpDir(), wireType) .rollCycle(TEST2_DAILY) .build()) { ExcerptAppender appender = chronicle.acquireAppender(); ExcerptTailer tailer = chronicle.createTailer(); int entries = chronicle.rollCycle().defaultIndexSpacing() * 2 + 2; for (long i = 0; i < entries; i++) { long finalI = i; appender.writeDocument(w -> w.writeEventName("hello").int64(finalI)); long seq = chronicle.rollCycle().toSequenceNumber(appender.lastIndexAppended()); assertEquals(i, seq); // System.out.println(chronicle.dump()); tailer.readDocument(w -> w.read().int64(finalI, (a, b) -> Assert.assertEquals((long) a, b))); } } }
@Test public void testWrittenAndReadIndexesAreTheSameOfTheFirstExcerpt() { File tmp = getTmpDir(); long expected; try (ChronicleQueue queue = single(tmp) .testBlockSize() .build()) { ExcerptAppender appender = queue.acquireAppender(); try (DocumentContext dc = appender.writingDocument()) { dc.wire().write().text("some-data"); expected = dc.index(); Assert.assertTrue(expected > 0); } appender.lastIndexAppended(); ExcerptTailer tailer = queue.createTailer(); try (DocumentContext dc = tailer.readingDocument()) { String text = dc.wire().read().text(); { long actualIndex = dc.index(); Assert.assertTrue(actualIndex > 0); Assert.assertEquals(expected, actualIndex); } { long actualIndex = tailer.index(); Assert.assertTrue(actualIndex > 0); Assert.assertEquals(expected, actualIndex); } } } }
@Test public void testAppendAndReadAtIndex() { try (final ChronicleQueue queue = builder(getTmpDir(), this.wireType) .rollCycle(TEST2_DAILY) .build()) { final ExcerptAppender appender = queue.acquireAppender(); appender.cycle(); for (int i = 0; i < 5; i++) { final int n = i; appender.writeDocument(w -> w.write(TestKey.test).int32(n)); assertEquals(i, queue.rollCycle().toSequenceNumber(appender.lastIndexAppended())); } final ExcerptTailer tailer = queue.createTailer(); for (int i = 0; i < 5; i++) { final long index = queue.rollCycle().toIndex(appender.cycle(), i); assertTrue(tailer.moveToIndex(index)); final int n = i; assertTrue(tailer.readDocument(r -> assertEquals(n, queue.rollCycle().toSequenceNumber(r.read(TestKey.test) .int32())))); long index2 = tailer.index(); long sequenceNumber = queue.rollCycle().toSequenceNumber(index2); assertEquals(n + 1, sequenceNumber); } } }
@Test public void testAppendAndRead() { try (final ChronicleQueue queue = builder(getTmpDir(), this.wireType) .build()) { final ExcerptAppender appender = queue.acquireAppender(); final int cycle = appender.cycle(); for (int i = 0; i < 10; i++) { final int n = i; appender.writeDocument(w -> w.write(TestKey.test).int32(n)); assertEquals(n, queue.rollCycle().toSequenceNumber(appender.lastIndexAppended())); } final ExcerptTailer tailer = queue.createTailer(); // Sequential read for (int i = 0; i < 10; i++) { final int n = i; assertTrue(tailer.readDocument(r -> assertEquals(n, r.read(TestKey.test).int32()))); assertEquals(n + 1, queue.rollCycle().toSequenceNumber(tailer.index())); } // Random read for (int i = 0; i < 10; i++) { final int n = i; assertTrue("n: " + n, tailer.moveToIndex(queue.rollCycle().toIndex(cycle, n))); assertTrue("n: " + n, tailer.readDocument(r -> assertEquals(n, r.read(TestKey.test).int32()))); assertEquals(n + 1, queue.rollCycle().toSequenceNumber(tailer.index())); } } }