Search in sources :

Example 16 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class MethodReaderObjectReuseTest method testOneOne.

@Test
public void testOneOne() {
    ClassAliasPool.CLASS_ALIASES.addAlias(PingDTO.class);
    try (ChronicleQueue cq = ChronicleQueueBuilder.single(OS.TARGET + "/MethodReaderObjectReuseTest-" + System.nanoTime()).build()) {
        PingDTO.constructionExpected++;
        PingDTO pdtio = new PingDTO();
        PingDTO.constructionExpected++;
        Pinger pinger = cq.acquireAppender().methodWriter(Pinger.class);
        for (int i = 0; i < 5; i++) {
            pinger.ping(pdtio);
            assertEquals(PingDTO.constructionExpected, PingDTO.constructionCounter);
            pdtio.bytes.append("hi");
        }
        StringBuilder sb = new StringBuilder();
        PingDTO.constructionExpected++;
        MethodReader reader = cq.createTailer().methodReader((Pinger) pingDTO -> sb.append("ping ").append(pingDTO));
        assertEquals(PingDTO.constructionExpected, PingDTO.constructionCounter);
        while (reader.readOne()) ;
        assertEquals("ping !PingDTO {\n" + "  bytes: \"\"\n" + "}\n" + "ping !PingDTO {\n" + "  bytes: hi\n" + "}\n" + "ping !PingDTO {\n" + "  bytes: hihi\n" + "}\n" + "ping !PingDTO {\n" + "  bytes: hihihi\n" + "}\n" + "ping !PingDTO {\n" + "  bytes: hihihihi\n" + "}\n", sb.toString());
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) Bytes(net.openhft.chronicle.bytes.Bytes) AbstractMarshallable(net.openhft.chronicle.wire.AbstractMarshallable) ClassAliasPool(net.openhft.chronicle.core.pool.ClassAliasPool) Test(org.junit.Test) OS(net.openhft.chronicle.core.OS) Assert.assertEquals(org.junit.Assert.assertEquals) MethodReader(net.openhft.chronicle.bytes.MethodReader) Test(org.junit.Test)

Example 17 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class StoreTailerTest method shouldConsiderSourceIdWhenDeterminingLastWrittenIndex.

@Test
public void shouldConsiderSourceIdWhenDeterminingLastWrittenIndex() throws Exception {
    final SingleChronicleQueue firstInputQueue = createQueue(dataDirectory, RollCycles.TEST_DAILY, 1, "firstInputQueue");
    // different RollCycle means that indicies are not identical to firstInputQueue
    final SingleChronicleQueue secondInputQueue = createQueue(dataDirectory, RollCycles.TEST_SECONDLY, 2, "secondInputQueue");
    final SingleChronicleQueue outputQueue = createQueue(dataDirectory, RollCycles.TEST_DAILY, 0, "outputQueue");
    ;
    final StringEvents firstWriter = firstInputQueue.acquireAppender().methodWriterBuilder(StringEvents.class).get();
    final HelloWorld secondWriter = secondInputQueue.acquireAppender().methodWriterBuilder(HelloWorld.class).get();
    // generate some data in the input queues
    firstWriter.onEvent("one");
    firstWriter.onEvent("two");
    secondWriter.hello("thirteen");
    secondWriter.hello("thirtyOne");
    final StringEvents eventSink = outputQueue.acquireAppender().methodWriterBuilder(StringEvents.class).recordHistory(true).get();
    final CapturingStringEvents outputWriter = new CapturingStringEvents(eventSink);
    final MethodReader firstMethodReader = firstInputQueue.createTailer().methodReader(outputWriter);
    final MethodReader secondMethodReader = secondInputQueue.createTailer().methodReader(outputWriter);
    // replay events from the inputs into the output queue
    assertThat(firstMethodReader.readOne(), is(true));
    assertThat(firstMethodReader.readOne(), is(true));
    assertThat(secondMethodReader.readOne(), is(true));
    assertThat(secondMethodReader.readOne(), is(true));
    // ensures that tailer is not moved to index from the incorrect source
    secondInputQueue.createTailer().afterLastWritten(outputQueue);
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) HelloWorld(net.openhft.chronicle.queue.service.HelloWorld) Test(org.junit.Test)

Example 18 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader 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)

Example 19 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class HelloWorldTest method testWithAsQueueService.

@Test
public void testWithAsQueueService() {
    // acts as three processes in one test
    // process A writes to the HelloWorld interface.
    // process B read fromt he HelloWorld interface and writes to the
    String input = OS.TARGET + "/input-" + System.nanoTime();
    String output = OS.TARGET + "/output-" + System.nanoTime();
    HelloReplier replier = createMock(HelloReplier.class);
    replier.reply("Hello April");
    replier.reply("Hello June");
    replay(replier);
    ServiceWrapperBuilder<HelloReplier> builder = ServiceWrapperBuilder.serviceBuilder(input, output, HelloReplier.class, HelloWorldImpl::new).inputSourceId(1).outputSourceId(2);
    try (CloseableHelloWorld helloWorld = builder.inputWriter(CloseableHelloWorld.class);
        MethodReader replyReader = builder.outputReader(replier);
        ServiceWrapper helloWorldService = builder.get()) {
        helloWorld.hello("April");
        helloWorld.hello("June");
        // System.out.println(helloWorldService.inputQueues()[0].dump());
        for (int i = 0; i < 2; i++) {
            while (!replyReader.readOne()) {
                Thread.yield();
            }
        }
        // System.out.println(helloWorldService.outputQueue().dump());
        verify(replier);
    } finally {
        try {
            IOTools.deleteDirWithFiles(new File(input), 2);
            IOTools.deleteDirWithFiles(new File(output), 2);
        } catch (IORuntimeException e) {
            e.printStackTrace();
        }
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) MethodReader(net.openhft.chronicle.bytes.MethodReader) File(java.io.File) Test(org.junit.Test)

Example 20 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class PublishDeltaGenerator method main.

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        throw new IllegalArgumentException("Usage: <program> [resource-name]");
    }
    Jvm.setExceptionHandlers((c, m, t) -> {
        System.out.println(m);
    }, (c, m, t) -> {
        System.out.println(m);
        t.printStackTrace();
    }, (c, m, t) -> System.out.println(m));
    final ConfigParser configParser = new ConfigParser(args[0]);
    final List<StageConfig> allStageConfigs = configParser.getAllStageConfigs();
    final StageConfig lastStageConfig = allStageConfigs.get(allStageConfigs.size() - 1);
    try (final SingleChronicleQueue pubQueue = SingleChronicleQueueBuilder.binary(configParser.getPublisherConfig().outputDir()).build();
        final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(lastStageConfig.getOutputPath()).build();
        final Writer resultsWriter = new FileWriter("publish-deltas.txt", false);
        final Writer s0Pub = new FileWriter("s0-deltas.txt", false);
        final Writer s1Pub = new FileWriter("s1-deltas.txt", false);
        final Writer s0s2Pub = new FileWriter("s0-s2-deltas.txt", false);
        final Writer s1s2Pub = new FileWriter("s1-s2-deltas.txt", false)) {
        final MethodReader reader = pubQueue.createTailer().methodReader(new CapturingReceiver(resultsWriter, m -> m.publishNanos));
        while (reader.readOne()) {
        // report
        }
        final MethodReader methodReader = queue.createTailer().methodReader(new DelegatingReceiver(new CapturingReceiver(s0Pub, m -> m.t0), new CapturingReceiver(s1Pub, m -> m.t1), new CapturingReceiver(s0s2Pub, m -> m.t2, 5), new CapturingReceiver(s1s2Pub, m -> m.t2, 6)));
        while (methodReader.readOne()) {
        // report
        }
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) FileWriter(java.io.FileWriter) LocalDateTime(java.time.LocalDateTime) IOException(java.io.IOException) EightyByteMessage(net.openhft.load.messages.EightyByteMessage) Jvm(net.openhft.chronicle.core.Jvm) TimeUnit(java.util.concurrent.TimeUnit) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) List(java.util.List) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) StageConfig(net.openhft.load.config.StageConfig) DateTimeFormatter(java.time.format.DateTimeFormatter) Writer(java.io.Writer) ZoneOffset(java.time.ZoneOffset) ToLongFunction(java.util.function.ToLongFunction) ConfigParser(net.openhft.load.config.ConfigParser) FileWriter(java.io.FileWriter) ConfigParser(net.openhft.load.config.ConfigParser) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) MethodReader(net.openhft.chronicle.bytes.MethodReader) StageConfig(net.openhft.load.config.StageConfig) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Aggregations

MethodReader (net.openhft.chronicle.bytes.MethodReader)22 Test (org.junit.Test)13 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)10 File (java.io.File)9 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 IOException (java.io.IOException)3 OS (net.openhft.chronicle.core.OS)3 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)3 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)3 ConfigParser (net.openhft.load.config.ConfigParser)3 StageConfig (net.openhft.load.config.StageConfig)3 FileWriter (java.io.FileWriter)2 Writer (java.io.Writer)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 IOTools (net.openhft.chronicle.core.io.IOTools)2 RollCycles (net.openhft.chronicle.queue.RollCycles)2 MessageHistory (net.openhft.chronicle.wire.MessageHistory)2 Assert (org.junit.Assert)2 Assert.assertEquals (org.junit.Assert.assertEquals)2