Add ServerLoadPatch, ServerStoppingPatch, and ServerTickPatch to enhance server lifecycle handling in GameServer. Include tests, update B42PatchSet, and integrate lifecycle management utilities.
This commit is contained in:
1 parent
600373dc61
commit
837367047c
11 files changed
+787
-3
No files matched your search
@@ -6,4 +6,8 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
implementation(project(":api"))
|
||||
|
||||
testImplementation(platform(libs.junit.bom))
|
||||
testImplementation(libs.junit.jupiter)
|
||||
testRuntimeOnly(libs.junit.platform.launcher)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package dev.overlord.zomboidforge.core;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* Phase of a server run, advanced by the hooks in the patched game code. {@link #loaded()} and
|
||||
* {@link #started()} each happen once and in this order; {@link #stopping()} can follow any phase,
|
||||
* because the JVM can be stopped at any time.
|
||||
*/
|
||||
public final class Lifecycle
|
||||
{
|
||||
public enum Phase
|
||||
{
|
||||
/** The JVM runs; mods are not loaded yet. */
|
||||
BOOTING,
|
||||
/** Mods are loaded; the game still loads the world and opens the network port. */
|
||||
LOADED,
|
||||
/** The main loop runs. */
|
||||
RUNNING,
|
||||
/** The shutdown started; the world is about to be saved. */
|
||||
STOPPING
|
||||
}
|
||||
|
||||
private final AtomicReference<Phase> phase = new AtomicReference<>(Phase.BOOTING);
|
||||
|
||||
public Phase phase() { return this.phase.get(); }
|
||||
|
||||
/** {@link Phase#BOOTING} to {@link Phase#LOADED}. */
|
||||
public void loaded() { this.advance(Phase.BOOTING, Phase.LOADED); }
|
||||
|
||||
/** {@link Phase#LOADED} to {@link Phase#RUNNING}. */
|
||||
public void started() { this.advance(Phase.LOADED, Phase.RUNNING); }
|
||||
|
||||
/**
|
||||
* Any phase to {@link Phase#STOPPING}.
|
||||
*
|
||||
* @return {@code false} when the phase already was {@link Phase#STOPPING}
|
||||
*/
|
||||
public boolean stopping() { return this.phase.getAndSet(Phase.STOPPING) != Phase.STOPPING; }
|
||||
|
||||
private void advance(Phase from, Phase to)
|
||||
{
|
||||
Phase current = this.phase.compareAndExchange(from, to);
|
||||
|
||||
if (current != from) throw new IllegalStateException("Cannot enter phase '" + to + "' from '" + current + "', only from '" + from + "'.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package dev.overlord.zomboidforge.core;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class LifecycleTest
|
||||
{
|
||||
@Test
|
||||
void advancesInOrder()
|
||||
{
|
||||
Lifecycle lifecycle = new Lifecycle();
|
||||
assertEquals(Lifecycle.Phase.BOOTING, lifecycle.phase());
|
||||
|
||||
lifecycle.loaded();
|
||||
assertEquals(Lifecycle.Phase.LOADED, lifecycle.phase());
|
||||
|
||||
lifecycle.started();
|
||||
assertEquals(Lifecycle.Phase.RUNNING, lifecycle.phase());
|
||||
|
||||
assertTrue(lifecycle.stopping());
|
||||
assertEquals(Lifecycle.Phase.STOPPING, lifecycle.phase());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsSkippedOrRepeatedPhases()
|
||||
{
|
||||
Lifecycle lifecycle = new Lifecycle();
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class, lifecycle::started);
|
||||
assertEquals("Cannot enter phase 'RUNNING' from 'BOOTING', only from 'LOADED'.", e.getMessage());
|
||||
assertEquals(Lifecycle.Phase.BOOTING, lifecycle.phase());
|
||||
|
||||
lifecycle.loaded();
|
||||
assertThrows(IllegalStateException.class, lifecycle::loaded);
|
||||
assertEquals(Lifecycle.Phase.LOADED, lifecycle.phase());
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopsFromAnyPhaseOnce()
|
||||
{
|
||||
Lifecycle booting = new Lifecycle();
|
||||
assertTrue(booting.stopping());
|
||||
assertFalse(booting.stopping());
|
||||
|
||||
Lifecycle loaded = new Lifecycle();
|
||||
loaded.loaded();
|
||||
assertTrue(loaded.stopping());
|
||||
assertThrows(IllegalStateException.class, loaded::started);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user