Search in sources :

Example 16 with SingleChronicleQueue

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);
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) Test(org.junit.Test)

Example 17 with SingleChronicleQueue

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);
        }
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Before(org.junit.Before)

Example 18 with SingleChronicleQueue

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();
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 19 with SingleChronicleQueue

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);
            }
        }
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)

Example 20 with SingleChronicleQueue

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));
        }
    }
}
Also used : Pretoucher(net.openhft.chronicle.queue.impl.single.Pretoucher) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) File(java.io.File)

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