|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright The OpenTelemetry Authors | 
|  | 3 | + * SPDX-License-Identifier: Apache-2.0 | 
|  | 4 | + */ | 
|  | 5 | + | 
|  | 6 | +package io.opentelemetry.javaagent.instrumentation.play.v2_6.server; | 
|  | 7 | + | 
|  | 8 | +import play.Mode; | 
|  | 9 | +import play.libs.concurrent.HttpExecution; | 
|  | 10 | +import play.mvc.Controller; | 
|  | 11 | +import play.mvc.Http; | 
|  | 12 | +import play.mvc.Results; | 
|  | 13 | +import play.routing.RoutingDsl; | 
|  | 14 | +import play.server.Server; | 
|  | 15 | +import scala.concurrent.ExecutionContextExecutor; | 
|  | 16 | +import java.util.concurrent.CompletableFuture; | 
|  | 17 | +import java.util.concurrent.ExecutorService; | 
|  | 18 | +import java.util.concurrent.Executors; | 
|  | 19 | + | 
|  | 20 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS; | 
|  | 21 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR; | 
|  | 22 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION; | 
|  | 23 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.INDEXED_CHILD; | 
|  | 24 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM; | 
|  | 25 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT; | 
|  | 26 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS; | 
|  | 27 | + | 
|  | 28 | +class PlayAsyncServerTest extends PlayServerTest { | 
|  | 29 | + | 
|  | 30 | +  private static final ExecutorService executor = Executors.newCachedThreadPool(); | 
|  | 31 | + | 
|  | 32 | +  @Override | 
|  | 33 | +  public Server setupServer(int port) { | 
|  | 34 | +    ExecutionContextExecutor executionContextExecutor = HttpExecution.fromThread(executor); | 
|  | 35 | +    return Server.forRouter(Mode.TEST, port, components -> | 
|  | 36 | +        RoutingDsl.fromComponents(components) | 
|  | 37 | +            .GET(SUCCESS.getPath()).routeAsync(() -> | 
|  | 38 | +                CompletableFuture.supplyAsync(() -> | 
|  | 39 | +                    controller(SUCCESS, () -> | 
|  | 40 | +                        Results.status(SUCCESS.getStatus(), SUCCESS.getBody()) | 
|  | 41 | +                    ), executionContextExecutor | 
|  | 42 | +                ) | 
|  | 43 | +            ) | 
|  | 44 | +            .GET(QUERY_PARAM.getPath()).routeAsync(() -> | 
|  | 45 | +                CompletableFuture.supplyAsync(() -> | 
|  | 46 | +                    controller(QUERY_PARAM, () -> | 
|  | 47 | +                        Results.status(QUERY_PARAM.getStatus(), QUERY_PARAM.getBody()) | 
|  | 48 | +                    ), executionContextExecutor | 
|  | 49 | +                ) | 
|  | 50 | +            ) | 
|  | 51 | +            .GET(REDIRECT.getPath()).routeAsync(() -> | 
|  | 52 | +                CompletableFuture.supplyAsync(() -> | 
|  | 53 | +                    controller(REDIRECT, () -> | 
|  | 54 | +                        Results.found(REDIRECT.getBody()) | 
|  | 55 | +                    ), executionContextExecutor | 
|  | 56 | +                ) | 
|  | 57 | +            ) | 
|  | 58 | +            .GET(ERROR.getPath()).routeAsync(() -> | 
|  | 59 | +                CompletableFuture.supplyAsync(() -> | 
|  | 60 | +                    controller(ERROR, () -> | 
|  | 61 | +                        Results.status(ERROR.getStatus(), ERROR.getBody()) | 
|  | 62 | +                    ), executionContextExecutor | 
|  | 63 | +                ) | 
|  | 64 | +            ) | 
|  | 65 | +            .GET(EXCEPTION.getPath()).routeAsync(() -> | 
|  | 66 | +                CompletableFuture.supplyAsync(() -> | 
|  | 67 | +                    controller(EXCEPTION, () -> { | 
|  | 68 | +                      throw new IllegalArgumentException(EXCEPTION.getBody()); | 
|  | 69 | +                    }), executionContextExecutor | 
|  | 70 | +                ) | 
|  | 71 | +            ) | 
|  | 72 | +            .GET(CAPTURE_HEADERS.getPath()).routeAsync(() -> { | 
|  | 73 | +              Http.Request request = Controller.request(); | 
|  | 74 | +              Http.Response response = Controller.response(); | 
|  | 75 | +              return CompletableFuture.supplyAsync(() -> | 
|  | 76 | +                  controller(CAPTURE_HEADERS, () -> { | 
|  | 77 | +                    request.header("X-Test-Request").ifPresent(value -> | 
|  | 78 | +                        response.setHeader("X-Test-Response", value) | 
|  | 79 | +                    ); | 
|  | 80 | +                    return Results.status(CAPTURE_HEADERS.getStatus(), CAPTURE_HEADERS.getBody()); | 
|  | 81 | +                  }), executionContextExecutor | 
|  | 82 | +              ); | 
|  | 83 | +            }) | 
|  | 84 | +            .GET(INDEXED_CHILD.getPath()).routeAsync(() -> { | 
|  | 85 | +              String id = Controller.request().getQueryString("id"); | 
|  | 86 | +              return CompletableFuture.supplyAsync(() -> | 
|  | 87 | +                  controller(INDEXED_CHILD, () -> { | 
|  | 88 | +                    INDEXED_CHILD.collectSpanAttributes(name -> "id".equals(name) ? id : null); | 
|  | 89 | +                    return Results.status(INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody()); | 
|  | 90 | +                  }), executionContextExecutor | 
|  | 91 | +              ); | 
|  | 92 | +            }) | 
|  | 93 | +            .build() | 
|  | 94 | +    ); | 
|  | 95 | +  } | 
|  | 96 | + | 
|  | 97 | +  @Override | 
|  | 98 | +  protected void stopServer(Server server) { | 
|  | 99 | +    server.stop(); | 
|  | 100 | +    executor.shutdown(); | 
|  | 101 | +  } | 
|  | 102 | +} | 
0 commit comments