Skip to content

Commit 00d698e

Browse files
authored
Fix flaky paly-ws test (open-telemetry#8943)
1 parent a3935c6 commit 00d698e

File tree

2 files changed

+71
-15
lines changed

2 files changed

+71
-15
lines changed

instrumentation/play/play-ws/play-ws-common/testing/src/main/groovy/PlayWsClientTestBase.groovy

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import java.util.concurrent.TimeUnit
2323
class PlayJavaWsClientTestBase extends PlayWsClientTestBaseBase<StandaloneWSRequest> {
2424
@Shared
2525
StandaloneWSClient wsClient
26+
@Shared
27+
StandaloneWSClient wsClientWithReadTimeout
2628

2729
@Override
2830
StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers) {
29-
def request = wsClient.url(uri.toURL().toString()).setFollowRedirects(true)
31+
def request = getClient(uri).url(uri.toURL().toString()).setFollowRedirects(true)
3032
headers.entrySet().each { entry -> request.addHeader(entry.getKey(), entry.getValue()) }
3133
return request.setMethod(method)
3234
}
@@ -43,22 +45,33 @@ class PlayJavaWsClientTestBase extends PlayWsClientTestBaseBase<StandaloneWSRequ
4345
}
4446
}
4547

48+
def getClient(URI uri) {
49+
if (uri.toString().contains("/read-timeout")) {
50+
return wsClientWithReadTimeout
51+
}
52+
return wsClient
53+
}
54+
4655
def setupSpec() {
4756
wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer)
57+
wsClientWithReadTimeout = new StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer)
4858
}
4959

5060
def cleanupSpec() {
5161
wsClient?.close()
62+
wsClientWithReadTimeout?.close()
5263
}
5364
}
5465

5566
class PlayJavaStreamedWsClientTestBase extends PlayWsClientTestBaseBase<StandaloneWSRequest> {
5667
@Shared
5768
StandaloneWSClient wsClient
69+
@Shared
70+
StandaloneWSClient wsClientWithReadTimeout
5871

5972
@Override
6073
StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers) {
61-
def request = wsClient.url(uri.toURL().toString()).setFollowRedirects(true)
74+
def request = getClient(uri).url(uri.toURL().toString()).setFollowRedirects(true)
6275
headers.entrySet().each { entry -> request.addHeader(entry.getKey(), entry.getValue()) }
6376
request.setMethod(method)
6477
return request
@@ -88,22 +101,33 @@ class PlayJavaStreamedWsClientTestBase extends PlayWsClientTestBaseBase<Standalo
88101
}
89102
}
90103

104+
def getClient(URI uri) {
105+
if (uri.toString().contains("/read-timeout")) {
106+
return wsClientWithReadTimeout
107+
}
108+
return wsClient
109+
}
110+
91111
def setupSpec() {
92112
wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer)
113+
wsClientWithReadTimeout = new StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer)
93114
}
94115

95116
def cleanupSpec() {
96117
wsClient?.close()
118+
wsClientWithReadTimeout?.close()
97119
}
98120
}
99121

100122
class PlayScalaWsClientTestBase extends PlayWsClientTestBaseBase<play.api.libs.ws.StandaloneWSRequest> {
101123
@Shared
102124
play.api.libs.ws.StandaloneWSClient wsClient
125+
@Shared
126+
play.api.libs.ws.StandaloneWSClient wsClientWithReadTimeout
103127

104128
@Override
105129
play.api.libs.ws.StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers) {
106-
return wsClient.url(uri.toURL().toString())
130+
return getClient(uri).url(uri.toURL().toString())
107131
.withMethod(method)
108132
.withFollowRedirects(true)
109133
.withHttpHeaders(JavaConverters.mapAsScalaMap(headers).toSeq())
@@ -135,22 +159,33 @@ class PlayScalaWsClientTestBase extends PlayWsClientTestBaseBase<play.api.libs.w
135159
}, ExecutionContext.global())
136160
}
137161

162+
def getClient(URI uri) {
163+
if (uri.toString().contains("/read-timeout")) {
164+
return wsClientWithReadTimeout
165+
}
166+
return wsClient
167+
}
168+
138169
def setupSpec() {
139170
wsClient = new play.api.libs.ws.ahc.StandaloneAhcWSClient(asyncHttpClient, materializer)
171+
wsClientWithReadTimeout = new play.api.libs.ws.ahc.StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer)
140172
}
141173

142174
def cleanupSpec() {
143175
wsClient?.close()
176+
wsClientWithReadTimeout?.close()
144177
}
145178
}
146179

147180
class PlayScalaStreamedWsClientTestBase extends PlayWsClientTestBaseBase<play.api.libs.ws.StandaloneWSRequest> {
148181
@Shared
149182
play.api.libs.ws.StandaloneWSClient wsClient
183+
@Shared
184+
play.api.libs.ws.StandaloneWSClient wsClientWithReadTimeout
150185

151186
@Override
152187
play.api.libs.ws.StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers) {
153-
return wsClient.url(uri.toURL().toString())
188+
return getClient(uri).url(uri.toURL().toString())
154189
.withMethod(method)
155190
.withFollowRedirects(true)
156191
.withHttpHeaders(JavaConverters.mapAsScalaMap(headers).toSeq())
@@ -193,11 +228,20 @@ class PlayScalaStreamedWsClientTestBase extends PlayWsClientTestBaseBase<play.ap
193228
}, ExecutionContext.global())
194229
}
195230

231+
def getClient(URI uri) {
232+
if (uri.toString().contains("/read-timeout")) {
233+
return wsClientWithReadTimeout
234+
}
235+
return wsClient
236+
}
237+
196238
def setupSpec() {
197239
wsClient = new play.api.libs.ws.ahc.StandaloneAhcWSClient(asyncHttpClient, materializer)
240+
wsClientWithReadTimeout = new play.api.libs.ws.ahc.StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer)
198241
}
199242

200243
def cleanupSpec() {
201244
wsClient?.close()
245+
wsClientWithReadTimeout?.close()
202246
}
203247
}

instrumentation/play/play-ws/play-ws-common/testing/src/main/groovy/PlayWsClientTestBaseBase.groovy

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ abstract class PlayWsClientTestBaseBase<REQUEST> extends HttpClientTest<REQUEST>
3030
@Shared
3131
AsyncHttpClient asyncHttpClient
3232

33+
@Shared
34+
AsyncHttpClient asyncHttpClientWithReadTimeout
35+
3336
@Shared
3437
ActorMaterializer materializer
3538

@@ -44,21 +47,30 @@ abstract class PlayWsClientTestBaseBase<REQUEST> extends HttpClientTest<REQUEST>
4447
// failure ahc will try the next address which isn't necessary for this test.
4548
RequestBuilderBase.DEFAULT_NAME_RESOLVER = new CustomNameResolver(ImmediateEventExecutor.INSTANCE)
4649

47-
AsyncHttpClientConfig asyncHttpClientConfig =
48-
new DefaultAsyncHttpClientConfig.Builder()
49-
.setMaxRequestRetry(0)
50-
.setShutdownQuietPeriod(0)
51-
.setShutdownTimeout(0)
52-
.setMaxRedirects(3)
53-
.setConnectTimeout(CONNECT_TIMEOUT_MS)
54-
.setReadTimeout(READ_TIMEOUT_MS)
55-
.build()
56-
57-
asyncHttpClient = new DefaultAsyncHttpClient(asyncHttpClientConfig)
50+
asyncHttpClient = createClient(false)
51+
asyncHttpClientWithReadTimeout = createClient(true)
52+
}
53+
54+
def createClient(boolean readTimeout) {
55+
DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder()
56+
.setMaxRequestRetry(0)
57+
.setShutdownQuietPeriod(0)
58+
.setShutdownTimeout(0)
59+
.setMaxRedirects(3)
60+
.setConnectTimeout(CONNECT_TIMEOUT_MS)
61+
62+
if (readTimeout) {
63+
builder.setReadTimeout(READ_TIMEOUT_MS)
64+
}
65+
66+
AsyncHttpClientConfig asyncHttpClientConfig =builder.build()
67+
return new DefaultAsyncHttpClient(asyncHttpClientConfig)
5868
}
5969

6070
def cleanupSpec() {
6171
system?.terminate()
72+
asyncHttpClient?.close()
73+
asyncHttpClientWithReadTimeout?.close()
6274
}
6375

6476
@Override

0 commit comments

Comments
 (0)