Merge pull request #80 from TheRealM18/1.20.1
fixed issues #72 and #77 the re-enter issue
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<UUID> 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");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user