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

luigidemasi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-kit.git

commit 25992fd2e2556d82cd417cde9a6953ff99668b01
Author: Luigi De Masi <[email protected]>
AuthorDate: Tue Nov 24 13:07:47 2026 +0100

    Fix #197: Make migration parser timeout tests deterministic
    
    Expire the wait budget when the worker reaches content selection or
    parsing. Exercise both possible file lists through the normal timeout
    and cancellation path regardless of how quickly the worker starts.
    
    Co-authored-by: OpenAI Codex <[email protected]>
---
 .../org/apache/camel/kit/graph/GraphBuilder.java   | 10 ++++--
 .../apache/camel/kit/graph/GraphBuilderTest.java   | 36 ++++++++++++++++++----
 2 files changed, 37 insertions(+), 9 deletions(-)

diff --git 
a/kit/camel-kit-graph/src/main/java/org/apache/camel/kit/graph/GraphBuilder.java
 
b/kit/camel-kit-graph/src/main/java/org/apache/camel/kit/graph/GraphBuilder.java
index 117511a7..a723b847 100644
--- 
a/kit/camel-kit-graph/src/main/java/org/apache/camel/kit/graph/GraphBuilder.java
+++ 
b/kit/camel-kit-graph/src/main/java/org/apache/camel/kit/graph/GraphBuilder.java
@@ -156,9 +156,8 @@ public class GraphBuilder {
             for (int i = 0; i < futures.size(); i++) {
                 ParserFuture parserFuture = futures.get(i);
                 try {
-                    long remaining = parserTimeoutUnit.toNanos(parserTimeout)
-                                     - (System.nanoTime() - 
parserFuture.submittedAt());
-                    ParserRun parserRun = 
parserFuture.future().get(Math.max(0, remaining), TimeUnit.NANOSECONDS);
+                    long remaining = 
remainingParserNanos(parserFuture.submittedAt());
+                    ParserRun parserRun = parserFuture.future().get(remaining, 
TimeUnit.NANOSECONDS);
                     ParserDiagnostic diagnostic = parserRun.diagnostic();
                     if (diagnostic.successful()) {
                         var provider = 
migrationProviders.get(parserFuture.task().parser());
@@ -527,6 +526,11 @@ public class GraphBuilder {
         return simpleName;
     }
 
+    long remainingParserNanos(long submittedAt) {
+        long elapsed = System.nanoTime() - submittedAt;
+        return Math.max(0, parserTimeoutUnit.toNanos(parserTimeout) - elapsed);
+    }
+
     private long elapsedMillis(long startedAt) {
         return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startedAt);
     }
diff --git 
a/kit/camel-kit-graph/src/test/java/org/apache/camel/kit/graph/GraphBuilderTest.java
 
b/kit/camel-kit-graph/src/test/java/org/apache/camel/kit/graph/GraphBuilderTest.java
index 71a093ed..83502834 100644
--- 
a/kit/camel-kit-graph/src/test/java/org/apache/camel/kit/graph/GraphBuilderTest.java
+++ 
b/kit/camel-kit-graph/src/test/java/org/apache/camel/kit/graph/GraphBuilderTest.java
@@ -6,6 +6,8 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -269,25 +271,47 @@ class GraphBuilderTest {
                 error::getMessage);
     }
 
-    @Test
-    void migrationTimeoutReportsOnlySelectedInputs(@TempDir Path root) throws 
Exception {
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    void migrationTimeoutReportsOnlySelectedInputs(boolean duringSelection, 
@TempDir Path root) throws Exception {
         Files.writeString(root.resolve("pom.xml"), "<project/>");
         Files.writeString(root.resolve("flow.xml"), "<mule 
xmlns='http://www.mulesoft.org/schema/mule/core'/>");
+        var phaseReached = new CompletableFuture<Void>();
         var parser = new MuleXmlFlowParser() {
+            @Override
+            public boolean acceptsContent(Path file) {
+                if (duringSelection) {
+                    awaitCancellation();
+                }
+                return super.acceptsContent(file);
+            }
+
             @Override
             public void parseFiles(Path projectRoot, List<Path> files, 
ProjectGraph graph) {
+                awaitCancellation();
+            }
+
+            private void awaitCancellation() {
+                phaseReached.complete(null);
                 try {
                     new CountDownLatch(1).await();
                 } catch (InterruptedException e) {
                     Thread.currentThread().interrupt();
+                    throw new CancellationException();
                 }
             }
         };
-        GraphBuildResult result = new GraphBuilder(new EmptyParser(), 
List.of(parser), 1, TimeUnit.SECONDS)
-                .buildWithDiagnostics(root);
-        ParserDiagnostic diagnostic = result.failedDiagnostics().getFirst();
+        var builder = new GraphBuilder(new EmptyParser(), List.of(parser), 1, 
TimeUnit.MINUTES) {
+            @Override
+            long remainingParserNanos(long submittedAt) {
+                // Expire the deadline only after the worker reaches the phase 
under test.
+                phaseReached.orTimeout(1, TimeUnit.MINUTES).join();
+                return 0;
+            }
+        };
+        var diagnostic = findDiagnostic(builder.buildWithDiagnostics(root), 
parser.getClass().getName());
         assertTrue(diagnostic.timedOut());
-        assertEquals(List.of("flow.xml"), diagnostic.scannedFiles());
+        assertEquals(duringSelection ? List.of() : List.of("flow.xml"), 
diagnostic.scannedFiles());
     }
 
     @Test

Reply via email to