From 7b3c9b52ba18c084c075f04e72618d4df0f85210 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sat, 3 Oct 2020 19:47:35 -0700 Subject: [PATCH 1/5] add relevance check to avoid creating additional entity --- .../moduletestingenvironment/ModuleTestingEnvironment.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/org/terasology/moduletestingenvironment/ModuleTestingEnvironment.java b/src/main/java/org/terasology/moduletestingenvironment/ModuleTestingEnvironment.java index 79ed8df..af99087 100644 --- a/src/main/java/org/terasology/moduletestingenvironment/ModuleTestingEnvironment.java +++ b/src/main/java/org/terasology/moduletestingenvironment/ModuleTestingEnvironment.java @@ -248,6 +248,11 @@ void setWorldGeneratorUri(String worldGeneratorUri) { * available */ public void forceAndWaitForGeneration(Vector3i blockPos) { + WorldProvider worldProvider = hostContext.get(WorldProvider.class); + if (worldProvider.isBlockRelevant(blockPos)) { + return; + } + // we need to add an entity with RegionRelevance in order to get a chunk generated LocationComponent locationComponent = new LocationComponent(); locationComponent.setWorldPosition(blockPos.toVector3f()); From 9218003c87c6c3a9cc01efa96c895afb6ba118dc Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Sat, 3 Oct 2020 21:44:50 -0700 Subject: [PATCH 2/5] use relevance check and add empty world --- .../ModuleTestingEnvironment.java | 2 +- .../fixtures/EmptyWorldGenerator.java | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/terasology/moduletestingenvironment/fixtures/EmptyWorldGenerator.java diff --git a/src/main/java/org/terasology/moduletestingenvironment/ModuleTestingEnvironment.java b/src/main/java/org/terasology/moduletestingenvironment/ModuleTestingEnvironment.java index af99087..a4c89b4 100644 --- a/src/main/java/org/terasology/moduletestingenvironment/ModuleTestingEnvironment.java +++ b/src/main/java/org/terasology/moduletestingenvironment/ModuleTestingEnvironment.java @@ -263,7 +263,7 @@ public void forceAndWaitForGeneration(Vector3i blockPos) { hostContext.get(EntityManager.class).create(locationComponent, relevanceRegionComponent).setAlwaysRelevant(true); - runWhile(() -> hostContext.get(WorldProvider.class).getBlock(blockPos).getURI().toString().equalsIgnoreCase("engine:unloaded")); + runWhile(() -> !worldProvider.isBlockRelevant(blockPos)); } /** diff --git a/src/main/java/org/terasology/moduletestingenvironment/fixtures/EmptyWorldGenerator.java b/src/main/java/org/terasology/moduletestingenvironment/fixtures/EmptyWorldGenerator.java new file mode 100644 index 0000000..e87bfc7 --- /dev/null +++ b/src/main/java/org/terasology/moduletestingenvironment/fixtures/EmptyWorldGenerator.java @@ -0,0 +1,36 @@ +// Copyright 2020 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 + +package org.terasology.moduletestingenvironment.fixtures; + +import com.google.common.collect.Lists; +import org.terasology.engine.SimpleUri; +import org.terasology.registry.In; +import org.terasology.world.generation.BaseFacetedWorldGenerator; +import org.terasology.world.generation.WorldBuilder; +import org.terasology.world.generator.RegisterWorldGenerator; +import org.terasology.world.generator.plugin.WorldGeneratorPlugin; +import org.terasology.world.generator.plugin.WorldGeneratorPluginLibrary; + +import java.util.ArrayList; +import java.util.List; + +@RegisterWorldGenerator(id = "empty", displayName = "empty") +public class EmptyWorldGenerator extends BaseFacetedWorldGenerator { + @In + private WorldGeneratorPluginLibrary worldGeneratorPluginLibrary; + + public EmptyWorldGenerator(SimpleUri uri) { + super(uri); + } + + @Override + protected WorldBuilder createWorld() { + return new WorldBuilder(new WorldGeneratorPluginLibrary() { + @Override + public List instantiateAllOfType(Class ofType) { + return new ArrayList<>(); + } + }); + } +} From 5a04ecf0f38e270740d589f0257733728779b847 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Thu, 15 Oct 2020 21:55:24 -0700 Subject: [PATCH 3/5] update docs --- .../fixtures/EmptyWorldGenerator.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/terasology/moduletestingenvironment/fixtures/EmptyWorldGenerator.java b/src/main/java/org/terasology/moduletestingenvironment/fixtures/EmptyWorldGenerator.java index e87bfc7..81156dd 100644 --- a/src/main/java/org/terasology/moduletestingenvironment/fixtures/EmptyWorldGenerator.java +++ b/src/main/java/org/terasology/moduletestingenvironment/fixtures/EmptyWorldGenerator.java @@ -3,7 +3,6 @@ package org.terasology.moduletestingenvironment.fixtures; -import com.google.common.collect.Lists; import org.terasology.engine.SimpleUri; import org.terasology.registry.In; import org.terasology.world.generation.BaseFacetedWorldGenerator; @@ -15,6 +14,9 @@ import java.util.ArrayList; import java.util.List; +/** + * Bare World Generator to generating empty chunks for testing. + */ @RegisterWorldGenerator(id = "empty", displayName = "empty") public class EmptyWorldGenerator extends BaseFacetedWorldGenerator { @In From fa6359f66dd9a9034b5d9229fd87ac89e09c3a64 Mon Sep 17 00:00:00 2001 From: Tobias Nett Date: Tue, 20 Oct 2020 21:28:30 +0200 Subject: [PATCH 4/5] split `testExample` into smaller tests --- .../moduletestingenvironment/ExampleTest.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/test/java/org/terasology/moduletestingenvironment/ExampleTest.java b/src/test/java/org/terasology/moduletestingenvironment/ExampleTest.java index f16f069..c3aab3b 100644 --- a/src/test/java/org/terasology/moduletestingenvironment/ExampleTest.java +++ b/src/test/java/org/terasology/moduletestingenvironment/ExampleTest.java @@ -24,7 +24,6 @@ import org.terasology.entitySystem.entity.EntityManager; import org.terasology.logic.players.LocalPlayer; import org.terasology.logic.players.event.ResetCameraEvent; -import org.terasology.math.geom.Vector3f; import org.terasology.math.geom.Vector3i; import org.terasology.moduletestingenvironment.extension.Dependencies; import org.terasology.network.ClientComponent; @@ -49,30 +48,46 @@ public class ExampleTest { private ModuleTestingHelper helper; @Test - public void testExample() { + public void testClientConnection() { + int currentClients = Lists.newArrayList(entityManager.getEntitiesWith(ClientComponent.class)).size(); + // create some clients (the library connects them automatically) Context clientContext1 = helper.createClient(); Context clientContext2 = helper.createClient(); + int expectedClients = currentClients + 2; + // wait for both clients to be known to the server - helper.runUntil(()-> Lists.newArrayList(entityManager.getEntitiesWith(ClientComponent.class)).size() == 2); - Assertions.assertEquals(2, Lists.newArrayList(entityManager.getEntitiesWith(ClientComponent.class)).size()); + helper.runUntil(() -> Lists.newArrayList(entityManager.getEntitiesWith(ClientComponent.class)).size() >= expectedClients); + Assertions.assertEquals(expectedClients, + Lists.newArrayList(entityManager.getEntitiesWith(ClientComponent.class)).size()); + } + @Test + public void testRunWhileTimeout() { // run while a condition is true or until a timeout passes - long expectedTime = time.getGameTimeInMs() + 1000; - boolean timedOut = helper.runWhile(1000, ()-> true); + long expectedTime = time.getGameTimeInMs() + 500; + boolean timedOut = helper.runWhile(500, () -> true); Assertions.assertTrue(timedOut); long currentTime = time.getGameTimeInMs(); Assertions.assertTrue(currentTime >= expectedTime); + } + + @Test + public void testSendEvent() { + Context clientContext = helper.createClient(); // send an event to a client's local player just for fun - clientContext1.get(LocalPlayer.class).getClientEntity().send(new ResetCameraEvent()); + clientContext.get(LocalPlayer.class).getClientEntity().send(new ResetCameraEvent()); + } + @Test + public void testWorldProvider() { // wait for a chunk to be generated helper.forceAndWaitForGeneration(Vector3i.zero()); // set a block's type and immediately read it back - worldProvider.setBlock(Vector3i.zero(), blockManager.getBlock("engine:air")); - Assertions.assertEquals("engine:air", worldProvider.getBlock(Vector3f.zero()).getURI().toString()); + worldProvider.setBlock(new org.joml.Vector3i(), blockManager.getBlock("engine:air")); + Assertions.assertEquals("engine:air", worldProvider.getBlock(new org.joml.Vector3i()).getURI().toString()); } } From 2c7c2a9ada1f8715f58a22a96815c090219e7cb7 Mon Sep 17 00:00:00 2001 From: Tobias Nett Date: Tue, 20 Oct 2020 21:28:47 +0200 Subject: [PATCH 5/5] update license header --- .../moduletestingenvironment/ExampleTest.java | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/terasology/moduletestingenvironment/ExampleTest.java b/src/test/java/org/terasology/moduletestingenvironment/ExampleTest.java index c3aab3b..b126515 100644 --- a/src/test/java/org/terasology/moduletestingenvironment/ExampleTest.java +++ b/src/test/java/org/terasology/moduletestingenvironment/ExampleTest.java @@ -1,18 +1,5 @@ -/* - * Copyright 2020 MovingBlocks - * - * 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. - */ +// Copyright 2020 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 package org.terasology.moduletestingenvironment; import com.google.common.collect.Lists;