This is an automated email from the ASF dual-hosted git repository. apupier pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit e349d66d086ab85d494d81f1b5b883f4c52363b0 Author: Aurélien Pupier <[email protected]> AuthorDate: Mon Oct 12 10:05:32 2026 +0200 CAMEL-25348: add test ensuring ACK is sent only after all window messages complete Add LumberjackOutOfOrderAckTest to verify that when messages in a window complete out of order (specifically when the last sequenced message completes while earlier messages are still processing), the ACK is not sent until every message in the window finishes processing. Co-authored-by: IBM Bob 2.2.1 Signed-off-by: Aurélien Pupier <[email protected]> --- .../lumberjack/LumberjackOutOfOrderAckTest.java | 146 +++++++++++++++++++++ .../camel/component/lumberjack/LumberjackUtil.java | 15 ++- 2 files changed, 157 insertions(+), 4 deletions(-) diff --git a/components/camel-lumberjack/src/test/java/org/apache/camel/component/lumberjack/LumberjackOutOfOrderAckTest.java b/components/camel-lumberjack/src/test/java/org/apache/camel/component/lumberjack/LumberjackOutOfOrderAckTest.java new file mode 100644 index 000000000000..edacf62f1854 --- /dev/null +++ b/components/camel-lumberjack/src/test/java/org/apache/camel/component/lumberjack/LumberjackOutOfOrderAckTest.java @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.lumberjack; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.awaitility.Awaitility; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.parallel.Isolated; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisabledOnOs(value = { OS.LINUX }, + architectures = { "s390x" }, + disabledReason = "This test does not run reliably on multiple platforms (see CAMEL-21438)") +@Isolated +class LumberjackOutOfOrderAckTest extends CamelTestSupport { + + private static final String ROUTE_ID = "lumberjack-" + LumberjackOutOfOrderAckTest.class.getSimpleName(); + private static final String MOCK_OUTPUT = "mock:output-" + LumberjackOutOfOrderAckTest.class.getSimpleName(); + + private final CountDownLatch firstMessageProcessing = new CountDownLatch(1); + private final CountDownLatch holdFirstMessage = new CountDownLatch(1); + private final AtomicBoolean first = new AtomicBoolean(true); + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("lumberjack:0.0.0.0:0").routeId(ROUTE_ID) + .threads(5) + .process(exchange -> { + if (first.compareAndSet(true, false)) { + firstMessageProcessing.countDown(); + if (!holdFirstMessage.await(5, TimeUnit.SECONDS)) { + throw new AssertionError("Timed out waiting for holdFirstMessage to be released"); + } + } + }) + .to(MOCK_OUTPUT); + } + }; + } + + private int getActualPort() { + return ((LumberjackConsumer) context.getRoute(ROUTE_ID).getConsumer()).getLocalPort(); + } + + @Test + void shouldNotSendAckUntilAllMessagesComplete() throws Exception { + MockEndpoint mock = getMockEndpoint(MOCK_OUTPUT); + mock.expectedMessageCount(14); + + NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(1); + try { + List<Integer> responses = new CopyOnWriteArrayList<>(); + ChannelInitializer<Channel> initializer = new ChannelInitializer<>() { + @Override + protected void initChannel(Channel ch) { + ch.pipeline().addLast(new SimpleChannelInboundHandler<ByteBuf>() { + @Override + protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { + assertThat(msg.readUnsignedByte()).isEqualTo((short) '2'); + assertThat(msg.readUnsignedByte()).isEqualTo((short) 'A'); + responses.add(msg.readInt()); + } + }); + } + }; + + Channel channel = new Bootstrap() + .group(eventLoopGroup) + .channel(NioSocketChannel.class) + .handler(initializer) + .connect("127.0.0.1", getActualPort()).sync().channel(); + + try { + // Send window of 15 messages + channel.writeAndFlush(LumberjackUtil.readSample("io/window15.bin")); + + // Wait until the first message has arrived and is blocked + boolean firstStarted = firstMessageProcessing.await(5, TimeUnit.SECONDS); + assertThat(firstStarted).as("First message should start processing").isTrue(); + + // Wait until the other 14 messages (including the 15th with sequenceNumber == windowSize) complete + mock.assertIsSatisfied(5, TimeUnit.SECONDS); + + // Give the Netty pipeline a moment to deliver any in-flight response + Thread.sleep(200); + + // Verify that no ACK has been sent yet because the first message is still in flight + assertThat(responses).as("No ACK should be sent before all messages in the window complete").isEmpty(); + + // Prepare mock for the remaining message + mock.reset(); + mock.expectedMessageCount(1); + + // Unblock the first message + holdFirstMessage.countDown(); + + // Wait for the last message to reach mock + mock.assertIsSatisfied(5, TimeUnit.SECONDS); + + // Assert that the ACK is now received + Awaitility.await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> { + assertThat(responses).containsExactly(15); + }); + } finally { + channel.close().sync(); + } + } finally { + eventLoopGroup.shutdownGracefully().syncUninterruptibly(); + } + } +} diff --git a/components/camel-lumberjack/src/test/java/org/apache/camel/component/lumberjack/LumberjackUtil.java b/components/camel-lumberjack/src/test/java/org/apache/camel/component/lumberjack/LumberjackUtil.java index b1a7a74e5570..71885d9b8ea8 100644 --- a/components/camel-lumberjack/src/test/java/org/apache/camel/component/lumberjack/LumberjackUtil.java +++ b/components/camel-lumberjack/src/test/java/org/apache/camel/component/lumberjack/LumberjackUtil.java @@ -99,9 +99,16 @@ final class LumberjackUtil { windows.stream().forEach(window -> channel.writeAndFlush(readSample(String.format("io/window%s.bin", window)))); if (waitForResult) { Awaitility.await().atMost(30, TimeUnit.SECONDS).untilAsserted( - () -> Assertions.assertThat(responses) - .as("Expected one ACK per window frame sent (%d), but received: %s", windows.size(), responses) - .hasSize(windows.size())); + () -> { + List<Integer> received; + synchronized (responses) { + received = new ArrayList<>(responses); + } + Assertions.assertThat(received) + .as("Expected one ACK per window frame sent (%d), but received: %s", windows.size(), + received) + .hasSize(windows.size()); + }); } channel.close().sync(); @@ -114,7 +121,7 @@ final class LumberjackUtil { } } - private static ByteBuf readSample(String resource) { + static ByteBuf readSample(String resource) { try (InputStream stream = LumberjackUtil.class.getResourceAsStream(resource)) { ByteArrayOutputStream output = new ByteArrayOutputStream(); int input;
