Merge pull request #240 from DataDog/tyler/fix-fallback

Fix test for v0.4 endpoint
This commit is contained in:
Andrew Kent 2018-02-26 09:24:20 -08:00 committed by GitHub
commit 837ba4f083
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 25 deletions

View File

@ -52,7 +52,7 @@ test {
} }
if (project.hasProperty("disableShadowRelocate") && disableShadowRelocate) { if (project.hasProperty("disableShadowRelocate") && disableShadowRelocate) {
exclude 'datadog/trace/agent/ShadowPackageRenamingTest.class' exclude 'datadog/trace/agent/integration/classloading/ShadowPackageRenamingTest.class'
} }
} }

View File

@ -10,10 +10,10 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -40,8 +40,8 @@ public class DDApi {
private final ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); private final ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
public DDApi(final String host, final int port) { public DDApi(final String host, final int port) {
if (endpointAvailable("http://" + host + ":" + port + TRACES_ENDPOINT_V4) if (traceEndpointAvailable("http://" + host + ":" + port + TRACES_ENDPOINT_V4)
&& endpointAvailable("http://" + host + ":" + port + SERVICES_ENDPOINT_V4)) { && serviceEndpointAvailable("http://" + host + ":" + port + SERVICES_ENDPOINT_V4)) {
this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT_V4; this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT_V4;
this.servicesEndpoint = "http://" + host + ":" + port + SERVICES_ENDPOINT_V4; this.servicesEndpoint = "http://" + host + ":" + port + SERVICES_ENDPOINT_V4;
} else { } else {
@ -163,11 +163,15 @@ public class DDApi {
} }
} }
private boolean endpointAvailable(final String endpoint) { private boolean traceEndpointAvailable(final String endpoint) {
return endpointAvailable(endpoint, true); return endpointAvailable(endpoint, Collections.emptyList(), true);
} }
private boolean endpointAvailable(final String endpoint, final boolean retry) { private boolean serviceEndpointAvailable(final String endpoint) {
return endpointAvailable(endpoint, Collections.emptyMap(), true);
}
private boolean endpointAvailable(final String endpoint, final Object data, final boolean retry) {
try { try {
final HttpURLConnection httpCon = getHttpURLConnection(endpoint); final HttpURLConnection httpCon = getHttpURLConnection(endpoint);
@ -175,13 +179,14 @@ public class DDApi {
httpCon.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(1)); httpCon.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(1));
httpCon.setReadTimeout((int) TimeUnit.SECONDS.toMillis(1)); httpCon.setReadTimeout((int) TimeUnit.SECONDS.toMillis(1));
final OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream()); final OutputStream out = httpCon.getOutputStream();
objectMapper.writeValue(out, data);
out.flush(); out.flush();
out.close(); out.close();
return httpCon.getResponseCode() == 200; return httpCon.getResponseCode() == 200;
} catch (final IOException e) { } catch (final IOException e) {
if (retry) { if (retry) {
return endpointAvailable(endpoint, false); return endpointAvailable(endpoint, data, false);
} }
} }
return false; return false;

View File

@ -30,10 +30,12 @@ class DDApiTest extends Specification {
def agent = ratpack { def agent = ratpack {
handlers { handlers {
put("v0.4/traces") { put("v0.4/traces") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
put("v0.4/services") { put("v0.4/services") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
} }
} }
@ -56,7 +58,8 @@ class DDApiTest extends Specification {
response.status(404).send() response.status(404).send()
} }
put("v0.4/services") { put("v0.4/services") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
} }
} }
@ -87,7 +90,8 @@ class DDApiTest extends Specification {
} }
} }
put("v0.4/services") { put("v0.4/services") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
} }
} }
@ -144,10 +148,12 @@ class DDApiTest extends Specification {
def agent = ratpack { def agent = ratpack {
handlers { handlers {
put("v0.4/traces") { put("v0.4/traces") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
put("v0.4/services") { put("v0.4/services") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
} }
} }
@ -167,7 +173,8 @@ class DDApiTest extends Specification {
def agent = ratpack { def agent = ratpack {
handlers { handlers {
put("v0.4/traces") { put("v0.4/traces") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
put("v0.4/services") { put("v0.4/services") {
response.status(404).send() response.status(404).send()
@ -193,7 +200,8 @@ class DDApiTest extends Specification {
def agent = ratpack { def agent = ratpack {
handlers { handlers {
put("v0.4/traces") { put("v0.4/traces") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
put("v0.4/services") { put("v0.4/services") {
requestContentType.set(request.contentType) requestContentType.set(request.contentType)
@ -243,11 +251,13 @@ class DDApiTest extends Specification {
def agent = ratpack { def agent = ratpack {
handlers { handlers {
put("v0.4/traces") { put("v0.4/traces") {
response.status(200).send('{"hello":"test"}') def status = request.contentLength > 0 ? 200 : 500
response.status(status).send('{"hello":"test"}')
} }
put("v0.4/services") { put("v0.4/services") {
if (servicesAvailable) { if (servicesAvailable) {
response.status(200).send('{"service-response":"from-test"}') def status = request.contentLength > 0 ? 200 : 500
response.status(status).send('{"service-response":"from-test"}')
} else { } else {
response.status(404).send('{"service-response":"from-test"}') response.status(404).send('{"service-response":"from-test"}')
} }
@ -280,10 +290,12 @@ class DDApiTest extends Specification {
def v3Agent = ratpack { def v3Agent = ratpack {
handlers { handlers {
put("v0.3/traces") { put("v0.3/traces") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
put("v0.3/services") { put("v0.3/services") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
} }
} }
@ -307,21 +319,25 @@ class DDApiTest extends Specification {
def agent = ratpack { def agent = ratpack {
handlers { handlers {
put("v0.3/traces") { put("v0.3/traces") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
put("v0.3/services") { put("v0.3/services") {
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
put("v0.4/traces") { put("v0.4/traces") {
Blocking.exec { Blocking.exec {
Thread.sleep(delayTrace) Thread.sleep(delayTrace)
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
} }
put("v0.4/services") { put("v0.4/services") {
Blocking.exec { Blocking.exec {
Thread.sleep(delayServices) Thread.sleep(delayServices)
response.status(200).send() def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
} }
} }
} }