gnodet-bot commented on code in PR #28861:
URL: https://github.com/apache/camel/pull/28861#discussion_r4464772335


##########
core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommittedBeforeClaimTest.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.file;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import 
org.apache.camel.support.processor.idempotent.MemoryIdempotentRepository;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+/**
+ * Deterministic regression test for CAMEL-25778: verifies {@code 
FileConsumer.isValidFile()} correctly handles the case
+ * where a concurrent commit thread moves the file between {@code 
inProgressRepository.contains()} and {@code add()}.
+ * <p/>
+ * The race is made deterministic by injecting a custom {@code 
inProgressRepository} whose {@code add()} deletes the
+ * file before delegating to the real repository — exactly what a concurrent 
commit thread would do (move file + remove
+ * in-progress entry). Without the fix, the file would be routed and the eager 
idempotent key would remain. With the
+ * fix, neither happens.
+ */
+class FileConsumerCommittedBeforeClaimTest extends ContextTestSupport {
+
+    private final MemoryIdempotentRepository idempotent = new 
MemoryIdempotentRepository();
+    private final AtomicBoolean claimed = new AtomicBoolean();
+
+    /** Custom in-progress repository that simulates a concurrent commit 
deleting the file inside add(). */
+    private final MemoryIdempotentRepository inProgress = new 
MemoryIdempotentRepository() {
+        @Override
+        public boolean add(String key) {
+            // Simulate: a commit on another thread lands between contains() 
and add() —
+            // the file is moved away (here: deleted) before this poll's add() 
completes.
+            try {
+                Files.deleteIfExists(Path.of(key));
+            } catch (IOException e) {
+                throw new UncheckedIOException(e);
+            }
+            claimed.set(true);
+            return super.add(key);
+        }
+    };
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        context.getRegistry().bind("inProgress", inProgress);
+        context.getRegistry().bind("idem", idempotent);
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from(fileUri("?initialDelay=0&delay=10"
+                             + "&inProgressRepository=#inProgress"
+                             + "&idempotent=true&idempotentRepository=#idem"))
+                        .routeId("myRoute").autoStartup(false)
+                        .to("mock:done");
+            }
+        };
+    }
+
+    @Test
+    void testFileGoneAfterClaimIsSkipped() throws Exception {
+        getMockEndpoint("mock:done").expectedMessageCount(0);
+
+        Path file = testDirectory().resolve("hello.txt");
+        Files.writeString(file, "Hello");
+        String key = file.toAbsolutePath().toString();
+
+        context.getRouteController().startRoute("myRoute");
+
+        // Wait until the in-progress add() was triggered (file deleted inside 
it)
+        Awaitility.await().atMost(10, TimeUnit.SECONDS).untilTrue(claimed);
+
+        // Allow one more poll cycle to verify nothing is re-routed
+        Thread.sleep(50);

Review Comment:
   🔧 **Nit — `Thread.sleep` prohibited by project convention:** 
[`AGENTS.md`](https://github.com/apache/camel/blob/main/AGENTS.md) states: 
*"Never add `Thread.sleep()`; when touching a test that has one, replace it."*
   
   This sleep is also redundant: after `Awaitility.await().untilTrue(claimed)`, 
the TOCTOU guard's cleanup 
(`getEndpoint().getInProgressRepository().remove(...)`) has already run 
synchronously on the same poll thread — `inProgress.contains(key)` is already 
`false` at this point. The final 
`getMockEndpoint("mock:done").assertIsSatisfied(2000)` provides a 2-second wait 
for the negative assertion. The `Thread.sleep(50)` line can be removed entirely.



-- 
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