Skip to content

Commit 39af191

Browse files
Fix events duplication
1 parent f61d4e7 commit 39af191

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

dd-java-agent/instrumentation/scalatest/src/main/java/datadog/trace/instrumentation/scalatest/ScalatestInstrumentation.java

+21
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public void methodAdvice(MethodTransformer transformer) {
5050
.and(takesArguments(1))
5151
.and(takesArgument(0, named("org.scalatest.events.Event"))),
5252
ScalatestInstrumentation.class.getName() + "$DispatchEventAdvice");
53+
transformer.applyAdvice(
54+
named("fireReadyEvents"),
55+
ScalatestInstrumentation.class.getName() + "$SuppressAsyncEventsAdvice");
5356
}
5457

5558
public static class DispatchEventAdvice {
@@ -76,4 +79,22 @@ public static void afterDispatchEvent() {
7679
CallDepthThreadLocalMap.decrementCallDepth(Reporter.class);
7780
}
7881
}
82+
83+
/**
84+
* {@link org.scalatest.tools.TestSortingReporter#fireReadyEvents} is triggered asynchronously. It
85+
* fires some events that are then delegated to other reporters. We need to suppress them (by
86+
* increasing the call depth so that {@link DispatchEventAdvice} is aborted) as the same events
87+
* are reported earlier synchronously from {@link org.scalatest.tools.TestSortingReporter#apply}
88+
*/
89+
public static class SuppressAsyncEventsAdvice {
90+
@Advice.OnMethodEnter
91+
public static void onAsyncEventsTrigger() {
92+
CallDepthThreadLocalMap.incrementCallDepth(Reporter.class);
93+
}
94+
95+
@Advice.OnMethodExit
96+
public static void afterAsyncEventsTrigger() {
97+
CallDepthThreadLocalMap.decrementCallDepth(Reporter.class);
98+
}
99+
}
79100
}

0 commit comments

Comments
 (0)