Search in sources :

Example 26 with SingleChronicleQueue

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

the class LastIndexAppendedTest method testLastIndexAppendedAcrossRestarts.

@Test
public void testLastIndexAppendedAcrossRestarts() throws Exception {
    String path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    for (int i = 0; i < 5; i++) {
        try (SingleChronicleQueue queue = ChronicleQueueBuilder.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) {
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 27 with SingleChronicleQueue

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

the class LastIndexAppendedTest method testTwoAppenders.

@Test
public void testTwoAppenders() throws Exception {
    File path = DirectoryUtils.tempDir("testTwoAppenders");
    long a_index;
    try (SingleChronicleQueue appender_queue = ChronicleQueueBuilder.single(path).testBlockSize().rollCycle(TEST_DAILY).build()) {
        ExcerptAppender appender = appender_queue.acquireAppender();
        for (int i = 0; i < 5; i++) {
            appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m -> m.write("msg").text("hello world ")));
        }
        a_index = appender.lastIndexAppended();
    }
    SingleChronicleQueue tailer_queue = ChronicleQueueBuilder.single(path).testBlockSize().rollCycle(TEST_DAILY).build();
    ExcerptTailer tailer = tailer_queue.createTailer();
    tailer = tailer.toStart();
    long t_index;
    t_index = doRead(tailer, 5);
    assertEquals(a_index, t_index);
    System.out.println("Continue appending");
    try (SingleChronicleQueue appender_queue = ChronicleQueueBuilder.single(path).testBlockSize().rollCycle(TEST_DAILY).build()) {
        ExcerptAppender appender = appender_queue.acquireAppender();
        for (int i = 0; i < 5; i++) {
            appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m -> m.write("msg").text("hello world2 ")));
        }
        a_index = appender.lastIndexAppended();
        assertTrue(a_index > t_index);
    }
    // if the tailer continues as well it should see the 5 new messages
    System.out.println("Reading messages added");
    t_index = doRead(tailer, 5);
    assertEquals(a_index, t_index);
    // if the tailer is expecting to read all the message again
    System.out.println("Reading all the messages again");
    tailer.toStart();
    t_index = doRead(tailer, 10);
    assertEquals(a_index, t_index);
    try {
        IOTools.deleteDirWithFiles(path, 2);
    } catch (Exception index) {
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) IOTools(net.openhft.chronicle.core.io.IOTools) TEST_DAILY(net.openhft.chronicle.queue.RollCycles.TEST_DAILY) Test(org.junit.Test) OS(net.openhft.chronicle.core.OS) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) File(java.io.File) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) File(java.io.File) Test(org.junit.Test)

Example 28 with SingleChronicleQueue

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

the class MessageReaderWriterTest method testWriteWhileReading.

@Test
public void testWriteWhileReading() {
    ClassAliasPool.CLASS_ALIASES.addAlias(Message1.class);
    ClassAliasPool.CLASS_ALIASES.addAlias(Message2.class);
    File path1 = DirectoryUtils.tempDir("testWriteWhileReading1");
    File path2 = DirectoryUtils.tempDir("testWriteWhileReading2");
    try (SingleChronicleQueue queue1 = SingleChronicleQueueBuilder.binary(path1).testBlockSize().build();
        SingleChronicleQueue queue2 = SingleChronicleQueueBuilder.binary(path2).testBlockSize().build()) {
        MethodReader reader2 = queue1.createTailer().methodReader(ObjectUtils.printAll(MessageListener.class));
        MessageListener writer2 = queue2.acquireAppender().methodWriter(MessageListener.class);
        MessageListener processor = new MessageProcessor(writer2);
        MethodReader reader1 = queue1.createTailer().methodReader(processor);
        MessageListener writer1 = queue1.acquireAppender().methodWriter(MessageListener.class);
        for (int i = 0; i < 3; i++) {
            // write a message
            writer1.method1(new Message1("hello"));
            writer1.method2(new Message2(234));
            // read those messages
            assertTrue(reader1.readOne());
            assertTrue(reader1.readOne());
            assertFalse(reader1.readOne());
            // read the produced messages
            assertTrue(reader2.readOne());
            assertTrue(reader2.readOne());
            assertFalse(reader2.readOne());
        }
    // System.out.println(queue1.dump());
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader) Test(org.junit.Test)

Example 29 with SingleChronicleQueue

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

the class OvertakeTest method threadingTest.

@Test
@Ignore("TODO FIX")
public void threadingTest() throws Exception {
    System.out.println("Continue appending");
    ExecutorService execService = Executors.newFixedThreadPool(2);
    SynchronousQueue<Long> sync = new SynchronousQueue<>();
    long t_index;
    MyAppender myapp = new MyAppender(sync);
    Future<Long> f = execService.submit(myapp);
    SingleChronicleQueue tailer_queue = SingleChronicleQueueBuilder.binary(path).testBlockSize().buffered(false).build();
    t_index = 0;
    MyTailer mytailer = new MyTailer(tailer_queue, t_index, sync);
    Future<Long> f2 = execService.submit(mytailer);
    t_index = f2.get(10, TimeUnit.SECONDS);
    a_index = f.get(10, TimeUnit.SECONDS);
    assertTrue(a_index == t_index);
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 30 with SingleChronicleQueue

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

the class OrderManagerTest method testWithQueue.

@Test
public void testWithQueue() {
    File queuePath = new File(OS.TARGET, "testWithQueue-" + System.nanoTime());
    try {
        try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(queuePath).testBlockSize().build()) {
            OrderIdeaListener orderManager = queue.acquireAppender().methodWriter(OrderIdeaListener.class, MarketDataListener.class);
            SidedMarketDataCombiner combiner = new SidedMarketDataCombiner((MarketDataListener) orderManager);
            // events in
            // not expected to trigger
            orderManager.onOrderIdea(new OrderIdea("EURUSD", Side.Buy, 1.1180, 2e6));
            combiner.onSidedPrice(new SidedPrice("EURUSD", 123456789000L, Side.Sell, 1.1172, 2e6));
            combiner.onSidedPrice(new SidedPrice("EURUSD", 123456789100L, Side.Buy, 1.1160, 2e6));
            combiner.onSidedPrice(new SidedPrice("EURUSD", 123456789100L, Side.Buy, 1.1167, 2e6));
            // expected to trigger
            orderManager.onOrderIdea(new OrderIdea("EURUSD", Side.Buy, 1.1165, 1e6));
        }
        // what we expect to happen
        OrderListener listener = createMock(OrderListener.class);
        listener.onOrder(new Order("EURUSD", Side.Buy, 1.1167, 1_000_000));
        replay(listener);
        try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(queuePath).testBlockSize().build()) {
            // build our scenario
            OrderManager orderManager = new OrderManager(listener);
            MethodReader reader = queue.createTailer().methodReader(orderManager);
            for (int i = 0; i < 5; i++) assertTrue(reader.readOne());
            assertFalse(reader.readOne());
        // System.out.println(queue.dump());
        }
        verify(listener);
    } finally {
        try {
            IOTools.shallowDeleteDirWithFiles(queuePath);
        } catch (Exception e) {
        }
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader) 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