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


The following commit(s) were added to refs/heads/main by this push:
     new 2cddebab6c2f CAMEL-23317: Fix flaky MailProducerUnsupportedCharsetTest
2cddebab6c2f is described below

commit 2cddebab6c2ff626f05968a3699c041dfb858639
Author: Aurélien Pupier <[email protected]>
AuthorDate: Mon Oct 19 09:50:03 2026 +0200

    CAMEL-23317: Fix flaky MailProducerUnsupportedCharsetTest
    
    POP3 has no SEEN flag and the consumer runs with delete=false by
    default,
    so every poll re-delivers all messages from the mailbox. With the
    consumer
    route registered before context.start() and an initialDelay of only
    100ms,
    the consumer could fire between the two sequential SMTP sends:
    
      poll 1: mailbox=[Hello World]    → mock receives Hello World
      send Bye World
      poll 2: mailbox=[Hello World, Bye World] → mock receives Hello World
    again + Bye World
    
    expectedBodiesReceivedInAnyOrder then consumed both Hello World slots
    and
    reported Bye World as missing (not that it was never fetched).
    
    Fix:
    1. Start the context first (needed for the producer template) without
    any
       consumer route, send both messages into the mailbox, then add the
       consumer route. Context.addRoutes() on a live context auto-starts the
       route, so the first poll finds both messages already present.
    2. Add delete=true to both POP3 consumer URIs so each message is
    delivered
       exactly once regardless of timing, making the tests robust even if
       subsequent polls happen before assertIsSatisfied completes.
    
    Co-authored-by: IBM Bob 2.2.1
    Signed-off-by: Aurélien Pupier <[email protected]>
---
 .../mail/MailProducerUnsupportedCharsetTest.java   | 34 ++++++++++++++--------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java
 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java
index 57978b967c30..6473a264aca6 100644
--- 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java
+++ 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java
@@ -46,19 +46,14 @@ public class MailProducerUnsupportedCharsetTest extends 
CamelTestSupport {
     public void testSendUnsupportedCharset() throws Exception {
         jones1.clear();
 
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() {
-                from(jones1.uriPrefix(Protocol.pop3) + 
"&initialDelay=100&delay=100&ignoreUnsupportedCharset=true")
-                        .to("mock:result");
-            }
-        });
+        // Start context first (needed for template) but without any consumer 
route yet.
+        // Both messages must be in the mailbox before the POP3 consumer 
starts polling.
+        // POP3 has no SEEN flag and runs with delete=false by default, so 
every poll
+        // re-delivers all messages. If the consumer fired between the two 
SMTP sends it
+        // would receive Hello World twice; expectedBodiesReceivedInAnyOrder 
would then
+        // consume both Hello slots and report Bye World as missing.
         context.start();
 
-        MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedBodiesReceivedInAnyOrder("Hello World\r\n", "Bye 
World\r\n");
-        mock.allMessages().header("Content-Type").isEqualTo("text/plain");
-
         Map<String, Object> headers = new HashMap<>();
         headers.put("To", jones1.getEmail());
         headers.put("Content-Type", "text/plain");
@@ -69,6 +64,21 @@ public class MailProducerUnsupportedCharsetTest extends 
CamelTestSupport {
         headers.put("Content-Type", "text/plain; charset=XXX");
         template.sendBodyAndHeaders(jones1.uriPrefix(Protocol.smtp) + 
"&ignoreUnsupportedCharset=true", "Bye World", headers);
 
+        // Set up expectations and add the consumer route only after both 
messages are in the
+        // mailbox. delete=true ensures each message is delivered exactly once 
regardless of
+        // timing, making the test robust even if subsequent polls happen 
before assertIsSatisfied.
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceivedInAnyOrder("Hello World\r\n", "Bye 
World\r\n");
+        mock.allMessages().header("Content-Type").isEqualTo("text/plain");
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                from(jones1.uriPrefix(Protocol.pop3) + 
"&initialDelay=100&delay=100&delete=true&ignoreUnsupportedCharset=true")
+                        .to("mock:result");
+            }
+        });
+
         MockEndpoint.assertIsSatisfied(context);
     }
 
@@ -79,7 +89,7 @@ public class MailProducerUnsupportedCharsetTest extends 
CamelTestSupport {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() {
-                from(jones2.uriPrefix(Protocol.pop3) + 
"&initialDelay=100&delay=100&ignoreUnsupportedCharset=false")
+                from(jones2.uriPrefix(Protocol.pop3) + 
"&initialDelay=100&delay=100&delete=true&ignoreUnsupportedCharset=false")
                         .to("mock:result");
             }
         });

Reply via email to