Allow loading agent classes.
MODIFY
dd-java-agent/testing/src/main/java/datadog/trace/agent/test/AgentTestRunner.java
dd-java-agent/testing/src/main/java/datadog/trace/agent/test/AgentTestRunner.java
@@ -86,8 +86,14 @@ public abstract class AgentTestRunner extends Specification {
throw new IllegalStateException("transformer already in place: " + activeTransformer);
}
+ final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
+ try {
+ Thread.currentThread().setContextClassLoader(AgentTestRunner.class.getClassLoader());
activeTransformer =
AgentInstaller.installBytebuddyAgent(instrumentation, new ErrorCountingListener());
+ } finally {
+ Thread.currentThread().setContextClassLoader(contextLoader);
+ }
}
@Before

MODIFY
dd-java-agent/testing/src/main/java/datadog/trace/agent/test/SpockRunner.java
dd-java-agent/testing/src/main/java/datadog/trace/agent/test/SpockRunner.java
@@ -21,7 +21,6 @@ import org.spockframework.runtime.Sputnik;
*
* <ul>
* <li> Adds agent bootstrap classes to bootstrap classpath.
- * <li> Runs tests in a custom classloader which cannot see core agent classes.
* </ul>
*/
public class SpockRunner extends Sputnik {
@@ -113,7 +112,9 @@ public class SpockRunner extends Sputnik {
TestUtils.createJarWithClasses(loader, bootstrapClasses.toArray(new String[0])).getFile());
}
- /** TODO: Doc */
+ /**
+ * Run test classes in a classloader which loads test classes before delegating.
+ */
private static class InstrumentationClassLoader extends java.lang.ClassLoader {
final ClassLoader parent;
final String shadowPrefix;
@@ -154,7 +155,7 @@ public class SpockRunner extends Sputnik {
for (int i = 0; i < AGENT_PACKAGE_PREFIXES.length; ++i) {
if (name.startsWith(AGENT_PACKAGE_PREFIXES[i])) {
throw new ClassNotFoundException(
- "refusing to load agent class" + name + " on test classloader.");
+ "refusing to load agent class: " + name + " on test classloader.");
}
}
}

MODIFY
dd-java-agent/testing/src/test/groovy/AgentTestRunnerTest.groovy
dd-java-agent/testing/src/test/groovy/AgentTestRunnerTest.groovy
import datadog.trace.agent.test.TestUtils
+import datadog.trace.agent.tooling.AgentInstaller
import java.lang.reflect.Field
-import static datadog.trace.agent.tooling.ClassLoaderMatcher.BOOTSTRAP_CLASSLOADER
-
import datadog.trace.agent.test.AgentTestRunner
class AgentTestRunnerTest extends AgentTestRunner {
+ private static final ClassLoader BOOTSTRAP_CLASSLOADER = null
+
static {
// when test class initializes, opentracing should be set up, but not the agent.
assert io.opentracing.Tracer.getClassLoader() == BOOTSTRAP_CLASSLOADER
@@ -18,6 +19,7 @@ class AgentTestRunnerTest extends AgentTestRunner {
io.opentracing.Tracer.getClassLoader() == BOOTSTRAP_CLASSLOADER
TEST_TRACER == TestUtils.getUnderlyingGlobalTracer()
getAgentTransformer() != null
+ datadog.trace.api.Trace.getClassLoader() == BOOTSTRAP_CLASSLOADER
}
def "logging works"() {
@@ -27,10 +29,6 @@ class AgentTestRunnerTest extends AgentTestRunner {
noExceptionThrown()
}
- def "can't see agent classes"() {
- // TODO
- }
-
private static getAgentTransformer() {
Field f
try {

Allow loading agent classes.