From 953b663b884a8012946baf2b52b0bec685175ceb Mon Sep 17 00:00:00 2001 From: Wagyourtail Date: Sun, 30 Jun 2024 01:55:29 -0500 Subject: [PATCH] fix test concurrency issues --- .../java/xyz/wagyourtail/downgradetest/TestFile.java | 10 ++++++---- .../utils/kotlin/xyz/wagyourtail/jvmdg/util/Util.kt | 2 ++ .../java/xyz/wagyourtail/jvmdg/test/ClassRunner.java | 9 +++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/downgradetest/src/main/java/xyz/wagyourtail/downgradetest/TestFile.java b/downgradetest/src/main/java/xyz/wagyourtail/downgradetest/TestFile.java index 94072a89..dadbe38f 100644 --- a/downgradetest/src/main/java/xyz/wagyourtail/downgradetest/TestFile.java +++ b/downgradetest/src/main/java/xyz/wagyourtail/downgradetest/TestFile.java @@ -10,16 +10,17 @@ public class TestFile { public static void main(String[] args) throws IOException { - new File("build/test").mkdirs(); - FileWriter writer = new FileWriter("build/test/test.txt", StandardCharsets.UTF_8); + var temp = Files.createTempFile("temp", "txt"); + Files.deleteIfExists(temp); + FileWriter writer = new FileWriter(temp.toAbsolutePath().toString(), StandardCharsets.UTF_8); writer.write("Hello World!\n"); writer.close(); - FileWriter writer2 = new FileWriter(new File("build/test/test.txt"), StandardCharsets.UTF_8, true); + FileWriter writer2 = new FileWriter(temp.toFile(), StandardCharsets.UTF_8, true); writer2.write("Goodbye World!"); writer2.close(); - FileReader reader = new FileReader("build/test/test.txt", StandardCharsets.UTF_8); + FileReader reader = new FileReader(temp.toAbsolutePath().toString(), StandardCharsets.UTF_8); StringBuilder sb = new StringBuilder(); int c; while ((c = reader.read()) != -1) { @@ -27,6 +28,7 @@ public static void main(String[] args) throws IOException { } reader.close(); System.out.println(sb.toString()); + Files.deleteIfExists(temp); } } diff --git a/gradle-plugin/src/utils/kotlin/xyz/wagyourtail/jvmdg/util/Util.kt b/gradle-plugin/src/utils/kotlin/xyz/wagyourtail/jvmdg/util/Util.kt index 34858c4c..84a80106 100644 --- a/gradle-plugin/src/utils/kotlin/xyz/wagyourtail/jvmdg/util/Util.kt +++ b/gradle-plugin/src/utils/kotlin/xyz/wagyourtail/jvmdg/util/Util.kt @@ -2,6 +2,8 @@ package xyz.wagyourtail.jvmdg.util import org.apache.commons.compress.archivers.zip.ZipFile import org.gradle.api.JavaVersion +import org.gradle.jvm.toolchain.JavaLanguageVersion +import org.gradle.jvm.toolchain.JavaToolchainSpec import org.objectweb.asm.ClassReader import org.objectweb.asm.Opcodes import org.objectweb.asm.tree.ClassNode diff --git a/src/test/java/xyz/wagyourtail/jvmdg/test/ClassRunner.java b/src/test/java/xyz/wagyourtail/jvmdg/test/ClassRunner.java index a56e313a..daa1357a 100644 --- a/src/test/java/xyz/wagyourtail/jvmdg/test/ClassRunner.java +++ b/src/test/java/xyz/wagyourtail/jvmdg/test/ClassRunner.java @@ -24,6 +24,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.zip.ZipEntry; @@ -104,7 +105,7 @@ public static Stream arguments() throws IOException { .unwrap(); } - private static final Map> originalResults = new HashMap<>(); + private static final Map> originalResults = new ConcurrentHashMap<>(); @BeforeAll public static void runOriginal() throws IOException, InterruptedException { @@ -128,7 +129,7 @@ public static void runOriginal() throws IOException, InterruptedException { } } - private static final Map downgradedPaths = new HashMap<>(); + private static final Map downgradedPaths = new ConcurrentHashMap<>(); private static Path getDowngradedPath(FlagsAndRunner flags) { String fName = original.getFileName().toString(); @@ -137,7 +138,7 @@ private static Path getDowngradedPath(FlagsAndRunner flags) { return original.resolveSibling(withoutExt + "-downgrade-" + flags.readableSlug() + ext); } - private static final Map shadedPaths = new HashMap<>(); + private static final Map shadedPaths = new ConcurrentHashMap<>(); private static Path getShadedPath(FlagsAndRunner flags) { String fName = original.getFileName().toString(); @@ -163,7 +164,7 @@ private static synchronized Path getDowngradedJar(FlagsAndRunner flags) { }); } - private static final Map apiPaths = new HashMap<>(); + private static final Map apiPaths = new ConcurrentHashMap<>(); private static synchronized Path getShadedJar(FlagsAndRunner flags) { return apiPaths.computeIfAbsent(flags, e -> {