Skip to content

Commit faf245c

Browse files
committed
Create latestDepTest module as the latest jar uses diff packages
1 parent 8e4c0f6 commit faf245c

File tree

6 files changed

+603
-1
lines changed

6 files changed

+603
-1
lines changed

instrumentation/play/play-ws/play-ws-2.1/javaagent/build.gradle.kts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,34 @@ dependencies {
4141
testInstrumentation(project(":instrumentation:netty:netty-4.1:javaagent"))
4242
testInstrumentation(project(":instrumentation:akka:akka-http-10.0:javaagent"))
4343
testInstrumentation(project(":instrumentation:akka:akka-actor-2.3:javaagent"))
44+
}
45+
46+
testing {
47+
suites {
48+
val latestDepTest by registering(JvmTestSuite::class) {
49+
dependencies {
50+
implementation("com.typesafe.play:play-ahc-ws-standalone_2.13:+")
51+
}
52+
}
53+
}
54+
}
4455

45-
latestDepTestLibrary("com.typesafe.play:play-ahc-ws-standalone_2.13:+")
56+
val testLatestDeps = findProperty("testLatestDeps") as Boolean
57+
tasks {
58+
if (testLatestDeps) {
59+
// disable regular test running and compiling tasks when latest dep test task is run
60+
named("test") {
61+
enabled = false
62+
}
63+
}
64+
65+
named("latestDepTest") {
66+
enabled = testLatestDeps
67+
}
68+
69+
check {
70+
dependsOn(testing.suites)
71+
}
4672
}
4773

4874
if (findProperty("testLatestDeps") as Boolean) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.playws.v2_1;
7+
8+
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
9+
import java.io.IOException;
10+
import java.net.URI;
11+
import java.util.Map;
12+
import java.util.concurrent.CompletionStage;
13+
import java.util.concurrent.ExecutionException;
14+
import org.junit.jupiter.api.AfterEach;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import play.libs.ws.StandaloneWSClient;
17+
import play.libs.ws.StandaloneWSRequest;
18+
import play.libs.ws.StandaloneWSResponse;
19+
import play.libs.ws.ahc.StandaloneAhcWSClient;
20+
21+
public class PlayJavaStreamedWsClientTest extends PlayWsClientBaseTest<StandaloneWSRequest> {
22+
23+
private static StandaloneWSClient wsClient;
24+
private static StandaloneWSClient wsClientWithReadTimeout;
25+
26+
@BeforeEach
27+
@Override
28+
void setup() {
29+
super.setup();
30+
wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer);
31+
wsClientWithReadTimeout =
32+
new StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer);
33+
autoCleanup.deferCleanup(wsClient);
34+
autoCleanup.deferCleanup(wsClientWithReadTimeout);
35+
}
36+
37+
@AfterEach
38+
@Override
39+
void tearDown() throws IOException {
40+
if (wsClient != null) {
41+
wsClient.close();
42+
}
43+
if (wsClientWithReadTimeout != null) {
44+
wsClientWithReadTimeout.close();
45+
}
46+
super.tearDown();
47+
}
48+
49+
@Override
50+
public StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers) {
51+
StandaloneWSRequest request = getClient(uri).url(uri.toString()).setFollowRedirects(true);
52+
headers.forEach(request::addHeader);
53+
request.setMethod(method);
54+
return request;
55+
}
56+
57+
@Override
58+
public int sendRequest(
59+
StandaloneWSRequest request, String method, URI uri, Map<String, String> headers)
60+
throws ExecutionException, InterruptedException {
61+
return internalSendRequest(request).toCompletableFuture().get().getStatus();
62+
}
63+
64+
@Override
65+
public void sendRequestWithCallback(
66+
StandaloneWSRequest request,
67+
String method,
68+
URI uri,
69+
Map<String, String> headers,
70+
HttpClientResult requestResult) {
71+
internalSendRequest(request)
72+
.whenComplete(
73+
(response, throwable) -> {
74+
if (throwable != null) {
75+
requestResult.complete(throwable.getCause());
76+
} else {
77+
requestResult.complete(response.getStatus());
78+
}
79+
});
80+
}
81+
82+
private static CompletionStage<StandaloneWSResponse> internalSendRequest(
83+
StandaloneWSRequest request) {
84+
CompletionStage<? extends StandaloneWSResponse> stream = request.stream();
85+
// The status can be ready before the body so explicitly call wait for body to be ready
86+
return stream
87+
.thenCompose(
88+
response -> response.getBodyAsSource().runFold("", (acc, out) -> "", materializer))
89+
.thenCombine(stream, (body, response) -> response);
90+
}
91+
92+
private static StandaloneWSClient getClient(URI uri) {
93+
if (uri.toString().contains("/read-timeout")) {
94+
return wsClientWithReadTimeout;
95+
}
96+
return wsClient;
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.playws.v2_1;
7+
8+
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
9+
import java.io.IOException;
10+
import java.net.URI;
11+
import java.util.Map;
12+
import java.util.concurrent.ExecutionException;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import play.libs.ws.StandaloneWSClient;
16+
import play.libs.ws.StandaloneWSRequest;
17+
import play.libs.ws.ahc.StandaloneAhcWSClient;
18+
19+
public class PlayJavaWsClientTest extends PlayWsClientBaseTest<StandaloneWSRequest> {
20+
21+
private static StandaloneWSClient wsClient;
22+
private static StandaloneWSClient wsClientWithReadTimeout;
23+
24+
@BeforeEach
25+
@Override
26+
void setup() {
27+
super.setup();
28+
wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer);
29+
wsClientWithReadTimeout =
30+
new StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer);
31+
autoCleanup.deferCleanup(wsClient);
32+
autoCleanup.deferCleanup(wsClientWithReadTimeout);
33+
}
34+
35+
@AfterEach
36+
@Override
37+
void tearDown() throws IOException {
38+
if (wsClient != null) {
39+
wsClient.close();
40+
}
41+
if (wsClientWithReadTimeout != null) {
42+
wsClientWithReadTimeout.close();
43+
}
44+
super.tearDown();
45+
}
46+
47+
@Override
48+
public StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers) {
49+
StandaloneWSRequest request = getClient(uri).url(uri.toString()).setFollowRedirects(true);
50+
headers.forEach(request::addHeader);
51+
return request.setMethod(method);
52+
}
53+
54+
@Override
55+
public int sendRequest(
56+
StandaloneWSRequest request, String method, URI uri, Map<String, String> headers)
57+
throws ExecutionException, InterruptedException {
58+
return request.execute().toCompletableFuture().get().getStatus();
59+
}
60+
61+
@Override
62+
public void sendRequestWithCallback(
63+
StandaloneWSRequest request,
64+
String method,
65+
URI uri,
66+
Map<String, String> headers,
67+
HttpClientResult requestResult) {
68+
request
69+
.execute()
70+
.whenComplete(
71+
(response, throwable) -> {
72+
if (throwable != null) {
73+
requestResult.complete(throwable);
74+
} else {
75+
requestResult.complete(response.getStatus());
76+
}
77+
});
78+
}
79+
80+
private static StandaloneWSClient getClient(URI uri) {
81+
if (uri.toString().contains("/read-timeout")) {
82+
return wsClientWithReadTimeout;
83+
}
84+
return wsClient;
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.playws.v2_1;
7+
8+
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
9+
import java.io.IOException;
10+
import java.net.URI;
11+
import java.util.Map;
12+
import java.util.concurrent.TimeUnit;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import play.api.libs.ws.StandaloneWSClient;
16+
import play.api.libs.ws.StandaloneWSRequest;
17+
import play.api.libs.ws.StandaloneWSResponse;
18+
import play.api.libs.ws.ahc.StandaloneAhcWSClient;
19+
import scala.Function1;
20+
import scala.concurrent.Await;
21+
import scala.concurrent.ExecutionContext;
22+
import scala.concurrent.Future;
23+
import scala.concurrent.duration.Duration;
24+
import scala.jdk.javaapi.CollectionConverters;
25+
import scala.util.Try;
26+
27+
public class PlayScalaStreamedWsClientTest extends PlayWsClientBaseTest<StandaloneWSRequest> {
28+
29+
private static StandaloneWSClient wsClient;
30+
private static StandaloneWSClient wsClientWithReadTimeout;
31+
32+
@BeforeEach
33+
@Override
34+
void setup() {
35+
super.setup();
36+
wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer);
37+
wsClientWithReadTimeout =
38+
new StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer);
39+
autoCleanup.deferCleanup(wsClient);
40+
autoCleanup.deferCleanup(wsClientWithReadTimeout);
41+
}
42+
43+
@AfterEach
44+
@Override
45+
void tearDown() throws IOException {
46+
if (wsClient != null) {
47+
wsClient.close();
48+
}
49+
if (wsClientWithReadTimeout != null) {
50+
wsClientWithReadTimeout.close();
51+
}
52+
super.tearDown();
53+
}
54+
55+
@Override
56+
public StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers)
57+
throws Exception {
58+
return getClient(uri)
59+
.url(uri.toURL().toString())
60+
.withMethod(method)
61+
.withFollowRedirects(true)
62+
.withHttpHeaders(CollectionConverters.asScala(headers).toList());
63+
}
64+
65+
@Override
66+
public int sendRequest(
67+
StandaloneWSRequest request, String method, URI uri, Map<String, String> headers)
68+
throws Exception {
69+
return Await.result(internalSendRequest(request), Duration.apply(10, TimeUnit.SECONDS))
70+
.status();
71+
}
72+
73+
@Override
74+
public void sendRequestWithCallback(
75+
StandaloneWSRequest request,
76+
String method,
77+
URI uri,
78+
Map<String, String> headers,
79+
HttpClientResult requestResult) {
80+
internalSendRequest(request)
81+
.onComplete(
82+
new Function1<Try<StandaloneWSResponse>, Void>() {
83+
@Override
84+
public Void apply(Try<StandaloneWSResponse> response) {
85+
if (response.isSuccess()) {
86+
requestResult.complete(response.get().status());
87+
} else {
88+
requestResult.complete(response.failed().get());
89+
}
90+
return null;
91+
}
92+
},
93+
ExecutionContext.global());
94+
}
95+
96+
private static Future<StandaloneWSResponse> internalSendRequest(StandaloneWSRequest request) {
97+
Future<StandaloneWSResponse> futureResponse = request.stream();
98+
// The status can be ready before the body so explicitly call wait for body to be ready
99+
Future<String> bodyResponse =
100+
futureResponse.flatMap(
101+
new Function1<StandaloneWSResponse, Future<String>>() {
102+
@Override
103+
public Future<String> apply(StandaloneWSResponse wsResponse) {
104+
return wsResponse.bodyAsSource().runFold("", (acc, out) -> "", materializer);
105+
}
106+
},
107+
ExecutionContext.global());
108+
return bodyResponse.flatMap(
109+
new Function1<String, Future<StandaloneWSResponse>>() {
110+
@Override
111+
public Future<StandaloneWSResponse> apply(String v1) {
112+
return futureResponse;
113+
}
114+
},
115+
ExecutionContext.global());
116+
}
117+
118+
private static StandaloneWSClient getClient(URI uri) {
119+
if (uri.toString().contains("/read-timeout")) {
120+
return wsClientWithReadTimeout;
121+
}
122+
return wsClient;
123+
}
124+
}

0 commit comments

Comments
 (0)