Skip to content

Commit

Permalink
Create latestDepTest module as the latest jar uses diff packages
Browse files Browse the repository at this point in the history
  • Loading branch information
heyams committed Oct 3, 2024
1 parent 8e4c0f6 commit faf245c
Show file tree
Hide file tree
Showing 6 changed files with 603 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,34 @@ dependencies {
testInstrumentation(project(":instrumentation:netty:netty-4.1:javaagent"))
testInstrumentation(project(":instrumentation:akka:akka-http-10.0:javaagent"))
testInstrumentation(project(":instrumentation:akka:akka-actor-2.3:javaagent"))
}

testing {
suites {
val latestDepTest by registering(JvmTestSuite::class) {
dependencies {
implementation("com.typesafe.play:play-ahc-ws-standalone_2.13:+")
}
}
}
}

latestDepTestLibrary("com.typesafe.play:play-ahc-ws-standalone_2.13:+")
val testLatestDeps = findProperty("testLatestDeps") as Boolean
tasks {
if (testLatestDeps) {
// disable regular test running and compiling tasks when latest dep test task is run
named("test") {
enabled = false
}
}

named("latestDepTest") {
enabled = testLatestDeps
}

check {
dependsOn(testing.suites)
}
}

if (findProperty("testLatestDeps") as Boolean) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v2_1;

import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import play.libs.ws.StandaloneWSClient;
import play.libs.ws.StandaloneWSRequest;
import play.libs.ws.StandaloneWSResponse;
import play.libs.ws.ahc.StandaloneAhcWSClient;

public class PlayJavaStreamedWsClientTest extends PlayWsClientBaseTest<StandaloneWSRequest> {

private static StandaloneWSClient wsClient;
private static StandaloneWSClient wsClientWithReadTimeout;

@BeforeEach
@Override
void setup() {
super.setup();
wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer);
wsClientWithReadTimeout =
new StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer);
autoCleanup.deferCleanup(wsClient);
autoCleanup.deferCleanup(wsClientWithReadTimeout);
}

@AfterEach
@Override
void tearDown() throws IOException {
if (wsClient != null) {
wsClient.close();
}
if (wsClientWithReadTimeout != null) {
wsClientWithReadTimeout.close();
}
super.tearDown();
}

@Override
public StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers) {
StandaloneWSRequest request = getClient(uri).url(uri.toString()).setFollowRedirects(true);
headers.forEach(request::addHeader);
request.setMethod(method);
return request;
}

@Override
public int sendRequest(
StandaloneWSRequest request, String method, URI uri, Map<String, String> headers)
throws ExecutionException, InterruptedException {
return internalSendRequest(request).toCompletableFuture().get().getStatus();
}

@Override
public void sendRequestWithCallback(
StandaloneWSRequest request,
String method,
URI uri,
Map<String, String> headers,
HttpClientResult requestResult) {
internalSendRequest(request)
.whenComplete(
(response, throwable) -> {
if (throwable != null) {
requestResult.complete(throwable.getCause());
} else {
requestResult.complete(response.getStatus());
}
});
}

private static CompletionStage<StandaloneWSResponse> internalSendRequest(
StandaloneWSRequest request) {
CompletionStage<? extends StandaloneWSResponse> stream = request.stream();
// The status can be ready before the body so explicitly call wait for body to be ready
return stream
.thenCompose(
response -> response.getBodyAsSource().runFold("", (acc, out) -> "", materializer))
.thenCombine(stream, (body, response) -> response);
}

private static StandaloneWSClient getClient(URI uri) {
if (uri.toString().contains("/read-timeout")) {
return wsClientWithReadTimeout;
}
return wsClient;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v2_1;

import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import play.libs.ws.StandaloneWSClient;
import play.libs.ws.StandaloneWSRequest;
import play.libs.ws.ahc.StandaloneAhcWSClient;

public class PlayJavaWsClientTest extends PlayWsClientBaseTest<StandaloneWSRequest> {

private static StandaloneWSClient wsClient;
private static StandaloneWSClient wsClientWithReadTimeout;

@BeforeEach
@Override
void setup() {
super.setup();
wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer);
wsClientWithReadTimeout =
new StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer);
autoCleanup.deferCleanup(wsClient);
autoCleanup.deferCleanup(wsClientWithReadTimeout);
}

@AfterEach
@Override
void tearDown() throws IOException {
if (wsClient != null) {
wsClient.close();
}
if (wsClientWithReadTimeout != null) {
wsClientWithReadTimeout.close();
}
super.tearDown();
}

@Override
public StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers) {
StandaloneWSRequest request = getClient(uri).url(uri.toString()).setFollowRedirects(true);
headers.forEach(request::addHeader);
return request.setMethod(method);
}

@Override
public int sendRequest(
StandaloneWSRequest request, String method, URI uri, Map<String, String> headers)
throws ExecutionException, InterruptedException {
return request.execute().toCompletableFuture().get().getStatus();
}

@Override
public void sendRequestWithCallback(
StandaloneWSRequest request,
String method,
URI uri,
Map<String, String> headers,
HttpClientResult requestResult) {
request
.execute()
.whenComplete(
(response, throwable) -> {
if (throwable != null) {
requestResult.complete(throwable);
} else {
requestResult.complete(response.getStatus());
}
});
}

private static StandaloneWSClient getClient(URI uri) {
if (uri.toString().contains("/read-timeout")) {
return wsClientWithReadTimeout;
}
return wsClient;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v2_1;

import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import play.api.libs.ws.StandaloneWSClient;
import play.api.libs.ws.StandaloneWSRequest;
import play.api.libs.ws.StandaloneWSResponse;
import play.api.libs.ws.ahc.StandaloneAhcWSClient;
import scala.Function1;
import scala.concurrent.Await;
import scala.concurrent.ExecutionContext;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import scala.jdk.javaapi.CollectionConverters;
import scala.util.Try;

public class PlayScalaStreamedWsClientTest extends PlayWsClientBaseTest<StandaloneWSRequest> {

private static StandaloneWSClient wsClient;
private static StandaloneWSClient wsClientWithReadTimeout;

@BeforeEach
@Override
void setup() {
super.setup();
wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer);
wsClientWithReadTimeout =
new StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer);
autoCleanup.deferCleanup(wsClient);
autoCleanup.deferCleanup(wsClientWithReadTimeout);
}

@AfterEach
@Override
void tearDown() throws IOException {
if (wsClient != null) {
wsClient.close();
}
if (wsClientWithReadTimeout != null) {
wsClientWithReadTimeout.close();
}
super.tearDown();
}

@Override
public StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers)
throws Exception {
return getClient(uri)
.url(uri.toURL().toString())
.withMethod(method)
.withFollowRedirects(true)
.withHttpHeaders(CollectionConverters.asScala(headers).toList());
}

@Override
public int sendRequest(
StandaloneWSRequest request, String method, URI uri, Map<String, String> headers)
throws Exception {
return Await.result(internalSendRequest(request), Duration.apply(10, TimeUnit.SECONDS))
.status();
}

@Override
public void sendRequestWithCallback(
StandaloneWSRequest request,
String method,
URI uri,
Map<String, String> headers,
HttpClientResult requestResult) {
internalSendRequest(request)
.onComplete(
new Function1<Try<StandaloneWSResponse>, Void>() {
@Override
public Void apply(Try<StandaloneWSResponse> response) {
if (response.isSuccess()) {
requestResult.complete(response.get().status());
} else {
requestResult.complete(response.failed().get());
}
return null;
}
},
ExecutionContext.global());
}

private static Future<StandaloneWSResponse> internalSendRequest(StandaloneWSRequest request) {
Future<StandaloneWSResponse> futureResponse = request.stream();
// The status can be ready before the body so explicitly call wait for body to be ready
Future<String> bodyResponse =
futureResponse.flatMap(
new Function1<StandaloneWSResponse, Future<String>>() {
@Override
public Future<String> apply(StandaloneWSResponse wsResponse) {
return wsResponse.bodyAsSource().runFold("", (acc, out) -> "", materializer);
}
},
ExecutionContext.global());
return bodyResponse.flatMap(
new Function1<String, Future<StandaloneWSResponse>>() {
@Override
public Future<StandaloneWSResponse> apply(String v1) {
return futureResponse;
}
},
ExecutionContext.global());
}

private static StandaloneWSClient getClient(URI uri) {
if (uri.toString().contains("/read-timeout")) {
return wsClientWithReadTimeout;
}
return wsClient;
}
}
Loading

0 comments on commit faf245c

Please sign in to comment.