Skip to content

Commit c434682

Browse files
zeitlingerlaurit
andauthored
Convert groovy tests in javaagent and javaagent-bootstrap modules (#14907)
Co-authored-by: Lauri Tulmin <[email protected]>
1 parent 6752935 commit c434682

File tree

10 files changed

+499
-387
lines changed

10 files changed

+499
-387
lines changed

javaagent-bootstrap/src/test/groovy/io/opentelemetry/javaagent/bootstrap/AgentClassLoaderTest.groovy

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.bootstrap;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import io.opentelemetry.sdk.OpenTelemetrySdk;
11+
import java.io.File;
12+
import java.lang.reflect.Method;
13+
import java.net.URL;
14+
import java.util.concurrent.Phaser;
15+
import org.junit.jupiter.api.Test;
16+
17+
class AgentClassLoaderTest {
18+
private static final Method getClassLoadingLockMethod;
19+
20+
static {
21+
// Use reflection to access protected getClassLoadingLock method
22+
try {
23+
getClassLoadingLockMethod =
24+
ClassLoader.class.getDeclaredMethod("getClassLoadingLock", String.class);
25+
getClassLoadingLockMethod.setAccessible(true);
26+
} catch (NoSuchMethodException exception) {
27+
throw new IllegalStateException(exception);
28+
}
29+
}
30+
31+
@Test
32+
void agentClassloaderDoesNotLockClassloadingAroundInstance() throws Exception {
33+
String className1 = "some/class/Name1";
34+
String className2 = "some/class/Name2";
35+
// any jar would do, use opentelemety sdk
36+
URL testJarLocation =
37+
OpenTelemetrySdk.class.getProtectionDomain().getCodeSource().getLocation();
38+
39+
try (AgentClassLoader loader = new AgentClassLoader(new File(testJarLocation.toURI()))) {
40+
Phaser threadHoldLockPhase = new Phaser(2);
41+
Phaser acquireLockFromMainThreadPhase = new Phaser(2);
42+
43+
// Use reflection to access protected getClassLoadingLock method
44+
Method getClassLoadingLockMethod =
45+
ClassLoader.class.getDeclaredMethod("getClassLoadingLock", String.class);
46+
getClassLoadingLockMethod.setAccessible(true);
47+
48+
Thread thread1 =
49+
new Thread(
50+
() -> {
51+
synchronized (getClassLoadingLock(loader, className1)) {
52+
threadHoldLockPhase.arrive();
53+
acquireLockFromMainThreadPhase.arriveAndAwaitAdvance();
54+
}
55+
});
56+
thread1.start();
57+
58+
Thread thread2 =
59+
new Thread(
60+
() -> {
61+
threadHoldLockPhase.arriveAndAwaitAdvance();
62+
synchronized (getClassLoadingLock(loader, className2)) {
63+
acquireLockFromMainThreadPhase.arrive();
64+
}
65+
});
66+
thread2.start();
67+
68+
thread1.join();
69+
thread2.join();
70+
boolean applicationDidNotDeadlock = true;
71+
72+
assertThat(applicationDidNotDeadlock).isTrue();
73+
}
74+
}
75+
76+
private static Object getClassLoadingLock(ClassLoader classLoader, String className) {
77+
try {
78+
return getClassLoadingLockMethod.invoke(classLoader, className);
79+
} catch (Exception exception) {
80+
throw new IllegalStateException(exception);
81+
}
82+
}
83+
84+
@Test
85+
void multiReleaseJar() throws Exception {
86+
boolean jdk8 = "1.8".equals(System.getProperty("java.specification.version"));
87+
Class<?> mrJarClass =
88+
Class.forName("io.opentelemetry.instrumentation.resources.internal.ProcessArguments");
89+
// sdk is a multi release jar
90+
URL multiReleaseJar = mrJarClass.getProtectionDomain().getCodeSource().getLocation();
91+
92+
try (AgentClassLoader loader =
93+
new AgentClassLoader(new File(multiReleaseJar.toURI())) {
94+
@Override
95+
protected String getClassSuffix() {
96+
return "";
97+
}
98+
}) {
99+
URL url =
100+
loader.findResource(
101+
"io/opentelemetry/instrumentation/resources/internal/ProcessArguments.class");
102+
103+
assertThat(url).isNotNull();
104+
// versioned resource is found when not running on jdk 8
105+
assertThat(url.toString().contains("META-INF/versions/9/")).isNotEqualTo(jdk8);
106+
107+
Class<?> clazz =
108+
loader.loadClass("io.opentelemetry.instrumentation.resources.internal.ProcessArguments");
109+
// class was loaded by agent loader used in this test
110+
assertThat(clazz.getClassLoader()).isEqualTo(loader);
111+
Method method = clazz.getDeclaredMethod("getProcessArguments");
112+
method.setAccessible(true);
113+
String[] result = (String[]) method.invoke(null);
114+
// jdk8 versions returns empty array, jdk9 version does not
115+
assertThat(result.length > 0).isNotEqualTo(jdk8);
116+
}
117+
}
118+
}

javaagent/src/test/groovy/io/opentelemetry/javaagent/AgentLoadedIntoBootstrapTest.groovy

Lines changed: 0 additions & 51 deletions
This file was deleted.

javaagent/src/test/groovy/io/opentelemetry/javaagent/LogLevelTest.groovy

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)