Search in sources :

Example 6 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender in project Chronicle-Queue by OpenHFT.

the class Queue28Test method test.

/*
     * Tailer doesn't work if created before the appender
     *
     * See https://higherfrequencytrading.atlassian.net/browse/QUEUE-28
     */
@Test
public void test() throws IOException, InterruptedException {
    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()));
    }
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Arrays(java.util.Arrays) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Collection(java.util.Collection) RunWith(org.junit.runner.RunWith) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) WireType(net.openhft.chronicle.wire.WireType) Jvm(net.openhft.chronicle.core.Jvm) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) ChronicleQueueTestBase(net.openhft.chronicle.queue.ChronicleQueueTestBase) Assert.assertFalse(org.junit.Assert.assertFalse) After(org.junit.After) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Parameterized(org.junit.runners.Parameterized) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 7 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender in project Chronicle-Queue by OpenHFT.

the class OrderManagerTest method testRestartingAService.

/*
    Fails when all the tests are run, but not this test alone.
     */
@Ignore("TODO FIX")
@Test
public void testRestartingAService() {
    File queuePath = new File(OS.TARGET, "testRestartingAService-" + System.nanoTime());
    File queuePath2 = new File(OS.TARGET, "testRestartingAService-down-" + System.nanoTime());
    try {
        try (SingleChronicleQueue out = SingleChronicleQueueBuilder.binary(queuePath).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
            SidedMarketDataListener combiner = out.acquireAppender().methodWriterBuilder(SidedMarketDataListener.class).recordHistory(true).get();
            combiner.onSidedPrice(new SidedPrice("EURUSD1", 123456789000L, Side.Sell, 1.1172, 2e6));
            combiner.onSidedPrice(new SidedPrice("EURUSD2", 123456789100L, Side.Buy, 1.1160, 2e6));
            for (int i = 2; i < 10; i += 2) {
                combiner.onSidedPrice(new SidedPrice("EURUSD3", 123456789100L, Side.Sell, 1.1173, 2.5e6));
                combiner.onSidedPrice(new SidedPrice("EURUSD4", 123456789100L, Side.Buy, 1.1167, 1.5e6));
            }
        }
        // TODO FIx for more.
        for (int i = 0; i < 10; i++) {
            // read one message at a time
            try (SingleChronicleQueue in = SingleChronicleQueueBuilder.binary(queuePath).testBlockSize().sourceId(1).build();
                SingleChronicleQueue out = SingleChronicleQueueBuilder.binary(queuePath2).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
                ExcerptAppender excerptAppender = out.acquireAppender();
                MarketDataListener mdListener = excerptAppender.methodWriterBuilder(MarketDataListener.class).recordHistory(true).get();
                SidedMarketDataCombiner combiner = new SidedMarketDataCombiner(mdListener);
                ExcerptTailer tailer = in.createTailer().afterLastWritten(out);
                assertEquals(i, in.rollCycle().toSequenceNumber(tailer.index()));
                MethodReader reader = tailer.methodReader(combiner);
                // System.out.println("#### IN\n" + in.dump());
                // System.out.println("#### OUT:\n" + out.dump());
                assertTrue("i: " + i, reader.readOne());
            }
        }
    } finally {
        try {
            IOTools.shallowDeleteDirWithFiles(queuePath);
            IOTools.shallowDeleteDirWithFiles(queuePath2);
        } catch (Exception e) {
        }
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender in project Chronicle-Queue by OpenHFT.

the class ChronicleReaderTest method before.

@Before
public void before() throws Exception {
    dataDir = DirectoryUtils.tempDir(ChronicleReaderTest.class.getSimpleName()).toPath();
    try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dataDir).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");
        }
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) Before(org.junit.Before)

Example 9 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender 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 10 with ExcerptAppender

use of net.openhft.chronicle.queue.ExcerptAppender in project Chronicle-Queue by OpenHFT.

the class DocumentOrderingTest method codeWithinPriorDocumentMustExecuteBeforeSubsequentDocumentWhenQueueIsEmpty.

@Test
public void codeWithinPriorDocumentMustExecuteBeforeSubsequentDocumentWhenQueueIsEmpty() throws Exception {
    try (final SingleChronicleQueue queue = builder(DirectoryUtils.tempDir("document-ordering"), 3_000L).build()) {
        final ExcerptAppender excerptAppender = queue.acquireAppender();
        final Future<RecordInfo> otherDocumentWriter;
        try (final DocumentContext documentContext = excerptAppender.writingDocument()) {
            // move time to beyond the next cycle
            clock.addAndGet(TimeUnit.SECONDS.toMillis(2L));
            otherDocumentWriter = attemptToWriteDocument(queue);
            // stall this thread, other thread should not be able to advance,
            // since this DocumentContext is still open
            LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2L));
            documentContext.wire().getValueOut().int32(counter.getAndIncrement());
        }
        assertEquals(1, otherDocumentWriter.get(5L, TimeUnit.SECONDS).counterValue);
        final ExcerptTailer tailer = queue.createTailer();
        expectValue(0, tailer);
        expectValue(1, tailer);
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Aggregations

ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)63 Test (org.junit.Test)55 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)34 File (java.io.File)33 DocumentContext (net.openhft.chronicle.wire.DocumentContext)28 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)21 MappedFile (net.openhft.chronicle.bytes.MappedFile)15 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)6 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 Ignore (org.junit.Ignore)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ValueOut (net.openhft.chronicle.wire.ValueOut)4 Wire (net.openhft.chronicle.wire.Wire)4 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 ExecutorService (java.util.concurrent.ExecutorService)3 Bytes (net.openhft.chronicle.bytes.Bytes)3 ChronicleQueueTestBase (net.openhft.chronicle.queue.ChronicleQueueTestBase)3