| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Streams;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.stream.Collectors;
|
| 10 | import java.util.stream.Stream;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.resources.ResourceKey;
|
| 13 | import net.minecraft.server.MinecraftServer;
|
| 14 | import net.minecraft.server.level.ServerLevel;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 | import net.minecraft.world.level.block.Rotation;
|
| 17 |
|
| 18 | public class GameTestBatchFactory {
|
| 19 | private static final int MAX_TESTS_PER_BATCH = 50;
|
| 20 | public static final GameTestBatchFactory.TestDecorator DIRECT = (test, level) -> Stream.of(
|
| 21 | new GameTestInfo(test, Rotation.NONE, level, RetryOptions.noRetries())
|
| 22 | );
|
| 23 |
|
| 24 | public static List<GameTestBatch> divideIntoBatches(
|
| 25 | final Collection<Holder.Reference<GameTestInstance>> allTests, final GameTestBatchFactory.TestDecorator decorator, final MinecraftServer server
|
| 26 | ) {
|
| 27 | Map<GameTestBatchFactory.BatchKey, List<Holder.Reference<GameTestInstance>>> testsPerBatch = allTests.stream()
|
| 28 | .collect(Collectors.groupingBy(instance -> new GameTestBatchFactory.BatchKey(instance.value().batch(), instance.value().info().dimension())));
|
| 29 | return testsPerBatch.entrySet().stream().flatMap(e -> {
|
| 30 | GameTestBatchFactory.BatchKey key = e.getKey();
|
| 31 | Holder<TestEnvironmentDefinition<?>> batchKey = key.environment();
|
| 32 | ResourceKey<Level> dimensionKey = key.dimension();
|
| 33 | ServerLevel level = server.getLevel(dimensionKey);
|
| 34 | if (level == null) {
|
| 35 | throw new IllegalStateException("Missing level for dimension: " + dimensionKey.identifier());
|
| 36 | }
|
| 37 |
|
| 38 | List<GameTestInfo> testsInBatch = e.getValue().stream().flatMap(test -> decorator.decorate((Holder.Reference<GameTestInstance>)test, level)).toList();
|
| 39 | return Streams.mapWithIndex(Lists.partition(testsInBatch, 50).stream(), (tests, index) -> toGameTestBatch(tests, batchKey, (int)index, dimensionKey));
|
| 40 | }).toList();
|
| 41 | }
|
| 42 |
|
| 43 | public static GameTestRunner.GameTestBatcher fromGameTestInfo() {
|
| 44 | return fromGameTestInfo(50);
|
| 45 | }
|
| 46 |
|
| 47 | public static GameTestRunner.GameTestBatcher fromGameTestInfo(final int maxTestsPerBatch) {
|
| 48 | return gameTestInfos -> {
|
| 49 | Map<GameTestBatchFactory.BatchKey, List<GameTestInfo>> testsPerBatch = gameTestInfos.stream()
|
| 50 | .filter(Objects::nonNull)
|
| 51 | .collect(Collectors.groupingBy(info -> new GameTestBatchFactory.BatchKey(info.getTest().batch(), info.getTest().info().dimension())));
|
| 52 | return testsPerBatch.entrySet()
|
| 53 | .stream()
|
| 54 | .flatMap(
|
| 55 | e -> {
|
| 56 | GameTestBatchFactory.BatchKey key = e.getKey();
|
| 57 | List<GameTestInfo> testsInBatch = e.getValue();
|
| 58 | return Streams.mapWithIndex(
|
| 59 | Lists.partition(testsInBatch, maxTestsPerBatch).stream(),
|
| 60 | (tests, index) -> toGameTestBatch(List.copyOf((Collection<? extends GameTestInfo>)tests), key.environment(), (int)index, key.dimension())
|
| 61 | );
|
| 62 | }
|
| 63 | )
|
| 64 | .toList();
|
| 65 | };
|
| 66 | }
|
| 67 |
|
| 68 | public static GameTestBatch toGameTestBatch(
|
| 69 | final Collection<GameTestInfo> tests, final Holder<TestEnvironmentDefinition<?>> batch, final int counter, final ResourceKey<Level> dimension
|
| 70 | ) {
|
| 71 | return new GameTestBatch(counter, tests, batch, dimension);
|
| 72 | }
|
| 73 |
|
| 74 | private record BatchKey(Holder<TestEnvironmentDefinition<?>> environment, ResourceKey<Level> dimension) {
|
| 75 | }
|
| 76 |
|
| 77 | @FunctionalInterface
|
| 78 | public interface TestDecorator {
|
| 79 | Stream<GameTestInfo> decorate(Holder.Reference<GameTestInstance> test, ServerLevel level);
|
| 80 | }
|
| 81 | }
|
| 82 |
|