Add some tests for Java CompletableFuture (DataDog/dd-trace-java#1546)
ADD
instrumentation/java-concurrent/src/test/groovy/CompletableFutureTest.groovy
+/*
+ * Copyright The OpenTelemetry Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import io.opentelemetry.auto.test.AgentTestRunner
+import spock.lang.Requires
+
+import java.util.concurrent.CompletableFuture
+
+import static io.opentelemetry.auto.test.utils.TraceUtils.basicSpan
+import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
+
+@Requires({ javaVersion >= 1.8 })
+class CompletableFutureTest extends AgentTestRunner {
+
+ def "test supplyAsync"() {
+ when:
+ CompletableFuture<String> completableFuture = runUnderTrace("parent") {
+ def result = CompletableFuture.supplyAsync {
+ runUnderTrace("child") {
+ "done"
+ }
+ }
+ return result
+ }
+
+ then:
+ completableFuture.get() == "done"
+
+ and:
+ assertTraces(1) {
+ trace(0, 2) {
+ basicSpan(it, 0, "parent")
+ basicSpan(it, 1, "child", span(0))
+ }
+ }
+ }
+
+ def "test thenApply"() {
+ when:
+ CompletableFuture<String> completableFuture = runUnderTrace("parent") {
+ CompletableFuture.supplyAsync {
+ "done"
+ }.thenApply { result ->
+ runUnderTrace("child") {
+ result
+ }
+ }
+ }
+
+ then:
+ completableFuture.get() == "done"
+
+ and:
+ assertTraces(1) {
+ trace(0, 2) {
+ basicSpan(it, 0, "parent")
+ basicSpan(it, 1, "child", span(0))
+ }
+ }
+ }
+
+ def "test thenApplyAsync"() {
+ when:
+ CompletableFuture<String> completableFuture = runUnderTrace("parent") {
+ def result = CompletableFuture.supplyAsync {
+ "done"
+ }.thenApplyAsync { result ->
+ runUnderTrace("child") {
+ result
+ }
+ }
+ return result
+ }
+
+ then:
+ completableFuture.get() == "done"
+
+ and:
+ assertTraces(1) {
+ trace(0, 2) {
+ basicSpan(it, 0, "parent")
+ basicSpan(it, 1, "child", span(0))
+ }
+ }
+ }
+
+ def "test thenCompose"() {
+ when:
+ CompletableFuture<String> completableFuture = runUnderTrace("parent") {
+ def result = CompletableFuture.supplyAsync {
+ "done"
+ }.thenCompose { result ->
+ CompletableFuture.supplyAsync {
+ runUnderTrace("child") {
+ result
+ }
+ }
+ }
+ return result
+ }
+
+ then:
+ completableFuture.get() == "done"
+
+ and:
+ assertTraces(1) {
+ trace(0, 2) {
+ basicSpan(it, 0, "parent")
+ basicSpan(it, 1, "child", span(0))
+ }
+ }
+ }
+
+ def "test thenComposeAsync"() {
+ when:
+ CompletableFuture<String> completableFuture = runUnderTrace("parent") {
+ def result = CompletableFuture.supplyAsync {
+ "done"
+ }.thenComposeAsync { result ->
+ CompletableFuture.supplyAsync {
+ runUnderTrace("child") {
+ result
+ }
+ }
+ }
+ return result
+ }
+
+ then:
+ completableFuture.get() == "done"
+
+ and:
+ assertTraces(1) {
+ trace(0, 2) {
+ basicSpan(it, 0, "parent")
+ basicSpan(it, 1, "child", span(0))
+ }
+ }
+ }
+
+ def "test compose and apply"() {
+ when:
+ CompletableFuture<String> completableFuture = runUnderTrace("parent") {
+ def result = CompletableFuture.supplyAsync {
+ "do"
+ }.thenCompose { result ->
+ CompletableFuture.supplyAsync {
+ result + "ne"
+ }
+ }.thenApplyAsync { result ->
+ runUnderTrace("child") {
+ result
+ }
+ }
+ return result
+ }
+
+ then:
+ completableFuture.get() == "done"
+
+ and:
+ assertTraces(1) {
+ trace(0, 2) {
+ basicSpan(it, 0, "parent")
+ basicSpan(it, 1, "child", span(0))
+ }
+ }
+ }
+}

MODIFY
settings.gradle
settings.gradle
@@ -79,6 +79,7 @@ include ':instrumentation:java-class-loader:jboss-testing'
include ':instrumentation:java-class-loader:osgi-testing'
include ':instrumentation:java-class-loader:tomcat-testing'
include ':instrumentation:java-concurrent'
+include ':instrumentation:java-concurrent:java-completablefuture'
include ':instrumentation:java-concurrent:kotlin-testing'
include ':instrumentation:java-concurrent:scala-testing'
include ':instrumentation:java-concurrent:akka-testing'

Add some tests for Java CompletableFuture (DataDog/dd-trace-java#1546)