From ee06a83dd9c38c29339a956a75629750b0ec9da8 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Wed, 16 Mar 2022 08:08:10 +0900 Subject: [PATCH] Migrate AkkaActorTest to scala (#5582) --- .../kotlin/otel.scala-conventions.gradle.kts | 1 + dependencyManagement/build.gradle.kts | 1 + .../src/test/groovy/AkkaActorTest.groovy | 45 ------ .../akkaactor/AkkaActorTest.scala | 128 ++++++++++++++++++ .../akkaactor}/AkkaActors.scala | 7 + .../junit/InstrumentationExtension.java | 4 + 6 files changed, 141 insertions(+), 45 deletions(-) delete mode 100644 instrumentation/akka/akka-actor-2.5/javaagent/src/test/groovy/AkkaActorTest.groovy create mode 100644 instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/io/opentelemetry/instrumentation/akkaactor/AkkaActorTest.scala rename instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/{ => io/opentelemetry/instrumentation/akkaactor}/AkkaActors.scala (95%) diff --git a/conventions/src/main/kotlin/otel.scala-conventions.gradle.kts b/conventions/src/main/kotlin/otel.scala-conventions.gradle.kts index 0dadb3225bed..7526753ca728 100644 --- a/conventions/src/main/kotlin/otel.scala-conventions.gradle.kts +++ b/conventions/src/main/kotlin/otel.scala-conventions.gradle.kts @@ -6,6 +6,7 @@ plugins { dependencies { testImplementation("org.scala-lang:scala-library") + testImplementation("org.scala-lang.modules:scala-java8-compat_2.11") } tasks { diff --git a/dependencyManagement/build.gradle.kts b/dependencyManagement/build.gradle.kts index 7d6d31348d79..98a200b84ae0 100644 --- a/dependencyManagement/build.gradle.kts +++ b/dependencyManagement/build.gradle.kts @@ -116,6 +116,7 @@ val DEPENDENCIES = listOf( "org.spockframework:spock-core:2.2-M1-groovy-4.0", "org.spockframework:spock-junit4:2.2-M1-groovy-4.0", "org.scala-lang:scala-library:2.11.12", + "org.scala-lang.modules:scala-java8-compat_2.11:1.0.2", // Note that this is only referenced as "org.springframework.boot" in build files, not the artifact name. "org.springframework.boot:spring-boot-dependencies:2.3.1.RELEASE" ) diff --git a/instrumentation/akka/akka-actor-2.5/javaagent/src/test/groovy/AkkaActorTest.groovy b/instrumentation/akka/akka-actor-2.5/javaagent/src/test/groovy/AkkaActorTest.groovy deleted file mode 100644 index d117260f89e8..000000000000 --- a/instrumentation/akka/akka-actor-2.5/javaagent/src/test/groovy/AkkaActorTest.groovy +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification - -class AkkaActorTest extends AgentInstrumentationSpecification { - - def "akka #testMethod #count"() { - setup: - AkkaActors akkaTester = new AkkaActors() - count.times { - akkaTester."$testMethod"() - } - - expect: - assertTraces(count) { - count.times { - trace(it, 2) { - span(0) { - name "parent" - attributes { - } - } - span(1) { - name "$expectedGreeting, Akka" - childOf span(0) - attributes { - } - } - } - } - } - - where: - testMethod | expectedGreeting | count - "basicTell" | "Howdy" | 1 - "basicAsk" | "Howdy" | 1 - "basicForward" | "Hello" | 1 - "basicTell" | "Howdy" | 150 - "basicAsk" | "Howdy" | 150 - "basicForward" | "Hello" | 150 - } -} diff --git a/instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/io/opentelemetry/instrumentation/akkaactor/AkkaActorTest.scala b/instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/io/opentelemetry/instrumentation/akkaactor/AkkaActorTest.scala new file mode 100644 index 000000000000..1c7c308b6afe --- /dev/null +++ b/instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/io/opentelemetry/instrumentation/akkaactor/AkkaActorTest.scala @@ -0,0 +1,128 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.akkaactor + +import collection.JavaConverters._ +import io.opentelemetry.api.common.Attributes +import io.opentelemetry.instrumentation.testing.junit.{ + AgentInstrumentationExtension, + InstrumentationExtension +} +import io.opentelemetry.sdk.testing.assertj.{SpanDataAssert, TraceAssert} +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.api.extension.RegisterExtension +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import scala.compat.java8.FunctionConverters._ + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class AkkaActorTest { + + @RegisterExtension val testing: InstrumentationExtension = + AgentInstrumentationExtension.create + + @ParameterizedTest + @ValueSource(ints = Array(1, 150)) + def basicTell(count: Int): Unit = { + val tester = new AkkaActors + (1 to count).foreach { _ => + tester.basicTell() + } + + val assertions = (1 to count) + .map(_ => + ((trace: TraceAssert) => { + trace.hasSpansSatisfyingExactly( + ((span: SpanDataAssert) => { + span + .hasName("parent") + .hasAttributes(Attributes.empty()) + () + }).asJava, + ((span: SpanDataAssert) => { + span + .hasName("Howdy, Akka") + .hasParent(trace.getSpan(0)) + .hasAttributes(Attributes.empty()) + () + }).asJava + ) + () + }).asJava + ) + .asJava + + testing.waitAndAssertTraces(assertions) + } + + @ParameterizedTest + @ValueSource(ints = Array(1, 150)) + def basicAsk(count: Int): Unit = { + val tester = new AkkaActors + (1 to count).foreach { _ => + tester.basicAsk() + } + + val assertions = (1 to count) + .map(_ => + ((trace: TraceAssert) => { + trace.hasSpansSatisfyingExactly( + ((span: SpanDataAssert) => { + span + .hasName("parent") + .hasAttributes(Attributes.empty()) + () + }).asJava, + ((span: SpanDataAssert) => { + span + .hasName("Howdy, Akka") + .hasParent(trace.getSpan(0)) + .hasAttributes(Attributes.empty()) + () + }).asJava + ) + () + }).asJava + ) + .asJava + + testing.waitAndAssertTraces(assertions) + } + + @ParameterizedTest + @ValueSource(ints = Array(1, 150)) + def basicForward(count: Int): Unit = { + val tester = new AkkaActors + (1 to count).foreach { _ => + tester.basicForward() + } + + val assertions = (1 to count) + .map(_ => + ((trace: TraceAssert) => { + trace.hasSpansSatisfyingExactly( + ((span: SpanDataAssert) => { + span + .hasName("parent") + .hasAttributes(Attributes.empty()) + () + }).asJava, + ((span: SpanDataAssert) => { + span + .hasName("Hello, Akka") + .hasParent(trace.getSpan(0)) + .hasAttributes(Attributes.empty()) + () + }).asJava + ) + () + }).asJava + ) + .asJava + + testing.waitAndAssertTraces(assertions) + } +} diff --git a/instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/AkkaActors.scala b/instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/io/opentelemetry/instrumentation/akkaactor/AkkaActors.scala similarity index 95% rename from instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/AkkaActors.scala rename to instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/io/opentelemetry/instrumentation/akkaactor/AkkaActors.scala index d4fce2f9a11a..1d0a7879c215 100644 --- a/instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/AkkaActors.scala +++ b/instrumentation/akka/akka-actor-2.5/javaagent/src/test/scala/io/opentelemetry/instrumentation/akkaactor/AkkaActors.scala @@ -1,3 +1,10 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.akkaactor + /* * Copyright The OpenTelemetry Authors * SPDX-License-Identifier: Apache-2.0 diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/InstrumentationExtension.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/InstrumentationExtension.java index b7e9a354eb0a..7fd6572e5562 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/InstrumentationExtension.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/InstrumentationExtension.java @@ -115,6 +115,10 @@ public final void waitAndAssertTraces(Consumer... assertions) { testRunner.waitAndAssertTraces(assertions); } + public final void waitAndAssertTraces(Iterable> assertions) { + testRunner.waitAndAssertTraces(assertions); + } + /** * Runs the provided {@code callback} inside the scope of an INTERNAL span with name {@code * spanName}.