use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class WriteBytesTest method shouldWriteBytes.
@Test
public void shouldWriteBytes() throws IOException {
try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpDir.newFolder()).testBlockSize().build()) {
final ExcerptAppender appender = queue.acquireAppender();
final HeapBytesStore<ByteBuffer> store = HeapBytesStore.uninitialized();
store.init(ByteBuffer.wrap(PAYLOAD));
appender.writeBytes(store);
final ExcerptTailer tailer = queue.createTailer();
final HeapBytesStore<byte[]> copy = HeapBytesStore.uninitialized();
copy.init(new byte[4]);
tailer.readBytes(copy.bytesForWrite());
assertTrue(Arrays.equals(PAYLOAD, copy.underlyingObject()));
}
}
use of net.openhft.chronicle.queue.ExcerptTailer 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()));
}
}
use of net.openhft.chronicle.queue.ExcerptTailer 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) {
}
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class AppenderFileHandleLeakTest method tailerShouldReleaseFileHandlesAsQueueRolls.
@Test
public void tailerShouldReleaseFileHandlesAsQueueRolls() throws Exception {
System.gc();
Thread.sleep(100);
assumeThat(OS.isLinux(), is(true));
final int messagesPerThread = 10;
try (SingleChronicleQueue queue = createQueue(currentTime::get)) {
final long openFileHandleCount = countFileHandlesOfCurrentProcess();
final List<Path> fileHandlesAtStart = new ArrayList<>(lastFileHandles);
final List<Future<Boolean>> futures = new LinkedList<>();
for (int i = 0; i < THREAD_COUNT; i++) {
futures.add(threadPool.submit(() -> {
for (int j = 0; j < messagesPerThread; j++) {
writeMessage(j, queue);
currentTime.addAndGet(100);
}
return Boolean.TRUE;
}));
}
for (Future<Boolean> future : futures) {
assertThat(future.get(1, TimeUnit.MINUTES), is(true));
}
waitForFileHandleCountToDrop(openFileHandleCount, fileHandlesAtStart);
fileHandlesAtStart.clear();
final long tailerOpenFileHandleCount = countFileHandlesOfCurrentProcess();
final ExcerptTailer tailer = queue.createTailer();
tailer.toStart();
final int expectedMessageCount = THREAD_COUNT * messagesPerThread;
int messageCount = 0;
storeFileListener.reset();
while (true) {
try (final DocumentContext ctx = tailer.readingDocument()) {
if (!ctx.isPresent()) {
break;
}
messageCount++;
}
}
assertThat(messageCount, is(expectedMessageCount));
assertThat(storeFileListener.toString(), storeFileListener.releasedCount, is(withinDelta(storeFileListener.acquiredCount, 3)));
waitForFileHandleCountToDrop(tailerOpenFileHandleCount, fileHandlesAtStart);
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class AppenderFileHandleLeakTest method readMessage.
private static void readMessage(final SingleChronicleQueue queue, final boolean manuallyReleaseResources, final Consumer<ExcerptTailer> refHolder) {
final Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer();
try {
final ExcerptTailer tailer = queue.createTailer();
while (bytes.isEmpty()) {
tailer.toStart().readBytes(bytes);
}
refHolder.accept(tailer);
assertThat(Math.signum(bytes.readInt()) >= 0, is(true));
if (manuallyReleaseResources) {
try {
((SingleChronicleQueueExcerpts.StoreTailer) tailer).releaseResources();
} catch (RuntimeException e) {
// ignore
}
}
} finally {
bytes.release();
}
}
Aggregations