private void write(@NotNull ChronicleQueue aChronicle, int messages) { final ExcerptAppender myAppender = aChronicle.acquireAppender(); for (int myCount = 0; myCount < messages; myCount++) { myAppender.writeDocument(aMarshallable -> aMarshallable.write().bytes(Long.toString(currentTimeMillis()).getBytes(StandardCharsets.UTF_8))); // System.out.println(Long.toHexString(myAppender.lastIndexAppended())); } } }
@Test public void uninitialisedTailerCreatedBeforeFirstAppendWithDirectionNoneShouldNotFindDocument() { final AtomicLong clock = new AtomicLong(System.currentTimeMillis()); String path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime(); final ChronicleQueue queue = SingleChronicleQueueBuilder.single(path).timeProvider(clock::get).testBlockSize() .rollCycle(RollCycles.TEST_SECONDLY).build(); final ExcerptTailer tailer = queue.createTailer(); tailer.direction(TailerDirection.NONE); final ExcerptAppender excerptAppender = queue.acquireAppender(); for (int i = 0; i < 10; i++) { excerptAppender.writeDocument(i, (out, value) -> { out.int32(value); }); } DocumentContext document = tailer.readingDocument(); assertFalse(document.isPresent()); }
@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()); } }
@Test public void testTailAfterClose() { final ChronicleQueue queue = SingleChronicleQueueBuilder.builder(getTmpDir(), WireType.BINARY). build(); final ExcerptAppender appender = queue.acquireAppender(); appender.writeDocument(w -> w.write(TestKey.test).int32(1)); queue.close(); try { appender.writeDocument(w -> w.write(TestKey.test).int32(2)); Assert.fail(); } catch (IllegalStateException e) { // ok } } }
@Test public void testSimpleWire() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); appender.writeDocument(wire -> wire.write(() -> "FirstName").text("Steve")); appender.writeDocument(wire -> wire.write(() -> "Surname").text("Jobs")); StringBuilder first = new StringBuilder(); StringBuilder surname = new StringBuilder(); final ExcerptTailer tailer = chronicle.createTailer(); tailer.readDocument(wire -> wire.read(() -> "FirstName").text(first)); tailer.readDocument(wire -> wire.read(() -> "Surname").text(surname)); Assert.assertEquals("Steve Jobs", first + " " + surname); } }
@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 shouldIndicateNoProcessIdWhenDocumentIsComplete() throws IOException { try (final RollingChronicleQueue queue = ChronicleQueue.singleBuilder(getTmpDir()). testBlockSize(). build()) { final QueueInspector inspector = new QueueInspector(queue); final ExcerptAppender appender = queue.acquireAppender(); appender.writeDocument(37L, ValueOut::int64); try (final DocumentContext ctx = appender.writingDocument()) { ctx.wire().write("foo").int32(17L); } final int writingThreadId = inspector.getWritingThreadId(); assertThat(writingThreadId, is(not(OS.getProcessId()))); assertThat(QueueInspector.isValidThreadId(writingThreadId), is(false)); } }
@Test public void shouldHandleLargeEpoch() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .epoch(System.currentTimeMillis()) .epoch(1284739200000L) .rollCycle(DAILY) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); appender.writeDocument(wire -> wire.write(() -> "key").text("value=v")); final ExcerptTailer excerptTailer = chronicle.createTailer().toStart(); assertThat(excerptTailer.readingDocument().isPresent(), is(true)); } }
/** * test that if we make EPOC the current time, then the cycle is == 0 * * @ */ @Test public void testEPOC() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .epoch(System.currentTimeMillis()) .rollCycle(RollCycles.HOURLY) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); appender.writeDocument(wire -> wire.write(() -> "key").text("value=v")); Assert.assertTrue(appender.cycle() == 0); } }
@Test public void shouldBeAbleToReadFromQueueWithNonZeroEpoch() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .epoch(System.currentTimeMillis()) .rollCycle(RollCycles.DAILY) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); appender.writeDocument(wire -> wire.write(() -> "key").text("value=v")); Assert.assertTrue(appender.cycle() == 0); final ExcerptTailer excerptTailer = chronicle.createTailer().toStart(); assertThat(excerptTailer.readingDocument().isPresent(), is(true)); } }
@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)); } }
@Test public void testToEnd2() { File dir = getTmpDir(); try (ChronicleQueue chronicle = builder(dir, wireType) .build(); ChronicleQueue chronicle2 = builder(dir, wireType) .build()) { ExcerptAppender append = chronicle2.acquireAppender(); append.writeDocument(w -> w.write(() -> "test").text("before text")); ExcerptTailer tailer = chronicle.createTailer(); // move to the end even though it doesn't exist yet. tailer.toEnd(); append.writeDocument(w -> w.write(() -> "test").text("text")); assertTrue(tailer.readDocument(w -> w.read(() -> "test").text("text", Assert::assertEquals))); } }
@Test public void shouldReadBackwardFromEndOfQueueWhenDirectionIsSetAfterMoveToEnd() { try (final ChronicleQueue queue = builder(getTmpDir(), this.wireType) .rollCycle(TEST2_DAILY) .build()) { final ExcerptAppender appender = queue.acquireAppender(); appender.writeDocument(w -> w.writeEventName("hello").text("world")); final ExcerptTailer tailer = queue.createTailer(); tailer.toEnd(); tailer.direction(TailerDirection.BACKWARD); assertThat(tailer.readingDocument().isPresent(), is(true)); } }
@Test public void testNegativeEPOC() { for (int h = -14; h <= 14; h++) { try (final ChronicleQueue chronicle = builder(getTmpDir(), wireType) .epoch(TimeUnit.HOURS.toMillis(h)) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); appender.writeDocument(wire -> wire.write(() -> "key").text("value=v")); chronicle.createTailer() .readDocument(wire -> { assertEquals("value=v", wire.read("key").text()); }); } } }
@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 testZeroLengthMessage() { try (ChronicleQueue chronicle = builder(getTmpDir(), wireType) .rollCycle(TEST_DAILY) .build()) { ExcerptAppender appender = chronicle.acquireAppender(); appender.writeDocument(w -> { }); // System.out.println(chronicle.dump()); ExcerptTailer tailer = chronicle.createTailer(); try (DocumentContext dc = tailer.readingDocument()) { assertFalse(dc.wire().hasMore()); } } }
@Test public void testForwardFollowedBackBackwardTailer() { try (ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .rollCycle(TEST2_DAILY) .build()) { ExcerptAppender appender = chronicle.acquireAppender(); int entries = chronicle.rollCycle().defaultIndexSpacing() + 2; for (int i = 0; i < entries; i++) { int finalI = i; appender.writeDocument(w -> w.writeEventName("hello").text("world" + finalI)); } for (int i = 0; i < 3; i++) { readForward(chronicle, entries); readBackward(chronicle, entries); } } }
@Test public void testToEndBeforeWrite() { 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 (int i = 0; i < entries; i++) { tailer.toEnd(); int finalI = i; appender.writeDocument(w -> w.writeEventName("hello").text("world" + finalI)); tailer.readDocument(w -> w.read().text("world" + finalI, Assert::assertEquals)); } } }
@Test public void test() { File dir = getTmpDir(); try (final ChronicleQueue queue = SingleChronicleQueueBuilder.builder(dir, wireType) .testBlockSize() .build()) { final ExcerptTailer tailer = queue.createTailer(); assertFalse(tailer.readDocument(r -> r.read(TestKey.test).int32())); final ExcerptAppender appender = queue.acquireAppender(); appender.writeDocument(w -> w.write(TestKey.test).int32(1)); Jvm.pause(100); assertTrue(tailer.readDocument(r -> r.read(TestKey.test).int32())); } }
@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))); } } }