11package datadog.trace.common.writer
22
3+ import static datadog.trace.api.config.TracerConfig.PRIORITIZATION_TYPE
4+
35import datadog.communication.ddagent.DDAgentFeaturesDiscovery
46import datadog.communication.ddagent.SharedCommunicationObjects
57import datadog.trace.api.Config
@@ -10,10 +12,16 @@ import datadog.trace.common.writer.ddintake.DDEvpProxyApi
1012import datadog.trace.common.writer.ddintake.DDIntakeApi
1113import datadog.trace.core.monitor.HealthMetrics
1214import datadog.trace.test.util.DDSpecification
13-
15+ import groovy.json.JsonBuilder
1416import java.util.stream.Collectors
15-
16- import static datadog.trace.api.config.TracerConfig.PRIORITIZATION_TYPE
17+ import okhttp3.Call
18+ import okhttp3.HttpUrl
19+ import okhttp3.MediaType
20+ import okhttp3.OkHttpClient
21+ import okhttp3.Protocol
22+ import okhttp3.Request
23+ import okhttp3.Response
24+ import okhttp3.ResponseBody
1725
1826class WriterFactoryTest extends DDSpecification {
1927
@@ -27,19 +35,30 @@ class WriterFactoryTest extends DDSpecification {
2735 config. isCiVisibilityEnabled() >> true
2836 config. isCiVisibilityCodeCoverageEnabled() >> false
2937
30- def agentFeaturesDiscovery = Mock (DDAgentFeaturesDiscovery )
31- agentFeaturesDiscovery. getEvpProxyEndpoint() >> DDAgentFeaturesDiscovery . V2_EVP_PROXY_ENDPOINT
32- agentFeaturesDiscovery. supportsContentEncodingHeadersWithEvpProxy() >> evpProxySupportsCompression
38+ // Mock agent info response
39+ def response = buildHttpResponse(hasEvpProxy, evpProxySupportsCompression, HttpUrl . parse(config. agentUrl + " /info" ))
40+
41+ // Mock HTTP client that simulates delayed response for async feature discovery
42+ def mockCall = Mock (Call )
43+ def mockHttpClient = Mock (OkHttpClient )
44+ mockCall. execute() >> {
45+ // Add a delay
46+ sleep(400 )
47+ return response
48+ }
49+ mockHttpClient. newCall(_ as Request ) >> mockCall
3350
51+ // Create SharedCommunicationObjects with mocked HTTP client
3452 def sharedComm = new SharedCommunicationObjects ()
35- sharedComm. setFeaturesDiscovery(agentFeaturesDiscovery)
53+ sharedComm. okHttpClient = mockHttpClient
54+ sharedComm. agentUrl = HttpUrl . parse(config. agentUrl)
3655 sharedComm. createRemaining(config)
3756
3857 def sampler = Mock (Sampler )
3958
4059 when :
41- agentFeaturesDiscovery. supportsEvpProxy() >> hasEvpProxy
4260 config. ciVisibilityAgentlessEnabled >> isCiVisibilityAgentlessEnabled
61+
4362 def writer = WriterFactory . createWriter(config, sharedComm, sampler, null , HealthMetrics . NO_OP , configuredType)
4463
4564 def apis
@@ -77,4 +96,28 @@ class WriterFactoryTest extends DDSpecification {
7796 " not-found" | false | false | true | DDIntakeWriter | [DDIntakeApi ] | true
7897 " not-found" | false | false | false | DDAgentWriter | [DDAgentApi ] | false
7998 }
99+
100+ Response buildHttpResponse (boolean hasEvpProxy , boolean evpProxySupportsCompression , HttpUrl agentUrl ) {
101+ def endpoints = []
102+ if (hasEvpProxy && evpProxySupportsCompression) {
103+ endpoints = [DDAgentFeaturesDiscovery . V4_EVP_PROXY_ENDPOINT ]
104+ } else if (hasEvpProxy) {
105+ endpoints = [DDAgentFeaturesDiscovery . V2_EVP_PROXY_ENDPOINT ]
106+ } else {
107+ endpoints = [DDAgentFeaturesDiscovery . V4_ENDPOINT ]
108+ }
109+
110+ def response = [
111+ " version" : " 7.40.0" ,
112+ " endpoints" : endpoints,
113+ ]
114+
115+ def builder = new Response.Builder ()
116+ .code(200 )
117+ .message(" OK" )
118+ .protocol(Protocol . HTTP_1_1 )
119+ .request(new Request.Builder (). url(agentUrl. resolve(" /info" )). build())
120+ .body(ResponseBody . create(MediaType . parse(" application/json" ), new JsonBuilder (response). toString()))
121+ return builder. build()
122+ }
80123}
0 commit comments