Skip to content

Commit

Permalink
test: add NetworkMode setting to MTE (#5037)
Browse files Browse the repository at this point in the history
  • Loading branch information
keturn authored Jun 5, 2022
1 parent c42f936 commit 182f308
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.terasology.engine.core.subsystem.openvr.OpenVRInput;
import org.terasology.engine.integrationenvironment.jupiter.MTEExtension;
import org.terasology.engine.network.JoinStatus;
import org.terasology.engine.network.NetworkMode;
import org.terasology.engine.network.NetworkSystem;
import org.terasology.engine.registry.CoreRegistry;
import org.terasology.engine.rendering.opengl.ScreenGrabber;
Expand Down Expand Up @@ -80,8 +81,10 @@ public class Engines {
PathManager pathManager;
PathManagerProvider.Cleaner pathManagerCleaner;
TerasologyEngine host;
private final NetworkMode networkMode;

public Engines(Set<String> dependencies, String worldGeneratorUri) {
public Engines(Set<String> dependencies, String worldGeneratorUri, NetworkMode networkMode) {
this.networkMode = networkMode;
this.dependencies.addAll(dependencies);

if (worldGeneratorUri != null) {
Expand All @@ -97,7 +100,7 @@ public Engines(Set<String> dependencies, String worldGeneratorUri) {
public void setup() {
mockPathManager();
try {
host = createHost();
host = createHost(networkMode);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down Expand Up @@ -256,11 +259,11 @@ protected void mockPathManager() {
PathManagerProvider.setPathManager(pathManager);
}

TerasologyEngine createHost() throws IOException {
TerasologyEngine createHost(NetworkMode networkMode) throws IOException {
TerasologyEngine host = createHeadlessEngine();
host.getFromEngineContext(SystemConfig.class).writeSaveGamesEnabled.set(false);
host.subscribeToStateChange(new HeadlessStateChangeListener(host));
host.changeState(new TestingStateHeadlessSetup(dependencies, worldGeneratorUri));
host.changeState(new TestingStateHeadlessSetup(dependencies, worldGeneratorUri, networkMode));

doneLoading = false;
host.subscribeToStateChange(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.terasology.engine.core.TerasologyEngine;
import org.terasology.engine.core.subsystem.headless.mode.StateHeadlessSetup;
import org.terasology.engine.game.GameManifest;
import org.terasology.engine.network.NetworkMode;
import org.terasology.engine.world.time.WorldTime;
import org.terasology.gestalt.naming.Name;

Expand All @@ -33,7 +34,8 @@ public class TestingStateHeadlessSetup extends StateHeadlessSetup {
strictModuleRequirements = true;
}

public TestingStateHeadlessSetup(Collection<String> dependencies, String worldGeneratorUri) {
public TestingStateHeadlessSetup(Collection<String> dependencies, String worldGeneratorUri, NetworkMode networkMode) {
super(networkMode);
this.dependencies = dependencies;
this.worldGeneratorUri = new SimpleUri(worldGeneratorUri);
checkArgument(this.worldGeneratorUri.isValid(), "Not a valid URI `%s`", worldGeneratorUri);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine.integrationenvironment.jupiter;

import org.terasology.engine.network.NetworkMode;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface IntegrationEnvironment {
NetworkMode networkMode() default NetworkMode.LISTEN_SERVER;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
import org.opentest4j.MultipleFailuresError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.integrationenvironment.Engines;
import org.terasology.engine.integrationenvironment.MainLoop;
import org.terasology.engine.integrationenvironment.ModuleTestingHelper;
import org.terasology.engine.integrationenvironment.Scopes;
import org.terasology.engine.network.NetworkMode;
import org.terasology.engine.registry.In;
import org.terasology.unittest.worlds.DummyWorldGenerator;

Expand All @@ -25,6 +28,8 @@
import java.util.Set;
import java.util.function.Function;

import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;

/**
* Sets up a Terasology environment for use with your {@index JUnit} 5 test.
* <p>
Expand Down Expand Up @@ -80,6 +85,8 @@
*/
public class MTEExtension implements ParameterResolver, TestInstancePostProcessor {

private static final Logger logger = LoggerFactory.getLogger(MTEExtension.class);

protected Function<ExtensionContext, ExtensionContext.Namespace> helperLifecycle = Scopes.PER_CLASS;
protected Function<ExtensionContext, Class<?>> getTestClass = Scopes::getTopTestClass;

Expand Down Expand Up @@ -149,6 +156,17 @@ public Set<String> getDependencyNames(ExtensionContext context) {
return dependencies != null ? Sets.newHashSet(dependencies.value()) : Collections.emptySet();
}

public NetworkMode getNetworkMode(ExtensionContext context) {
return getAnnotationWithDefault(context, IntegrationEnvironment::networkMode);
}

private <T> T getAnnotationWithDefault(ExtensionContext context, Function<IntegrationEnvironment, T> method) {
var ann =
findAnnotation(context.getRequiredTestClass(), IntegrationEnvironment.class)
.orElseGet(ToReadDefaultValuesFrom::getDefaults);
return method.apply(ann);
}

/**
* Get the Engines for this test.
* <p>
Expand All @@ -165,7 +183,11 @@ public Set<String> getDependencyNames(ExtensionContext context) {
protected Engines getEngines(ExtensionContext context) {
ExtensionContext.Store store = context.getStore(helperLifecycle.apply(context));
EnginesCleaner autoCleaner = store.getOrComputeIfAbsent(
EnginesCleaner.class, k -> new EnginesCleaner(getDependencyNames(context), getWorldGeneratorUri(context)),
EnginesCleaner.class, k -> new EnginesCleaner(
getDependencyNames(context),
getWorldGeneratorUri(context),
getNetworkMode(context)
),
EnginesCleaner.class);
return autoCleaner.engines;
}
Expand All @@ -179,8 +201,8 @@ protected Engines getEngines(ExtensionContext context) {
static class EnginesCleaner implements ExtensionContext.Store.CloseableResource {
protected Engines engines;

EnginesCleaner(Set<String> dependencyNames, String worldGeneratorUri) {
engines = new Engines(dependencyNames, worldGeneratorUri);
EnginesCleaner(Set<String> dependencyNames, String worldGeneratorUri, NetworkMode networkMode) {
engines = new Engines(dependencyNames, worldGeneratorUri, networkMode);
engines.setup();
}

Expand All @@ -190,4 +212,11 @@ public void close() {
engines = null;
}
}

@IntegrationEnvironment
private static final class ToReadDefaultValuesFrom {
static IntegrationEnvironment getDefaults() {
return ToReadDefaultValuesFrom.class.getDeclaredAnnotation(IntegrationEnvironment.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.integrationenvironment;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.terasology.engine.integrationenvironment.jupiter.IntegrationEnvironment;
import org.terasology.engine.integrationenvironment.jupiter.MTEExtension;
import org.terasology.engine.logic.players.LocalPlayer;
import org.terasology.engine.network.NetworkMode;
import org.terasology.engine.network.NetworkSystem;

import static com.google.common.truth.Truth.assertThat;

@Tag("MteTest")
@ExtendWith(MTEExtension.class)
@IntegrationEnvironment(networkMode = NetworkMode.DEDICATED_SERVER)
public class NetworkModeLocalServerTest {
@Test
void testNetworkMode(NetworkSystem network) {
assertThat(network.getMode()).isEqualTo(NetworkMode.DEDICATED_SERVER);
}

@Test
void testForLocalPlayer(NetworkSystem network, LocalPlayer player) {
assertThat(network.getPlayers()).hasSize(1);
var client = network.getPlayers().iterator().next();
assertThat(client.isLocal()).isTrue();
assertThat(player.isValid()).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.integrationenvironment;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.terasology.engine.integrationenvironment.jupiter.IntegrationEnvironment;
import org.terasology.engine.integrationenvironment.jupiter.MTEExtension;
import org.terasology.engine.logic.players.LocalPlayer;
import org.terasology.engine.network.NetworkMode;
import org.terasology.engine.network.NetworkSystem;

import static com.google.common.truth.Truth.assertThat;

@Tag("MteTest")
@ExtendWith(MTEExtension.class)
@IntegrationEnvironment(networkMode = NetworkMode.NONE)
public class NetworkModeNoneTest {
@Test
void testNetworkMode(NetworkSystem network) {
assertThat(network.getMode()).isEqualTo(NetworkMode.NONE);
}

@Test
void testForLocalPlayer(NetworkSystem network, LocalPlayer player) {
assertThat(network.getPlayers()).hasSize(1);
var client = network.getPlayers().iterator().next();
assertThat(client.isLocal()).isTrue();
assertThat(player.isValid()).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.integrationenvironment;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.terasology.engine.integrationenvironment.jupiter.IntegrationEnvironment;
import org.terasology.engine.integrationenvironment.jupiter.MTEExtension;
import org.terasology.engine.logic.players.LocalPlayer;
import org.terasology.engine.network.NetworkMode;
import org.terasology.engine.network.NetworkSystem;

import static com.google.common.truth.Truth.assertThat;

@Tag("MteTest")
@ExtendWith(MTEExtension.class)
@IntegrationEnvironment(networkMode = NetworkMode.LISTEN_SERVER)
public class NetworkModeServerTest {
@Test
void testNetworkMode(NetworkSystem network) {
assertThat(network.getMode()).isEqualTo(NetworkMode.LISTEN_SERVER);
}

@Test
void testServerHasNoPlayers(NetworkSystem network, LocalPlayer player) {
assertThat(network.getPlayers()).isEmpty();
assertThat(player.isValid()).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ public class StateHeadlessSetup extends AbstractState {

protected boolean strictModuleRequirements;

private final NetworkMode networkMode;

public StateHeadlessSetup() {
this(NetworkMode.LISTEN_SERVER);
}

public StateHeadlessSetup(NetworkMode networkMode) {
this.networkMode = networkMode;
}

@Override
Expand All @@ -58,7 +65,7 @@ public void init(GameEngine gameEngine) {
config.getUniverseConfig().setSpawnWorldTitle(worldInfo.getTitle());
config.getUniverseConfig().setUniverseSeed(gameManifest.getSeed());

gameEngine.changeState(new StateLoading(gameManifest, NetworkMode.LISTEN_SERVER));
gameEngine.changeState(new StateLoading(gameManifest, networkMode));
}

public GameManifest createGameManifest() {
Expand Down

0 comments on commit 182f308

Please sign in to comment.