use of io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class UntetheredSubscriptionTest method shouldRejoinAfterResting.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
public void shouldRejoinAfterResting(final String channel) {
final AtomicInteger unavailableImageCount = new AtomicInteger();
final AtomicInteger availableImageCount = new AtomicInteger();
final UnavailableImageHandler unavailableHandler = (image) -> unavailableImageCount.incrementAndGet();
final AvailableImageHandler availableHandler = (image) -> availableImageCount.incrementAndGet();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
};
final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
final String untetheredChannel = channel + "|tether=false";
final String publicationChannel = channel.startsWith("aeron-spy") ? channel.substring(10) : channel;
boolean pollingUntethered = true;
try (Subscription tetheredSub = aeron.addSubscription(channel, STREAM_ID);
Subscription untetheredSub = aeron.addSubscription(untetheredChannel, STREAM_ID, availableHandler, unavailableHandler);
Publication publication = aeron.addPublication(publicationChannel, STREAM_ID)) {
while (!tetheredSub.isConnected() || !untetheredSub.isConnected()) {
Tests.yield();
aeron.conductorAgentInvoker().invoke();
}
while (true) {
if (publication.offer(srcBuffer) < 0) {
Tests.yield();
aeron.conductorAgentInvoker().invoke();
}
if (pollingUntethered && untetheredSub.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT) > 0) {
pollingUntethered = false;
}
tetheredSub.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT);
if (unavailableImageCount.get() == 1) {
while (availableImageCount.get() < 2) {
Tests.yield();
aeron.conductorAgentInvoker().invoke();
}
return;
}
}
}
}
use of io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class PublicationUnblockTest method shouldUnblockNonCommittedMessage.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldUnblockNonCommittedMessage(final String channel) {
final MutableInteger fragmentCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> fragmentCount.value++;
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
Publication publicationOne = aeron.addPublication(channel, STREAM_ID);
Publication publicationTwo = aeron.addPublication(channel, STREAM_ID)) {
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[driver.context().mtuLength()]);
final int length = 128;
srcBuffer.setMemory(0, length, (byte) 66);
final BufferClaim bufferClaim = new BufferClaim();
while (publicationOne.tryClaim(length, bufferClaim) < 0L) {
Tests.yield();
}
bufferClaim.buffer().setMemory(bufferClaim.offset(), length, (byte) 65);
bufferClaim.commit();
while (publicationTwo.offer(srcBuffer, 0, length) < 0L) {
Tests.yield();
}
while (publicationOne.tryClaim(length, bufferClaim) < 0L) {
Tests.yield();
}
while (publicationTwo.offer(srcBuffer, 0, length) < 0L) {
Tests.yield();
}
final int expectedFragments = 3;
int numFragments = 0;
do {
final int fragments = subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT);
if (fragments == 0) {
Tests.yield();
}
numFragments += fragments;
} while (numFragments < expectedFragments);
assertEquals(expectedFragments, numFragments);
assertEquals(expectedFragments, fragmentCount.value);
}
}
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 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 ExclusivePublicationTest method shouldPublishFromIndependentExclusivePublications.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldPublishFromIndependentExclusivePublications(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 = 778;
int totalFragmentsRead = 0;
final MutableInteger messageCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
assertEquals(MESSAGE_LENGTH, length);
messageCount.value++;
};
Tests.awaitConnections(subscription, 2);
for (int i = 0; i < expectedNumberOfFragments; i += 2) {
while (publicationOne.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
}
while (publicationTwo.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
}
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
}
do {
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
} while (totalFragmentsRead < expectedNumberOfFragments);
assertEquals(expectedNumberOfFragments, messageCount.value);
}
}
Aggregations