From 52bf73be1046d94767ab0373df3e6cb08d05adb6 Mon Sep 17 00:00:00 2001 From: Robert MacRae Date: Fri, 22 Aug 2025 08:13:49 -0300 Subject: [PATCH] fixed issues #72 and #77 the re-enter issue --- .../oc2/common/event/ForgeEventHandlers.java | 26 ----- .../cil/oc2/common/util/AsyncTestUtils.java | 100 ------------------ 2 files changed, 126 deletions(-) delete mode 100644 src/main/java/li/cil/oc2/common/util/AsyncTestUtils.java diff --git a/src/main/java/li/cil/oc2/common/event/ForgeEventHandlers.java b/src/main/java/li/cil/oc2/common/event/ForgeEventHandlers.java index aa65b2b3..b5b49f31 100644 --- a/src/main/java/li/cil/oc2/common/event/ForgeEventHandlers.java +++ b/src/main/java/li/cil/oc2/common/event/ForgeEventHandlers.java @@ -2,7 +2,6 @@ package li.cil.oc2.common.event; import li.cil.oc2.api.API; import li.cil.oc2.common.config.AsyncConfig; -import li.cil.oc2.common.util.AsyncTestUtils; import li.cil.oc2.common.util.AsyncUtils; import net.minecraft.server.MinecraftServer; import net.minecraftforge.event.server.ServerAboutToStartEvent; @@ -36,31 +35,6 @@ public final class ForgeEventHandlers { public static void handleServerAboutToStart(final ServerAboutToStartEvent event) { server = event.getServer(); LOGGER.info("Server starting, initializing async components"); - - // Safely check if we should run async tests - boolean shouldRunTests = false; - try { - shouldRunTests = AsyncConfig.SERVER != null && AsyncConfig.SERVER.runAsyncTests.get(); - } catch (IllegalStateException e) { - LOGGER.warn("Config not available, skipping async tests"); - } - - // Run async tests if enabled and config is available - if (shouldRunTests) { - LOGGER.info("Running async operation tests..."); - AsyncTestUtils.verifyAsyncOperations() - .thenAccept(uuid -> { - if (uuid != null) { - LOGGER.debug("Async test completed with UUID: {}", uuid); - } else { - LOGGER.debug("Async test completed"); - } - }) - .exceptionally(e -> { - LOGGER.error("Async test failed", e); - return null; - }); - } } @SubscribeEvent diff --git a/src/main/java/li/cil/oc2/common/util/AsyncTestUtils.java b/src/main/java/li/cil/oc2/common/util/AsyncTestUtils.java deleted file mode 100644 index e3ca8aca..00000000 --- a/src/main/java/li/cil/oc2/common/util/AsyncTestUtils.java +++ /dev/null @@ -1,100 +0,0 @@ -package li.cil.oc2.common.util; - -import li.cil.oc2.common.config.AsyncConfig; -import li.cil.oc2.common.event.ForgeEventHandlers; -import net.minecraft.server.MinecraftServer; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.UUID; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; -import java.util.function.BooleanSupplier; - -/** - * Utility class for testing async functionality. - */ -public final class AsyncTestUtils { - private static final Logger LOGGER = LogManager.getLogger(); - private static final int TEST_TIMEOUT_MS = 5000; - - /** - * Waits for a condition to become true, with a timeout. - * - * @param condition The condition to wait for. - * @param timeoutMs The maximum time to wait in milliseconds. - * @return true if the condition became true within the timeout, false otherwise. - */ - public static boolean waitForCondition(BooleanSupplier condition, long timeoutMs) { - final long startTime = System.currentTimeMillis(); - while (!condition.getAsBoolean()) { - if (System.currentTimeMillis() - startTime > timeoutMs) { - return false; - } - try { - Thread.sleep(10); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return false; - } - } - return true; - } - - /** - * Verifies that async operations are working correctly. - * - * @return A future that completes with a test UUID when verification is done. - */ - public static CompletableFuture verifyAsyncOperations() { - if (!AsyncConfig.SERVER.asyncStorageOperations.get()) { - return CompletableFuture.completedFuture(null); - } - - LOGGER.info("Verifying async operations..."); - - // Test basic async execution - return CompletableFuture.supplyAsync(() -> { - if (AsyncConfig.SERVER.enableSuperDebug.get()) { - LOGGER.debug("Async test operation running on thread: {}", Thread.currentThread().getName()); - } - - // Add a small delay to ensure async behavior - try { - Thread.sleep(100); - return null; // Return value from the supplier - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new RuntimeException("Async test interrupted", e); - } - }, AsyncUtils.getAsyncExecutor()) - .thenCompose(v -> { - // Verify we can switch back to server thread - return AsyncUtils.onServerThread(() -> { - MinecraftServer server = ForgeEventHandlers.getCurrentServer(); - if (server == null) { - throw new IllegalStateException("Server not available during async test"); - } - - if (AsyncConfig.SERVER.enableSuperDebug.get()) { - LOGGER.debug("Successfully switched back to server thread"); - } - - // Generate a test UUID for storage testing - UUID testId = UUID.randomUUID(); - if (AsyncConfig.SERVER.enableSuperDebug.get()) { - LOGGER.debug("Generated test UUID: {}", testId); - } - - return testId; - }); - }) - .whenComplete((result, throwable) -> { - if (throwable != null) { - LOGGER.error("Async test failed", throwable); - } else { - LOGGER.info("Async operations verified successfully"); - } - }); - } -}