-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathOkHttp3AsyncTest.groovy
97 lines (80 loc) · 2.58 KB
/
OkHttp3AsyncTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import datadog.trace.agent.test.naming.TestingGenericHttpNamingConventions
import datadog.trace.agent.test.utils.TraceUtils
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Headers
import okhttp3.MediaType
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import okhttp3.internal.http.HttpMethod
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicReference
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan
import static java.util.concurrent.TimeUnit.SECONDS
abstract class OkHttp3AsyncTest extends OkHttp3Test {
@Override
int doRequest(String method, URI uri, Map<String, String> headers, String body, Closure callback) {
def reqBody = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse("text/plain"), body) : null
def request = new Request.Builder()
.url(uri.toURL())
.method(method, reqBody)
.headers(Headers.of(headers))
.build()
AtomicReference<Response> responseRef = new AtomicReference()
AtomicReference<Exception> exRef = new AtomicReference()
def latch = new CountDownLatch(1)
client.newCall(request).enqueue(new Callback() {
void onResponse(Call call, Response response) {
responseRef.set(response)
callback?.call()
latch.countDown()
}
void onFailure(Call call, IOException e) {
exRef.set(e)
callback?.call()
latch.countDown()
}
})
latch.await(10, SECONDS)
if (exRef.get() != null) {
throw exRef.get()
}
return responseRef.get().code()
}
def "callbacks should carry context with error = #error" () {
when:
def captured = noopSpan()
try {
TraceUtils.runUnderTrace("parent", {
doRequest(method, url, ["Datadog-Meta-Lang": "java"], "", { captured = AgentTracer.activeSpan() })
})
} catch (Exception e) {
assert error == true
}
then:
"parent".contentEquals(captured.getOperationName())
where:
url | error
server.address.resolve("/success") | false
new URI("http://240.0.0.1") | true
method = "GET"
}
}
class OkHttp3AsyncV0ForkedTest extends OkHttp3AsyncTest {
@Override
int version() {
return 0
}
@Override
String service() {
return null
}
@Override
String operation() {
return "okhttp.request"
}
}
class OkHttp3AsyncV1ForkedTest extends OkHttp3AsyncTest implements TestingGenericHttpNamingConventions.ClientV1 {
}