Merge upstream port lane d

NativeStructureWorldgenAccess keeps both sides: writes are still recorded for the stale deferred-record check, and they go through the POI reconciler when one is bound.
This commit is contained in:
piexel committed 2026-11-10 01:32:04 +01:00
commit 4b1d85177f
69 files changed
+2413 -255

No files matched your search

@@ -0,0 +1,192 @@
package art.arcane.iris.core;
import art.arcane.iris.spi.IrisLogging;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.MockedStatic;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mockStatic;
public class ServerTimeoutConfigurationTest {
private static final String SPIGOT_KEY = ServerTimeoutConfiguration.SPIGOT_TIMEOUT_KEY;
private static final String PAPER_KEY = ServerTimeoutConfiguration.PAPER_WATCHDOG_KEY;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void originalValuesSurviveRestartsAndRestoreIndependently() throws Exception {
Path spigot = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
Path paper = configuration("paper-global.yml", "watchdog:\n early-warning-delay: 10000\n early-warning-every: 5000\n");
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
ServerConfigurator.increaseKeepAliveSpigot(spigot, timeouts());
ServerConfigurator.increasePaperWatchdog(paper, timeouts());
ServerConfigurator.increaseKeepAliveSpigot(spigot, timeouts());
timeouts().restore(SPIGOT_KEY);
assertEquals(60L, load(spigot).getLong(SPIGOT_KEY));
assertEquals(180000L, load(paper).getLong(PAPER_KEY));
timeouts().restore(PAPER_KEY);
assertEquals(10000L, load(paper).getLong(PAPER_KEY));
assertEquals(5000L, load(paper).getLong("watchdog.early-warning-every"));
assertFalse(Files.readString(recoveryFile()).contains("spigot.yml"));
assertFalse(Files.readString(recoveryFile()).contains("paper-global.yml"));
}
}
@Test
public void valueAtOrAboveTheMinimumIsLeftAlone() throws Exception {
String original = "settings:\n timeout-time: 600\n";
Path file = configuration("spigot.yml", original);
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
assertEquals(original, Files.readString(file));
assertFalse(Files.exists(recoveryFile()));
}
}
@Test
public void restoringAbsentSettingRemovesTheOverride() throws Exception {
Path file = configuration("spigot.yml", "settings:\n unrelated: retained\n");
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
assertEquals(300L, load(file).getLong(SPIGOT_KEY));
timeouts().restore(SPIGOT_KEY);
assertFalse(load(file).isSet(SPIGOT_KEY));
assertEquals("retained", load(file).getString("settings.unrelated"));
}
}
@Test
public void restorePreservesOperatorChangesAndOtherSettings() throws Exception {
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
Files.writeString(file, "# Operator settings\nsettings:\n timeout-time: 90\n unrelated: retained\n");
String changed = Files.readString(file);
timeouts().restore(SPIGOT_KEY);
assertEquals(changed, Files.readString(file));
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
timeouts().restore(SPIGOT_KEY);
assertEquals(90L, load(file).getLong(SPIGOT_KEY));
assertEquals("retained", load(file).getString("settings.unrelated"));
}
}
@Test
public void symlinkSurvivesRaiseAndRestore() throws Exception {
Path target = configuration("shared-spigot.yml", "settings:\n timeout-time: 60\n");
Path link = temporaryFolder.getRoot().toPath().resolve("spigot.yml");
Files.createSymbolicLink(link, target.getFileName());
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
ServerConfigurator.increaseKeepAliveSpigot(link, timeouts());
assertEquals(300L, load(target).getLong(SPIGOT_KEY));
timeouts().restore(SPIGOT_KEY);
assertTrue(Files.isSymbolicLink(link));
assertEquals(60L, load(target).getLong(SPIGOT_KEY));
}
}
@Test
public void aLaterAdjustmentRecordsTheOperatorsNewOriginal() throws Exception {
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
Files.writeString(file, "settings:\n timeout-time: 90\n");
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
timeouts().restore(SPIGOT_KEY);
assertEquals(90L, load(file).getLong(SPIGOT_KEY));
}
}
@Test
public void failedRecoveryWriteLeavesServerSettingsUntouched() throws Exception {
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
Path blocked = temporaryFolder.newFile("blocked").toPath();
ServerTimeoutConfiguration timeouts = new ServerTimeoutConfiguration(blocked.resolve("server-timeouts.json"));
assertThrows(IOException.class, () -> timeouts.raise(file, SPIGOT_KEY, 300L));
assertEquals(60L, load(file).getLong(SPIGOT_KEY));
}
@Test
public void failedServerWriteKeepsTheOriginalRecorded() throws Exception {
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
try (MockedStatic<ServerConfigurator> writes = mockStatic(ServerConfigurator.class, CALLS_REAL_METHODS)) {
writes.when(() -> ServerConfigurator.writeStringAtomic(eq(file.toRealPath()), anyString()))
.thenThrow(new IOException("blocked server write"));
assertThrows(IOException.class, () -> timeouts().raise(file, SPIGOT_KEY, 300L));
}
assertEquals(60L, load(file).getLong(SPIGOT_KEY));
assertTrue(Files.readString(recoveryFile()).contains("\"original\": 60"));
}
@Test
public void interruptedRestoreRetriesWithoutOverwritingLaterOperatorEdits() throws Exception {
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
try (MockedStatic<ServerConfigurator> writes = mockStatic(ServerConfigurator.class, CALLS_REAL_METHODS)) {
writes.when(() -> ServerConfigurator.writeStringAtomic(eq(recoveryFile()), anyString()))
.thenThrow(new IOException("interrupted recovery write"));
assertThrows(IOException.class, () -> timeouts().restore(SPIGOT_KEY));
}
assertTrue(Files.readString(recoveryFile()).contains("spigot.yml"));
Files.writeString(file, "settings:\n timeout-time: 120\n");
timeouts().restore(SPIGOT_KEY);
assertEquals(120L, load(file).getLong(SPIGOT_KEY));
assertFalse(Files.readString(recoveryFile()).contains("spigot.yml"));
}
}
@Test
public void recordForADeletedFileIsDropped() throws Exception {
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class)) {
ServerConfigurator.increaseKeepAliveSpigot(file, timeouts());
Files.delete(file);
timeouts().restore(SPIGOT_KEY);
assertFalse(Files.readString(recoveryFile()).contains("spigot.yml"));
}
}
@Test
public void malformedRecoveryDataPreventsNewConfigurationWrites() throws Exception {
Path file = configuration("spigot.yml", "settings:\n timeout-time: 60\n");
Files.writeString(recoveryFile(), "{invalid");
assertThrows(IOException.class, () -> timeouts().raise(file, SPIGOT_KEY, 300L));
assertEquals(60L, load(file).getLong(SPIGOT_KEY));
assertEquals("{invalid", Files.readString(recoveryFile()));
}
private Path configuration(String name, String content) throws IOException {
Path file = temporaryFolder.newFile(name).toPath();
Files.writeString(file, content);
return file;
}
private ServerTimeoutConfiguration timeouts() {
return new ServerTimeoutConfiguration(recoveryFile());
}
private Path recoveryFile() {
return temporaryFolder.getRoot().toPath().resolve("server-timeouts.json");
}
private static YamlConfiguration load(Path file) throws Exception {
YamlConfiguration configuration = new YamlConfiguration();
configuration.load(file.toFile());
return configuration;
}
}
@@ -67,12 +67,26 @@ public class IrisWorldRemovalServiceTest {
assertTrue(result.succeeded());
assertTrue(result.registryChanged());
assertEquals(
List.of("resolve", "begin", "evacuate", "unload", "close", "unregister", "unregisterRegistry", "end"),
List.of("resolve", "evacuate", "begin", "unload", "close", "unregister", "unregisterRegistry", "end"),
backend.operations
);
assertTrue(coordinator.isIdle());
}
@Test
public void mantleBypassMaintenanceStartsOnlyAfterPlayersLeave() throws Exception {
LifecycleOperationCoordinator coordinator = new LifecycleOperationCoordinator();
FakeBackend backend = new FakeBackend(target("evacuation-failed"));
backend.evacuation = CompletableFuture.failedFuture(new IllegalStateException("no destination world"));
IrisWorldRemovalService service = new IrisWorldRemovalService(coordinator, backend);
IrisWorldRemovalService.RemovalResult result = service.remove("evacuation-failed", true).join();
assertEquals(IrisWorldRemovalService.RemovalStatus.TELEPORT_FAILED, result.status());
assertEquals(List.of("resolve", "evacuate"), backend.operations);
assertTrue(coordinator.isIdle());
}
@Test
public void configurationFailureStopsBeforeDeletionAndRetainsCause() throws Exception {
LifecycleOperationCoordinator coordinator = new LifecycleOperationCoordinator();
@@ -22,12 +22,37 @@ public class StudioOpenCoordinatorCloseSequenceTest {
StudioOpenCoordinator.sequenceStudioClose(
() -> phase(phases, "evacuate"),
() -> phases.add("maintenance"),
() -> phase(phases, "unload"),
() -> phase(phases, "close-generator"),
() -> phase(phases, "delete-folders")
).join();
assertEquals(List.of("evacuate", "unload", "close-generator", "delete-folders"), phases);
assertEquals(List.of("evacuate", "maintenance", "unload", "close-generator", "delete-folders"), phases);
}
@Test
public void evacuationFailureNeverEntersMaintenance() {
ArrayList<String> phases = new ArrayList<>();
IllegalStateException failure = new IllegalStateException("evacuation failed");
try {
StudioOpenCoordinator.sequenceStudioClose(
() -> {
phases.add("evacuate");
return CompletableFuture.failedFuture(failure);
},
() -> phases.add("maintenance"),
() -> phase(phases, "unload"),
() -> phase(phases, "close-generator"),
() -> phase(phases, "delete-folders")
).join();
fail("Expected evacuation failure");
} catch (CompletionException exception) {
assertSame(failure, exception.getCause());
}
assertEquals(List.of("evacuate"), phases);
}
@Test
@@ -38,6 +63,7 @@ public class StudioOpenCoordinatorCloseSequenceTest {
try {
StudioOpenCoordinator.sequenceStudioClose(
() -> phase(phases, "evacuate"),
() -> phases.add("maintenance"),
() -> {
phases.add("unload");
return CompletableFuture.failedFuture(failure);
@@ -50,7 +76,7 @@ public class StudioOpenCoordinatorCloseSequenceTest {
assertSame(failure, exception.getCause());
}
assertEquals(List.of("evacuate", "unload"), phases);
assertEquals(List.of("evacuate", "maintenance", "unload"), phases);
}
@Test
@@ -61,6 +87,7 @@ public class StudioOpenCoordinatorCloseSequenceTest {
try {
StudioOpenCoordinator.sequenceStudioClose(
() -> phase(phases, "evacuate"),
() -> phases.add("maintenance"),
() -> phase(phases, "unload"),
() -> {
phases.add("close-generator");
@@ -73,7 +100,7 @@ public class StudioOpenCoordinatorCloseSequenceTest {
assertSame(failure, exception.getCause());
}
assertEquals(List.of("evacuate", "unload", "close-generator"), phases);
assertEquals(List.of("evacuate", "maintenance", "unload", "close-generator"), phases);
}
@Test
@@ -84,6 +111,7 @@ public class StudioOpenCoordinatorCloseSequenceTest {
CompletableFuture<Void> close = StudioOpenCoordinator.sequenceStudioClose(
() -> phase(phases, "evacuate"),
() -> phases.add("maintenance"),
() -> {
phases.add("unload");
return unload;
@@ -100,7 +128,7 @@ public class StudioOpenCoordinatorCloseSequenceTest {
} catch (CompletionException exception) {
assertEquals("Studio close stopped after its terminal timeout.", exception.getCause().getMessage());
}
assertEquals(List.of("evacuate", "unload"), phases);
assertEquals(List.of("evacuate", "maintenance", "unload"), phases);
}
@Test
@@ -0,0 +1,52 @@
package art.arcane.iris.engine;
import art.arcane.iris.core.nms.INMS;
import art.arcane.iris.core.nms.INMSBinding;
import art.arcane.iris.engine.framework.Engine;
import org.bukkit.Chunk;
import org.junit.Test;
import org.mockito.MockedStatic;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class IrisWorldManagerNativePoiTest {
@Test
public void chunkLoadReconcilesPoisWithoutAnOptionalCleanupExecutor() {
IrisWorldManager manager = mock(IrisWorldManager.class, CALLS_REAL_METHODS);
Engine engine = mock(Engine.class);
Chunk chunk = mock(Chunk.class);
INMSBinding binding = mock(INMSBinding.class);
when(manager.getEngine()).thenReturn(engine);
try (MockedStatic<INMS> nms = mockStatic(INMS.class)) {
nms.when(INMS::get).thenReturn(binding);
manager.onChunkLoad(chunk, false);
}
verify(binding).reconcileNativeStructurePois(chunk);
}
@Test
public void chunkLoadPropagatesPoiFailuresToTheRuntimeEventReporter() {
IrisWorldManager manager = mock(IrisWorldManager.class, CALLS_REAL_METHODS);
Engine engine = mock(Engine.class);
Chunk chunk = mock(Chunk.class);
INMSBinding binding = mock(INMSBinding.class);
IllegalStateException failure = new IllegalStateException("Native POI storage failure");
when(manager.getEngine()).thenReturn(engine);
doThrow(failure).when(binding).reconcileNativeStructurePois(chunk);
try (MockedStatic<INMS> nms = mockStatic(INMS.class)) {
nms.when(INMS::get).thenReturn(binding);
assertSame(failure, assertThrows(IllegalStateException.class,
() -> manager.onChunkLoad(chunk, true)));
}
}
}
@@ -0,0 +1,154 @@
package art.arcane.iris.platform.bukkit;
import art.arcane.iris.spi.PlatformBiome;
import com.github.benmanes.caffeine.cache.Cache;
import org.bukkit.NamespacedKey;
import org.junit.Before;
import org.junit.Test;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public class BukkitBiomeTest {
private Class<?> biomeType;
private Method wrap;
@Before
public void loadAdapterWithoutServerRegistryInitialization() throws Exception {
ClassLoader loader = new BiomeClassLoader();
biomeType = loader.loadClass("org.bukkit.block.Biome");
wrap = loader.loadClass(BukkitBiome.class.getName()).getMethod("of", biomeType);
}
@Test
public void cachedHandlesKeepCanonicalIdentityWithoutReadingTheKeyAgain() throws Exception {
AtomicInteger keyReads = new AtomicInteger();
Object biome = biome("iris", "riverbank", keyReads);
PlatformBiome wrapped = wrap(biome);
assertEquals("iris:riverbank", wrapped.key());
assertEquals("iris", wrapped.namespace());
assertSame(biome, wrapped.nativeHandle());
assertSame(wrapped, wrap(biome));
assertSame(wrapped, wrap(biome));
assertEquals(1, keyReads.get());
}
@Test
public void replacementRegistryHandleCannotReturnThePreviousNativeBiome() throws Exception {
Object previous = biome("iris", "registry_replacement", new AtomicInteger());
Object replacement = biome("iris", "registry_replacement", new AtomicInteger());
PlatformBiome oldHandle = wrap(previous);
PlatformBiome newHandle = wrap(replacement);
assertNotSame(oldHandle, newHandle);
assertEquals(oldHandle.key(), newHandle.key());
assertSame(previous, oldHandle.nativeHandle());
assertSame(replacement, newHandle.nativeHandle());
assertSame(newHandle, wrap(replacement));
}
@Test
public void customAndVanillaNamespacesRemainDistinct() throws Exception {
PlatformBiome vanilla = wrap(biome("minecraft", "plains", new AtomicInteger()));
PlatformBiome custom = wrap(biome("iris", "plains", new AtomicInteger()));
assertEquals("minecraft:plains", vanilla.key());
assertEquals("minecraft", vanilla.namespace());
assertEquals("iris:plains", custom.key());
assertNotSame(vanilla, custom);
}
@Test
public void cacheEvictionPreservesBiomeKeysAndNativeHandles() throws Exception {
Object retained = biome("iris", "retained", new AtomicInteger());
PlatformBiome original = wrap(retained);
for (int index = 0; index < 8_192; index++) {
wrap(biome("iris", "eviction_" + index, new AtomicInteger()));
}
Field field = wrap.getDeclaringClass().getDeclaredField("CACHE");
field.setAccessible(true);
Cache<?, ?> cache = (Cache<?, ?>) field.get(null);
cache.cleanUp();
assertTrue(cache.estimatedSize() <= 4_096);
PlatformBiome restored = wrap(retained);
assertEquals(original.key(), restored.key());
assertEquals(original.namespace(), restored.namespace());
assertSame(retained, restored.nativeHandle());
}
private PlatformBiome wrap(Object biome) throws Exception {
return (PlatformBiome) wrap.invoke(null, biome);
}
private Object biome(String namespace, String name, AtomicInteger keyReads) {
NamespacedKey key = new NamespacedKey(namespace, name);
return Proxy.newProxyInstance(biomeType.getClassLoader(), new Class<?>[]{biomeType}, (proxy, method, arguments) -> switch (method.getName()) {
case "getKey" -> {
keyReads.incrementAndGet();
yield key;
}
case "hashCode" -> System.identityHashCode(proxy);
case "equals" -> proxy == arguments[0];
case "toString" -> key.toString();
default -> throw new UnsupportedOperationException(method.getName());
});
}
private static final class BiomeClassLoader extends ClassLoader {
private BiomeClassLoader() {
super(BukkitBiomeTest.class.getClassLoader());
}
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (!name.equals("org.bukkit.block.Biome") && !name.equals(BukkitBiome.class.getName())) {
return super.loadClass(name, resolve);
}
synchronized (getClassLoadingLock(name)) {
Class<?> loaded = findLoadedClass(name);
if (loaded == null) {
try (InputStream source = getParent().getResourceAsStream(name.replace('.', '/') + ".class")) {
if (source == null) {
throw new ClassNotFoundException(name);
}
byte[] bytecode = source.readAllBytes();
if (name.equals("org.bukkit.block.Biome")) {
ClassWriter writer = new ClassWriter(0);
new ClassReader(bytecode).accept(new ClassVisitor(Opcodes.ASM9, writer) {
@Override
public MethodVisitor visitMethod(int access, String method, String descriptor, String signature, String[] exceptions) {
return method.equals("<clinit>") ? null : super.visitMethod(access, method, descriptor, signature, exceptions);
}
}, 0);
bytecode = writer.toByteArray();
}
loaded = defineClass(name, bytecode, 0, bytecode.length);
} catch (IOException exception) {
throw new ClassNotFoundException(name, exception);
}
}
if (resolve) {
resolveClass(loaded);
}
return loaded;
}
}
}
}