Skip to content

Commit 8685703

Browse files
Fix Cucumber test retries running under Suite (#11850)
feat: avoid final field modifications in junit instrumentation fix: cucumber retries running through `Suite` Merge branch 'master' into daniel.mohedano/fix-cucumber-retries Co-authored-by: daniel.mohedano <daniel.mohedano@datadoghq.com>
1 parent 0ec0f9a commit 8685703

8 files changed

Lines changed: 1310 additions & 10 deletions

File tree

dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/TestDataFactory.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import java.util.Collections;
66
import java.util.HashMap;
77
import java.util.Map;
8+
import java.util.Optional;
89
import java.util.function.Function;
910
import java.util.function.Predicate;
1011
import javax.annotation.Nullable;
1112
import org.junit.platform.engine.TestDescriptor;
12-
import org.junit.platform.engine.UniqueId;
1313

1414
public abstract class TestDataFactory {
1515

@@ -46,29 +46,36 @@ private static <K, V> Map<K, V> addEntry(Map<K, V> originalMap, K key, V value)
4646
}
4747

4848
public static TestIdentifier createTestIdentifier(TestDescriptor testDescriptor) {
49-
UniqueId uniqueId = testDescriptor.getUniqueId();
50-
return uniqueId
51-
.getEngineId()
49+
return engineId(testDescriptor)
5250
.map(TEST_IDENTIFIER_FACTORY_BY_ENGINE_ID::get)
5351
.orElse(JUnitPlatformUtils::toTestIdentifier)
5452
.apply(testDescriptor);
5553
}
5654

5755
public static TestSourceData createTestSourceData(TestDescriptor testDescriptor) {
58-
UniqueId uniqueId = testDescriptor.getUniqueId();
59-
return uniqueId
60-
.getEngineId()
56+
return engineId(testDescriptor)
6157
.map(TEST_SOURCE_DATA_FACTORY_BY_ENGINE_ID::get)
6258
.orElse(JUnitPlatformUtils::toTestSourceData)
6359
.apply(testDescriptor);
6460
}
6561

6662
public static boolean shouldBeTraced(TestDescriptor testDescriptor) {
67-
UniqueId uniqueId = testDescriptor.getUniqueId();
68-
return uniqueId
69-
.getEngineId()
63+
return engineId(testDescriptor)
7064
.map(TEST_DESCRIPTOR_FILTER_BY_ENGINE_ID::get)
7165
.map(filter -> filter.test(testDescriptor))
7266
.orElse(true);
7367
}
68+
69+
/**
70+
* Resolves the innermost {@code engine} segment rather than the root one returned by the JUnit
71+
* Platform {@code UniqueId#getEngineId()}.
72+
*
73+
* <p>Per-engine factories are registered under leaf engine ids ({@code cucumber}, {@code spock},
74+
* ...). When a framework runs nested under {@code junit-platform-suite-engine}, the unique id is
75+
* rooted at the suite engine (e.g. {@code
76+
* [engine:junit-platform-suite]/[suite:...]/[engine:cucumber]/...}).
77+
*/
78+
private static Optional<String> engineId(TestDescriptor testDescriptor) {
79+
return Optional.ofNullable(JUnitPlatformUtils.getEngineId(testDescriptor));
80+
}
7481
}

dd-smoke-tests/maven/src/test/java/datadog/smoketest/MavenSmokeTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static void closeMockBackend() throws Exception {
9494
"succeed-jacoco-argline | test_successful_maven_run_with_jacoco_and_argline | 3.9.9 | 5 | 1 | true | true | false | true | [] | 8 ",
9595
"succeed-cucumber | test_successful_maven_run_with_cucumber | 3.9.9 | 4 | 1 | true | false | false | true | [] | 8 ",
9696
"failed-flaky-retries | test_failed_maven_run_flaky_retries | 3.9.9 | 8 | 5 | false | false | true | true | [] | 8 ",
97+
"cucumber-suite-flaky | test_maven_run_cucumber_suite_flaky_retries | 3.9.9 | 8 | 5 | false | false | true | false | [] | 17 ",
9798
"succeed-junit-platform | test_successful_maven_run_junit_platform_runner | 3.9.9 | 4 | 0 | true | false | false | false | [] | 8 ",
9899
"succeed-arg-line-property | test_successful_maven_run_with_arg_line_property | 3.9.9 | 4 | 0 | true | false | false | false | [\"-DargLine='-Dmy-custom-property=provided-via-command-line'\"] | 8 ",
99100
"succeed-multi-forks-j8 | test_successful_maven_run_multiple_forks | 3.9.9 | 5 | 1 | true | true | false | true | [] | 8 ",
@@ -127,6 +128,10 @@ void testMavenRun(
127128
"Maven Smoke Tests Project maven-surefire-plugin default-test",
128129
"datadog.smoke.TestFailed",
129130
"test_failed");
131+
mockBackend.givenFlakyTest(
132+
"Maven Smoke Tests Project maven-surefire-plugin default-test",
133+
"classpath:datadog/smoke/basic_arithmetic.feature:Basic Arithmetic",
134+
"Basic Arithmetic - Addition"); // cucumber.junit-platform.naming-strategy=long
130135

131136
mockBackend.givenTestsSkipping(testsSkipping);
132137
mockBackend.givenSkippableTest(
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[ {
2+
"files" : [ {
3+
"filename" : "src/test/resources/datadog/smoke/basic_arithmetic.feature"
4+
}, {
5+
"filename" : "src/test/java/datadog/smoke/calculator/CalculatorSteps.java"
6+
} ],
7+
"span_id" : ${content_parent_id},
8+
"test_session_id" : ${content_test_session_id},
9+
"test_suite_id" : ${content_test_suite_id}
10+
}, {
11+
"files" : [ {
12+
"filename" : "src/test/resources/datadog/smoke/basic_arithmetic.feature"
13+
}, {
14+
"filename" : "src/test/java/datadog/smoke/calculator/CalculatorSteps.java"
15+
} ],
16+
"span_id" : ${content_parent_id_2},
17+
"test_session_id" : ${content_test_session_id},
18+
"test_suite_id" : ${content_test_suite_id}
19+
}, {
20+
"files" : [ {
21+
"filename" : "src/test/resources/datadog/smoke/basic_arithmetic.feature"
22+
}, {
23+
"filename" : "src/test/java/datadog/smoke/calculator/CalculatorSteps.java"
24+
} ],
25+
"span_id" : ${content_parent_id_3},
26+
"test_session_id" : ${content_test_session_id},
27+
"test_suite_id" : ${content_test_suite_id}
28+
}, {
29+
"files" : [ {
30+
"filename" : "src/test/resources/datadog/smoke/basic_arithmetic.feature"
31+
}, {
32+
"filename" : "src/test/java/datadog/smoke/calculator/CalculatorSteps.java"
33+
} ],
34+
"span_id" : ${content_parent_id_4},
35+
"test_session_id" : ${content_test_session_id},
36+
"test_suite_id" : ${content_test_suite_id}
37+
}, {
38+
"files" : [ {
39+
"filename" : "src/test/resources/datadog/smoke/basic_arithmetic.feature"
40+
}, {
41+
"filename" : "src/test/java/datadog/smoke/calculator/CalculatorSteps.java"
42+
} ],
43+
"span_id" : ${content_parent_id_5},
44+
"test_session_id" : ${content_test_session_id},
45+
"test_suite_id" : ${content_test_suite_id}
46+
} ]

0 commit comments

Comments
 (0)