|
| 1 | +package com.carrotsearch.randomizedtesting.tests; |
| 2 | + |
| 3 | +import static com.carrotsearch.randomizedtesting.tests.infra.TestInfra.*; |
| 4 | +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; |
| 5 | +import static org.junit.platform.testkit.engine.EventConditions.event; |
| 6 | +import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure; |
| 7 | +import static org.junit.platform.testkit.engine.EventConditions.test; |
| 8 | + |
| 9 | +import com.carrotsearch.randomizedtesting.jupiter.Randomized; |
| 10 | +import com.carrotsearch.randomizedtesting.jupiter.RandomizedContext; |
| 11 | +import com.carrotsearch.randomizedtesting.jupiter.RepeatExecutionTestEngine; |
| 12 | +import com.carrotsearch.randomizedtesting.jupiter.SeedChain; |
| 13 | +import com.carrotsearch.randomizedtesting.jupiter.SysProps; |
| 14 | +import com.carrotsearch.randomizedtesting.tests.infra.IgnoreInStandaloneRuns; |
| 15 | +import com.carrotsearch.randomizedtesting.tests.infra.TestInfra; |
| 16 | +import java.io.PrintWriter; |
| 17 | +import java.util.List; |
| 18 | +import org.assertj.core.api.Assertions; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +/** Verifies that {@link RepeatExecutionTestEngine} correctly multiplies test execution. */ |
| 22 | +public class F007_TestReiteration { |
| 23 | + @Test |
| 24 | + void noReiterationsByDefault() { |
| 25 | + var result = |
| 26 | + collectExecutionResults( |
| 27 | + TestInfra.testKitBuilder(RepeatExecutionTestEngine.ENGINE_ID) |
| 28 | + .selectors(selectClass(SimpleTest.class))); |
| 29 | + |
| 30 | + result |
| 31 | + .results() |
| 32 | + .testEvents() |
| 33 | + .assertThatEvents() |
| 34 | + .doNotHave(event(finishedWithFailure())) |
| 35 | + .haveExactly(0, event(test())); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testsAreMultipliedByIterationCount() { |
| 40 | + var result = |
| 41 | + collectExecutionResults( |
| 42 | + TestInfra.testKitBuilder(RepeatExecutionTestEngine.ENGINE_ID) |
| 43 | + .configurationParameter(SysProps.TESTS_ITERS.propertyKey, "3") |
| 44 | + .selectors(selectClass(SimpleTest.class))); |
| 45 | + |
| 46 | + result.results().testEvents().assertStatistics(s -> s.finished(3).succeeded(3)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void seedsAreIdenticalAcrossIterationsWithFixedRootSeed() { |
| 51 | + var iterations = 5; |
| 52 | + var result = |
| 53 | + collectExecutionResults( |
| 54 | + TestInfra.testKitBuilder(RepeatExecutionTestEngine.ENGINE_ID) |
| 55 | + .configurationParameter(SysProps.TESTS_ITERS.propertyKey, "" + iterations) |
| 56 | + .configurationParameter(SysProps.TESTS_SEED.propertyKey, "DEADBEEF") |
| 57 | + .selectors(selectClass(SimpleTest.class))); |
| 58 | + |
| 59 | + result |
| 60 | + .results() |
| 61 | + .testEvents() |
| 62 | + .assertStatistics(s -> s.finished(iterations).succeeded(iterations)); |
| 63 | + |
| 64 | + // Each iteration should have the same seed chain because we strip the top-level reiteration |
| 65 | + // segments. |
| 66 | + Assertions.assertThat(result.capturedOutput().values().stream().distinct()) |
| 67 | + .as("seed chains should be the same across iterations") |
| 68 | + .hasSize(1); |
| 69 | + |
| 70 | + Assertions.assertThat( |
| 71 | + result.capturedOutput().values().stream() |
| 72 | + .map(value -> value.split("\\s")[0]) |
| 73 | + .map(v -> SeedChain.parse(v)) |
| 74 | + .map(chain -> chain.seeds().getFirst().toString())) |
| 75 | + .allMatch(v -> v.equals("DEADBEEF")); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void seedsAreDifferentAcrossIterationsWithNoRootSeed() { |
| 80 | + var iterations = 5; |
| 81 | + var result = |
| 82 | + collectExecutionResults( |
| 83 | + TestInfra.testKitBuilder(RepeatExecutionTestEngine.ENGINE_ID) |
| 84 | + .configurationParameter(SysProps.TESTS_ITERS.propertyKey, "" + iterations) |
| 85 | + .selectors(selectClass(SimpleTest.class))); |
| 86 | + |
| 87 | + result |
| 88 | + .results() |
| 89 | + .testEvents() |
| 90 | + .assertStatistics(s -> s.finished(iterations).succeeded(iterations)); |
| 91 | + |
| 92 | + // Each iteration should have a random root seed if there is no top-level fixed seed. |
| 93 | + Assertions.assertThat(result.capturedOutput().values().stream().distinct()) |
| 94 | + .as("seed chains should be different across iterations") |
| 95 | + .hasSizeBetween(iterations - 2, iterations); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void randomnessIsIdenticalForJupiterAndReiteratedTests() { |
| 100 | + List<String> jupiterResults; |
| 101 | + List<String> repeatedExecutionResults; |
| 102 | + |
| 103 | + { |
| 104 | + var result = |
| 105 | + collectExecutionResults( |
| 106 | + TestInfra.testKitBuilder() |
| 107 | + .configurationParameter(SysProps.TESTS_SEED.propertyKey, "DEADBEEF") |
| 108 | + .selectors(selectClass(SimpleTest.class))); |
| 109 | + |
| 110 | + result.results().testEvents().assertStatistics(s -> s.finished(1).succeeded(1)); |
| 111 | + |
| 112 | + jupiterResults = result.capturedOutput().values().stream().toList(); |
| 113 | + } |
| 114 | + |
| 115 | + { |
| 116 | + var result = |
| 117 | + collectExecutionResults( |
| 118 | + TestInfra.testKitBuilder(RepeatExecutionTestEngine.ENGINE_ID) |
| 119 | + .configurationParameter(SysProps.TESTS_ITERS.propertyKey, "1") |
| 120 | + .configurationParameter(SysProps.TESTS_SEED.propertyKey, "DEADBEEF") |
| 121 | + .selectors(selectClass(SimpleTest.class))); |
| 122 | + |
| 123 | + result.results().testEvents().assertStatistics(s -> s.finished(1).succeeded(1)); |
| 124 | + |
| 125 | + repeatedExecutionResults = result.capturedOutput().values().stream().toList(); |
| 126 | + } |
| 127 | + |
| 128 | + Assertions.assertThat(jupiterResults).containsExactlyElementsOf(repeatedExecutionResults); |
| 129 | + } |
| 130 | + |
| 131 | + @Randomized |
| 132 | + static class SimpleTest extends IgnoreInStandaloneRuns { |
| 133 | + @Test |
| 134 | + void test(PrintWriter pw, RandomizedContext ctx) { |
| 135 | + pw.println(ctx.getSeedChain() + " " + ctx.getRandom().nextLong()); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments