Skip to content
Open
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 @@ -59,7 +59,11 @@ public void methodAdvice(MethodTransformer transformer) {
@Override
public String[] helperClassNames() {
return new String[] {
commonPackageName + ".TestNGUtils", commonPackageName + ".TestNGClassListener"
commonPackageName + ".TestEventsHandlerHolder",
commonPackageName + ".execution.RetryAnalyzer",
commonPackageName + ".execution.RetryAnnotationTransformer",
commonPackageName + ".TestNGUtils",
commonPackageName + ".TestNGClassListener"
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public void methodAdvice(MethodTransformer transformer) {
@Override
public String[] helperClassNames() {
return new String[] {
commonPackageName + ".TestNGUtils", commonPackageName + ".TestNGClassListener"
commonPackageName + ".TestEventsHandlerHolder",
commonPackageName + ".execution.RetryAnalyzer",
commonPackageName + ".execution.RetryAnnotationTransformer",
commonPackageName + ".TestNGUtils",
commonPackageName + ".TestNGClassListener"
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public String[] helperClassNames() {
commonPackageName + ".TestEventsHandlerHolder",
commonPackageName + ".TestNGClassListener",
commonPackageName + ".execution.RetryAnalyzer",
commonPackageName + ".execution.RetryAnnotationTransformer",
commonPackageName + ".TracingListener",
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public String[] helperClassNames() {
parentPackageName + ".TestNGClassListener",
parentPackageName + ".TestNGUtils",
parentPackageName + ".TestEventsHandlerHolder",
parentPackageName + ".execution.RetryAnalyzer",
parentPackageName + ".execution.RetryAnnotationTransformer",
packageName + ".FailFastOrderInterceptor",
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ public static void addTracingListener(@Advice.This final TestNG testNG) {
testNG.addListener((ITestNGListener) suiteListener);

if (Config.get().isCiVisibilityExecutionPoliciesEnabled()) {
final RetryAnnotationTransformer transformer =
new RetryAnnotationTransformer(testNG.getAnnotationTransformer());
testNG.addListener(transformer);
testNG.addListener(new RetryAnnotationTransformer(testNG.getAnnotationTransformer()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package datadog.trace.instrumentation.testng;

import static net.bytebuddy.matcher.ElementMatchers.isConstructor;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.api.Config;
import net.bytebuddy.asm.Advice;
import org.testng.ITestNGMethod;
import org.testng.internal.annotations.IAnnotationFinder;

@AutoService(InstrumenterModule.class)
public class TestNGMethodInstrumentation extends InstrumenterModule.CiVisibility
implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice {
public TestNGMethodInstrumentation() {
super("testng", "test-retry");
}

@Override
public boolean isEnabled() {
return super.isEnabled() && Config.get().isCiVisibilityExecutionPoliciesEnabled();
}

@Override
public String instrumentedType() {
return "org.testng.internal.TestNGMethod";
}

@Override
public void methodAdvice(MethodTransformer transformer) {
transformer.applyAdvice(isConstructor(), getClass().getName() + "$ConfigureRetryAdvice");
}

public static class ConfigureRetryAdvice {
@Advice.OnMethodExit
public static void configure(
@Advice.This ITestNGMethod method,
@Advice.FieldValue("m_annotationFinder") IAnnotationFinder annotationFinder) {
TestNGUtils.configureRetryAnalyzer(method, annotationFinder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import datadog.trace.api.Config;
import datadog.trace.api.civisibility.CIConstants;
import datadog.trace.api.civisibility.config.TestIdentifier;
import datadog.trace.api.civisibility.config.TestSourceData;
import datadog.trace.api.civisibility.telemetry.tag.SkipReason;
import java.lang.reflect.Method;
import java.util.List;
import net.bytebuddy.asm.Advice;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;

Expand Down Expand Up @@ -57,14 +60,23 @@ public static void invokeMethod(
@Advice.Argument(1) final Object instance,
@Advice.Argument(2) final Object[] parameters) {
TestIdentifier testIdentifier = TestNGUtils.toTestIdentifier(method, instance, parameters);
ITestResult invocation = Reporter.getCurrentTestResult();
List<String> groups =
invocation != null
&& method.equals(TestNGUtils.toTestSourceData(invocation).getTestMethod())
? TestNGUtils.getGroups(invocation)
: TestNGUtils.getGroups(method);
SkipReason skipReason =
TestEventsHandlerHolder.TEST_EVENTS_HANDLER.skipReason(testIdentifier);
TestEventsHandlerHolder.TEST_EVENTS_HANDLER.skipReason(
testIdentifier,
new TestSourceData(
instance != null ? instance.getClass() : method.getDeclaringClass(), method),
groups);
if (skipReason == null) {
return;
}

if (skipReason == SkipReason.ITR) {
List<String> groups = TestNGUtils.getGroups(method);
if (groups.contains(CIConstants.Tags.ITR_UNSKIPPABLE_TAG)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import datadog.json.JsonWriter;
import datadog.trace.api.civisibility.config.LibraryCapability;
import datadog.trace.api.civisibility.config.TestExclusions;
import datadog.trace.api.civisibility.config.TestIdentifier;
import datadog.trace.api.civisibility.config.TestSourceData;
import datadog.trace.api.civisibility.events.TestSuiteDescriptor;
import datadog.trace.instrumentation.testng.execution.RetryAnalyzer;
import datadog.trace.instrumentation.testng.execution.RetryAnnotationTransformer;
import datadog.trace.util.ComparableVersion;
import java.io.InputStream;
import java.lang.invoke.MethodHandle;
Expand All @@ -26,9 +29,11 @@
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestNG;
import org.testng.annotations.ITestAnnotation;
import org.testng.annotations.Test;
import org.testng.internal.ConstructorOrMethod;
import org.testng.internal.ITestResultNotifier;
import org.testng.internal.annotations.IAnnotationFinder;
import org.testng.xml.XmlTest;

public abstract class TestNGUtils {
Expand All @@ -48,6 +53,51 @@ public abstract class TestNGUtils {
private static final MethodHandle TEST_METHOD_GET_RETRY_ANALYZER_LEGACY =
METHOD_HANDLES.method(ITestNGMethod.class, "getRetryAnalyzer");

private static final MethodHandle TEST_METHOD_GET_RETRY_ANALYZER_CLASS =
METHOD_HANDLES.method(ITestNGMethod.class, "getRetryAnalyzerClass");
private static final MethodHandle TEST_METHOD_SET_RETRY_ANALYZER_CLASS =
METHOD_HANDLES.method(ITestNGMethod.class, "setRetryAnalyzerClass", Class.class);
private static final MethodHandle TEST_METHOD_SET_RETRY_ANALYZER_LEGACY =
METHOD_HANDLES.method(ITestNGMethod.class, "setRetryAnalyzer", IRetryAnalyzer.class);

/** Apply Datadog policy after TestNG resolves annotations and native analyzer construction. */
public static void configureRetryAnalyzer(
ITestNGMethod method, IAnnotationFinder annotationFinder) {
TestSourceData source =
new TestSourceData(method.getRealClass(), method.getConstructorOrMethod().getMethod());
ITestAnnotation annotation =
(ITestAnnotation)
annotationFinder.findAnnotation(source.getTestMethod(), ITestAnnotation.class);
if (annotation == null) {
annotation =
(ITestAnnotation)
annotationFinder.findAnnotation(
source.getTestMethod().getDeclaringClass(), ITestAnnotation.class);
}
if (RetryAnnotationTransformer.hasFrameworkOverride(annotation)) {
return;
}
if (TestExclusions.noAutoRetry(source, Arrays.asList(method.getGroups()))) {
if (TEST_METHOD_GET_RETRY_ANALYZER_CLASS != null) {
Class<?> nativeAnalyzer =
METHOD_HANDLES.invoke(TEST_METHOD_GET_RETRY_ANALYZER_CLASS, method);
if (nativeAnalyzer != null
&& !nativeAnalyzer
.getName()
.equals("org.testng.internal.annotations.DisabledRetryAnalyzer")) {
return;
}
} else if (METHOD_HANDLES.invoke(TEST_METHOD_GET_RETRY_ANALYZER_LEGACY, method) != null) {
return;
}
}
if (TEST_METHOD_SET_RETRY_ANALYZER_CLASS != null) {
METHOD_HANDLES.invoke(TEST_METHOD_SET_RETRY_ANALYZER_CLASS, method, RetryAnalyzer.class);
} else {
METHOD_HANDLES.invoke(TEST_METHOD_SET_RETRY_ANALYZER_LEGACY, method, new RetryAnalyzer());
}
}

private static final ComparableVersion testNGv75 = new ComparableVersion("7.5");
private static final ComparableVersion testNGv70 = new ComparableVersion("7.0");

Expand Down Expand Up @@ -233,11 +283,17 @@ public static IRetryAnalyzer getRetryAnalyzer(ITestResult result) {
return null;
}
IRetryAnalyzer analyzer = METHOD_HANDLES.invoke(TEST_METHOD_GET_RETRY_ANALYZER, method, result);
if (analyzer != null) {
return analyzer;
} else {
return METHOD_HANDLES.invoke(TEST_METHOD_GET_RETRY_ANALYZER_LEGACY, method);
if (analyzer == null) {
analyzer = METHOD_HANDLES.invoke(TEST_METHOD_GET_RETRY_ANALYZER_LEGACY, method);
}
if (analyzer != null
&& !(analyzer instanceof RetryAnalyzer)
&& TestExclusions.noAutoRetry(toTestSourceData(result), getGroups(result))) {
// TestNG invokes its own analyzer. Keep a separate Datadog policy for reporting and
// quarantine.
return RetryAnalyzer.forFrameworkMethod(method, toTestIdentifier(result));
}
return analyzer;
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,37 @@
import datadog.trace.instrumentation.testng.TestNGUtils;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import org.testng.IRetryAnalyzer;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;

public class RetryAnalyzer implements IRetryAnalyzer {

private static final Map<ITestNGMethod, Map<TestIdentifier, RetryAnalyzer>> FRAMEWORK_TRACKERS =
Collections.synchronizedMap(
new WeakHashMap<ITestNGMethod, Map<TestIdentifier, RetryAnalyzer>>());
private volatile TestExecutionPolicy executionPolicy;

public static RetryAnalyzer forFrameworkMethod(ITestNGMethod method, TestIdentifier test) {
synchronized (FRAMEWORK_TRACKERS) {
Map<TestIdentifier, RetryAnalyzer> trackers = FRAMEWORK_TRACKERS.get(method);
if (trackers == null) {
trackers = new HashMap<>();
FRAMEWORK_TRACKERS.put(method, trackers);
}
RetryAnalyzer tracker = trackers.get(test);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Isolate native-retry suppression state per invocation

For a quarantined @NoAutoRetry test with a native retry analyzer and parallel data-provider invocations, distinct parameter objects with identical toString() values produce the same TestIdentifier and share this tracker. Both workers can set its suppressFailures flag before either consumes it; getAndResetSuppressFailures() then clears it for one worker, leaving the other's quarantined failure blocking the build. Isolate suppression state per ITestResult, updating both RetryAnalyzer and the execution advice.

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🤖 Bits Code Review · Open Bits AI session

if (tracker == null) {
tracker = new RetryAnalyzer();
trackers.put(test, tracker);
}
return tracker;
}
}

@SuppressFBWarnings(
value = "AT_STALE_THREAD_WRITE_OF_PRIMITIVE",
justification = "the field is confined to a single thread")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,64 @@
package datadog.trace.instrumentation.testng.execution;

import datadog.trace.util.MethodHandles;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.TestNG;
import org.testng.annotations.ITestAnnotation;

public class RetryAnnotationTransformer implements IAnnotationTransformer {
private static final MethodHandles METHOD_HANDLES =
new MethodHandles(TestNG.class.getClassLoader());
private static final MethodHandle GET_RETRY_ANALYZER_CLASS =
METHOD_HANDLES.method(ITestAnnotation.class, "getRetryAnalyzerClass");
private static final MethodHandle GET_RETRY_ANALYZER =
METHOD_HANDLES.method(ITestAnnotation.class, "getRetryAnalyzer");
private static final Map<ITestAnnotation, Boolean> FRAMEWORK_OVERRIDES =
Collections.synchronizedMap(new WeakHashMap<ITestAnnotation, Boolean>());
private final IAnnotationTransformer delegate;

public RetryAnnotationTransformer(IAnnotationTransformer delegate) {
this.delegate = delegate;
}

public static boolean hasFrameworkOverride(ITestAnnotation annotation) {
if (annotation == null) {
return false;
}
Boolean overridden = FRAMEWORK_OVERRIDES.get(annotation);
// A user-installed transformer can replace ours after TestNG construction.
return overridden == null || overridden;
}

private static Class<?> retryAnalyzerClass(ITestAnnotation annotation) {
if (GET_RETRY_ANALYZER_CLASS != null) {
return METHOD_HANDLES.invoke(GET_RETRY_ANALYZER_CLASS, annotation);
}
IRetryAnalyzer analyzer = METHOD_HANDLES.invoke(GET_RETRY_ANALYZER, annotation);
return analyzer != null ? analyzer.getClass() : null;
}

@Override
public void transform(
ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
FRAMEWORK_OVERRIDES.put(annotation, false);
Class<?> nativeAnalyzer = retryAnalyzerClass(annotation);
annotation.setRetryAnalyzer(RetryAnalyzer.class);
if (delegate != null) {
delegate.transform(annotation, testClass, testConstructor, testMethod);
}
if (retryAnalyzerClass(annotation) != RetryAnalyzer.class) {
// Preserve the existing precedence of explicit user transformer overrides.
FRAMEWORK_OVERRIDES.put(annotation, true);
} else if (nativeAnalyzer != null) {
// Let TestNG resolve class inheritance and effective groups before selecting DD policy.
annotation.setRetryAnalyzer(nativeAnalyzer);
}
}
}
Loading
Loading