From a0ee9011cefac60d641183a24f955eaf08ee35c3 Mon Sep 17 00:00:00 2001 From: Pelotrio <45769595+Pelotrio@users.noreply.github.com> Date: Mon, 5 Oct 2026 18:23:13 +0200 Subject: [PATCH 1/4] Tests that only need a project load its index, and an index names the JDK it holds ProjectTreeTest built the runtime index, the whole JDK, every time a project opened, and its await for the index came near its five-second limit on a loaded machine. Its mod archive now holds a class and its index is cached before the project opens, as cache-loading tests already do. Two waits wait for what they assert instead of a fixed time. The index read Companion's own JDK but recorded the game's Java home, and a runtime cache is keyed by the game's inventory alone: a Companion on another JVM could load an index of another JDK. The JDK source now names the one read, and a cache holding another is rebuilt. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../totalDebugCompanion/runtime/IndexCache.java | 9 +++++++++ .../runtime/RuntimeIndexService.java | 10 +++++----- .../inspection/InspectionReadsTest.java | 2 +- .../runtime/IndexCacheTest.java | 10 ++++++++++ .../ui/components/treeView/ProjectTreeTest.java | 15 ++++++++++++--- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java index ff9e5257a..236eba784 100644 --- a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java +++ b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java @@ -3,6 +3,7 @@ import com.github.minecraft_ta.totalDebugCompanion.bytecode.RuntimeSnapshotBytecodeSource.Source; import com.github.minecraft_ta.totaldebug.storage.AtomicFiles; import com.github.minecraft_ta.totaldebug.storage.JsonFiles; +import com.github.minecraft_ta.totaldebug.storage.RuntimeInventory.ModuleKind; import com.github.minecraft_ta.totaldebug.storage.RuntimeInventory.RuntimeModule; import com.github.tth05.jindex.ClassIndex; import com.google.gson.JsonArray; @@ -154,9 +155,17 @@ static void requireSourcePaths(Manifest manifest) throws IOException { if (!Files.isRegularFile(path) && !Files.isDirectory(path)) { throw new IOException("Prepared runtime source is unavailable: " + path); } + if (source.module().kind() == ModuleKind.JAVA_RUNTIME && !path.equals(javaHome())) { + throw new IOException("The index holds the classes of another Java runtime: " + path); + } } } + /** The Java runtime whose classes an index holds: the one Companion runs on. */ + static Path javaHome() { + return Path.of(System.getProperty("java.home")); + } + private static JsonObject toJson(Manifest manifest) { JsonObject json = new JsonObject(); json.addProperty("format", FORMAT); diff --git a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/RuntimeIndexService.java b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/RuntimeIndexService.java index 1f3d499f6..07445534e 100644 --- a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/RuntimeIndexService.java +++ b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/RuntimeIndexService.java @@ -353,7 +353,7 @@ private void buildOrLoad(Work work) { } checkpoint(work); update(work, new Status(Phase.BUILDING, "Building class index", null)); - return buildSnapshot(work, prepareInputs(inventory), IndexIdentity.runtime(inventory.id()), indexFile, inventory.javaHome()); + return buildSnapshot(work, prepareInputs(inventory), IndexIdentity.runtime(inventory.id()), indexFile); }); publishReady(work, ready); } catch (CancellationException ignored) { @@ -424,7 +424,7 @@ private void buildOrLoadLocal(Work work) throws IOException { update(work, new Status(Phase.BUILDING, work.detail + "Building local mod index", null)); var prepared = LocalModSources.prepare(scan, () -> checkpoint(work)); work.sourceDetail = prepared.detail(); - return buildSnapshot(work, prepared.inputs(), identity, indexFile, System.getProperty("java.home")); + return buildSnapshot(work, prepared.inputs(), identity, indexFile); }); publishReady(work, ready); } @@ -433,8 +433,7 @@ private ReadySnapshot buildSnapshot( Work work, List prepared, IndexIdentity identity, - Path indexFile, - String javaHome + Path indexFile ) throws IOException { AtomicFiles.cleanupAbandonedStaging(indexFile.getParent()); checkpoint(work); @@ -459,9 +458,10 @@ private ReadySnapshot buildSnapshot( try (var phase = RuntimePhase.start("index.jdk-inputs")) { addJdkClasses(jdkSourceId, indexSources); } + // The JDK read is Companion's own, which the mod starts on the game's Java; a cache records it (IndexCache). publishedSources.add(new RuntimeSnapshotBytecodeSource.Source( jdkSourceId, - Path.of(javaHome), + IndexCache.javaHome(), "jrt:/", new RuntimeInventory.RuntimeModule( "java-runtime", diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/inspection/InspectionReadsTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/inspection/InspectionReadsTest.java index cd62c1e81..6d84b58f5 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/inspection/InspectionReadsTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/inspection/InspectionReadsTest.java @@ -119,8 +119,8 @@ void aNavigationShowingAHiddenLivePageReadsItOnce() throws Exception { UiTestScope.showPages(panel); panel.session().refresh(); }); - Thread.sleep(500); // A read asks for the snippets twice: whether the side can run, then to run. + UiTestScope.await(() -> reads.get() >= asked + 2); assertEquals(asked + 2, reads.get(), "the navigation's read answers the read the live interval asked for"); } finally { UiTestScope.onEdt(panel::dispose); diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java index ca365ef42..18b0fbaae 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java @@ -45,4 +45,14 @@ void roundTripsNativeIndexAndSourceIdentityAsOneFile() throws Exception { assertThrows(IOException.class, () -> IndexCache.requireSources(manifest)); } } + + @Test + void anIndexOfAnotherJavaRuntimeIsNotUsed() throws Exception { + var java = new RuntimeInventory.RuntimeModule("java-runtime", "Java Runtime", RuntimeInventory.ModuleKind.JAVA_RUNTIME); + IndexCache.requireSourcePaths(new IndexCache.Manifest("test", + List.of(new RuntimeSnapshotBytecodeSource.Source(0, IndexCache.javaHome(), "jrt:/", java)))); + var other = new IndexCache.Manifest("test", + List.of(new RuntimeSnapshotBytecodeSource.Source(0, this.home, "jrt:/", java))); + assertThrows(IOException.class, () -> IndexCache.requireSourcePaths(other)); + } } diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/components/treeView/ProjectTreeTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/components/treeView/ProjectTreeTest.java index ccfc082f4..5c2aa1a46 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/components/treeView/ProjectTreeTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/components/treeView/ProjectTreeTest.java @@ -10,6 +10,7 @@ import com.github.minecraft_ta.totalDebugCompanion.navigation.WorldTab; import com.github.minecraft_ta.totalDebugCompanion.project.ProjectScope; import com.github.minecraft_ta.totalDebugCompanion.runtime.RuntimeIndexService; +import com.github.minecraft_ta.totalDebugCompanion.runtime.RuntimeTestSources; import com.github.minecraft_ta.totalDebugCompanion.session.CompanionLaunchConfiguration; import com.github.minecraft_ta.totalDebugCompanion.session.CompanionProfile; import com.github.minecraft_ta.totalDebugCompanion.storage.ChangeRecord; @@ -423,8 +424,7 @@ void theFirstAndTheLastChangeAddAndRemoveTheModpackRootAlone() throws Exception private static void assertReveals(FileTreeView tree, Callable> reveal, String name) throws Exception { CompletableFuture revealed = UiTestScope.onEdt(reveal); assertTrue(revealed.get(10, TimeUnit.SECONDS), name + " is revealed"); - settle(); - assertEquals(name, UiTestScope.onEdt(() -> selectedName(tree)), name + " is selected"); + UiTestScope.await(() -> name.equals(selectedName(tree))); } /** The catalog of a pack with one mod, {@code testmod}, ready. */ @@ -434,12 +434,21 @@ private void withCatalog(ProjectScope scope) throws Exception { settle(); } - /** A mod archive in the game's {@code mods} folder, which the Runtime root lists before a game runs. */ + /** + * A mod archive in the game's {@code mods} folder, which the Runtime root lists before a game runs, with its index + * already cached: the project loads it rather than indexing the JDK, which none of these tests is about. + */ private static void modJar(Path game) throws Exception { try (var zip = new ZipOutputStream(Files.newOutputStream(Files.createDirectories(game.resolve("mods")).resolve("sample.jar")))) { zip.putNextEntry(new ZipEntry("pack.mcmeta")); zip.write("{}".getBytes(StandardCharsets.UTF_8)); + String fixture = Before.class.getName().replace('.', '/') + ".class"; + zip.putNextEntry(new ZipEntry(fixture)); + try (var bytes = ProjectTreeTest.class.getClassLoader().getResourceAsStream(fixture)) { + bytes.transferTo(zip); + } } + RuntimeTestSources.writeLocalCache(game); } /** A script in a folder of the instance's Scripts. */ From 2751799b4e2a75994a496562e65169e78a2b6a5d Mon Sep 17 00:00:00 2001 From: Pelotrio <45769595+Pelotrio@users.noreply.github.com> Date: Tue, 6 Oct 2026 00:23:07 +0200 Subject: [PATCH 2/4] An index's Java runtime compares as an absolute, normalized path Co-Authored-By: Claude Opus 5.5 (1M context) --- .../totalDebugCompanion/runtime/IndexCache.java | 2 +- .../totalDebugCompanion/runtime/IndexCacheTest.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java index 236eba784..1c9812697 100644 --- a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java +++ b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java @@ -163,7 +163,7 @@ static void requireSourcePaths(Manifest manifest) throws IOException { /** The Java runtime whose classes an index holds: the one Companion runs on. */ static Path javaHome() { - return Path.of(System.getProperty("java.home")); + return Path.of(System.getProperty("java.home")).toAbsolutePath().normalize(); } private static JsonObject toJson(Manifest manifest) { diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java index 18b0fbaae..de5e12a83 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java @@ -55,4 +55,17 @@ void anIndexOfAnotherJavaRuntimeIsNotUsed() throws Exception { List.of(new RuntimeSnapshotBytecodeSource.Source(0, this.home, "jrt:/", java))); assertThrows(IOException.class, () -> IndexCache.requireSourcePaths(other)); } + + @Test + void aJavaHomeWrittenWithRedundantPartsIsTheSameRuntime() throws Exception { + var java = new RuntimeInventory.RuntimeModule("java-runtime", "Java Runtime", RuntimeInventory.ModuleKind.JAVA_RUNTIME); + String property = System.getProperty("java.home"); + System.setProperty("java.home", Path.of(property, ".").toString()); + try { + IndexCache.requireSourcePaths(new IndexCache.Manifest("test", + List.of(new RuntimeSnapshotBytecodeSource.Source(0, IndexCache.javaHome(), "jrt:/", java)))); + } finally { + System.setProperty("java.home", property); + } + } } From 747455eff37d22baaa75d0a95c9a810451f2e9f3 Mon Sep 17 00:00:00 2001 From: Pelotrio <45769595+Pelotrio@users.noreply.github.com> Date: Tue, 6 Oct 2026 00:33:56 +0200 Subject: [PATCH 3/4] An index names the build of the Java runtime it holds, which an update in place changes Co-Authored-By: Claude Opus 5.5 (1M context) --- .../runtime/IndexCache.java | 9 ++++++++ .../runtime/IndexCacheTest.java | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java index 1c9812697..499bddfdd 100644 --- a/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java +++ b/companion/src/main/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCache.java @@ -120,6 +120,9 @@ static Manifest read(Path file) throws IOException { if (JsonFiles.integer(json, "format") != FORMAT) { throw new IOException("Unsupported runtime index format: " + file); } + if (!javaVersion().equals(JsonFiles.string(json, "javaVersion"))) { + throw new IOException("The index holds the classes of another Java runtime: " + JsonFiles.string(json, "javaVersion")); + } List sources = new ArrayList<>(); var ids = new HashSet(); for (var value : JsonFiles.array(json, "sources")) { @@ -166,9 +169,15 @@ static Path javaHome() { return Path.of(System.getProperty("java.home")).toAbsolutePath().normalize(); } + /** The version and build of that runtime, which an update in place changes. */ + static String javaVersion() { + return System.getProperty("java.runtime.version"); + } + private static JsonObject toJson(Manifest manifest) { JsonObject json = new JsonObject(); json.addProperty("format", FORMAT); + json.addProperty("javaVersion", javaVersion()); json.addProperty("sourceKind", manifest.identity().kind().name()); json.addProperty("sourceIdentity", manifest.identity().value()); if (!manifest.detail().isBlank()) json.addProperty("detail", manifest.detail()); diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java index de5e12a83..9941f5eb6 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/runtime/IndexCacheTest.java @@ -56,6 +56,27 @@ void anIndexOfAnotherJavaRuntimeIsNotUsed() throws Exception { assertThrows(IOException.class, () -> IndexCache.requireSourcePaths(other)); } + @Test + void anIndexWrittenOnAnotherBuildOfTheJavaRuntimeIsNotUsed() throws Exception { + Path file = this.home.resolve("index.jindex"); + var java = new RuntimeInventory.RuntimeModule("java-runtime", "Java Runtime", RuntimeInventory.ModuleKind.JAVA_RUNTIME); + var manifest = new IndexCache.Manifest("test", + List.of(new RuntimeSnapshotBytecodeSource.Source(0, IndexCache.javaHome(), "jrt:/", java))); + try (var input = IndexCacheTest.class.getResourceAsStream("IndexCacheTest.class"); + ClassIndex index = ClassIndex.fromBytes(List.of(input.readAllBytes())); + ClassIndex ignored = IndexCache.write(file, index, manifest)) { + assertEquals(manifest, IndexCache.read(file)); + } + String version = System.getProperty("java.runtime.version"); + // The same path after an update in place. + System.setProperty("java.runtime.version", version + "-updated"); + try { + assertThrows(IOException.class, () -> IndexCache.read(file)); + } finally { + System.setProperty("java.runtime.version", version); + } + } + @Test void aJavaHomeWrittenWithRedundantPartsIsTheSameRuntime() throws Exception { var java = new RuntimeInventory.RuntimeModule("java-runtime", "Java Runtime", RuntimeInventory.ModuleKind.JAVA_RUNTIME); From 49dcbc297bb3eb5ac8b6375e51de658f9325c50f Mon Sep 17 00:00:00 2001 From: Pelotrio <45769595+Pelotrio@users.noreply.github.com> Date: Tue, 6 Oct 2026 00:33:56 +0200 Subject: [PATCH 4/4] Probe JVMs get a minute to start on a loaded runner CompanionRenderingDefaultsTest's probe did not start within 15 s while two CI runs shared the runner. The waits end when the process exits, so the limit costs nothing when it starts at once; a deadlock still fails. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../totalDebugCompanion/CompanionRenderingDefaultsTest.java | 3 ++- .../ui/views/SearchEverywherePopupProcessTest.java | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/CompanionRenderingDefaultsTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/CompanionRenderingDefaultsTest.java index 821c375c7..a1969814f 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/CompanionRenderingDefaultsTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/CompanionRenderingDefaultsTest.java @@ -25,7 +25,8 @@ class CompanionRenderingDefaultsTest { var builder = new ProcessBuilder(command).redirectErrorStream(true); for (String name : List.of("JAVA_TOOL_OPTIONS", "JDK_JAVA_OPTIONS", "_JAVA_OPTIONS")) builder.environment().remove(name); Process process = builder.start(); - boolean finished = process.waitFor(15, TimeUnit.SECONDS); + // A JVM on the whole test class path starts slowly on a loaded runner; the wait ends when it exits. + boolean finished = process.waitFor(60, TimeUnit.SECONDS); if (!finished) process.destroyForcibly().waitFor(); String output = new String(process.getInputStream().readAllBytes()); assertTrue(finished, output); diff --git a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/views/SearchEverywherePopupProcessTest.java b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/views/SearchEverywherePopupProcessTest.java index 819979401..b9aa2cb32 100644 --- a/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/views/SearchEverywherePopupProcessTest.java +++ b/companion/src/test/java/com/github/minecraft_ta/totalDebugCompanion/ui/views/SearchEverywherePopupProcessTest.java @@ -28,8 +28,9 @@ void firstConstructionDoesNotDeadlockAwtClassInitialization() throws Exception { "-cp", System.getProperty("java.class.path"), Probe.class.getName()) .redirectErrorStream(true).redirectOutput(output.toFile()).start(); try { - assertTrue(process.waitFor(5, TimeUnit.SECONDS), - () -> "Popup initialization did not finish within five seconds: " + output); + // A deadlock never ends; a minute leaves a slow start on a loaded runner its time. + assertTrue(process.waitFor(60, TimeUnit.SECONDS), + () -> "Popup initialization did not finish within a minute: " + output); assertEquals(0, process.exitValue(), Files.readString(output)); } finally { if (process.isAlive()) process.destroyForcibly().waitFor();