|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.agentic.ai.agent.orchestration.extension.file; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.nio.file.StandardOpenOption; |
| 23 | +import java.nio.file.attribute.PosixFilePermissions; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.condition.EnabledOnOs; |
| 28 | +import org.junit.jupiter.api.condition.OS; |
| 29 | +import org.junit.jupiter.api.io.TempDir; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.ValueSource; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 35 | + |
| 36 | +/** 验证后端检查文件后、真实 rg 搜索前,文件增长越过大小上限的情况。 */ |
| 37 | +@EnabledOnOs({ OS.MAC, OS.LINUX }) |
| 38 | +public class LocalFilesystemBackendFileGrowthTest { |
| 39 | + |
| 40 | + private static final String MARKER = "SIZE_LIMIT_MARKER"; |
| 41 | + |
| 42 | + @TempDir |
| 43 | + Path root; |
| 44 | + |
| 45 | + /** 验证显式搜索只返回目标文件,兼容隐藏文件、忽略规则和文件名中的特殊字符。 */ |
| 46 | + @ParameterizedTest |
| 47 | + @ValueSource(strings = { "plain.txt", ".hidden[?]*{x}.txt", "!special.txt", "space name.txt", "back\\slash.txt" }) |
| 48 | + void explicitSearchKeepsItsOriginalFileScope(String name) throws Exception { |
| 49 | + findRipgrep(); |
| 50 | + Files.writeString(this.root.resolve(name), MARKER + "\n"); |
| 51 | + Files.writeString(this.root.resolve("other.txt"), MARKER + "\n"); |
| 52 | + Files.writeString(Files.createDirectory(this.root.resolve("nested")).resolve(name), MARKER + "\n"); |
| 53 | + Files.writeString(this.root.resolve(".ignore"), "*\n"); |
| 54 | + final LocalFilesystemBackend backend = new LocalFilesystemBackend(this.root.toString(), true, 1); |
| 55 | + final Object result = backend.grepRaw(MARKER, "/" + name, null); |
| 56 | + assertTrue(result instanceof List<?>, "Expected a match list"); |
| 57 | + final List<?> matches = (List<?>) result; |
| 58 | + assertEquals(1, matches.size(), "Explicit search must not include sibling or nested files"); |
| 59 | + assertTrue(matches.get(0) instanceof GrepMatch); |
| 60 | + assertEquals("/" + name.replace("\\", "/"), ((GrepMatch) matches.get(0)).getPath()); |
| 61 | + } |
| 62 | + |
| 63 | + /** 用信号文件同步扩容与搜索,分别验证目录输入和显式文件输入。 */ |
| 64 | + @ParameterizedTest(name = "explicitFile={0}") |
| 65 | + @ValueSource(booleans = { false, true }) |
| 66 | + void skipsFileThatGrowsAfterBackendCheck(boolean explicitFile) throws Exception { |
| 67 | + final Path realRipgrep = findRipgrep(); |
| 68 | + final Path workspace = Files.createDirectory(this.root.resolve("workspace")); |
| 69 | + final Path growingFile = Files.writeString(workspace.resolve("growing.txt"), "initial content\n"); |
| 70 | + assertTrue(Files.size(growingFile) < 1024L * 1024L); |
| 71 | + final Path ready = this.root.resolve("rg-ready"); |
| 72 | + final Path release = this.root.resolve("rg-release"); |
| 73 | + final Path result = this.root.resolve("result.txt"); |
| 74 | + final Path log = this.root.resolve("probe.log"); |
| 75 | + final Path bin = createRipgrepGate(); |
| 76 | + final String java = Path.of(System.getProperty("java.home"), "bin", "java").toString(); |
| 77 | + final String classpath = System.getProperty("surefire.test.class.path", System.getProperty("java.class.path")); |
| 78 | + final ProcessBuilder builder = new ProcessBuilder(java, "-cp", classpath, |
| 79 | + LocalFilesystemBackendFileGrowthTest.class.getName(), workspace.toString(), |
| 80 | + Boolean.toString(explicitFile), result.toString()); |
| 81 | + builder.environment().put("PATH", bin.toString()); |
| 82 | + builder.environment().put("ARGI_GROWTH_READY", ready.toString()); |
| 83 | + builder.environment().put("ARGI_GROWTH_RELEASE", release.toString()); |
| 84 | + builder.environment().put("ARGI_GROWTH_REAL_RG", realRipgrep.toString()); |
| 85 | + builder.environment().remove("RIPGREP_CONFIG_PATH"); |
| 86 | + builder.redirectErrorStream(true).redirectOutput(log.toFile()); |
| 87 | + final Process process = builder.start(); |
| 88 | + try { |
| 89 | + awaitRipgrepGate(ready, process, log); |
| 90 | + Files.writeString(growingFile, "padding\n".repeat(150_000) + MARKER + "\n", StandardCharsets.UTF_8, |
| 91 | + StandardOpenOption.APPEND); |
| 92 | + assertTrue(Files.size(growingFile) > 1024L * 1024L); |
| 93 | + Files.writeString(release, "continue"); |
| 94 | + assertTrue(process.waitFor(15, TimeUnit.SECONDS), "Probe JVM did not finish after releasing rg"); |
| 95 | + assertEquals(0, process.exitValue(), Files.readString(log)); |
| 96 | + assertTrue(Files.isRegularFile(result), "Probe did not produce a search result"); |
| 97 | + assertEquals("matches=0", Files.readString(result), |
| 98 | + "The file grew beyond 1 MiB after the backend check; explicitFile=" + explicitFile); |
| 99 | + } |
| 100 | + finally { |
| 101 | + process.descendants().forEach(ProcessHandle::destroyForcibly); |
| 102 | + process.destroyForcibly(); |
| 103 | + assertTrue(process.waitFor(5, TimeUnit.SECONDS), "Probe JVM did not stop"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** 定位 PATH 中真实可执行的 rg,缺失时明确指出测试环境不满足要求。 */ |
| 108 | + private static Path findRipgrep() throws Exception { |
| 109 | + final String path = System.getenv().getOrDefault("PATH", ""); |
| 110 | + for (String directory : path.split(File.pathSeparator)) { |
| 111 | + final Path candidate = Path.of(directory, "rg"); |
| 112 | + if (Files.isRegularFile(candidate) && Files.isExecutable(candidate)) { |
| 113 | + return candidate.toRealPath(); |
| 114 | + } |
| 115 | + } |
| 116 | + throw new IllegalStateException("Install ripgrep and add its directory to the test JVM PATH"); |
| 117 | + } |
| 118 | + |
| 119 | + /** 创建测试专用 rg 包装脚本,收到放行信号后以原参数执行真实 rg。 */ |
| 120 | + private Path createRipgrepGate() throws Exception { |
| 121 | + final Path bin = Files.createDirectory(this.root.resolve("bin")); |
| 122 | + final Path wrapper = Files.writeString(bin.resolve("rg"), """ |
| 123 | + #!/bin/sh |
| 124 | + set -eu |
| 125 | + : > "$ARGI_GROWTH_READY" |
| 126 | + attempt=0 |
| 127 | + while [ ! -f "$ARGI_GROWTH_RELEASE" ]; do |
| 128 | + if [ "$attempt" -ge 2000 ]; then |
| 129 | + exit 70 |
| 130 | + fi |
| 131 | + /bin/sleep 0.01 |
| 132 | + attempt=$((attempt + 1)) |
| 133 | + done |
| 134 | + exec "$ARGI_GROWTH_REAL_RG" "$@" |
| 135 | + """, StandardCharsets.UTF_8); |
| 136 | + Files.setPosixFilePermissions(wrapper, PosixFilePermissions.fromString("rwx------")); |
| 137 | + return bin; |
| 138 | + } |
| 139 | + |
| 140 | + /** 等待 rg 包装脚本发出就绪信号,不通过固定延时猜测文件检查时序。 */ |
| 141 | + private static void awaitRipgrepGate(Path ready, Process process, Path log) throws Exception { |
| 142 | + final long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(15); |
| 143 | + while (!Files.exists(ready)) { |
| 144 | + assertTrue(process.isAlive(), "Probe exited before reaching rg: " + Files.readString(log)); |
| 145 | + assertTrue(System.nanoTime() < deadline, "Timed out waiting for rg gate"); |
| 146 | + Thread.sleep(10); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** 子 JVM 入口:以 1 MiB 上限调用真实后端,并将搜索匹配数量写入结果文件。 */ |
| 151 | + public static void main(String[] args) throws Exception { |
| 152 | + final LocalFilesystemBackend backend = new LocalFilesystemBackend(args[0], true, 1); |
| 153 | + final boolean explicitFile = Boolean.parseBoolean(args[1]); |
| 154 | + final Object result = backend.grepRaw(MARKER, explicitFile ? "/growing.txt" : "/", "growing.txt"); |
| 155 | + if (!(result instanceof List<?> matches)) { |
| 156 | + throw new IllegalStateException("Expected grepRaw to return a match list"); |
| 157 | + } |
| 158 | + Files.writeString(Path.of(args[2]), "matches=" + matches.size(), StandardCharsets.UTF_8); |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments