Search in sources :

Example 6 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class ChronicleReaderTest method shouldReadQueueWithNonDefaultRollCycle.

@Test
public void shouldReadQueueWithNonDefaultRollCycle() {
    Path path = DirectoryUtils.tempDir("shouldReadQueueWithNonDefaultRollCycle").toPath();
    path.toFile().mkdirs();
    try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).rollCycle(RollCycles.MINUTELY).testBlockSize().build()) {
        final ExcerptAppender excerptAppender = queue.acquireAppender();
        final MethodWriterBuilder<StringEvents> methodWriterBuilder = excerptAppender.methodWriterBuilder(StringEvents.class);
        methodWriterBuilder.recordHistory(true);
        final StringEvents events = methodWriterBuilder.build();
        for (int i = 0; i < 24; i++) {
            events.say(i % 2 == 0 ? "hello" : "goodbye");
        }
    }
    new ChronicleReader().withBasePath(path).withMessageSink(capturedOutput::add).execute();
    assertFalse(capturedOutput.isEmpty());
}
Also used : Path(java.nio.file.Path) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) Test(org.junit.Test)

Example 7 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class ServiceWrapperBuilder method outputReader.

@NotNull
public MethodReader outputReader(Object... impls) {
    SingleChronicleQueue queue = outputQueue();
    MethodReader reader = queue.createTailer().methodReader(impls);
    reader.closeIn(true);
    return reader;
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) MethodReader(net.openhft.chronicle.bytes.MethodReader) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class ContendedWriterTest method test.

private void test(String name, Config... configs) {
    System.out.println(name);
    File path = DirectoryUtils.tempDir(name);
    SingleChronicleQueue[] queues = new SingleChronicleQueue[configs.length];
    StartAndMonitor[] startAndMonitors = new StartAndMonitor[configs.length];
    try {
        for (int i = 0; i < configs.length; i++) {
            queues[i] = SingleChronicleQueueBuilder.binary(path).testBlockSize().progressOnContention(configs[i].progressOnContention).build();
            startAndMonitors[i] = new StartAndMonitor(queues[i], Integer.toString(i), configs[i].writePause, configs[i].pauseBetweenWrites);
        }
        // warmup
        Jvm.pause(5_000);
        running.set(false);
        Jvm.pause(50);
        running.set(true);
        for (int i = 0; i < configs.length; i++) {
            startAndMonitors[i] = new StartAndMonitor(queues[i], Integer.toString(i), configs[i].writePause, configs[i].pauseBetweenWrites);
        }
        Jvm.pause(Jvm.isDebug() ? 30_000 : 15_000);
        running.set(false);
        Jvm.pause(50);
        for (int i = 0; i < configs.length; i++) {
            System.out.println("thread" + i + " progress=" + configs[i].progressOnContention + " writePause=" + configs[i].writePause + " between=" + configs[i].pauseBetweenWrites + ": " + startAndMonitors[i].histo.toMicrosFormat());
        }
    } finally {
        Closeable.closeQuietly(queues);
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) File(java.io.File)

Example 9 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class CreateAtIndexTest method testWrittenAndReadIndexesAreTheSameOfTheFirstExcerpt.

// TODO: 2 or more threads soak test
@Test
public void testWrittenAndReadIndexesAreTheSameOfTheFirstExcerpt() throws Exception {
    String tmp = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    long expected;
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.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);
            }
        }
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 10 with SingleChronicleQueue

use of net.openhft.chronicle.queue.impl.single.SingleChronicleQueue in project Chronicle-Queue by OpenHFT.

the class CreateAtIndexTest method testWriteBytesWithIndex.

@Test
public void testWriteBytesWithIndex() throws Exception {
    String tmp = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().rollCycle(TEST_DAILY).build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        appender.writeBytes(0x421d00000000L, HELLO_WORLD);
        appender.writeBytes(0x421d00000001L, HELLO_WORLD);
    }
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        String before = queue.dump();
        appender.writeBytes(0x421d00000000L, HELLO_WORLD);
        String after = queue.dump();
        assertEquals(before, after);
    }
    boolean runIfAssertsOn = false;
    // assert runIfAssertsOn = true;
    if (runIfAssertsOn) {
        try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
            InternalAppender appender = (InternalAppender) queue.acquireAppender();
            String before = queue.dump();
            try {
                appender.writeBytes(0x421d00000000L, Bytes.from("hellooooo world"));
                fail();
            } catch (IllegalStateException e) {
            // expected
            }
            String after = queue.dump();
            assertEquals(before, after);
        }
    }
    // try too far
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        try {
            appender.writeBytes(0x421d00000003L, HELLO_WORLD);
            fail();
        } catch (IllegalStateException e) {
            assertEquals("Unable to move to index 421d00000003 beyond the end of the queue", e.getMessage());
        }
    }
    try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(tmp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        appender.writeBytes(0x421d00000002L, HELLO_WORLD);
        appender.writeBytes(0x421d00000003L, HELLO_WORLD);
    }
    try {
        IOTools.deleteDirWithFiles(tmp, 2);
    } catch (IORuntimeException ignored) {
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) InternalAppender(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueExcerpts.InternalAppender) Test(org.junit.Test)

Aggregations

SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)36 Test (org.junit.Test)24 File (java.io.File)13 MethodReader (net.openhft.chronicle.bytes.MethodReader)10 DocumentContext (net.openhft.chronicle.wire.DocumentContext)10 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)7 Ignore (org.junit.Ignore)6 OS (net.openhft.chronicle.core.OS)4 IOTools (net.openhft.chronicle.core.io.IOTools)4 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)4 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)4 Path (java.nio.file.Path)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 NotNull (org.jetbrains.annotations.NotNull)3 Assert (org.junit.Assert)3 Before (org.junit.Before)3 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 PrintStream (java.io.PrintStream)2