Skip to content

Commit 1addd43

Browse files
committed
refactor test
1 parent ef58870 commit 1addd43

File tree

3 files changed

+27
-64
lines changed

3 files changed

+27
-64
lines changed

instrumentation/pekko/pekko-http-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/pekkohttp/v1_0/PekkoHttpTestSetup.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

instrumentation/pekko/pekko-http-1.0/javaagent/src/test/scala/io/opentelemetry/javaagent/instrumentation/pekkohttp/v1_0/PekkoHttpServerWithActorTest.scala

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,45 @@ package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0
77

88
import java.net.URI
99

10+
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerUsingTest
11+
import io.opentelemetry.instrumentation.test.utils.PortUtils
12+
import io.opentelemetry.testing.internal.armeria.client.{ClientFactory, WebClient}
13+
import io.opentelemetry.testing.internal.armeria.client.logging.LoggingClient
1014
import io.opentelemetry.testing.internal.armeria.common.{
1115
AggregatedHttpRequest,
16+
HttpHeaderNames,
1217
HttpMethod
1318
}
1419
import org.assertj.core.api.Assertions.assertThat
15-
import org.junit.jupiter.api.{AfterAll, BeforeAll, Test, TestInstance}
20+
import org.junit.jupiter.api.{AfterAll, Test, TestInstance}
1621

1722
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1823
class PekkoHttpServerWithActorTest {
1924

20-
val setup = new PekkoHttpTestSetup()
21-
22-
@BeforeAll def setupOptions(): Unit = {
23-
PekkoHttpTestWebServerWithActor.start(setup.getPort())
24-
}
25+
private val port = PortUtils.findOpenPort()
26+
val server = new PekkoHttpTestWebServerWithActor(port)
2527

2628
@AfterAll def cleanup(): Unit = {
27-
PekkoHttpTestWebServerWithActor.stop()
29+
server.stop()
2830
}
2931

32+
private val minute = java.time.Duration.ofMinutes(1)
33+
private val client = WebClient.builder()
34+
.responseTimeout(minute)
35+
.writeTimeout(minute)
36+
.factory(ClientFactory.builder().connectTimeout(minute).build())
37+
.setHeader(HttpHeaderNames.USER_AGENT, AbstractHttpServerUsingTest.TEST_USER_AGENT)
38+
.setHeader(HttpHeaderNames.X_FORWARDED_FOR, AbstractHttpServerUsingTest.TEST_CLIENT_IP)
39+
.decorator(LoggingClient.newDecorator())
40+
.build()
41+
3042
@Test def testSpan(): Unit = {
31-
val address = new URI(s"http://localhost:${setup.getPort()}/")
43+
val address = new URI(s"http://localhost:$port")
3244
val request = AggregatedHttpRequest.of(
3345
HttpMethod.GET,
3446
address.resolve("/test").toString
3547
)
36-
val response = setup.getClient().execute(request).aggregate.join
48+
val response = client.execute(request).aggregate.join
3749
assertThat(response.status.code).isEqualTo(200)
3850
val responseText = response.contentUtf8
3951
val splits = responseText.split("\n")

instrumentation/pekko/pekko-http-1.0/javaagent/src/test/scala/io/opentelemetry/javaagent/instrumentation/pekkohttp/v1_0/PekkoHttpTestWebServerWithActor.scala

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import org.apache.pekko.util.Timeout
1616
import scala.concurrent.{Await, ExecutionContext}
1717
import scala.concurrent.duration.DurationInt
1818

19-
object PekkoHttpTestWebServerWithActor {
19+
class PekkoHttpTestWebServerWithActor(port: Int) {
2020
implicit val system: ActorSystem = ActorSystem("http-server-with-actor")
2121
// needed for the future flatMap/onComplete in the end
2222
implicit val executionContext: ExecutionContext = system.dispatcher
@@ -28,9 +28,9 @@ object PekkoHttpTestWebServerWithActor {
2828
}
2929
}
3030

31-
val spanTestActor = system.actorOf(Props[SpanTestActor]())
31+
val spanTestActor = system.actorOf(Props(new SpanTestActor))
3232

33-
var route = get {
33+
val route = get {
3434
path("test") {
3535
complete {
3636
val otelSummary = spanSummary(Span.current())
@@ -42,17 +42,11 @@ object PekkoHttpTestWebServerWithActor {
4242
}
4343
}
4444

45-
private var binding: ServerBinding = null
46-
47-
def start(port: Int): Unit = synchronized {
48-
if (null == binding) {
49-
binding =
50-
Await.result(Http().bindAndHandle(route, "localhost", port), 10.seconds)
51-
}
52-
}
45+
var binding: ServerBinding =
46+
Await.result(Http().bindAndHandle(route, "localhost", port), 10.seconds)
5347

5448
def stop(): Unit = synchronized {
55-
if (null != binding) {
49+
if (binding != null) {
5650
binding.unbind()
5751
system.terminate()
5852
binding = null

0 commit comments

Comments
 (0)