| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import it.unimi.dsi.fastutil.longs.LongArraySet;
|
| 7 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.stream.Collectors;
|
| 12 | import net.minecraft.server.MinecraftServer;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.util.Util;
|
| 15 | import net.minecraft.world.level.ChunkPos;
|
| 16 | import net.minecraft.world.level.block.entity.TestInstanceBlockEntity;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 | import org.slf4j.Logger;
|
| 19 |
|
| 20 | public class GameTestRunner {
|
| 21 | public static final int DEFAULT_TESTS_PER_ROW = 8;
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | private final MinecraftServer server;
|
| 24 | private final GameTestTicker testTicker;
|
| 25 | private final List<GameTestInfo> allTestInfos;
|
| 26 | private ImmutableList<GameTestBatch> batches;
|
| 27 | private final List<GameTestBatchListener> batchListeners = Lists.newArrayList();
|
| 28 | private final List<GameTestInfo> scheduledForRerun = Lists.newArrayList();
|
| 29 | private final GameTestRunner.GameTestBatcher testBatcher;
|
| 30 | private boolean stopped = true;
|
| 31 | private TestEnvironmentDefinition.@Nullable Activation<?> currentEnvironment;
|
| 32 | private final GameTestRunner.StructureSpawner existingStructureSpawner;
|
| 33 | private final GameTestRunner.StructureSpawner newStructureSpawner;
|
| 34 | private final boolean haltOnError;
|
| 35 | private final boolean clearBetweenBatches;
|
| 36 |
|
| 37 | protected GameTestRunner(
|
| 38 | final GameTestRunner.GameTestBatcher batcher,
|
| 39 | final Collection<GameTestBatch> batches,
|
| 40 | final MinecraftServer server,
|
| 41 | final GameTestTicker testTicker,
|
| 42 | final GameTestRunner.StructureSpawner existingStructureSpawner,
|
| 43 | final GameTestRunner.StructureSpawner newStructureSpawner,
|
| 44 | final boolean haltOnError,
|
| 45 | final boolean clearBetweenBatches
|
| 46 | ) {
|
| 47 | this.server = server;
|
| 48 | this.testTicker = testTicker;
|
| 49 | this.testBatcher = batcher;
|
| 50 | this.existingStructureSpawner = existingStructureSpawner;
|
| 51 | this.newStructureSpawner = newStructureSpawner;
|
| 52 | this.batches = ImmutableList.copyOf(batches);
|
| 53 | this.haltOnError = haltOnError;
|
| 54 | this.clearBetweenBatches = clearBetweenBatches;
|
| 55 | this.allTestInfos = this.batches.stream().flatMap(batch -> batch.gameTestInfos().stream()).collect(Util.toMutableList());
|
| 56 | testTicker.setRunner(this);
|
| 57 | this.allTestInfos.forEach(info -> info.addListener(new ReportGameListener()));
|
| 58 | }
|
| 59 |
|
| 60 | public List<GameTestInfo> getTestInfos() {
|
| 61 | return this.allTestInfos;
|
| 62 | }
|
| 63 |
|
| 64 | public void start() {
|
| 65 | this.stopped = false;
|
| 66 | this.runBatch(0);
|
| 67 | }
|
| 68 |
|
| 69 | public void stop() {
|
| 70 | this.stopped = true;
|
| 71 | if (this.currentEnvironment != null) {
|
| 72 | this.endCurrentEnvironment();
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | public void rerunTest(final GameTestInfo info) {
|
| 77 | GameTestInfo copy = info.copyReset();
|
| 78 | info.getListeners().forEach(listener -> listener.testAddedForRerun(info, copy, this));
|
| 79 | this.allTestInfos.add(copy);
|
| 80 | this.scheduledForRerun.add(copy);
|
| 81 | if (this.stopped) {
|
| 82 | this.runScheduledRerunTests();
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | private void runBatch(final int batchIndex) {
|
| 87 | if (batchIndex >= this.batches.size()) {
|
| 88 | this.endCurrentEnvironment();
|
| 89 | this.runScheduledRerunTests();
|
| 90 | } else {
|
| 91 | if (batchIndex > 0 && this.clearBetweenBatches) {
|
| 92 | GameTestBatch lastBatch = this.batches.get(batchIndex - 1);
|
| 93 | lastBatch.gameTestInfos().forEach(gameTestInfo -> {
|
| 94 | ServerLevel level = gameTestInfo.getLevel();
|
| 95 | TestInstanceBlockEntity testInstanceBlockEntity = gameTestInfo.getTestInstanceBlockEntity();
|
| 96 | StructureUtils.clearSpaceForStructure(testInstanceBlockEntity.getTestBoundingBox(), level);
|
| 97 | level.destroyBlock(testInstanceBlockEntity.getBlockPos(), false);
|
| 98 | });
|
| 99 | }
|
| 100 |
|
| 101 | final GameTestBatch currentBatch = this.batches.get(batchIndex);
|
| 102 | this.existingStructureSpawner.onBatchStart(this.server);
|
| 103 | this.newStructureSpawner.onBatchStart(this.server);
|
| 104 | Collection<GameTestInfo> testInfosForThisBatch = this.createStructuresForBatch(currentBatch.gameTestInfos());
|
| 105 | LOGGER.info(
|
| 106 | "Running test environment '{}' batch {} ({} tests)...", currentBatch.environment().getRegisteredName(), currentBatch.index(), testInfosForThisBatch.size()
|
| 107 | );
|
| 108 | this.endCurrentEnvironment();
|
| 109 | this.currentEnvironment = TestEnvironmentDefinition.activate(currentBatch.environment().value(), this.server.getLevel(currentBatch.dimension()));
|
| 110 | this.batchListeners.forEach(listener -> listener.testBatchStarting(currentBatch));
|
| 111 | final MultipleTestTracker currentBatchTracker = new MultipleTestTracker();
|
| 112 | testInfosForThisBatch.forEach(currentBatchTracker::addTestToTrack);
|
| 113 | currentBatchTracker.addListener(new GameTestListener() {
|
| 114 | private void testCompleted(final GameTestInfo testInfo) {
|
| 115 | if (currentBatchTracker.isDone()) {
|
| 116 | GameTestRunner.this.batchListeners.forEach(listener -> listener.testBatchFinished(currentBatch));
|
| 117 | LongSet forcedChunks = new LongArraySet(testInfo.getLevel().getForceLoadedChunks());
|
| 118 | forcedChunks.forEach(pos -> testInfo.getLevel().setChunkForced(ChunkPos.getX(pos), ChunkPos.getZ(pos), false));
|
| 119 | GameTestRunner.this.runBatch(batchIndex + 1);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public void testStructureLoaded(final GameTestInfo testInfo) {
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public void testPassed(final GameTestInfo testInfo, final GameTestRunner runner) {
|
| 129 | testInfo.getTestInstanceBlockEntity().removeBarriers();
|
| 130 | this.testCompleted(testInfo);
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | public void testFailed(final GameTestInfo testInfo, final GameTestRunner runner) {
|
| 135 | if (GameTestRunner.this.haltOnError) {
|
| 136 | GameTestRunner.this.endCurrentEnvironment();
|
| 137 | LongSet forcedChunks = new LongArraySet(testInfo.getLevel().getForceLoadedChunks());
|
| 138 | forcedChunks.forEach(pos -> testInfo.getLevel().setChunkForced(ChunkPos.getX(pos), ChunkPos.getZ(pos), false));
|
| 139 | GameTestTicker.SINGLETON.clear();
|
| 140 | } else {
|
| 141 | this.testCompleted(testInfo);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public void testAddedForRerun(final GameTestInfo original, final GameTestInfo copy, final GameTestRunner runner) {
|
| 147 | }
|
| 148 | });
|
| 149 | testInfosForThisBatch.forEach(this.testTicker::add);
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | private void endCurrentEnvironment() {
|
| 154 | if (this.currentEnvironment != null) {
|
| 155 | this.currentEnvironment.teardown();
|
| 156 | this.currentEnvironment = null;
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | private void runScheduledRerunTests() {
|
| 161 | if (!this.scheduledForRerun.isEmpty()) {
|
| 162 | LOGGER.info("Starting re-run of tests: {}", this.scheduledForRerun.stream().map(info -> info.id().toString()).collect(Collectors.joining(", ")));
|
| 163 | this.batches = ImmutableList.copyOf(this.testBatcher.batch(this.scheduledForRerun));
|
| 164 | this.scheduledForRerun.clear();
|
| 165 | this.stopped = false;
|
| 166 | this.runBatch(0);
|
| 167 | } else {
|
| 168 | this.batches = ImmutableList.of();
|
| 169 | this.stopped = true;
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | public void addListener(final GameTestBatchListener listener) {
|
| 174 | this.batchListeners.add(listener);
|
| 175 | }
|
| 176 |
|
| 177 | private Collection<GameTestInfo> createStructuresForBatch(final Collection<GameTestInfo> batch) {
|
| 178 | return batch.stream().map(this::spawn).flatMap(Optional::stream).toList();
|
| 179 | }
|
| 180 |
|
| 181 | private Optional<GameTestInfo> spawn(final GameTestInfo testInfo) {
|
| 182 | return testInfo.getTestBlockPos() == null ? this.newStructureSpawner.spawnStructure(testInfo) : this.existingStructureSpawner.spawnStructure(testInfo);
|
| 183 | }
|
| 184 |
|
| 185 | public static class Builder {
|
| 186 | private final MinecraftServer server;
|
| 187 | private final GameTestTicker testTicker = GameTestTicker.SINGLETON;
|
| 188 | private GameTestRunner.GameTestBatcher batcher = GameTestBatchFactory.fromGameTestInfo();
|
| 189 | private GameTestRunner.StructureSpawner existingStructureSpawner = GameTestRunner.StructureSpawner.IN_PLACE;
|
| 190 | private GameTestRunner.StructureSpawner newStructureSpawner = GameTestRunner.StructureSpawner.NOT_SET;
|
| 191 | private final Collection<GameTestBatch> batches;
|
| 192 | private boolean haltOnError = false;
|
| 193 | private boolean clearBetweenBatches = false;
|
| 194 |
|
| 195 | private Builder(final Collection<GameTestBatch> batches, final MinecraftServer server) {
|
| 196 | this.batches = batches;
|
| 197 | this.server = server;
|
| 198 | }
|
| 199 |
|
| 200 | public static GameTestRunner.Builder fromBatches(final Collection<GameTestBatch> batches, final MinecraftServer server) {
|
| 201 | return new GameTestRunner.Builder(batches, server);
|
| 202 | }
|
| 203 |
|
| 204 | public static GameTestRunner.Builder fromInfo(final Collection<GameTestInfo> tests, final MinecraftServer server) {
|
| 205 | return fromBatches(GameTestBatchFactory.fromGameTestInfo().batch(tests), server);
|
| 206 | }
|
| 207 |
|
| 208 | public GameTestRunner.Builder haltOnError() {
|
| 209 | this.haltOnError = true;
|
| 210 | return this;
|
| 211 | }
|
| 212 |
|
| 213 | public GameTestRunner.Builder clearBetweenBatches() {
|
| 214 | this.clearBetweenBatches = true;
|
| 215 | return this;
|
| 216 | }
|
| 217 |
|
| 218 | public GameTestRunner.Builder newStructureSpawner(final GameTestRunner.StructureSpawner structureSpawner) {
|
| 219 | this.newStructureSpawner = structureSpawner;
|
| 220 | return this;
|
| 221 | }
|
| 222 |
|
| 223 | public GameTestRunner.Builder existingStructureSpawner(final StructureGridSpawner spawner) {
|
| 224 | this.existingStructureSpawner = spawner;
|
| 225 | return this;
|
| 226 | }
|
| 227 |
|
| 228 | public GameTestRunner.Builder batcher(final GameTestRunner.GameTestBatcher batcher) {
|
| 229 | this.batcher = batcher;
|
| 230 | return this;
|
| 231 | }
|
| 232 |
|
| 233 | public GameTestRunner build() {
|
| 234 | return new GameTestRunner(
|
| 235 | this.batcher,
|
| 236 | this.batches,
|
| 237 | this.server,
|
| 238 | this.testTicker,
|
| 239 | this.existingStructureSpawner,
|
| 240 | this.newStructureSpawner,
|
| 241 | this.haltOnError,
|
| 242 | this.clearBetweenBatches
|
| 243 | );
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | public interface GameTestBatcher {
|
| 248 | Collection<GameTestBatch> batch(Collection<GameTestInfo> infos);
|
| 249 | }
|
| 250 |
|
| 251 | public interface StructureSpawner {
|
| 252 | GameTestRunner.StructureSpawner IN_PLACE = testInfo -> Optional.ofNullable(testInfo.prepareTestStructure()).map(e -> e.startExecution(1));
|
| 253 | GameTestRunner.StructureSpawner NOT_SET = testInfo -> Optional.empty();
|
| 254 |
|
| 255 | Optional<GameTestInfo> spawnStructure(GameTestInfo testInfo);
|
| 256 |
|
| 257 | default void onBatchStart(final MinecraftServer server) {
|
| 258 | }
|
| 259 | }
|
| 260 | }
|
| 261 |
|