Don't use isAlive() in tests and do more in the CLI app
MODIFY
dd-smoke-tests/cli/cli.gradle
dd-smoke-tests/cli/cli.gradle
@@ -13,6 +13,8 @@ jar {
}
dependencies {
+ compile project(':dd-trace-api')
+
testCompile project(':dd-smoke-tests')
}

MODIFY
dd-smoke-tests/cli/src/main/java/datadog/smoketest/cli/CliApplication.java
dd-smoke-tests/cli/src/main/java/datadog/smoketest/cli/CliApplication.java
package datadog.smoketest.cli;
-/** Simple application that sleeps for a bit than quits. */
+import datadog.trace.api.Trace;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+
+/** Simple application that makes a request to example.com then quits. */
public class CliApplication {
- /** @param args Only argument is the sleep delay (in seconds) */
public static void main(final String[] args) throws InterruptedException {
- final int delay = Integer.parseInt(args[0]);
+ final CliApplication app = new CliApplication();
+
+ System.out.println("Making request");
- System.out.println("Going to shut down after " + delay + "seconds");
+ app.makeRequest();
- Thread.sleep(delay * 1000);
+ System.out.println("Finished making request");
+ }
- System.out.println("Shutting down");
+ @Trace(operationName = "example")
+ public void makeRequest() {
+ try {
+ final URL url = new URL("http://www.example.com/");
+ final URLConnection connection = url.openConnection();
+ connection.setConnectTimeout(1000);
+ connection.connect();
+ connection.getInputStream();
+ } catch (final IOException e) {
+ // Ignore. The goal of this app is to attempt the connection not necessarily to succeed
+ }
}
}

MODIFY
dd-smoke-tests/cli/src/test/groovy/datadog/smoketest/CliApplicationSmokeTest.groovy
dd-smoke-tests/cli/src/test/groovy/datadog/smoketest/CliApplicationSmokeTest.groovy
package datadog.smoketest
-import spock.util.concurrent.PollingConditions
-class CliApplicationSmokeTest extends AbstractSmokeTest {
- // Estimate for the amount of time instrumentation takes plus a little extra
- private static final int INSTRUMENTATION_DELAY = 6 + 5
+import java.util.concurrent.TimeUnit
- private static final int SHUTDOWN_DELAY = 2
+class CliApplicationSmokeTest extends AbstractSmokeTest {
+ // Estimate for the amount of time instrumentation, plus request, plus some extra
+ private static final int TIMEOUT_SECS = 10
@Override
ProcessBuilder createProcessBuilder() {
@@ -15,20 +14,15 @@ class CliApplicationSmokeTest extends AbstractSmokeTest {
List<String> command = new ArrayList<>()
command.add(javaPath())
command.addAll(defaultJavaProperties)
- command.addAll((String[]) ["-jar", cliShadowJar, String.valueOf(SHUTDOWN_DELAY)])
+ command.addAll((String[]) ["-jar", cliShadowJar])
ProcessBuilder processBuilder = new ProcessBuilder(command)
processBuilder.directory(new File(buildDirectory))
}
def "Cli application process ends before timeout"() {
- setup:
- def conditions = new PollingConditions(timeout: INSTRUMENTATION_DELAY, initialDelay: SHUTDOWN_DELAY)
-
expect:
- serverProcess.isAlive()
+ assert serverProcess.waitFor(TIMEOUT_SECS, TimeUnit.SECONDS)
- conditions.eventually {
- assert !serverProcess.isAlive()
- }
+ assert serverProcess.exitValue() == 0
}
}

Don't use isAlive() in tests and do more in the CLI app