davsclaus commented on code in PR #29576: URL: https://github.com/apache/camel/pull/29576#discussion_r4530674371
########## components/camel-chaos/src/test/java/org/apache/camel/component/chaos/ChaosReplayTest.java: ########## @@ -0,0 +1,157 @@ +/* + * 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.chaos; + +import java.io.File; +import java.nio.file.Files; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.util.FileUtil; +import org.apache.camel.util.json.JsonArray; +import org.apache.camel.util.json.JsonObject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.awaitility.Awaitility.await; + +/** + * CAMEL-26817: the files a file route took, which it moved to .camel, are put back, so the route takes the same files + * again and an experiment can be run again with the same messages. + */ +class ChaosReplayTest extends ChaosTestSupport { + + private static final File BASE = new File("target/chaos-replay"); + + @BeforeEach + void files() throws Exception { + FileUtil.removeDir(BASE); + new File(BASE, "orders").mkdirs(); + // written under a hidden name, which the routes do not take, and then renamed + write("inbox/a.txt", "A"); + write("inbox/b.txt", "B"); + write("kept/c.txt", "C"); + } + + private static void write(String name, String body) throws Exception { + File file = new File(BASE, name); + file.getParentFile().mkdirs(); + File hidden = new File(file.getParentFile(), "." + file.getName()); + Files.writeString(hidden.toPath(), body); + Files.move(hidden.toPath(), file.toPath()); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("file:target/chaos-replay/inbox?initialDelay=0&delay=50").routeId("inbox").to("mock:inbox") + .to("seda:ship"); + from("seda:ship").routeId("ship").to("file:target/chaos-replay/archive"); + from("file:target/chaos-replay/orders?move=done/${file:name}&initialDelay=0&delay=50").routeId("orders") + .to("mock:orders"); + from("file:target/chaos-replay/kept?noop=true&initialDelay=0&delay=50").routeId("kept").to("mock:kept"); + } + }; + } + + @Test + void theFilesTheRouteTookArePutBack() throws Exception { + MockEndpoint inbox = getMockEndpoint("mock:inbox"); + inbox.expectedBodiesReceivedInAnyOrder("A", "B"); + MockEndpoint.assertIsSatisfied(context); + File done = new File(BASE, "inbox/.camel"); + await().atMost(10, TimeUnit.SECONDS).until(() -> ChaosAdvisor.takenFiles(done).size() == 2); + + // what to try says how: the files it took, put back + ChaosAdvisor.Feed feed = ChaosAdvisor.feed(context.getRoute("inbox")); + assertThat(feed.replay()).isEqualTo(done.getAbsolutePath()); + assertThat(feed.files()).isEqualTo(2); + assertThat(feed.hint()).contains("the 2 files the route took") + .endsWith(": an experiment with --feed=inbox --body=replay:"); + + inbox.reset(); + inbox.expectedBodiesReceivedInAnyOrder("A", "B"); + Map<String, Object> replayed = chaos().replayFiles("inbox"); + assertThat(replayed).containsEntry("route", "inbox").containsEntry("files", 2); + MockEndpoint.assertIsSatisfied(context); + + // moved over the files there: the same two, so a replay is the same each time + await().atMost(10, TimeUnit.SECONDS).until(() -> new File(BASE, "inbox").listFiles(File::isFile).length == 0); Review Comment: Fixed in 0b24ef20b8b9: the await checks for null before `.length`. _Claude Code on behalf of davsclaus_ ########## components/camel-chaos/src/main/java/org/apache/camel/component/chaos/ChaosManager.java: ########## @@ -593,6 +620,63 @@ public Map<String, Object> suggest() { return answer; } + /** + * Puts the files a file route took back into its folder, so it takes the same files again (CAMEL-26817): the files + * it moved to {@code .camel} (or the folder of its {@code move} option), under their own names. The route moves + * them there again, over the copies, so a replay is the same each time, as an experiment that is run again needs. + * Each is written under a hidden name first, which the route does not take, and then renamed. A file of that name + * that the route has not taken yet is left as it is. + * + * @param routeId the id of the route + * @return {@code route}, {@code folder} (where the files were), {@code files} (how many + * were put back) and {@code skipped} (those still waiting in the folder) + * @throws IllegalArgumentException when the route does not take files it moves, or has taken none + */ + public Map<String, Object> replayFiles(String routeId) { + Route route = routeId != null ? camelContext.getRoute(routeId) : null; + if (route == null) { + throw new IllegalArgumentException("There is no route " + routeId); + } + File done = route.getEndpoint() != null ? ChaosAdvisor.doneFolder(route.getEndpoint()) : null; + if (done == null) { + throw new IllegalArgumentException( + "Route " + routeId + " does not take files it moves to a folder (file: without noop or delete)"); + } + List<File> taken = ChaosAdvisor.takenFiles(done); + if (taken.isEmpty()) { + throw new IllegalArgumentException("Route " + routeId + " has taken no files yet: " + done + " is empty"); + } + if (taken.size() > MAX_REPLAY) { + taken = taken.subList(0, MAX_REPLAY); + } + File folder = ChaosAdvisor.inputFolder(route.getEndpoint()); + int files = 0; + int skipped = 0; + for (File file : taken) { + Path target = folder.toPath().resolve(file.getName()); + if (Files.exists(target)) { + skipped++; + continue; + } + Path hidden = folder.toPath().resolve(".chaos-" + file.getName()); + try { + Files.copy(file.toPath(), hidden, StandardCopyOption.REPLACE_EXISTING); + Files.move(hidden, target, StandardCopyOption.ATOMIC_MOVE); + files++; + } catch (IOException e) { + throw new IllegalStateException("Cannot put back " + file + ": " + e.getMessage(), e); Review Comment: Fixed in 0b24ef20b8b9: the move falls back to a non-atomic move on `AtomicMoveNotSupportedException`, and on any `IOException` the hidden `.chaos-<name>` copy is deleted before the error is thrown, so nothing is left in the folder the route consumes from. _Claude Code on behalf of davsclaus_ -- 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]
