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());
}
}
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);
}
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) {
}
}
}
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();
}
}
}
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
}
}
}
Aggregations