private void appendToQueue(final ChronicleQueue queue) { for (int i = 0; i < 31; i++) { final ExcerptAppender appender = queue.acquireAppender(); if (queue.isClosed()) return; try (final DocumentContext dc = appender.writingDocument()) { dc.wire().write("foo"); } } }
/** * Add a test message with the given ExcerptAppender and return the index position of the entry * * @param appender ExceptAppender * @param msg test message * @return index position of the entry */ private long appendEntry(@NotNull final ExcerptAppender appender, String msg) { DocumentContext dc = appender.writingDocument(); try { dc.wire().write().text(msg); return dc.index(); } finally { dc.close(); } }
@Test public void tailerRollBackTest() { final File source = DirectoryUtils.tempDir("testCopyQueue-source"); try (final ChronicleQueue q = binary(source).build()) { try (DocumentContext dc = q.acquireAppender().writingDocument()) { dc.wire().write("hello").text("hello-world"); } try (DocumentContext dc = q.acquireAppender().writingDocument()) { dc.wire().write("hello2").text("hello-world-2"); } } }
@Test public void testDtoAbstractMarshallable() { File tmp = DirectoryUtils.tempDir("abstractBytesMarshalTest"); DtoAbstractMarshallable dto = new DtoAbstractMarshallable(); dto.age = 45; dto.name.append("rob"); try (ChronicleQueue q = ChronicleQueue.singleBuilder(tmp).build()) { try (DocumentContext dc = q.acquireAppender().writingDocument()) { dc.wire().write("who").object(dto); } try (DocumentContext dc = q.createTailer().readingDocument()) { String yaml = dc.toString(); System.out.println(yaml); DtoAbstractMarshallable who = (DtoAbstractMarshallable) dc.wire().read("who").object(); System.out.println(who); Assert.assertTrue(yaml.contains(who.toString())); } } }
@Test public void testCleanupDir() { File tmpDir = getTmpDir(); try (final ChronicleQueue queue = builder(tmpDir, wireType) .build()) { final ExcerptAppender appender = queue.acquireAppender(); try (DocumentContext dc = appender.writingDocument()) { dc.wire().write("hello").text("world"); } } DirectoryUtils.deleteDir(tmpDir); if (OS.isWindows()) { System.err.println("#460 Directory clean up not supported on Windows"); } else { Assert.assertFalse(tmpDir.exists()); } }
@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 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) { } }
@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()); } }
private void createQueueAndWriteData(TimeProvider timeProvider, File path) { final ChronicleQueue queue = SingleChronicleQueueBuilder .binary(path) .testBlockSize() .rollCycle(RollCycles.TEST_DAILY) .timeProvider(timeProvider) .build(); ExcerptAppender excerptAppender = queue.acquireAppender(); try (DocumentContext dc = excerptAppender.writingDocument(false)) { dc.wire().write(() -> "test").int64(0); } } }
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 testFromSizePrefixedBlobs() { try (final ChronicleQueue queue = binary(getTmpDir()) .build()) { try (DocumentContext dc = queue.acquireAppender().writingDocument()) { dc.wire().write("some").text("data"); } String s = null; DocumentContext dc0; try (DocumentContext dc = queue.createTailer().readingDocument()) { s = Wires.fromSizePrefixedBlobs(dc); if (!encryption) Assert.assertTrue(s.contains("some: data")); dc0 = dc; } String out = Wires.fromSizePrefixedBlobs(dc0); Assert.assertEquals(s, out); } }
@Test(expected = IllegalStateException.class) public void shouldBlowUpIfTryingToCreateQueueWithUnparseableRollCycle() { File tmpDir = getTmpDir(); try (final ChronicleQueue queue = builder(tmpDir, wireType).rollCycle(new RollCycleDefaultingTest.MyRollcycle()).build()) { try (DocumentContext documentContext = queue.acquireAppender().writingDocument()) { documentContext.wire().write("somekey").text("somevalue"); } } try (final ChronicleQueue ignored = builder(tmpDir, wireType).rollCycle(HOURLY).build()) { } }
@Test public void testDtoBytesMarshallable() { File tmp = DirectoryUtils.tempDir("abstractBytesMarshalTest"); DtoBytesMarshallable dto = new DtoBytesMarshallable(); dto.age = 45; dto.name.append("rob"); try (ChronicleQueue q = ChronicleQueue.singleBuilder(tmp).build()) { try (DocumentContext dc = q.acquireAppender().writingDocument()) { dc.wire().write("who").object(dto); } try (DocumentContext dc = q.createTailer().readingDocument()) { DtoBytesMarshallable who = (DtoBytesMarshallable) dc.wire().read("who").object(); Assert.assertEquals("!net.openhft.chronicle.queue.DtoBytesMarshallableTest$DtoBytesMarshallable {\n" + " name: rob,\n" + " age: 45\n" + "}\n", who.toString()); } } }
@Test public void shouldDetermineWritingProcessIdWhenDocumentIsNotComplete() 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(); // commentedVanillaMsgSequenceHandler out as fails on Mac OS // assertThat(writingThreadId, is(Affinity.getThreadId())); // we no longer write header before the document is closed // assertThat(QueueInspector.isValidThreadId(writingThreadId), is(true)); } } }
@Test public void shouldNotBlowUpIfTryingToCreateQueueWithIncorrectRollCycle() { File tmpDir = getTmpDir(); try (final ChronicleQueue queue = builder(tmpDir, wireType).rollCycle(DAILY).build()) { try (DocumentContext documentContext = queue.acquireAppender().writingDocument()) { documentContext.wire().write("somekey").text("somevalue"); } } // we don't store which RollCycles enum was used and we try and match by format string, we // match the first RollCycles with the same format string, which may not // be the RollCycles it was written with try (final ChronicleQueue ignored = builder(tmpDir, wireType).rollCycle(HOURLY).build()) { assertEquals(TEST_DAILY, ignored.rollCycle()); } }
@Test public void shouldOverrideDifferentEpoch() { File tmpDir = getTmpDir(); try (final ChronicleQueue queue = builder(tmpDir, wireType).rollCycle(TEST_SECONDLY).epoch(100).build()) { try (DocumentContext documentContext = queue.acquireAppender().writingDocument()) { documentContext.wire().write("somekey").text("somevalue"); } } try (final ChronicleQueue ignored = builder(tmpDir, wireType).rollCycle(TEST_SECONDLY).epoch(10).build()) { assertEquals(100, ((SingleChronicleQueue) ignored).epoch()); } }
@Test public void testReadingWritingMarshallableDocument() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { MyMarshable myMarshable = new MyMarshable(); final ExcerptAppender appender = chronicle.acquireAppender(); try (DocumentContext dc = appender.writingDocument()) { dc.wire().write("myMarshable").typedMarshallable(myMarshable); } ExcerptTailer tailer = chronicle.createTailer(); try (DocumentContext dc = tailer.readingDocument()) { Assert.assertEquals(myMarshable, dc.wire().read(() -> "myMarshable").typedMarshallable()); } } }
@Test public void testReadingSecondDocumentNotExist() { try (final ChronicleQueue chronicle = builder(getTmpDir(), this.wireType) .build()) { final ExcerptAppender appender = chronicle.acquireAppender(); try (DocumentContext dc = appender.writingDocument()) { dc.wire().write(() -> "FirstName").text("Quartilla"); } final ExcerptTailer tailer = chronicle.createTailer(); try (DocumentContext dc = tailer.readingDocument()) { String text = dc.wire().read(() -> "FirstName").text(); Assert.assertEquals("Quartilla", text); } try (DocumentContext dc = tailer.readingDocument()) { assertFalse(dc.isPresent()); } } }
@Test public void testReadWriteHourly() { File tmpDir = getTmpDir(); try (final ChronicleQueue qAppender = builder(tmpDir, wireType).rollCycle(HOURLY).build()) { try (DocumentContext documentContext = qAppender.acquireAppender().writingDocument()) { documentContext.wire().write("somekey").text("somevalue"); } } try (final ChronicleQueue qTailer = builder(tmpDir, wireType).rollCycle(HOURLY).build()) { try (DocumentContext documentContext2 = qTailer.createTailer().readingDocument()) { String str = documentContext2.wire().read("somekey").text(); Assert.assertEquals("somevalue", str); } } }