Browse Source

Migrate akka http test to scala (#5586)

* Migrate akka http test to scala

* Don't use compat module

* Fix latest deps
Anuraag Agrawal 3 years ago
parent
commit
2c8b9ff1ce

+ 0 - 69
instrumentation/akka/akka-http-10.0/javaagent/src/test/groovy/AkkaHttpClientInstrumentationTest.groovy

@@ -1,69 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import akka.actor.ActorSystem
-import akka.http.javadsl.Http
-import akka.http.javadsl.model.HttpMethods
-import akka.http.javadsl.model.HttpRequest
-import akka.http.javadsl.model.HttpResponse
-import akka.http.javadsl.model.headers.RawHeader
-import akka.stream.ActorMaterializer
-import io.opentelemetry.instrumentation.test.AgentTestTrait
-import io.opentelemetry.instrumentation.test.base.HttpClientTest
-import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest
-import io.opentelemetry.instrumentation.testing.junit.http.SingleConnection
-import spock.lang.Shared
-
-import java.util.concurrent.TimeUnit
-
-class AkkaHttpClientInstrumentationTest extends HttpClientTest<HttpRequest> implements AgentTestTrait {
-
-  @Shared
-  ActorSystem system = ActorSystem.create()
-  @Shared
-  ActorMaterializer materializer = ActorMaterializer.create(system)
-
-  @Override
-  HttpRequest buildRequest(String method, URI uri, Map<String, String> headers) {
-    return HttpRequest.create(uri.toString())
-      .withMethod(HttpMethods.lookup(method).get())
-      .addHeaders(headers.collect { RawHeader.create(it.key, it.value) })
-  }
-
-  @Override
-  int sendRequest(HttpRequest request, String method, URI uri, Map<String, String> headers) {
-    HttpResponse response = Http.get(system)
-      .singleRequest(request, materializer)
-      .toCompletableFuture()
-      .get(10, TimeUnit.SECONDS)
-
-    response.discardEntityBytes(materializer)
-
-    return response.status().intValue()
-  }
-
-  @Override
-  void sendRequestWithCallback(HttpRequest request, String method, URI uri, Map<String, String> headers, AbstractHttpClientTest.RequestResult requestResult) {
-    Http.get(system).singleRequest(request, materializer).whenComplete { response, throwable ->
-      if (throwable == null) {
-        response.discardEntityBytes(materializer)
-      }
-      requestResult.complete({ response.status().intValue() }, throwable)
-    }
-  }
-
-  @Override
-  boolean testRedirects() {
-    false
-  }
-
-  @Override
-  SingleConnection createSingleConnection(String host, int port) {
-    // singleConnection test would require instrumentation to support requests made through pools
-    // (newHostConnectionPool, superPool, etc), which is currently not supported.
-    return null
-  }
-
-}

+ 0 - 57
instrumentation/akka/akka-http-10.0/javaagent/src/test/groovy/AkkaHttpServerInstrumentationTest.groovy

@@ -1,57 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import io.opentelemetry.api.common.AttributeKey
-import io.opentelemetry.instrumentation.test.AgentTestTrait
-import io.opentelemetry.instrumentation.test.base.HttpServerTest
-import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint
-
-abstract class AkkaHttpServerInstrumentationTest extends HttpServerTest<Object> implements AgentTestTrait {
-
-// FIXME: This doesn't work because we don't support bindAndHandle.
-//  @Override
-//  def startServer(int port) {
-//    AkkaHttpTestWebServer.start(port)
-//  }
-//
-//  @Override
-//  void stopServer(Object ignore) {
-//    AkkaHttpTestWebServer.stop()
-//  }
-
-  @Override
-  boolean testCapturedHttpHeaders() {
-    false
-  }
-
-  @Override
-  Set<AttributeKey<?>> httpAttributes(ServerEndpoint endpoint) {
-    []
-  }
-}
-
-class AkkaHttpServerInstrumentationTestSync extends AkkaHttpServerInstrumentationTest {
-  @Override
-  def startServer(int port) {
-    AkkaHttpTestSyncWebServer.start(port)
-  }
-
-  @Override
-  void stopServer(Object ignore) {
-    AkkaHttpTestSyncWebServer.stop()
-  }
-}
-
-class AkkaHttpServerInstrumentationTestAsync extends AkkaHttpServerInstrumentationTest {
-  @Override
-  def startServer(int port) {
-    AkkaHttpTestAsyncWebServer.start(port)
-  }
-
-  @Override
-  void stopServer(Object ignore) {
-    AkkaHttpTestAsyncWebServer.stop()
-  }
-}

+ 35 - 0
instrumentation/akka/akka-http-10.0/javaagent/src/test/scala/io/opentelemetry/javaagent/instrumentation/akkahttp/AbstractHttpServerInstrumentationTest.scala

@@ -0,0 +1,35 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.akkahttp
+
+import io.opentelemetry.api.common.AttributeKey
+
+import collection.JavaConverters._
+import io.opentelemetry.instrumentation.testing.junit.http.{
+  AbstractHttpServerTest,
+  HttpServerTestOptions,
+  ServerEndpoint
+}
+
+import java.util
+import java.util.Collections
+import java.util.function.Function
+
+abstract class AbstractHttpServerInstrumentationTest
+    extends AbstractHttpServerTest[Object] {
+
+  override final protected def configure(
+      options: HttpServerTestOptions
+  ): Unit = {
+    options.setTestCaptureHttpHeaders(false)
+    options.setHttpAttributes(
+      new Function[ServerEndpoint, util.Set[AttributeKey[_]]] {
+        override def apply(v1: ServerEndpoint): util.Set[AttributeKey[_]] =
+          Collections.emptySet()
+      }
+    )
+  }
+}

+ 107 - 0
instrumentation/akka/akka-http-10.0/javaagent/src/test/scala/io/opentelemetry/javaagent/instrumentation/akkahttp/AkkaHttpClientInstrumentationTest.scala

@@ -0,0 +1,107 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.akkahttp
+
+import akka.actor.ActorSystem
+import akka.dispatch.ExecutionContexts
+import akka.http.scaladsl.Http
+import akka.http.scaladsl.model._
+import akka.http.javadsl.model.HttpHeader
+import akka.http.scaladsl.model.headers.RawHeader
+import akka.stream.ActorMaterializer
+import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension
+import io.opentelemetry.instrumentation.testing.junit.http.{
+  AbstractHttpClientTest,
+  HttpClientInstrumentationExtension,
+  HttpClientTestOptions,
+  SingleConnection
+}
+
+import java.net.URI
+import java.util
+import java.util.concurrent.Executor
+import org.junit.jupiter.api.extension.RegisterExtension
+
+import java.util.function.BiFunction
+import scala.collection.JavaConverters._
+import scala.concurrent.Await
+import scala.concurrent.ExecutionContext
+import scala.concurrent.duration._
+import scala.language.postfixOps
+import scala.util.{Failure, Success}
+
+class AkkaHttpClientInstrumentationTest
+    extends AbstractHttpClientTest[HttpRequest] {
+
+  @RegisterExtension val _: InstrumentationExtension =
+    HttpClientInstrumentationExtension.forAgent()
+
+  val system: ActorSystem = ActorSystem.create()
+  implicit val materializer: ActorMaterializer =
+    ActorMaterializer.create(system)
+
+  override protected def buildRequest(
+      method: String,
+      uri: URI,
+      h: util.Map[String, String]
+  ): HttpRequest =
+    HttpRequest(HttpMethods.getForKey(method).get)
+      .withUri(uri.toString)
+      .addHeaders(
+        h.entrySet()
+          .asScala
+          .map(e => RawHeader(e.getKey, e.getValue): HttpHeader)
+          .asJava
+      )
+
+  override protected def sendRequest(
+      request: HttpRequest,
+      method: String,
+      uri: URI,
+      headers: util.Map[String, String]
+  ): Int = {
+    val response = Await.result(
+      Http.get(system).singleRequest(request),
+      10 seconds
+    )
+    response.discardEntityBytes(materializer)
+    response.status.intValue()
+  }
+
+  override protected def sendRequestWithCallback(
+      request: HttpRequest,
+      method: String,
+      uri: URI,
+      headers: util.Map[String, String],
+      requestResult: AbstractHttpClientTest.RequestResult
+  ): Unit = {
+    implicit val ec: ExecutionContext =
+      ExecutionContexts.fromExecutor(new Executor {
+        override def execute(command: Runnable): Unit = command.run()
+      })
+    Http
+      .get(system)
+      .singleRequest(request)
+      .onComplete {
+        case Success(response: HttpResponse) => {
+          response.discardEntityBytes(materializer)
+          requestResult.complete(response.status.intValue())
+        }
+        case Failure(error) => requestResult.complete(error)
+      }
+  }
+
+  override protected def configure(options: HttpClientTestOptions): Unit = {
+    options.disableTestRedirects()
+    // singleConnection test would require instrumentation to support requests made through pools
+    // (newHostConnectionPool, superPool, etc), which is currently not supported.
+    options.setSingleConnectionFactory(
+      new BiFunction[String, Integer, SingleConnection] {
+        override def apply(t: String, u: Integer): SingleConnection = null
+      }
+    )
+  }
+}

+ 25 - 0
instrumentation/akka/akka-http-10.0/javaagent/src/test/scala/io/opentelemetry/javaagent/instrumentation/akkahttp/AkkaHttpServerInstrumentationTestAsync.scala

@@ -0,0 +1,25 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.akkahttp
+
+import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension
+import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension
+import org.junit.jupiter.api.extension.RegisterExtension
+
+class AkkaHttpServerInstrumentationTestAsync
+    extends AbstractHttpServerInstrumentationTest {
+
+  @RegisterExtension val _: InstrumentationExtension =
+    HttpServerInstrumentationExtension.forAgent()
+
+  override protected def setupServer(): AnyRef = {
+    AkkaHttpTestAsyncWebServer.start(port)
+    null
+  }
+
+  override protected def stopServer(server: Object): Unit =
+    AkkaHttpTestAsyncWebServer.stop()
+}

+ 28 - 0
instrumentation/akka/akka-http-10.0/javaagent/src/test/scala/io/opentelemetry/javaagent/instrumentation/akkahttp/AkkaHttpServerInstrumentationTestSync.scala

@@ -0,0 +1,28 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.akkahttp
+
+import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension
+import io.opentelemetry.instrumentation.testing.junit.{
+  AgentInstrumentationExtension,
+  InstrumentationExtension
+}
+import org.junit.jupiter.api.extension.RegisterExtension
+
+class AkkaHttpServerInstrumentationTestSync
+    extends AbstractHttpServerInstrumentationTest {
+
+  @RegisterExtension val _: InstrumentationExtension =
+    HttpServerInstrumentationExtension.forAgent()
+
+  override protected def setupServer(): AnyRef = {
+    AkkaHttpTestSyncWebServer.start(port)
+    null
+  }
+
+  override protected def stopServer(server: Object): Unit =
+    AkkaHttpTestSyncWebServer.stop()
+}

+ 2 - 0
instrumentation/akka/akka-http-10.0/javaagent/src/test/scala/AkkaHttpTestAsyncWebServer.scala → instrumentation/akka/akka-http-10.0/javaagent/src/test/scala/io/opentelemetry/javaagent/instrumentation/akkahttp/AkkaHttpTestAsyncWebServer.scala

@@ -3,6 +3,8 @@
  * SPDX-License-Identifier: Apache-2.0
  */
 
+package io.opentelemetry.javaagent.instrumentation.akkahttp
+
 import akka.actor.ActorSystem
 import akka.http.scaladsl.Http
 import akka.http.scaladsl.Http.ServerBinding

+ 2 - 0
instrumentation/akka/akka-http-10.0/javaagent/src/test/scala/AkkaHttpTestSyncWebServer.scala → instrumentation/akka/akka-http-10.0/javaagent/src/test/scala/io/opentelemetry/javaagent/instrumentation/akkahttp/AkkaHttpTestSyncWebServer.scala

@@ -3,6 +3,8 @@
  * SPDX-License-Identifier: Apache-2.0
  */
 
+package io.opentelemetry.javaagent.instrumentation.akkahttp
+
 import akka.actor.ActorSystem
 import akka.http.scaladsl.Http
 import akka.http.scaladsl.Http.ServerBinding

+ 2 - 0
instrumentation/akka/akka-http-10.0/javaagent/src/test/scala/AkkaHttpTestWebServer.scala → instrumentation/akka/akka-http-10.0/javaagent/src/test/scala/io/opentelemetry/javaagent/instrumentation/akkahttp/AkkaHttpTestWebServer.scala

@@ -3,6 +3,8 @@
  * SPDX-License-Identifier: Apache-2.0
  */
 
+package io.opentelemetry.javaagent.instrumentation.akkahttp
+
 import akka.actor.ActorSystem
 import akka.http.scaladsl.Http
 import akka.http.scaladsl.Http.ServerBinding