This is an automated email from the ASF dual-hosted git repository.

davsclaus 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 094fe6c2256 chore: stabilizing DefaultSupervisingRouteControllerTest 
(#16007)
094fe6c2256 is described below

commit 094fe6c2256b8eabfdcfaeeb2f1836d66fcf83b4
Author: AurĂ©lien Pupier <[email protected]>
AuthorDate: Sat Oct 19 08:39:13 2024 +0200

    chore: stabilizing DefaultSupervisingRouteControllerTest (#16007)
    
    Previous improvement of assertion was when the list of failure was not
    arriving always in the same order. But the test is still flaky, there
    are 2 reasons:
    * the exception was not set yet as all retries were not finished
    * the lists used in test to collect events and failures were not
    synchronized and can be called in different threads. It seems that it
    was causing some elements to not be added correctly. I suspected a
    caught ConcurrentModificationException but in debug mode it is not hit.
    So not sure the root reason using syncehonizedList seems to do the
    trick.
    Using @RepeatedTest locally, I had between 5 and 15 tests in error on 50
    attempts before. Now I hit 100% success.
    
    Signed-off-by: AurĂ©lien Pupier <[email protected]>
---
 .../DefaultSupervisingRouteControllerTest.java       | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultSupervisingRouteControllerTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultSupervisingRouteControllerTest.java
index 37499d9efd2..e90f7818ed5 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultSupervisingRouteControllerTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultSupervisingRouteControllerTest.java
@@ -16,7 +16,9 @@
  */
 package org.apache.camel.impl.engine;
 
+import java.time.Duration;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
@@ -37,7 +39,10 @@ import org.apache.camel.spi.SupervisingRouteController;
 import org.apache.camel.support.SimpleEventNotifierSupport;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class DefaultSupervisingRouteControllerTest extends ContextTestSupport {
 
@@ -58,8 +63,8 @@ public class DefaultSupervisingRouteControllerTest extends 
ContextTestSupport {
         src.setInitialDelay(100);
         src.setThreadPoolSize(2);
 
-        List<CamelEvent.RouteRestartingFailureEvent> failures = new 
ArrayList<>();
-        List<CamelEvent.RouteRestartingEvent> events = new ArrayList<>();
+        List<CamelEvent.RouteRestartingFailureEvent> failures = 
Collections.synchronizedList(new ArrayList<>());
+        List<CamelEvent.RouteRestartingEvent> events = 
Collections.synchronizedList(new ArrayList<>());
 
         context.getManagementStrategy().addEventNotifier(new 
SimpleEventNotifierSupport() {
             @Override
@@ -94,8 +99,10 @@ public class DefaultSupervisingRouteControllerTest extends 
ContextTestSupport {
         // cake was not able to start
         assertEquals("Stopped", 
context.getRouteController().getRouteStatus("cake").toString());
 
+        await("Await all exceptions and retries finished")
+                .atMost(Duration.ofMillis(src.getInitialDelay() + 
src.getBackOffDelay() * (src.getBackOffMaxAttempts() + 1)))
+                .untilAsserted(() -> 
assertNotNull(src.getRestartException("cake")));
         Throwable e = src.getRestartException("cake");
-        assertNotNull(e);
         assertEquals("Cannot start", e.getMessage());
         boolean b = e instanceof IllegalArgumentException;
         assertTrue(b);
@@ -105,6 +112,7 @@ public class DefaultSupervisingRouteControllerTest extends 
ContextTestSupport {
 
         assertEquals(10, failures.size(),
                 "There should have 2 x 1 initial + 2 x 3 restart failure + 2 x 
1 exhausted failures.");
+
         assertEquals(6, events.size(), "There should have been 2 x 3 restart 
attempts.");
 
         assertEquals(2, failures.stream().filter(failure -> 
failure.isExhausted()).count(),
@@ -135,8 +143,8 @@ public class DefaultSupervisingRouteControllerTest extends 
ContextTestSupport {
         src.setInitialDelay(100);
         src.setThreadPoolSize(2);
 
-        List<CamelEvent.RouteRestartingFailureEvent> failure = new 
ArrayList<>();
-        List<CamelEvent.RouteRestartingEvent> events = new ArrayList<>();
+        List<CamelEvent.RouteRestartingFailureEvent> failure = 
Collections.synchronizedList(new ArrayList<>());
+        List<CamelEvent.RouteRestartingEvent> events = 
Collections.synchronizedList(new ArrayList<>());
 
         context.getManagementStrategy().addEventNotifier(new 
SimpleEventNotifierSupport() {
             @Override

Reply via email to