Skip to content

Commit 5fa4404

Browse files
committed
Cleanup some of the game tests to use more specific types and avoid using mutable to capture intermediary results
1 parent fd6e6e9 commit 5fa4404

File tree

3 files changed

+57
-79
lines changed

3 files changed

+57
-79
lines changed

src/gameTest/java/mekanism/common/tests/network/InventoryNetworkTest.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ public static void colorIsNotPriority(final DynamicTest test) {
281281
@GameTest(template = UPGRADEABLE, setupTicks = SETUP_TICKS, timeoutTicks = TIMEOUT_TICKS)
282282
@TestHolder(description = "Tests that newly pulled items will go to the destination that had its path upgraded, "
283283
+ "but any items that were already en-route will continue to the destination they had already calculated.")
284-
public static void upgradeFurtherPath(final DynamicTest test) {
285-
test.onGameTest(helper -> helper.startSequence()
284+
public static void upgradeFurtherPath(final ExtendedGameTestHelper helper) {
285+
helper.startSequence()
286286
//Wait a few seconds for it to pull some items out, and upgrade the transporter to the further destination
287287
.thenExecuteAfter(4 * SharedConstants.TICKS_PER_SECOND, () -> applyAlloyUpgrade(helper, new BlockPos(9, 1, 0), AlloyTier.INFUSED))
288288
//Wait a few seconds for transferring to happen then validate stuff
@@ -293,16 +293,15 @@ public static void upgradeFurtherPath(final DynamicTest test) {
293293
GameTestUtils.validateContainerHas(helper, new BlockPos(8, 1, 0), 0, new ItemStack(Items.STONE, 12));
294294
//Validate start is also empty
295295
helper.assertContainerEmpty(3, 1, 0);
296-
}).thenSucceed()
297-
);
296+
}).thenSucceed();
298297
}
299298

300299
@GameTest(template = UPGRADEABLE, setupTicks = SETUP_TICKS, timeoutTicks = TIMEOUT_TICKS)
301300
@TestHolder(description = "Tests that newly pulled items will go to the destination that had its path upgraded, "
302301
+ "but any items that were already en-route will continue to the destination they had "
303302
+ "already calculated as the new destination is slightly \"closer\".")
304-
public static void upgradeFurtherOverlapping(final DynamicTest test) {
305-
test.onGameTest(helper -> helper.startSequence()
303+
public static void upgradeFurtherOverlapping(final ExtendedGameTestHelper helper) {
304+
helper.startSequence()
306305
//Wait a few seconds for it to pull some items out, and upgrade the transporter to the further destination
307306
.thenExecuteAfter(4 * SharedConstants.TICKS_PER_SECOND, () -> applyAlloyUpgrade(helper, new BlockPos(6, 1, 2), AlloyTier.INFUSED))
308307
//Wait a few seconds for transferring to happen then validate stuff
@@ -313,14 +312,13 @@ public static void upgradeFurtherOverlapping(final DynamicTest test) {
313312
GameTestUtils.validateContainerHas(helper, new BlockPos(8, 1, 0), 0, new ItemStack(Items.STONE, 12));
314313
//Validate start is also empty
315314
helper.assertContainerEmpty(3, 1, 0);
316-
}).thenSucceed()
317-
);
315+
}).thenSucceed();
318316
}
319317

320318
@GameTest(template = UPGRADEABLE, setupTicks = SETUP_TICKS, timeoutTicks = TIMEOUT_TICKS)
321319
@TestHolder(description = "Tests that all items pre- and post-upgrade will go to the original destination.")
322-
public static void upgradeExisting(final DynamicTest test) {
323-
test.onGameTest(helper -> helper.startSequence()
320+
public static void upgradeExisting(final ExtendedGameTestHelper helper) {
321+
helper.startSequence()
324322
//Wait a few seconds for it to pull some items out, and upgrade the transporter to the further destination
325323
.thenExecuteAfter(4 * SharedConstants.TICKS_PER_SECOND, () -> applyAlloyUpgrade(helper, new BlockPos(3, 1, 2), AlloyTier.INFUSED))
326324
//Wait a few seconds for transferring to happen then validate stuff
@@ -331,8 +329,7 @@ public static void upgradeExisting(final DynamicTest test) {
331329
helper.assertContainerEmpty(8, 1, 0);
332330
//Validate start is also empty
333331
helper.assertContainerEmpty(3, 1, 0);
334-
}).thenSucceed()
335-
);
332+
}).thenSucceed();
336333
}
337334

338335
private static void applyAlloyUpgrade(ExtendedGameTestHelper helper, BlockPos relativePos, AlloyTier tier) {

src/gameTest/java/mekanism/common/tests/network/TransmitterNetworkTest.java

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@
1414
import net.minecraft.gametest.framework.GameTest;
1515
import net.minecraft.world.level.ChunkPos;
1616
import net.minecraft.world.level.block.Blocks;
17-
import net.minecraft.world.level.block.state.BlockState;
1817
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
19-
import net.neoforged.testframework.DynamicTest;
2018
import net.neoforged.testframework.annotation.ForEachTest;
2119
import net.neoforged.testframework.annotation.RegisterStructureTemplate;
2220
import net.neoforged.testframework.annotation.TestHolder;
2321
import net.neoforged.testframework.gametest.ExtendedGameTestHelper;
2422
import net.neoforged.testframework.gametest.StructureTemplateBuilder;
25-
import org.apache.commons.lang3.mutable.MutableInt;
26-
import org.apache.commons.lang3.mutable.MutableObject;
2723
import org.jetbrains.annotations.NotNull;
2824
import org.jetbrains.annotations.Nullable;
2925

@@ -42,8 +38,8 @@ public class TransmitterNetworkTest {
4238

4339
@GameTest(template = STRAIGHT_CABLE, setupTicks = SETUP_TICKS, batch = "1")
4440
@TestHolder(description = "Tests that reloading intermediary chunks does not cause a network to break.")
45-
public static void reloadIntermediary(final DynamicTest test) {
46-
test.onGameTest(helper -> GameTestUtils.succeedIfAfterReload(helper, new ChunkPos(1, 0), new MatchingNetworkValidator(helper)));
41+
public static void reloadIntermediary(final ExtendedGameTestHelper helper) {
42+
GameTestUtils.succeedIfAfterReload(helper, new ChunkPos(1, 0), new MatchingNetworkValidator(helper));
4743
}
4844

4945
/**
@@ -53,33 +49,27 @@ public static void reloadIntermediary(final DynamicTest test) {
5349
@GameTest(template = STRAIGHT_CABLE, setupTicks = SETUP_TICKS, batch = "2")
5450
@TestHolder(description = "Tests that when part of a network becomes inaccessible but still loaded, "
5551
+ "we are able to properly remove the transmitters and then recover when the chunk becomes accessible again.")
56-
public static void inaccessibleNotUnloaded(final DynamicTest test) {
57-
test.onGameTest(helper -> {
58-
ChunkPos relativeChunk = new ChunkPos(1, 0);
59-
BlockPos relativeTargetTransmitter = new BlockPos(0, 1, 0);
60-
MutableInt lastLevel = new MutableInt();
61-
//This is used
62-
MutableObject<BlockState> lastState = new MutableObject<>(Blocks.AIR.defaultBlockState());
63-
helper.startSequence()
64-
.thenWaitUntil(() -> lastLevel.setValue(GameTestUtils.setChunkLoadLevel(helper, relativeChunk, GameTestUtils.INACCESSIBLE_LEVEL)))
65-
//Wait 5 ticks in case anything needs more time to process after the chunk unloads
66-
//Force a rebuild of the network by breaking one transmitter
67-
.thenExecuteAfter(5, () -> {
68-
//Update the last state, so we can set it again afterwards
69-
lastState.setValue(helper.getBlockState(relativeTargetTransmitter));
70-
helper.setBlock(relativeTargetTransmitter, Blocks.AIR);
71-
})
72-
//Wait 5 ticks to ensure it has time to process everything (expected to only take two ticks)
73-
//Set the block back to what it was before (the transmitter)
74-
.thenExecuteAfter(5, () -> helper.setBlock(relativeTargetTransmitter, lastState.getValue()))
75-
//Wait 5 ticks to ensure it has time to process everything (expected to only take two ticks)
76-
//Set the chunk level back to what it was before (aka loading it fully again)
77-
.thenExecuteAfter(5, () -> GameTestUtils.setChunkLoadLevel(helper, relativeChunk, lastLevel.getValue()))
78-
//Wait 5 ticks in case anything needs more time to process after the chunk loads
79-
.thenIdle(5)
80-
.thenWaitUntil(0, new MatchingNetworkValidator(helper))
81-
.thenSucceed();
82-
});
52+
public static void inaccessibleNotUnloaded(final ExtendedGameTestHelper helper) {
53+
ChunkPos relativeChunk = new ChunkPos(1, 0);
54+
BlockPos relativeTargetTransmitter = new BlockPos(0, 1, 0);
55+
helper.startSequence()
56+
.thenMap(() -> GameTestUtils.setChunkLoadLevel(helper, relativeChunk, GameTestUtils.INACCESSIBLE_LEVEL))
57+
//Wait 5 ticks in case anything needs more time to process after the chunk unloads
58+
.thenIdle(5)
59+
.thenSequence(sequence -> sequence.thenMap(() -> helper.getBlockState(relativeTargetTransmitter))
60+
//Force a rebuild of the network by breaking one transmitter
61+
.thenExecute(() -> helper.setBlock(relativeTargetTransmitter, Blocks.AIR))
62+
//Wait 5 ticks to ensure it has time to process everything (expected to only take two ticks)
63+
//Set the block back to what it was before (the transmitter)
64+
.thenExecuteAfter(5, state -> helper.setBlock(relativeTargetTransmitter, state))
65+
)
66+
//Wait 5 ticks to ensure it has time to process everything (expected to only take two ticks)
67+
//Set the chunk level back to what it was before (aka loading it fully again)
68+
.thenExecuteAfter(5, level -> GameTestUtils.setChunkLoadLevel(helper, relativeChunk, level))
69+
//Wait 5 ticks in case anything needs more time to process after the chunk loads
70+
.thenIdle(5)
71+
.thenWaitUntil(0, new MatchingNetworkValidator(helper))
72+
.thenSucceed();
8373
}
8474

8575
private static void forEachTransmitter(ExtendedGameTestHelper helper, TriConsumer<TileEntityTransmitter, Transmitter<?, ?, ?>, BlockPos> consumer) {

src/gameTest/java/mekanism/common/tests/util/GameTestUtils.java

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import net.minecraft.world.level.block.entity.BlockEntity;
1515
import net.neoforged.neoforge.items.IItemHandler;
1616
import net.neoforged.testframework.gametest.ExtendedGameTestHelper;
17-
import org.apache.commons.lang3.mutable.MutableInt;
1817
import org.jetbrains.annotations.Nullable;
1918

2019
public class GameTestUtils {
@@ -24,22 +23,26 @@ public class GameTestUtils {
2423

2524
static final boolean DEBUG_CHUNK_LOADING = false;
2625

27-
public static void succeedIfAfterUnload(ExtendedGameTestHelper helper, ChunkPos relativePos, Runnable criteria) {
28-
helper.startSequence()
29-
.thenWaitUntil(() -> unloadChunk(helper, relativePos))
30-
//Wait 5 ticks in case anything needs more time to process after the chunk unloads
31-
.thenIdle(5)
32-
.thenWaitUntil(0, criteria)
33-
.thenSucceed();
34-
}
35-
3626
public static void succeedIfAfterReload(ExtendedGameTestHelper helper, ChunkPos relativePos, Runnable afterReload) {
37-
MutableInt lastLevel = new MutableInt(UNLOAD_LEVEL);
3827
helper.startSequence()
39-
.thenWaitUntil(() -> unloadChunk(helper, relativePos, lastLevel))
28+
.thenMap(() -> unloadChunk(helper, relativePos))
29+
.thenWaitUntil(() -> {
30+
ChunkPos absolutePos = absolutePos(helper, relativePos);
31+
long absPos = absolutePos.toLong();
32+
if (GameTestEventListeners.watchedChunks.containsKey(absPos)) {
33+
//Watched chunk
34+
if (GameTestEventListeners.watchedChunks.get(absPos)) {
35+
//Is loaded, but we are watching, throw exception so that we keep waiting for it to unload
36+
fail(helper, "Chunk has not been marked as unloaded yet", absolutePos, relativePos);
37+
} else {
38+
//Remove the watch on the chunk now that it has been unloaded
39+
GameTestEventListeners.watchedChunks.remove(absPos);
40+
}
41+
}
42+
})
4043
//Wait 5 ticks in case anything needs more time to process after the chunk unloads
4144
.thenIdle(5)
42-
.thenWaitUntil(() -> loadChunk(helper, relativePos, lastLevel))
45+
.thenWaitUntil(level -> loadChunk(helper, relativePos, level))
4346
//Wait 5 ticks in case anything needs more time to process after the chunk loads
4447
.thenIdle(5)
4548
.thenWaitUntil(0, afterReload)
@@ -48,23 +51,11 @@ public static void succeedIfAfterReload(ExtendedGameTestHelper helper, ChunkPos
4851

4952
//TODO - GameTest: Can we make unloads not cause the game to crash if a player tries to run a test that uses them instead of using game test server?
5053
// Most likely the answer is no
51-
private static void unloadChunk(ExtendedGameTestHelper helper, ChunkPos relativePos) {
52-
unloadChunk(helper, relativePos, new MutableInt());
53-
}
54-
55-
private static void unloadChunk(ExtendedGameTestHelper helper, ChunkPos relativePos, MutableInt levelMemory) {
54+
private static int unloadChunk(ExtendedGameTestHelper helper, ChunkPos relativePos) {
55+
int level = UNLOAD_LEVEL;
5656
ChunkPos absolutePos = absolutePos(helper, relativePos);
5757
long absPos = absolutePos.toLong();
58-
if (GameTestEventListeners.watchedChunks.containsKey(absPos)) {
59-
//Watched chunk
60-
if (GameTestEventListeners.watchedChunks.get(absPos)) {
61-
//Is loaded, but we are watching, throw exception so that we keep waiting for it to unload
62-
fail(helper, "Chunk has not been marked as unloaded yet", absolutePos, relativePos);
63-
} else {
64-
//Remove the watch on the chunk now that it has been unloaded
65-
GameTestEventListeners.watchedChunks.remove(absPos);
66-
}
67-
} else if (WorldUtils.isChunkLoaded(helper.getLevel(), absolutePos)) {
58+
if (WorldUtils.isChunkLoaded(helper.getLevel(), absolutePos)) {
6859
//If the chunk isn't watched and is loaded we want to try and unload it
6960
ChunkMap chunkMap = helper.getLevel().getChunkSource().chunkMap;
7061
DistanceManager distanceManager = chunkMap.getDistanceManager();
@@ -77,25 +68,25 @@ private static void unloadChunk(ExtendedGameTestHelper helper, ChunkPos relative
7768
Mekanism.logger.info("Trying to unload chunk at: {}, {}", absolutePos.x, absolutePos.z);
7869
}
7970
//If it is currently loaded, queue it for unload
80-
levelMemory.setValue(holder.getTicketLevel());
81-
distanceManager.updateChunkScheduling(absPos, UNLOAD_LEVEL, holder, holder.getTicketLevel());
71+
level = holder.getTicketLevel();
72+
distanceManager.updateChunkScheduling(absPos, UNLOAD_LEVEL, holder, level);
8273
//And then unload it
8374
chunkMap.processUnloads(ConstantPredicates.ALWAYS_TRUE);
84-
fail(helper, "Chunk queued for unloading", absolutePos, relativePos);
8575
} else if (DEBUG_CHUNK_LOADING) {
8676
//Note: Even with debug logging enabled odds are this case isn't even possible due to the earlier check to skip if unloaded
8777
Mekanism.logger.info("Trying to unload already unloaded chunk at: {}, {}", absolutePos.x, absolutePos.z);
8878
}
8979
} else if (DEBUG_CHUNK_LOADING) {
9080
Mekanism.logger.info("Chunk at: {}, {} is already unloaded", absolutePos.x, absolutePos.z);
9181
}
82+
return level;
9283
}
9384

9485
private static void loadChunk(ExtendedGameTestHelper helper, ChunkPos relativePos) {
95-
loadChunk(helper, relativePos, new MutableInt(ChunkMap.FORCED_TICKET_LEVEL));
86+
loadChunk(helper, relativePos, ChunkMap.FORCED_TICKET_LEVEL);
9687
}
9788

98-
private static void loadChunk(ExtendedGameTestHelper helper, ChunkPos relativePos, MutableInt levelMemory) {
89+
private static void loadChunk(ExtendedGameTestHelper helper, ChunkPos relativePos, int level) {
9990
ChunkPos absolutePos = absolutePos(helper, relativePos);
10091
long absPos = absolutePos.toLong();
10192
if (GameTestEventListeners.watchedChunks.containsKey(absPos)) {
@@ -119,7 +110,7 @@ private static void loadChunk(ExtendedGameTestHelper helper, ChunkPos relativePo
119110
Mekanism.logger.info("Trying to load chunk at: {}, {}", absolutePos.x, absolutePos.z);
120111
}
121112
//Load the chunk to the level it was unloaded at
122-
holder = distanceManager.updateChunkScheduling(absPos, levelMemory.getValue(), null, UNLOAD_LEVEL);
113+
holder = distanceManager.updateChunkScheduling(absPos, level, null, UNLOAD_LEVEL);
123114
if (holder == null) {//Should never happen unless start value was unloaded
124115
fail(helper, "Error loading chunk", absolutePos, relativePos);
125116
} else {

0 commit comments

Comments
 (0)