Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tracing skipped suites in MUnit 1.0.1 #7605

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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