gnodet commented on code in PR #23125: URL: https://github.com/apache/camel/pull/23125#discussion_r3224042400
########## 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: Nit: this comment says "for 2s TTL that's 200ms" but the production code does `Math.max(ttl / 10, 60000)`, which clamps to a **1-minute minimum**, not 200ms. If the cleanup interval is really 60s, the `await().atMost(5, SECONDS)` on line 60 will time out before the first cleanup fires. Worth verifying this test passes reliably — the comment at minimum should be corrected. _Claude Code on behalf of Guillaume Nodet_ ########## 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: `Thread.sleep()` is not allowed in test code per project conventions — please use Awaitility instead. For example: ```java // Verify task remains present during the wait period (well below TTL) await().during(1, TimeUnit.SECONDS) .atMost(2, TimeUnit.SECONDS) .untilAsserted(() -> assertEquals(1, component.getPendingAsyncTasks().size())); ``` This is more robust and avoids flaky timing issues. _Claude Code on behalf of Guillaume Nodet_ ########## components/camel-ai/camel-docling/src/main/java/org/apache/camel/component/docling/DoclingComponent.java: ########## @@ -51,6 +58,26 @@ public DoclingComponent(CamelContext context) { this.configuration = new DoclingConfiguration(); } + @Override + protected void doStart() throws Exception { + super.doStart(); + + // Start scheduled cleanup task for expired async tasks + long ttl = configuration.getAsyncTaskTtl(); + long cleanupInterval = Math.max(ttl / 10, 60000); // Run cleanup every 10% of TTL, minimum 1 minute + + cleanupExecutor = getCamelContext().getExecutorServiceManager() + .newScheduledThreadPool(this, "DoclingAsyncTaskCleanup", 1); + + cleanupExecutor.scheduleWithFixedDelay( + this::cleanupExpiredTasks, + cleanupInterval, + cleanupInterval, + TimeUnit.MILLISECONDS); + + LOG.info("Started async task cleanup with TTL={}ms, cleanup interval={}ms", ttl, cleanupInterval); Review Comment: Suggestion: consider using `LOG.debug(...)` here and at the other `LOG.info` calls in `cleanupExpiredTasks()` (line 142) and `doStop()` (line 153). For a component library, routine lifecycle events and periodic housekeeping are typically logged at DEBUG to avoid noisy output for end users. _Claude Code on behalf of Guillaume Nodet_ -- 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]
