-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathClassInjectingForkedTest.groovy
More file actions
59 lines (47 loc) · 1.94 KB
/
ClassInjectingForkedTest.groovy
File metadata and controls
59 lines (47 loc) · 1.94 KB
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
package locator
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.config.inversion.ConfigHelper
import net.bytebuddy.agent.builder.AgentBuilder
import net.bytebuddy.description.type.TypeDescription
import net.bytebuddy.dynamic.DynamicType
import net.bytebuddy.utility.JavaModule
import spock.lang.Shared
import java.lang.instrument.ClassFileTransformer
class ClassInjectingForkedTest extends InstrumentationSpecification {
static volatile ClassFileTransformer extraTransformer = null
@Override
protected void configurePreAgent() {
super.configurePreAgent()
// Opt out of strict config validation - test module loads test instrumentations with fake names
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.TEST)
// Since this method is not at all configurePreAgent, but more like
// configurePreAgentAndOhByTheWayBeforeEveryTest we need to not install
// the extra transformer multiple times
if (!extraTransformer) {
AgentBuilder builder = new AgentBuilder.Default()
builder = ClassInjectingTransformer.instrument(builder)
extraTransformer = builder.installOn(INSTRUMENTATION)
}
}
@Override
protected void cleanupAfterAgent() {
if (extraTransformer) {
INSTRUMENTATION.removeTransformer(extraTransformer)
extraTransformer = null
}
super.cleanupAfterAgent()
}
@Override
void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, boolean loaded, DynamicType dynamicType) {
transformed += typeDescription.name
}
@Shared
def transformed = []
def "should find classes injected via defineClass"() {
setup:
def instrumented = new ClassInjectingTestInstrumentation.ToBeInstrumented("test")
expect:
transformed.contains('locator.ClassInjectingTestInstrumentation$ToBeInstrumented')
instrumented.message == "test:instrumented:${ClassInjectingTransformer.NAME}"
}
}