Skip to content

Commit

Permalink
Fix tracing skipped suites in MUnit 1.0.1 (#7605)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog committed Sep 13, 2024
1 parent a7d9a79 commit 8f1e767
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ muzzle {
pass {
group = 'org.scalameta'
module = 'munit_2.13'
versions = '[0.7.28,1.0.0]'
versions = '[0.7.28,)'
}
}

Expand All @@ -28,10 +28,7 @@ dependencies {
testImplementation group: 'org.scala-lang', name: 'scala-library', version: '2.13.10'
testImplementation group: 'org.scalameta', name: 'munit_2.13', version: '0.7.28'

// latest version as of august 2024 is 1.0.1, but that version changes which notifications are sent when a test is skipped,
// making the tests fail. See https://github.com/scalameta/munit/issues/813 and https://github.com/DataDog/dd-trace-java/pull/7502/commits/ecda25e
// TODO replace the fixed version with '+' once the github issue is resolved OR the code/tests are updated to accept the new behavior.
latestDepTestImplementation group: 'org.scalameta', name: 'munit_2.13', version: '1.0.0'
latestDepTestImplementation group: 'org.scalameta', name: 'munit_2.13', version: '+'
}

compileTestGroovy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.InternalSpanTypes;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.util.Strings;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
Expand Down Expand Up @@ -149,7 +150,7 @@ public void testIgnored(final Description description) {

if (Strings.isNotBlank(testName)) {
TestDescriptor testDescriptor = MUnitUtils.toTestDescriptor(description);
if (!isTestInProgress()) {
if (!isSpanInProgress(InternalSpanTypes.TEST)) {
// earlier versions of MUnit (e.g. 0.7.28) trigger "testStarted" event for ignored tests,
// while newer versions don't
TestSuiteDescriptor suiteDescriptor = MUnitUtils.toSuiteDescriptor(description);
Expand All @@ -173,21 +174,41 @@ public void testIgnored(final Description description) {

} else if (testClass != null) {
TestSuiteDescriptor suiteDescriptor = MUnitUtils.toSuiteDescriptor(description);

boolean suiteStarted = isSpanInProgress(InternalSpanTypes.TEST_SUITE_END);
if (!suiteStarted) {
// there is a bug in MUnit 1.0.1+: start/finish events are not fired for skipped suites
List<String> categories = getCategories(description);
TestEventsHandlerHolder.TEST_EVENTS_HANDLER.onTestSuiteStart(
suiteDescriptor,
testSuiteName,
FRAMEWORK_NAME,
FRAMEWORK_VERSION,
testClass,
categories,
false,
TestFrameworkInstrumentation.MUNIT);
}

TestEventsHandlerHolder.TEST_EVENTS_HANDLER.onTestSuiteSkip(suiteDescriptor, null);
for (Description child : description.getChildren()) {
testCaseIgnored(child);
}

if (!suiteStarted) {
TestEventsHandlerHolder.TEST_EVENTS_HANDLER.onTestSuiteFinish(suiteDescriptor);
}
}
}

private boolean isTestInProgress() {
private static boolean isSpanInProgress(UTF8BytesString type) {
final AgentScope scope = AgentTracer.activeScope();
if (scope == null) {
return false;
}
AgentSpan scopeSpan = scope.span();
String spanType = scopeSpan.getSpanType();
return spanType != null && spanType.contentEquals(InternalSpanTypes.TEST);
return spanType != null && spanType.contentEquals(type);
}

private void testCaseIgnored(final Description description) {
Expand Down

0 comments on commit 8f1e767

Please sign in to comment.