Run module warm-ups with the generation cache warm-up, and for hotloaded and restarted worlds

IrisEngine warms modules after GenerationCacheWarmer for the first world (studio in
the background, server worlds synchronously) and after every hotload or in-place
restart; generation waits for all of them through GenerationCacheWarmGate.
This commit is contained in:
piexel committed 2026-10-09 20:24:49 +02:00
1 parent d6228c3074
commit 2775a4965b
7 files changed
+367 -24

No files matched your search

@@ -108,6 +108,11 @@ public class ModuleWorldsTest {
world.state(Remembered.class, ignored -> new Remembered(calls));
}
@Override
public void warmUp(PackWorld world) {
record("warmed");
}
@Override
public void worldStopped(PackWorld world) {
record("stopped");
@@ -198,6 +203,40 @@ public class ModuleWorldsTest {
assertThrows(IllegalStateException.class, () -> world.state(Remembered.class, ignored -> null));
}
@Test
public void theWarmUpRunsOnlyForAStartedWorldAndWarmsTheWorldItStarted() {
EngineGeneration generation = generation();
generation.warmModules();
assertEquals("nothing started yet", List.of(), calls);
generation.startModules();
generation.warmModules();
assertEquals(List.of("loaded", "started", "warmed"), calls);
reloadResources();
generation.restartModules();
calls.clear();
generation.warmModules();
assertEquals("the restarted world is warmed too", List.of("warmed"), calls);
generation.close();
calls.clear();
generation.warmModules();
assertEquals("a closed runtime warms nothing", List.of(), calls);
}
@Test
public void aFailedWarmUpNamesTheModuleAndLeavesTheWorldRunning() {
failingHook = "warmed";
EngineGeneration generation = generation();
generation.startModules();
IllegalStateException failure = assertThrows(IllegalStateException.class, generation::warmModules);
assertTrue(failure.getMessage(), failure.getMessage().contains("test:recording"));
generation.world().state(Remembered.class, ignored -> new Remembered(calls));
}
@Test
public void everyWorldOfOneSnapshotStartsButThePackLoadsOnce() {
EngineGeneration first = generation();
@@ -0,0 +1,177 @@
/*
* Iris is a World Generator for Minecraft Servers
* Copyright (c) 2026 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.engine;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.core.modules.ModuleResourceFixture;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.framework.EngineGeneration;
import art.arcane.iris.engine.framework.SeedManager;
import com.volmit.FeatureRegistry;
import com.volmit.IrisModule;
import com.volmit.ModuleInfo;
import com.volmit.ModuleLifecycle;
import com.volmit.stage.StageGraph;
import com.volmit.world.PackWorld;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class GenerationCacheWarmGateTest {
private static final long WAIT_SECONDS = 10;
@Test
public void theFirstWorldIsWarmOnlyAfterTheHostAndThenTheModulesWarmedUp() {
GenerationCacheWarmGate gate = new GenerationCacheWarmGate();
List<String> steps = new ArrayList<>();
assertTrue(gate.isPending());
gate.warmFirstWorld(() -> {
steps.add("host");
assertTrue("modules warm up after the host", gate.isPending());
}, () -> {
steps.add("modules");
assertTrue(gate.isPending());
});
assertEquals(List.of("host", "modules"), steps);
assertFalse(gate.isPending());
gate.await();
}
/** A module whose warm-up takes as long as the test says, and records the world it warmed. */
private static final class SlowWarmUpModule implements IrisModule, ModuleLifecycle {
private final CountDownLatch warming = new CountDownLatch(1);
private final CountDownLatch release = new CountDownLatch(1);
private volatile PackWorld warmed;
@Override
public ModuleInfo info() {
return ModuleInfo.of("test:slow", "Slow", "1");
}
@Override
public void install(StageGraph graph) {
}
@Override
public void register(FeatureRegistry registry) {
registry.registerLifecycle(this);
}
@Override
public void warmUp(PackWorld world) {
warming.countDown();
awaitQuietly(release);
warmed = world;
}
}
private static EngineGeneration generationOf(SlowWarmUpModule module) {
IrisData data = mock(IrisData.class);
ModuleResourceFixture.serve(data);
ModuleResourceFixture.serveModules(data, module);
SeedManager seeds = mock(SeedManager.class);
Engine engine = mock(Engine.class);
when(engine.getData()).thenReturn(data);
when(engine.getSeedManager()).thenReturn(seeds);
return new EngineGeneration(engine);
}
@Test
public void generationWaitsForAModulesSlowWarmUp() throws Exception {
GenerationCacheWarmGate gate = new GenerationCacheWarmGate();
SlowWarmUpModule module = new SlowWarmUpModule();
EngineGeneration generation = generationOf(module);
generation.startModules();
ExecutorService background = Executors.newSingleThreadExecutor();
try {
CompletableFuture<Void> warmUp = CompletableFuture.runAsync(
() -> gate.warmFirstWorld(() -> {
}, generation::warmModules), background);
assertTrue(module.warming.await(WAIT_SECONDS, TimeUnit.SECONDS));
CompletableFuture<Void> generating = CompletableFuture.runAsync(gate::await);
assertThrows(TimeoutException.class, () -> generating.get(200, TimeUnit.MILLISECONDS));
assertTrue(gate.isPending());
module.release.countDown();
warmUp.get(WAIT_SECONDS, TimeUnit.SECONDS);
generating.get(WAIT_SECONDS, TimeUnit.SECONDS);
assertFalse(gate.isPending());
assertSame("the module warmed the world it started", generation.world(), module.warmed);
} finally {
module.release.countDown();
background.shutdownNow();
}
}
@Test
public void aFailedWarmUpFailsWhatWaitsForIt() {
GenerationCacheWarmGate gate = new GenerationCacheWarmGate();
assertThrows(IllegalStateException.class, () -> gate.warmFirstWorld(() -> {
throw new IllegalStateException("host cannot warm");
}, () -> {
}));
IllegalStateException refused = assertThrows(IllegalStateException.class, gate::await);
assertEquals("host cannot warm", refused.getCause().getMessage());
}
@Test
public void worldsStartedLaterHoldGenerationUntilTheyAreWarmToo() {
GenerationCacheWarmGate gate = new GenerationCacheWarmGate();
gate.skipFirstWorld();
assertFalse(gate.isPending());
CompletableFuture<Void> hotloaded = gate.beginRestartedWorld();
CompletableFuture<Void> restarted = gate.beginRestartedWorld();
assertTrue(gate.isPending());
hotloaded.complete(null);
assertTrue("the second world is still warming", gate.isPending());
restarted.complete(null);
assertFalse(gate.isPending());
gate.await();
}
private static void awaitQuietly(CountDownLatch latch) {
try {
latch.await(WAIT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
}
}
}