oscerd commented on code in PR #23125:
URL: https://github.com/apache/camel/pull/23125#discussion_r3224558827


##########
components/camel-ai/camel-docling/src/test/java/org/apache/camel/component/docling/DoclingAsyncTaskTtlTest.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.docling;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * Test TTL-based cleanup of pending async conversion tasks.
+ */
+public class DoclingAsyncTaskTtlTest extends CamelTestSupport {
+
+    @Test
+    public void testAsyncTaskTtlEviction() throws Exception {
+        // Get the component to access pending tasks map
+        DoclingComponent component = context.getComponent("docling", 
DoclingComponent.class);
+        assertNotNull(component);
+
+        // Configure with a short TTL (2 seconds) for testing
+        component.getConfiguration().setAsyncTaskTtl(2000);
+        component.getConfiguration().setUseDoclingServe(true);
+        
component.getConfiguration().setDoclingServeUrl("http://localhost:5001";);
+
+        // Start the component to trigger cleanup scheduler
+        context.start();
+
+        // Simulate adding a task entry directly (since we don't have a real 
docling-serve)
+        String taskId = "test-task-1";
+        AsyncTaskEntry entry = new AsyncTaskEntry(taskId, new 
java.util.concurrent.CompletableFuture<>());
+        component.getPendingAsyncTasks().put(taskId, entry);
+
+        // Verify task is present
+        assertEquals(1, component.getPendingAsyncTasks().size());
+
+        // Wait for TTL to expire and cleanup to run
+        // Cleanup runs every 10% of TTL (minimum 1 minute), but for 2s TTL 
that's 200ms
+        // Add buffer for cleanup execution
+        await().atMost(5, TimeUnit.SECONDS)
+                .untilAsserted(() -> assertEquals(0, 
component.getPendingAsyncTasks().size()));
+    }
+
+    @Test
+    public void testAsyncTaskNotEvictedBeforeTtl() throws Exception {
+        // Get the component
+        DoclingComponent component = context.getComponent("docling", 
DoclingComponent.class);
+        assertNotNull(component);
+
+        // Configure with a longer TTL (10 seconds)
+        component.getConfiguration().setAsyncTaskTtl(10000);
+        component.getConfiguration().setUseDoclingServe(true);
+        
component.getConfiguration().setDoclingServeUrl("http://localhost:5001";);
+
+        context.start();
+
+        // Add a task entry
+        String taskId = "test-task-2";
+        AsyncTaskEntry entry = new AsyncTaskEntry(taskId, new 
java.util.concurrent.CompletableFuture<>());
+        component.getPendingAsyncTasks().put(taskId, entry);
+
+        // Verify task is present
+        assertEquals(1, component.getPendingAsyncTasks().size());
+
+        // Wait a short time (less than TTL)
+        Thread.sleep(1000);
+

Review Comment:
   Fixed in 7c1479fa90e — replaced `Thread.sleep(1000)` with the suggested 
Awaitility `during/atMost` pattern.
   
   _Claude Code on behalf of Andrea Cosentino_



##########
components/camel-ai/camel-docling/src/test/java/org/apache/camel/component/docling/DoclingAsyncTaskTtlTest.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.docling;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * Test TTL-based cleanup of pending async conversion tasks.
+ */
+public class DoclingAsyncTaskTtlTest extends CamelTestSupport {
+
+    @Test
+    public void testAsyncTaskTtlEviction() throws Exception {
+        // Get the component to access pending tasks map
+        DoclingComponent component = context.getComponent("docling", 
DoclingComponent.class);
+        assertNotNull(component);
+
+        // Configure with a short TTL (2 seconds) for testing
+        component.getConfiguration().setAsyncTaskTtl(2000);
+        component.getConfiguration().setUseDoclingServe(true);
+        
component.getConfiguration().setDoclingServeUrl("http://localhost:5001";);
+
+        // Start the component to trigger cleanup scheduler
+        context.start();
+
+        // Simulate adding a task entry directly (since we don't have a real 
docling-serve)
+        String taskId = "test-task-1";
+        AsyncTaskEntry entry = new AsyncTaskEntry(taskId, new 
java.util.concurrent.CompletableFuture<>());
+        component.getPendingAsyncTasks().put(taskId, entry);
+
+        // Verify task is present
+        assertEquals(1, component.getPendingAsyncTasks().size());
+
+        // Wait for TTL to expire and cleanup to run
+        // Cleanup runs every 10% of TTL (minimum 1 minute), but for 2s TTL 
that's 200ms
+        // Add buffer for cleanup execution

Review Comment:
   Good catch — you're right that the comment was wrong and, more importantly, 
with the 60s minimum the cleanup interval was *60s* for a 2s TTL test, so the 
`await().atMost(5, SECONDS)` was unreachable.
   
   Fixed in 7c1479fa90e by:
   1. Lowering the cleanup-interval floor from 60s to **1s**. The cleanup is 
cheap when no tasks are expired (early-returns on empty map; otherwise just a 
hashmap iteration), so the lower minimum is safe and makes the configurable TTL 
actually meaningful for short values. For the default 24h TTL this changes 
nothing (ttl/10 = 2.4h > 1s either way).
   2. Correcting the test comment to match: "Cleanup runs every 10% of TTL with 
a 1 second minimum, so for a 2s TTL the interval is 1s."
   3. Bumping `atMost` from 5s → 8s to give headroom for TTL (2s) + first 
cleanup tick (~1s) + scheduler jitter.
   4. Also overrode `isUseAdviceWith()` to return `true` — otherwise 
CamelTestSupport auto-starts the context (and the component's `doStart`) 
*before* the test code sets the TTL, so the scheduler was being built with the 
default 24h TTL even when the test set 2s.
   
   All 3 tests in `DoclingAsyncTaskTtlTest` now pass deterministically (~7s for 
the whole class).
   
   _Claude Code on behalf of Andrea Cosentino_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to