use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class ExclusivePublicationTest method shouldOfferTwoBuffersFromConcurrentExclusivePublications.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldOfferTwoBuffersFromConcurrentExclusivePublications(final String channel) {
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
ExclusivePublication publicationOne = aeron.addExclusivePublication(channel, STREAM_ID);
ExclusivePublication publicationTwo = aeron.addExclusivePublication(channel, STREAM_ID)) {
final int expectedNumberOfFragments = 20_000;
final int fragmentsPerThread = expectedNumberOfFragments / 2;
final MutableInteger messageCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
assertEquals(MESSAGE_LENGTH + SIZE_OF_INT, length);
final int publisherId = buffer.getInt(offset);
if (1 == publisherId) {
assertEquals(Byte.MIN_VALUE, buffer.getByte(offset + SIZE_OF_INT));
} else if (2 == publisherId) {
assertEquals(Byte.MAX_VALUE, buffer.getByte(offset + SIZE_OF_INT));
} else {
fail("unknown publisherId=" + publisherId);
}
messageCount.value++;
};
Tests.awaitConnections(subscription, 2);
final UnsafeBuffer pubOneHeader = new UnsafeBuffer(new byte[SIZE_OF_INT]);
pubOneHeader.putInt(0, 1);
final UnsafeBuffer pubOnePayload = new UnsafeBuffer(new byte[MESSAGE_LENGTH]);
pubOnePayload.setMemory(0, MESSAGE_LENGTH, Byte.MIN_VALUE);
final UnsafeBuffer pubTwoHeader = new UnsafeBuffer(new byte[SIZE_OF_INT]);
pubTwoHeader.putInt(0, 2);
final UnsafeBuffer pubTwoPayload = new UnsafeBuffer(new byte[MESSAGE_LENGTH]);
pubTwoPayload.setMemory(0, MESSAGE_LENGTH, Byte.MAX_VALUE);
final ExecutorService threadPool = Executors.newFixedThreadPool(2);
try {
final CountDownLatch latch = new CountDownLatch(2);
threadPool.submit(() -> {
latch.countDown();
latch.await();
for (int count = 0; count < fragmentsPerThread; count++) {
while (publicationOne.offer(pubOneHeader, 0, SIZE_OF_INT, pubOnePayload, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
}
}
return null;
});
threadPool.submit(() -> {
latch.countDown();
latch.await();
for (int count = 0; count < fragmentsPerThread; count++) {
while (publicationTwo.offer(pubTwoHeader, 0, SIZE_OF_INT, pubTwoPayload, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
}
}
return null;
});
threadPool.shutdown();
int totalFragmentsRead = 0;
do {
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
} while (totalFragmentsRead < expectedNumberOfFragments);
} finally {
threadPool.shutdownNow();
}
assertEquals(expectedNumberOfFragments, messageCount.value);
}
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class ClientErrorHandlerTest method shouldHaveCorrectTermBufferLength.
@Test
@InterruptAfter(10)
@SuppressWarnings("try")
void shouldHaveCorrectTermBufferLength() {
final MediaDriver.Context ctx = new MediaDriver.Context().errorHandler(Tests::onError).dirDeleteOnStart(true);
final ErrorHandler mockErrorHandlerOne = mock(ErrorHandler.class);
final Aeron.Context clientCtxOne = new Aeron.Context().errorHandler(mockErrorHandlerOne);
final ErrorHandler mockErrorHandlerTwo = mock(ErrorHandler.class);
final Aeron.Context clientCtxTwo = new Aeron.Context().errorHandler(mockErrorHandlerTwo).subscriberErrorHandler(RethrowingErrorHandler.INSTANCE);
try (TestMediaDriver ignore = TestMediaDriver.launch(ctx, testWatcher);
Aeron aeronOne = Aeron.connect(clientCtxOne);
Aeron aeronTwo = Aeron.connect(clientCtxTwo);
Publication publication = aeronOne.addPublication(CHANNEL, STREAM_ID);
Subscription subscriptionOne = aeronOne.addSubscription(CHANNEL, STREAM_ID);
Subscription subscriptionTwo = aeronTwo.addSubscription(CHANNEL, STREAM_ID)) {
awaitConnected(subscriptionOne);
awaitConnected(subscriptionTwo);
assertEquals(clientCtxOne.errorHandler(), clientCtxOne.subscriberErrorHandler());
assertNotEquals(clientCtxTwo.errorHandler(), clientCtxTwo.subscriberErrorHandler());
final UnsafeBuffer buffer = new UnsafeBuffer(new byte[100]);
while (publication.offer(buffer) < 0) {
Tests.yield();
}
final RuntimeException expectedException = new RuntimeException("Expected");
final FragmentHandler handler = (buffer1, offset, length, header) -> {
throw expectedException;
};
while (0 == subscriptionOne.poll(handler, 1)) {
Tests.yield();
}
verify(mockErrorHandlerOne).onError(expectedException);
try {
while (0 == subscriptionTwo.poll(handler, 1)) {
Tests.yield();
}
fail("Expected exception");
} catch (final Exception ex) {
assertEquals(expectedException, ex);
}
verify(mockErrorHandlerTwo, never()).onError(any());
} finally {
ctx.deleteDirectory();
}
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class MultiSubscriberTest method shouldReceiveMessageOnSeparateSubscriptions.
@Test
@InterruptAfter(10)
void shouldReceiveMessageOnSeparateSubscriptions() {
final FragmentHandler mockFragmentHandlerOne = mock(FragmentHandler.class);
final FragmentHandler mockFragmentHandlerTwo = mock(FragmentHandler.class);
final FragmentAssembler adapterOne = new FragmentAssembler(mockFragmentHandlerOne);
final FragmentAssembler adapterTwo = new FragmentAssembler(mockFragmentHandlerTwo);
try (Subscription subscriptionOne = aeron.addSubscription(CHANNEL_1, STREAM_ID);
Subscription subscriptionTwo = aeron.addSubscription(CHANNEL_2, STREAM_ID);
Publication publication = aeron.addPublication(CHANNEL_1, STREAM_ID)) {
final byte[] expectedBytes = "Hello, World! here is a small message".getBytes();
final UnsafeBuffer srcBuffer = new UnsafeBuffer(expectedBytes);
assertEquals(0, subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT));
assertEquals(0, subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT));
while (!subscriptionOne.isConnected() || !subscriptionTwo.isConnected()) {
Tests.yield();
}
while (publication.offer(srcBuffer) < 0L) {
Tests.yield();
}
while (subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT) == 0) {
Tests.yield();
}
while (subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT) == 0) {
Tests.yield();
}
verifyData(srcBuffer, mockFragmentHandlerOne);
verifyData(srcBuffer, mockFragmentHandlerTwo);
}
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class DriverLoggingAgentTest method testLogMediaDriverEvents.
private void testLogMediaDriverEvents(final String channel, final String enabledEvents, final EnumSet<DriverEventCode> expectedEvents) {
before(enabledEvents, expectedEvents);
final MediaDriver.Context driverCtx = new MediaDriver.Context().errorHandler(Tests::onError).publicationLingerTimeoutNs(0).timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(1));
try (MediaDriver mediaDriver = MediaDriver.launch(driverCtx)) {
try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
Publication publication = aeron.addPublication(channel, STREAM_ID)) {
final UnsafeBuffer offerBuffer = new UnsafeBuffer(new byte[32]);
while (publication.offer(offerBuffer) < 0) {
Tests.yield();
}
final MutableInteger counter = new MutableInteger();
final FragmentHandler handler = (buffer, offset, length, header) -> counter.value++;
while (0 == subscription.poll(handler, 1)) {
Tests.yield();
}
assertEquals(counter.get(), 1);
}
final Supplier<String> errorMessage = () -> "Pending events: " + WAIT_LIST;
while (!WAIT_LIST.isEmpty()) {
Tests.yieldingIdle(errorMessage);
}
}
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class SubscriptionTest method shouldReadData.
@Test
public void shouldReadData() {
subscription.addImage(imageOneMock);
when(imageOneMock.poll(any(FragmentHandler.class), anyInt())).then((invocation) -> {
final FragmentHandler handler = (FragmentHandler) invocation.getArguments()[0];
handler.onFragment(atomicReadBuffer, HEADER_LENGTH, READ_BUFFER_CAPACITY - HEADER_LENGTH, header);
return 1;
});
assertEquals(1, subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT));
verify(fragmentHandler).onFragment(eq(atomicReadBuffer), eq(HEADER_LENGTH), eq(READ_BUFFER_CAPACITY - HEADER_LENGTH), any(Header.class));
}
Aggregations