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