-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Join a dedicated server in the automated client test. (#4057)
* Join a dedicated server in the automated client test. * Add missing header
- Loading branch information
Showing
5 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/TestDedicatedServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.test.base.client; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
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; | ||
|
||
public class TestDedicatedServer implements Closeable { | ||
public static final AtomicReference<MinecraftDedicatedServer> DEDICATED_SERVER_REF = new AtomicReference<>(); | ||
private static final Duration START_TIMEOUT = Duration.ofMinutes(5); | ||
|
||
final ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
MinecraftDedicatedServer server; | ||
|
||
public TestDedicatedServer() { | ||
assert DEDICATED_SERVER_REF.get() == null : "A dedicated server is already running"; | ||
executor.execute(this::run); | ||
waitUntilReady(); | ||
Objects.requireNonNull(server); | ||
} | ||
|
||
public String getConnectionAddress() { | ||
return "localhost:" + server.getServerPort(); | ||
} | ||
|
||
public void runCommand(String command) { | ||
submitAndWait(server -> { | ||
server.enqueueCommand(command, server.getCommandSource()); | ||
return null; | ||
}); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
private void setupServer() { | ||
try { | ||
Files.writeString(Paths.get("eula.txt"), "eula=true"); | ||
Files.writeString(Paths.get("server.properties"), "online-mode=false"); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private void waitUntilReady() { | ||
long startTime = System.currentTimeMillis(); | ||
|
||
while (DEDICATED_SERVER_REF.get() == null) { | ||
if (System.currentTimeMillis() - startTime > START_TIMEOUT.toMillis()) { | ||
throw new RuntimeException("Timeout while waiting for the server to start"); | ||
} | ||
|
||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
server = DEDICATED_SERVER_REF.get(); | ||
DEDICATED_SERVER_REF.set(null); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
server.stop(true); | ||
executor.close(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...Client/java/net/fabricmc/fabric/test/base/client/mixin/MinecraftDedicatedServerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.test.base.client.mixin; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import net.minecraft.server.dedicated.MinecraftDedicatedServer; | ||
|
||
import net.fabricmc.fabric.test.base.client.TestDedicatedServer; | ||
|
||
@Mixin(MinecraftDedicatedServer.class) | ||
public abstract class MinecraftDedicatedServerMixin { | ||
@Inject(method = "setupServer", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/dedicated/MinecraftDedicatedServer;loadWorld()V")) | ||
private void captureServerInstance(CallbackInfoReturnable<Boolean> cir) { | ||
// Capture the server instance once the server is ready to be connected to | ||
TestDedicatedServer.DEDICATED_SERVER_REF.set((MinecraftDedicatedServer) (Object) this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters