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

Conversation

Earthcomputer
Copy link
Contributor

Implement changes to client auto-test threading as discussed on Discord. More detailed implementation notes can be found on the docs to ThreadingImpl.

I didn't put much thought into class naming, or what should or shouldn't be in the API, etc, as this is all still internal, and deciding on a nice clean API is left for future work.

@modmuss50 modmuss50 added enhancement New feature or request test labels Nov 29, 2024
Copy link
Contributor

@Alexander01998 Alexander01998 left a comment

Choose a reason for hiding this comment

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

This looks really solid overall! 👍 I won't pretend to understand all of the multithreading logic, but we can see from the tests that it works and I can also see that you've put a lot of effort into making sure every edge case has safety checks included.

Comment on lines 221 to 225
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.

Comment on lines -65 to -72
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();
}

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.

Copy link
Member

@modmuss50 modmuss50 left a comment

Choose a reason for hiding this comment

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

Ill be honest I dont full understand the impl, but the principle is sound.

public static Runnable taskToRun = null;

public static void enterPhase(int phase) {
while ((PHASER.getPhase() & PHASE_MASK) != phase) {
Copy link
Member

Choose a reason for hiding this comment

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

This is the only bit that is midly confusing to me (I think), is this method blocking until the current phase has been reached from somewhere else? My tottal lack of knowledge of Phaser doesnt help

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method blocks until the given tick phase has been reached on all threads registered to the Phaser ("parties"), while allowing other parties to advance through the other tick phases (which is what the loop is for). The arriveAndAwaitAdvance is a barrier that all parties must call before they are all unblocked at once.

A Phaser maintains a counter called the "phase" which increments each time a barrier is crossed. It starts at 0 and when it surpasses Integer.MAX_VALUE it resets back to 0 again (negative values happen when the Phaser is terminated, which is not functionality we're using). Using the lower 2 bits of this counter that already exists tells us which of our 4 tick phases we're currently in. It's possible to track that separately by subclassing the Phaser and overriding its onAdvance method, but I figured why not take advantage of a counter that already exists.

@modmuss50 modmuss50 added last call If you care, make yourself heard right away! merge me please Pull requests that are ready to merge and removed last call If you care, make yourself heard right away! labels Dec 5, 2024
@modmuss50 modmuss50 merged commit 453d4f9 into FabricMC:1.21.4 Dec 5, 2024
4 checks passed
@Earthcomputer Earthcomputer deleted the client-test-threading branch December 5, 2024 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request merge me please Pull requests that are ready to merge test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants