Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement changes to client auto-test threading #4256

Merged
merged 6 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@

import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.clickScreenButton;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.closeScreen;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.computeOnClient;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.connectToServer;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.enableDebugHud;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.openGameMenu;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.openInventory;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.setPerspective;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.submitAndWait;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.takeScreenshot;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.waitForLoadingComplete;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.waitForScreen;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.waitForServerStop;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.waitForTitleScreenFade;
import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.waitForWorldTicks;

Expand All @@ -35,7 +36,6 @@
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;

import com.mojang.authlib.GameProfile;
import org.spongepowered.asm.mixin.MixinEnvironment;
Expand All @@ -54,28 +54,21 @@
import net.fabricmc.loader.api.FabricLoader;

public class FabricApiAutoTestClient implements ClientModInitializer {
public static final boolean IS_AUTO_TEST = System.getProperty("fabric.autoTest") != null;

@Override
public void onInitializeClient() {
if (System.getProperty("fabric.autoTest") == null) {
if (!IS_AUTO_TEST) {
return;
}

var thread = new Thread(() -> {
try {
runTest();
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
});
thread.setName("Fabric Auto Test");
thread.start();
ThreadingImpl.runTestThread(this::runTest);
}

private void runTest() {
waitForLoadingComplete();

final boolean onboardAccessibility = submitAndWait(client -> client.options.onboardAccessibility);
final boolean onboardAccessibility = computeOnClient(client -> client.options.onboardAccessibility);

if (onboardAccessibility) {
waitForScreen(AccessibilityOnboardingScreen.class);
Expand All @@ -86,7 +79,7 @@ private void runTest() {
{
waitForScreen(TitleScreen.class);
waitForTitleScreenFade();
takeScreenshot("title_screen", Duration.ZERO);
takeScreenshot("title_screen", 0);
clickScreenButton("menu.singleplayer");
}

Expand All @@ -113,7 +106,7 @@ private void runTest() {
{
enableDebugHud();
waitForWorldTicks(200);
takeScreenshot("in_game_overworld", Duration.ZERO);
takeScreenshot("in_game_overworld", 0);
}

MixinEnvironment.getCurrentEnvironment().audit();
Expand All @@ -136,18 +129,19 @@ private void runTest() {
takeScreenshot("game_menu");
clickScreenButton("menu.returnToMenu");
waitForScreen(TitleScreen.class);
waitForServerStop();
}

try (var server = new TestDedicatedServer()) {
connectToServer(server);
waitForWorldTicks(5);

final GameProfile profile = submitAndWait(MinecraftClient::getGameProfile);
final GameProfile profile = computeOnClient(MinecraftClient::getGameProfile);
server.runCommand("op " + profile.getName());
server.runCommand("gamemode creative " + profile.getName());

waitForWorldTicks(20);
takeScreenshot("server_in_game", Duration.ZERO);
takeScreenshot("server_in_game", 0);

{ // Test that we can enter and exit configuration
server.runCommand("debugconfig config " + profile.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package net.fabricmc.fabric.test.base.client;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Predicate;

import org.apache.commons.lang3.function.FailableConsumer;
import org.apache.commons.lang3.function.FailableFunction;
import org.apache.commons.lang3.mutable.MutableObject;

import net.minecraft.SharedConstants;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Drawable;
import net.minecraft.client.gui.screen.GameMenuScreen;
Expand All @@ -48,10 +50,21 @@
import net.fabricmc.fabric.test.base.client.mixin.TitleScreenAccessor;
import net.fabricmc.loader.api.FabricLoader;

// Provides thread safe utils for interacting with a running game.
public final class FabricClientTestHelper {
public static void waitForLoadingComplete() {
waitFor("Loading to complete", client -> client.getOverlay() == null, Duration.ofMinutes(5));
// client is not ticking and can't accept tasks, waitFor doesn't work so we'll do this until then
while (!ThreadingImpl.clientCanAcceptTasks) {
runTick();

try {
//noinspection BusyWait
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

waitFor("Loading to complete", client -> client.getOverlay() == null, 5 * SharedConstants.TICKS_PER_MINUTE);
}

public static void waitForScreen(Class<? extends Screen> screenClass) {
Expand All @@ -66,7 +79,7 @@ public static void openGameMenu() {
public static void openInventory() {
setScreen((client) -> new InventoryScreen(Objects.requireNonNull(client.player)));

boolean creative = submitAndWait(client -> Objects.requireNonNull(client.player).isCreative());
boolean creative = computeOnClient(client -> Objects.requireNonNull(client.player).isCreative());
waitForScreen(creative ? CreativeInventoryScreen.class : InventoryScreen.class);
}

Expand All @@ -75,24 +88,20 @@ public static void closeScreen() {
}

private static void setScreen(Function<MinecraftClient, Screen> screenSupplier) {
submit(client -> {
client.setScreen(screenSupplier.apply(client));
return null;
});
runOnClient(client -> client.setScreen(screenSupplier.apply(client)));
}

public static void takeScreenshot(String name) {
takeScreenshot(name, Duration.ofMillis(50));
takeScreenshot(name, 1);
}

public static void takeScreenshot(String name, Duration delay) {
public static void takeScreenshot(String name, int delayTicks) {
// Allow time for any screens to open
waitFor(delay);
runTicks(delayTicks);

submitAndWait(client -> {
runOnClient(client -> {
ScreenshotRecorder.saveScreenshot(FabricLoader.getInstance().getGameDir().toFile(), name + ".png", client.getFramebuffer(), (message) -> {
});
return null;
});
}

Expand Down Expand Up @@ -145,30 +154,23 @@ private static boolean pressMatchingButton(ClickableWidget widget, String text)

public static void waitForWorldTicks(long ticks) {
// Wait for the world to be loaded and get the start ticks
waitFor("World load", client -> client.world != null && !(client.currentScreen instanceof LevelLoadingScreen), Duration.ofMinutes(30));
final long startTicks = submitAndWait(client -> client.world.getTime());
waitFor("World load", client -> Objects.requireNonNull(client.world).getTime() > startTicks + ticks, Duration.ofMinutes(10));
waitFor("World load", client -> client.world != null && !(client.currentScreen instanceof LevelLoadingScreen), 30 * SharedConstants.TICKS_PER_MINUTE);
final long startTicks = computeOnClient(client -> client.world.getTime());
waitFor("World load", client -> Objects.requireNonNull(client.world).getTime() > startTicks + ticks, 10 * SharedConstants.TICKS_PER_MINUTE);
}

public static void enableDebugHud() {
submitAndWait(client -> {
client.inGameHud.getDebugHud().toggleDebugHud();
return null;
});
runOnClient(client -> client.inGameHud.getDebugHud().toggleDebugHud());
}

public static void setPerspective(Perspective perspective) {
submitAndWait(client -> {
client.options.setPerspective(perspective);
return null;
});
runOnClient(client -> client.options.setPerspective(perspective));
}

public static void connectToServer(TestDedicatedServer server) {
submitAndWait(client -> {
runOnClient(client -> {
final var serverInfo = new ServerInfo("localhost", server.getConnectionAddress(), ServerInfo.ServerType.OTHER);
ConnectScreen.connect(client.currentScreen, client, ServerAddress.parse(server.getConnectionAddress()), serverInfo, false, null);
return null;
});
}

Expand All @@ -182,41 +184,43 @@ public static void waitForTitleScreenFade() {
});
}

private static void waitFor(String what, Predicate<MinecraftClient> predicate) {
waitFor(what, predicate, Duration.ofSeconds(10));
public static void waitForServerStop() {
waitFor("Server stop", client -> !ThreadingImpl.isServerRunning, SharedConstants.TICKS_PER_MINUTE);
}

private static void waitFor(String what, Predicate<MinecraftClient> predicate, Duration timeout) {
final LocalDateTime end = LocalDateTime.now().plus(timeout);

while (true) {
boolean result = submitAndWait(predicate::test);
private static void waitFor(String what, Predicate<MinecraftClient> predicate) {
waitFor(what, predicate, 10 * SharedConstants.TICKS_PER_SECOND);
}

if (result) {
break;
}
private static void waitFor(String what, Predicate<MinecraftClient> predicate, int timeoutTicks) {
int tickCount;

if (LocalDateTime.now().isAfter(end)) {
throw new RuntimeException("Timed out waiting for " + what);
}
for (tickCount = 0; tickCount < timeoutTicks && !computeOnClient(predicate::test); tickCount++) {
runTick();
}

waitFor(Duration.ofMillis(50));
if (tickCount == timeoutTicks && !computeOnClient(predicate::test)) {
throw new RuntimeException("Timed out waiting for " + what);
}
}

private static void waitFor(Duration duration) {
try {
Thread.sleep(duration.toMillis());
} catch (InterruptedException e) {
throw new RuntimeException(e);
public static void runTicks(int ticks) {
for (int i = 0; i < ticks; i++) {
runTick();
}
}

private static <T> CompletableFuture<T> submit(Function<MinecraftClient, T> function) {
return MinecraftClient.getInstance().submit(() -> function.apply(MinecraftClient.getInstance()));
public static void runTick() {
ThreadingImpl.runTick();
}

public static <E extends Throwable> void runOnClient(FailableConsumer<MinecraftClient, E> action) throws E {
ThreadingImpl.runOnClient(() -> action.accept(MinecraftClient.getInstance()));
}

public static <T> T submitAndWait(Function<MinecraftClient, T> function) {
return submit(function).join();
public static <T, E extends Throwable> T computeOnClient(FailableFunction<MinecraftClient, T, E> action) throws E {
MutableObject<T> result = new MutableObject<>();
runOnClient(minecraft -> result.setValue(action.apply(minecraft)));
return result.getValue();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Calling it getFromClient() instead of computeOnClient() would make it clearer that this method returns a value while runOnClient() doesn't.

Your use of compute here isn't wrong, I believe the Java API does something similar, but I think many people use run and compute synonymously and wouldn't immediately grasp that this is the difference between them. get vs run makes it really obvious.

Copy link
Contributor Author

@Earthcomputer Earthcomputer Nov 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this is the more common terminology, although I'm not super attached to it. But it should be immediately obvious what the difference between the two functions are from the parameter type

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like simple language, but yeah compute is pretty common too. I don't think parameter/return types are usually the first thing I would look at when working with a new API, but then again this will eventually have Javadoc that can make it obvious more quickly.

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;

import net.minecraft.server.Main;
import net.minecraft.server.dedicated.MinecraftDedicatedServer;
Expand All @@ -51,25 +49,14 @@ public String getConnectionAddress() {
}

public void runCommand(String command) {
submitAndWait(server -> {
server.enqueueCommand(command, server.getCommandSource());
return null;
});
ThreadingImpl.runOnServer(() -> server.getCommandManager().executeWithPrefix(server.getCommandSource(), command));
}

private void run() {
setupServer();
Main.main(new String[]{});
}

private <T> CompletableFuture<T> submit(Function<MinecraftDedicatedServer, T> function) {
return server.submit(() -> function.apply(server));
}

private <T> T submitAndWait(Function<MinecraftDedicatedServer, T> function) {
return submit(function).join();
}

Comment on lines -65 to -72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there no runOnServer() / computeOnServer() helpers here? I mean, I can't say that I've used the old TestDedicatedServer.submitAndWait() in actual test code, but this seems like something that more server-heavy mods would find useful in their tests. Obviously one can run ThreadingImpl.runOnServer() directly instead, as you've done above, but it feels inconsistent to have these helpers on one side and not the other.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were private and unused so I deleted them, the equivalent is in ThreadingImpl. We can think about what helpers are useful to expose in a future PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

private void setupServer() {
try {
Files.writeString(Paths.get("eula.txt"), "eula=true");
Expand Down Expand Up @@ -100,7 +87,12 @@ private void waitUntilReady() {

@Override
public void close() {
server.stop(true);
server.stop(false);

while (server.getThread().isAlive()) {
ThreadingImpl.runTick();
}

executor.close();
}
}
Loading