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

Replace string constants with a dedicated enum for test statuses #7218

Merged
merged 1 commit into from
Jun 19, 2024
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 @@ -3,7 +3,6 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;

import datadog.trace.api.Config;
import datadog.trace.api.civisibility.CIConstants;
import datadog.trace.api.civisibility.telemetry.CiVisibilityCountMetric;
import datadog.trace.api.civisibility.telemetry.CiVisibilityMetricCollector;
import datadog.trace.api.civisibility.telemetry.tag.EventType;
Expand Down Expand Up @@ -71,7 +70,7 @@ public AbstractTestModule(

// setting status to skip initially,
// as we do not know in advance whether the module will have any children
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_SKIP);
span.setTag(Tags.TEST_STATUS, TestStatus.skip);

testDecorator.afterStart(span);

Expand All @@ -89,11 +88,11 @@ public void setTag(String key, Object value) {
public void setErrorInfo(Throwable error) {
span.setError(true);
span.addThrowable(error);
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_FAIL);
span.setTag(Tags.TEST_STATUS, TestStatus.fail);
}

public void setSkipReason(String skipReason) {
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_SKIP);
span.setTag(Tags.TEST_STATUS, TestStatus.skip);
if (skipReason != null) {
span.setTag(Tags.TEST_SKIP_REASON, skipReason);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;

import datadog.trace.api.Config;
import datadog.trace.api.civisibility.CIConstants;
import datadog.trace.api.civisibility.telemetry.CiVisibilityCountMetric;
import datadog.trace.api.civisibility.telemetry.CiVisibilityMetricCollector;
import datadog.trace.api.civisibility.telemetry.TagValue;
Expand Down Expand Up @@ -72,7 +71,7 @@ public AbstractTestSession(

// setting status to skip initially,
// as we do not know in advance whether the session will have any children
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_SKIP);
span.setTag(Tags.TEST_STATUS, TestStatus.skip);

span.setResourceName(projectName);

Expand Down Expand Up @@ -106,11 +105,11 @@ public void setTag(String key, Object value) {
public void setErrorInfo(Throwable error) {
span.setError(true);
span.addThrowable(error);
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_FAIL);
span.setTag(Tags.TEST_STATUS, TestStatus.fail);
}

public void setSkipReason(String skipReason) {
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_SKIP);
span.setTag(Tags.TEST_STATUS, TestStatus.skip);
if (skipReason != null) {
span.setTag(Tags.TEST_SKIP_REASON, skipReason);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public TestImpl(
span.setTag(Tags.TEST_MODULE_ID, moduleId);
span.setTag(Tags.TEST_SESSION_ID, sessionId);

span.setTag(Tags.TEST_STATUS, CIConstants.TEST_PASS);
span.setTag(Tags.TEST_STATUS, TestStatus.pass);

if (testClass != null && !testClass.getName().equals(testSuiteName)) {
span.setTag(Tags.TEST_SOURCE_CLASS, testClass.getName());
Expand Down Expand Up @@ -178,12 +178,12 @@ public void setTag(String key, Object value) {
public void setErrorInfo(Throwable error) {
span.setError(true);
span.addThrowable(error);
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_FAIL);
span.setTag(Tags.TEST_STATUS, TestStatus.fail);
}

@Override
public void setSkipReason(String skipReason) {
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_SKIP);
span.setTag(Tags.TEST_STATUS, TestStatus.skip);
if (skipReason != null) {
span.setTag(Tags.TEST_SKIP_REASON, skipReason);

Expand Down Expand Up @@ -222,7 +222,7 @@ public void end(@Nullable Long endTime) {
CoverageBridge.removeThreadLocalCoverageProbeStore();
boolean coveragesGathered =
context.getCoverageProbeStore().report(sessionId, suiteId, span.getSpanId());
if (!coveragesGathered && !CIConstants.TEST_SKIP.equals(span.getTag(Tags.TEST_STATUS))) {
if (!coveragesGathered && !TestStatus.skip.equals(span.getTag(Tags.TEST_STATUS))) {
// test is not skipped, but no coverages were gathered
metricCollector.add(CiVisibilityCountMetric.CODE_COVERAGE_IS_EMPTY, 1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package datadog.trace.civisibility.domain;

public enum TestStatus {
pass,
fail,
skip
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;

import datadog.trace.api.Config;
import datadog.trace.api.civisibility.CIConstants;
import datadog.trace.api.civisibility.DDTestSuite;
import datadog.trace.api.civisibility.telemetry.CiVisibilityCountMetric;
import datadog.trace.api.civisibility.telemetry.CiVisibilityMetricCollector;
Expand Down Expand Up @@ -103,7 +102,7 @@ public TestSuiteImpl(

// setting status to skip initially,
// as we do not know in advance whether the suite will have any children
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_SKIP);
span.setTag(Tags.TEST_STATUS, TestStatus.skip);

this.testClass = testClass;
if (this.testClass != null) {
Expand Down Expand Up @@ -138,12 +137,12 @@ public void setTag(String key, Object value) {
public void setErrorInfo(Throwable error) {
span.setError(true);
span.addThrowable(error);
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_FAIL);
span.setTag(Tags.TEST_STATUS, TestStatus.fail);
}

@Override
public void setSkipReason(String skipReason) {
span.setTag(Tags.TEST_STATUS, CIConstants.TEST_SKIP);
span.setTag(Tags.TEST_STATUS, TestStatus.skip);
if (skipReason != null) {
span.setTag(Tags.TEST_SKIP_REASON, skipReason);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package datadog.trace.civisibility.events;

import datadog.trace.api.civisibility.CIConstants;
import datadog.trace.api.civisibility.config.ModuleExecutionSettings;
import datadog.trace.api.civisibility.events.BuildEventsHandler;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.civisibility.config.JvmInfo;
import datadog.trace.civisibility.config.JvmInfoFactory;
import datadog.trace.civisibility.domain.BuildSystemModule;
import datadog.trace.civisibility.domain.BuildSystemSession;
import datadog.trace.civisibility.domain.TestStatus;
import java.io.File;
import java.nio.file.Path;
import java.util.Collection;
Expand Down Expand Up @@ -86,7 +86,7 @@ public ModuleInfo onTestModuleStart(

BuildSystemSession testSession = inProgressTestSessions.get(sessionKey);
BuildSystemModule testModule = testSession.testModuleStart(moduleName, null, outputClassesDirs);
testModule.setTag(Tags.TEST_STATUS, CIConstants.TEST_PASS);
testModule.setTag(Tags.TEST_STATUS, TestStatus.pass);

if (additionalTags != null) {
for (Map.Entry<String, Object> e : additionalTags.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package datadog.trace.civisibility.utils;

import datadog.trace.api.civisibility.CIConstants;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.civisibility.domain.TestStatus;
import datadog.trace.civisibility.ipc.TestFramework;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -97,24 +97,24 @@ private static void setFrameworks(AgentSpan span, Collection<TestFramework> fram
}

private static void propagateStatus(AgentSpan parentSpan, AgentSpan childSpan) {
String childStatus = (String) childSpan.getTag(Tags.TEST_STATUS);
TestStatus childStatus = (TestStatus) childSpan.getTag(Tags.TEST_STATUS);
if (childStatus == null) {
return;
}

String parentStatus = (String) parentSpan.getTag(Tags.TEST_STATUS);
TestStatus parentStatus = (TestStatus) parentSpan.getTag(Tags.TEST_STATUS);
switch (childStatus) {
case CIConstants.TEST_PASS:
if (parentStatus == null || CIConstants.TEST_SKIP.equals(parentStatus)) {
parentSpan.setTag(Tags.TEST_STATUS, CIConstants.TEST_PASS);
case pass:
if (parentStatus == null || TestStatus.skip.equals(parentStatus)) {
parentSpan.setTag(Tags.TEST_STATUS, TestStatus.pass);
}
break;
case CIConstants.TEST_FAIL:
parentSpan.setTag(Tags.TEST_STATUS, CIConstants.TEST_FAIL);
case fail:
parentSpan.setTag(Tags.TEST_STATUS, TestStatus.fail);
break;
case CIConstants.TEST_SKIP:
case skip:
if (parentStatus == null) {
parentSpan.setTag(Tags.TEST_STATUS, CIConstants.TEST_SKIP);
parentSpan.setTag(Tags.TEST_STATUS, TestStatus.skip);
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package datadog.trace.api.civisibility;

public interface CIConstants {
String TEST_PASS = "pass";
String TEST_FAIL = "fail";
String TEST_SKIP = "skip";

/**
* Indicates that early flakiness detection feature was aborted in a test session because too many
* test cases were considered new.
Expand Down
Loading