mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 16:54:04 +00:00
Created modded worlds off-thread
- /iris create, /iris world create, /iris world enable and /iris world replace-overworld acknowledge at once, build the world in the background, and report success or the existing failure messages to the sender when it finishes - Pack validation, the pack dimension check, generation history creation, pack data loading and the engine build run off the server thread; the server thread only registers the world, creates the level and publishes it - A second create of a world that is still being created, and delete, disable or update of that world, are refused until it is live - Server shutdown cancels in-flight creations, waits for an engine that is still building, and rolls back its level and registry entry - Startup restore and world creation share one dimension injection orchestrator, and abandoning an injection also closes an engine that was already built - Added the creating-in-background message to every locale file
This commit is contained in:
31 files changed
+1203
-378
No files matched your search
+166
-19
@@ -7,7 +7,11 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@@ -17,7 +21,7 @@ import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ModdedDimensionRestoreTest {
|
||||
public class ModdedDimensionInjectionTest {
|
||||
private static final long STEP_TIMEOUT_SECONDS = 10L;
|
||||
|
||||
@Test
|
||||
@@ -26,13 +30,13 @@ public class ModdedDimensionRestoreTest {
|
||||
RecordingSteps steps = new RecordingSteps(dimensions.size());
|
||||
steps.prepareBarrier = new CountDownLatch(dimensions.size());
|
||||
|
||||
List<ModdedDimensionRestore.Result> results;
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
List<ModdedDimensionInjection.Result> results;
|
||||
try (ModdedDimensionInjection<String, String, String> restore = ModdedDimensionInjection.start(dimensions, steps)) {
|
||||
results = restore.finish();
|
||||
}
|
||||
|
||||
assertEquals(dimensions.size(), results.size());
|
||||
for (ModdedDimensionRestore.Result result : results) {
|
||||
for (ModdedDimensionInjection.Result result : results) {
|
||||
assertNull(result.failure());
|
||||
}
|
||||
assertFalse(steps.prepareThreads.contains(Thread.currentThread()));
|
||||
@@ -44,7 +48,7 @@ public class ModdedDimensionRestoreTest {
|
||||
RecordingSteps steps = new RecordingSteps(dimensions.size());
|
||||
steps.buildBarrier = new CountDownLatch(dimensions.size());
|
||||
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
try (ModdedDimensionInjection<String, String, String> restore = ModdedDimensionInjection.start(dimensions, steps)) {
|
||||
restore.finish();
|
||||
}
|
||||
|
||||
@@ -61,8 +65,8 @@ public class ModdedDimensionRestoreTest {
|
||||
steps.prepareFailure = failure;
|
||||
steps.failingIndex = 1;
|
||||
|
||||
List<ModdedDimensionRestore.Result> results;
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
List<ModdedDimensionInjection.Result> results;
|
||||
try (ModdedDimensionInjection<String, String, String> restore = ModdedDimensionInjection.start(dimensions, steps)) {
|
||||
results = restore.finish();
|
||||
}
|
||||
|
||||
@@ -82,14 +86,14 @@ public class ModdedDimensionRestoreTest {
|
||||
steps.buildFailure = failure;
|
||||
steps.failingIndex = 0;
|
||||
|
||||
List<ModdedDimensionRestore.Result> results;
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
List<ModdedDimensionInjection.Result> results;
|
||||
try (ModdedDimensionInjection<String, String, String> restore = ModdedDimensionInjection.start(dimensions, steps)) {
|
||||
results = restore.finish();
|
||||
}
|
||||
|
||||
assertSame(failure, results.get(0).failure());
|
||||
assertNull(results.get(1).failure());
|
||||
assertEquals(List.of("level-0"), steps.abandoned);
|
||||
assertEquals(List.of("level-0:null"), steps.abandoned);
|
||||
assertEquals(List.of("level-1:engine-1"), steps.completed);
|
||||
}
|
||||
|
||||
@@ -101,8 +105,8 @@ public class ModdedDimensionRestoreTest {
|
||||
steps.beginFailure = failure;
|
||||
steps.failingIndex = 1;
|
||||
|
||||
List<ModdedDimensionRestore.Result> results;
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
List<ModdedDimensionInjection.Result> results;
|
||||
try (ModdedDimensionInjection<String, String, String> restore = ModdedDimensionInjection.start(dimensions, steps)) {
|
||||
results = restore.finish();
|
||||
}
|
||||
|
||||
@@ -126,7 +130,7 @@ public class ModdedDimensionRestoreTest {
|
||||
release.countDown();
|
||||
});
|
||||
|
||||
try (ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps)) {
|
||||
try (ModdedDimensionInjection<String, String, String> restore = ModdedDimensionInjection.start(dimensions, steps)) {
|
||||
restore.awaitPreparations();
|
||||
|
||||
assertEquals(dimensions.size(), steps.prepared.get());
|
||||
@@ -143,7 +147,7 @@ public class ModdedDimensionRestoreTest {
|
||||
List<ModdedDimensionRegistryStore.PersistentDimension> dimensions = dimensions(3);
|
||||
RecordingSteps steps = new RecordingSteps(dimensions.size());
|
||||
|
||||
ModdedDimensionRestore<String, String, String> restore = ModdedDimensionRestore.start(dimensions, steps);
|
||||
ModdedDimensionInjection<String, String, String> restore = ModdedDimensionInjection.start(dimensions, steps);
|
||||
restore.close();
|
||||
|
||||
assertTrue(steps.discardedAll.await(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS));
|
||||
@@ -151,6 +155,143 @@ public class ModdedDimensionRestoreTest {
|
||||
assertEquals(Set.of("prepared-0", "prepared-1", "prepared-2"), Set.copyOf(steps.discarded));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncInjectionReturnsAtOnceAndMutatesLevelsOnlyOnTheServerExecutor() throws Exception {
|
||||
ModdedDimensionRegistryStore.PersistentDimension dimension = dimensions(1).getFirst();
|
||||
RecordingSteps steps = new RecordingSteps(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
steps.prepareGate = release;
|
||||
ExecutorService server = Executors.newSingleThreadExecutor(Thread.ofPlatform().name("test-server").factory());
|
||||
try {
|
||||
Thread serverThread = server.submit(Thread::currentThread).get();
|
||||
ModdedDimensionInjection.Async<String, String, String> async =
|
||||
ModdedDimensionInjection.async(dimension, steps, server);
|
||||
async.start();
|
||||
|
||||
assertFalse(async.result().isDone());
|
||||
release.countDown();
|
||||
ModdedDimensionInjection.Result result = async.result().get(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||
|
||||
assertNull(result.failure());
|
||||
assertSame(dimension, result.dimension());
|
||||
assertEquals(List.of("level-0:engine-0"), steps.completed);
|
||||
assertEquals(Set.of(serverThread), steps.serverThreads);
|
||||
assertFalse(steps.prepareThreads.contains(serverThread));
|
||||
assertFalse(steps.prepareThreads.contains(Thread.currentThread()));
|
||||
assertFalse(steps.buildThreads.contains(serverThread));
|
||||
} finally {
|
||||
server.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncPreparationFailureCompletesWithoutCreatingALevel() throws Exception {
|
||||
ModdedDimensionRegistryStore.PersistentDimension dimension = dimensions(1).getFirst();
|
||||
RecordingSteps steps = new RecordingSteps(1);
|
||||
IllegalStateException failure = new IllegalStateException("pack refused");
|
||||
steps.prepareFailure = failure;
|
||||
steps.failingIndex = 0;
|
||||
ExecutorService server = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
ModdedDimensionInjection.Async<String, String, String> async =
|
||||
ModdedDimensionInjection.async(dimension, steps, server);
|
||||
async.start();
|
||||
|
||||
ModdedDimensionInjection.Result result = async.result().get(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||
|
||||
assertSame(failure, result.failure());
|
||||
assertTrue(steps.begun.isEmpty());
|
||||
assertTrue(steps.completed.isEmpty());
|
||||
} finally {
|
||||
server.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asyncEngineFailureAbandonsTheCreatedLevel() throws Exception {
|
||||
ModdedDimensionRegistryStore.PersistentDimension dimension = dimensions(1).getFirst();
|
||||
RecordingSteps steps = new RecordingSteps(1);
|
||||
IllegalStateException failure = new IllegalStateException("engine refused");
|
||||
steps.buildFailure = failure;
|
||||
steps.failingIndex = 0;
|
||||
ExecutorService server = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
ModdedDimensionInjection.Async<String, String, String> async =
|
||||
ModdedDimensionInjection.async(dimension, steps, server);
|
||||
async.start();
|
||||
|
||||
ModdedDimensionInjection.Result result = async.result().get(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||
|
||||
assertSame(failure, result.failure());
|
||||
assertEquals(List.of("level-0:null"), steps.abandoned);
|
||||
assertTrue(steps.completed.isEmpty());
|
||||
} finally {
|
||||
server.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cancellingWhilePreparingDiscardsThePreparationWithoutCreatingALevel() throws Exception {
|
||||
ModdedDimensionRegistryStore.PersistentDimension dimension = dimensions(1).getFirst();
|
||||
RecordingSteps steps = new RecordingSteps(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
steps.prepareGate = release;
|
||||
ExecutorService server = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
ModdedDimensionInjection.Async<String, String, String> async =
|
||||
ModdedDimensionInjection.async(dimension, steps, server);
|
||||
async.start();
|
||||
|
||||
server.submit(async::cancel).get(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||
release.countDown();
|
||||
ModdedDimensionInjection.Result result = async.result().get(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||
|
||||
assertTrue(result.failure() instanceof CancellationException);
|
||||
assertTrue(steps.discardedAll.await(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS));
|
||||
assertEquals(List.of("prepared-0"), steps.discarded);
|
||||
server.submit(() -> null).get(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||
assertTrue(steps.begun.isEmpty());
|
||||
} finally {
|
||||
server.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cancellingWhileBuildingWaitsForTheEngineAndAbandonsTheLevelWithIt() throws Exception {
|
||||
ModdedDimensionRegistryStore.PersistentDimension dimension = dimensions(1).getFirst();
|
||||
RecordingSteps steps = new RecordingSteps(1);
|
||||
CountDownLatch building = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
steps.buildStarted = building;
|
||||
steps.buildGate = release;
|
||||
ExecutorService server = Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
ModdedDimensionInjection.Async<String, String, String> async =
|
||||
ModdedDimensionInjection.async(dimension, steps, server);
|
||||
async.start();
|
||||
assertTrue(building.await(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS));
|
||||
|
||||
Future<?> cancelled = server.submit(async::cancel);
|
||||
Thread.ofPlatform().daemon(true).start(() -> {
|
||||
try {
|
||||
Thread.sleep(200L);
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
release.countDown();
|
||||
});
|
||||
cancelled.get(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||
ModdedDimensionInjection.Result result = async.result().get(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||
server.submit(() -> null).get(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||
|
||||
assertTrue(result.failure() instanceof CancellationException);
|
||||
assertEquals(List.of("level-0:engine-0"), steps.abandoned);
|
||||
assertTrue(steps.completed.isEmpty());
|
||||
} finally {
|
||||
server.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<ModdedDimensionRegistryStore.PersistentDimension> dimensions(int count) {
|
||||
List<ModdedDimensionRegistryStore.PersistentDimension> dimensions = new ArrayList<>(count);
|
||||
for (int index = 0; index < count; index++) {
|
||||
@@ -189,15 +330,15 @@ public class ModdedDimensionRestoreTest {
|
||||
}
|
||||
try {
|
||||
if (!gate.await(STEP_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
|
||||
throw new IllegalStateException("Preparation gate never opened");
|
||||
throw new IllegalStateException("Restore step gate never opened");
|
||||
}
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Interrupted while waiting for the preparation gate", interrupted);
|
||||
throw new IllegalStateException("Interrupted while waiting for a restore step gate", interrupted);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class RecordingSteps implements ModdedDimensionRestore.Steps<String, String, String> {
|
||||
private static final class RecordingSteps implements ModdedDimensionInjection.Steps<String, String, String> {
|
||||
private final Set<Thread> prepareThreads = ConcurrentHashMap.newKeySet();
|
||||
private final Set<Thread> buildThreads = ConcurrentHashMap.newKeySet();
|
||||
private final Set<Thread> serverThreads = ConcurrentHashMap.newKeySet();
|
||||
@@ -210,6 +351,8 @@ public class ModdedDimensionRestoreTest {
|
||||
private CountDownLatch prepareGate;
|
||||
private CountDownLatch prepareBarrier;
|
||||
private CountDownLatch buildBarrier;
|
||||
private CountDownLatch buildStarted;
|
||||
private CountDownLatch buildGate;
|
||||
private RuntimeException prepareFailure;
|
||||
private RuntimeException beginFailure;
|
||||
private RuntimeException buildFailure;
|
||||
@@ -248,6 +391,10 @@ public class ModdedDimensionRestoreTest {
|
||||
public String build(String level, String prepared) {
|
||||
buildThreads.add(Thread.currentThread());
|
||||
await(buildBarrier);
|
||||
if (buildStarted != null) {
|
||||
buildStarted.countDown();
|
||||
}
|
||||
awaitGate(buildGate);
|
||||
int index = index(level);
|
||||
if (buildFailure != null && index == failingIndex) {
|
||||
throw buildFailure;
|
||||
@@ -262,9 +409,9 @@ public class ModdedDimensionRestoreTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abandon(String level, Throwable failure) {
|
||||
public void abandon(String level, String engine, Throwable failure) {
|
||||
serverThreads.add(Thread.currentThread());
|
||||
abandoned.add(level);
|
||||
abandoned.add(level + ":" + engine);
|
||||
}
|
||||
|
||||
@Override
|
||||
+3
-3
@@ -36,7 +36,7 @@ public class ModdedStartupReinjectFailureTest {
|
||||
IllegalStateException failure = new IllegalStateException("Iris generation history is unusable for 'iris:moon'");
|
||||
|
||||
try (MockedStatic<ModdedIrisLog> log = mockStatic(ModdedIrisLog.class)) {
|
||||
ModdedStartup.finishDimensionRestore(ModdedDimensionRestore.start(List.of(moon), new FailingSteps(failure)));
|
||||
ModdedStartup.finishDimensionRestore(ModdedDimensionInjection.start(List.of(moon), new FailingSteps(failure)));
|
||||
|
||||
log.verify(() -> ModdedIrisLog.error(argThat((String line) -> line != null && line.contains("iris:moon")
|
||||
&& line.startsWith("Iris refused"))), times(1));
|
||||
@@ -66,7 +66,7 @@ public class ModdedStartupReinjectFailureTest {
|
||||
verify(laterOperator, never()).sendMessage(any());
|
||||
}
|
||||
|
||||
private record FailingSteps(RuntimeException failure) implements ModdedDimensionRestore.Steps<String, String, String> {
|
||||
private record FailingSteps(RuntimeException failure) implements ModdedDimensionInjection.Steps<String, String, String> {
|
||||
@Override
|
||||
public String prepare(ModdedDimensionRegistryStore.PersistentDimension dimension) {
|
||||
throw failure;
|
||||
@@ -88,7 +88,7 @@ public class ModdedStartupReinjectFailureTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abandon(String level, Throwable failure) {
|
||||
public void abandon(String level, String engine, Throwable failure) {
|
||||
throw new AssertionError("A failed preparation has no level to abandon");
|
||||
}
|
||||
|
||||
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
package art.arcane.iris.modded.command;
|
||||
|
||||
import art.arcane.iris.modded.MissingPackDimensionException;
|
||||
import art.arcane.iris.modded.ModdedDimensionCreations;
|
||||
import art.arcane.iris.pack.BrokenPackException;
|
||||
import art.arcane.volmlib.nativelib.minecraft26_2.modded.NativeCommandSource;
|
||||
import art.arcane.volmlib.nativelib.minecraft26_2.modded.NativeCommandText;
|
||||
import art.arcane.volmlib.nativelib.minecraft26_2.modded.NativeModdedServer;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.server.Bootstrap;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ModdedWorldCreateCommandTest {
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@BeforeClass
|
||||
public static void bootstrap() {
|
||||
SharedConstants.tryDetectVersion();
|
||||
Bootstrap.bootStrap();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAcknowledgesAtOnceAndReportsTheLiveWorldFromALaterServerTask() throws Exception {
|
||||
CompletableFuture<Void> creation = new CompletableFuture<>();
|
||||
Harness harness = harness();
|
||||
|
||||
try (MockedStatic<ModdedPackCommands> packs = mockStatic(ModdedPackCommands.class);
|
||||
MockedStatic<ModdedDimensionCreations> creations = mockStatic(ModdedDimensionCreations.class)) {
|
||||
packs.when(ModdedPackCommands::packsRoot).thenReturn(harness.packs());
|
||||
creations.when(() -> ModdedDimensionCreations.create(harness.server(), "irisworldgen:moon", "overworld", "overworld", 7L))
|
||||
.thenReturn(creation);
|
||||
|
||||
assertEquals(1, ModdedWorldCommands.createWorld(harness.source(), "moon", "overworld", 7L));
|
||||
assertTrue(String.join("\n", harness.success()), harness.success().stream()
|
||||
.anyMatch((String line) -> line.contains("irisworldgen:moon") && !line.startsWith("Created")));
|
||||
assertFalse(harness.success().stream().anyMatch((String line) -> line.startsWith("Created Iris world")));
|
||||
|
||||
creation.complete(null);
|
||||
harness.runServerTasks();
|
||||
}
|
||||
|
||||
assertTrue(String.join("\n", harness.success()), harness.success().stream()
|
||||
.anyMatch((String line) -> line.startsWith("Created Iris world irisworldgen:moon from pack 'overworld'")));
|
||||
assertTrue(harness.failure().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void creationFailureIsReportedAsAFailedInjection() throws Exception {
|
||||
Harness harness = harness();
|
||||
List<String> failures = run(harness, new IllegalStateException("Iris generation history is unusable for 'irisworldgen:moon'."));
|
||||
|
||||
assertTrue(String.join("\n", failures), failures.stream().anyMatch((String line) ->
|
||||
line.startsWith("Failed to inject Iris world 'irisworldgen:moon'") && line.contains("generation history is unusable")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void brokenPackIsReportedWithItsValidationReasons() throws Exception {
|
||||
Harness harness = harness();
|
||||
List<String> failures = run(harness, new BrokenPackException("overworld", List.of("biomes/plains.json is invalid")));
|
||||
|
||||
assertTrue(String.join("\n", failures), failures.stream().anyMatch((String line) -> line.contains("irisworldgen:moon")
|
||||
&& line.contains("overworld")));
|
||||
assertTrue(String.join("\n", failures), failures.stream().anyMatch((String line) -> line.contains("biomes/plains.json is invalid")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingPackDimensionIsReportedAsAMissingDimensionFile() throws Exception {
|
||||
Harness harness = harness();
|
||||
List<String> failures = run(harness, new MissingPackDimensionException("overworld", "overworld"));
|
||||
|
||||
assertTrue(String.join("\n", failures), failures.stream().anyMatch((String line) ->
|
||||
line.contains("dimensions/overworld.json")));
|
||||
}
|
||||
|
||||
private List<String> run(Harness harness, RuntimeException failure) throws Exception {
|
||||
CompletableFuture<Void> creation = new CompletableFuture<>();
|
||||
try (MockedStatic<ModdedPackCommands> packs = mockStatic(ModdedPackCommands.class);
|
||||
MockedStatic<ModdedDimensionCreations> creations = mockStatic(ModdedDimensionCreations.class)) {
|
||||
packs.when(ModdedPackCommands::packsRoot).thenReturn(harness.packs());
|
||||
creations.when(() -> ModdedDimensionCreations.create(harness.server(), "irisworldgen:moon", "overworld", "overworld", 7L))
|
||||
.thenReturn(creation);
|
||||
|
||||
assertEquals(1, ModdedWorldCommands.createWorld(harness.source(), "moon", "overworld", 7L));
|
||||
assertTrue(harness.failure().isEmpty());
|
||||
creation.completeExceptionally(failure);
|
||||
harness.runServerTasks();
|
||||
}
|
||||
return harness.failure();
|
||||
}
|
||||
|
||||
private Harness harness() throws Exception {
|
||||
File packs = temporaryFolder.newFolder("packs");
|
||||
assertTrue(new File(packs, "overworld").mkdirs());
|
||||
NativeModdedServer server = mock(NativeModdedServer.class);
|
||||
List<Runnable> serverTasks = new ArrayList<>();
|
||||
doAnswer((InvocationOnMock invocation) -> {
|
||||
serverTasks.add(invocation.getArgument(0));
|
||||
return null;
|
||||
}).when(server).execute(any());
|
||||
List<String> success = new ArrayList<>();
|
||||
List<String> failure = new ArrayList<>();
|
||||
NativeCommandSource source = mock(NativeCommandSource.class);
|
||||
when(source.server()).thenReturn(server);
|
||||
when(source.isGameMaster()).thenReturn(true);
|
||||
doAnswer((InvocationOnMock invocation) -> {
|
||||
Supplier<NativeCommandText> text = invocation.getArgument(0);
|
||||
success.add(text.get().getString());
|
||||
return null;
|
||||
}).when(source).sendSuccess(any(), anyBoolean());
|
||||
doAnswer((InvocationOnMock invocation) -> {
|
||||
NativeCommandText text = invocation.getArgument(0);
|
||||
failure.add(text.getString());
|
||||
return null;
|
||||
}).when(source).sendFailure(any());
|
||||
return new Harness(packs, server, source, serverTasks, success, failure);
|
||||
}
|
||||
|
||||
private record Harness(
|
||||
File packs,
|
||||
NativeModdedServer server,
|
||||
NativeCommandSource source,
|
||||
List<Runnable> serverTasks,
|
||||
List<String> success,
|
||||
List<String> failure
|
||||
) {
|
||||
void runServerTasks() {
|
||||
while (!serverTasks.isEmpty()) {
|
||||
serverTasks.removeFirst().run();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user