Skip to content

Commit fa5c247

Browse files
committed
convert test
1 parent cbcb20d commit fa5c247

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

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

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,46 @@ void agentClassloaderDoesNotLockClassloadingAroundInstance() throws Exception {
2121
String className1 = "some/class/Name1";
2222
String className2 = "some/class/Name2";
2323
// any jar would do, use opentelemety sdk
24-
URL testJarLocation = OpenTelemetrySdk.class.getProtectionDomain().getCodeSource().getLocation();
24+
URL testJarLocation =
25+
OpenTelemetrySdk.class.getProtectionDomain().getCodeSource().getLocation();
2526

2627
try (AgentClassLoader loader = new AgentClassLoader(new File(testJarLocation.toURI()))) {
2728
Phaser threadHoldLockPhase = new Phaser(2);
2829
Phaser acquireLockFromMainThreadPhase = new Phaser(2);
2930

3031
// Use reflection to access protected getClassLoadingLock method
31-
Method getClassLoadingLockMethod = ClassLoader.class.getDeclaredMethod("getClassLoadingLock", String.class);
32+
Method getClassLoadingLockMethod =
33+
ClassLoader.class.getDeclaredMethod("getClassLoadingLock", String.class);
3234
getClassLoadingLockMethod.setAccessible(true);
3335

34-
Thread thread1 = new Thread(() -> {
35-
try {
36-
Object lock1 = getClassLoadingLockMethod.invoke(loader, className1);
37-
synchronized (lock1) {
38-
threadHoldLockPhase.arrive();
39-
acquireLockFromMainThreadPhase.arriveAndAwaitAdvance();
40-
}
41-
} catch (Exception e) {
42-
throw new RuntimeException(e);
43-
}
44-
});
36+
Thread thread1 =
37+
new Thread(
38+
() -> {
39+
try {
40+
Object lock1 = getClassLoadingLockMethod.invoke(loader, className1);
41+
synchronized (lock1) {
42+
threadHoldLockPhase.arrive();
43+
acquireLockFromMainThreadPhase.arriveAndAwaitAdvance();
44+
}
45+
} catch (Exception e) {
46+
throw new RuntimeException(e);
47+
}
48+
});
4549
thread1.start();
4650

47-
Thread thread2 = new Thread(() -> {
48-
try {
49-
threadHoldLockPhase.arriveAndAwaitAdvance();
50-
Object lock2 = getClassLoadingLockMethod.invoke(loader, className2);
51-
synchronized (lock2) {
52-
acquireLockFromMainThreadPhase.arrive();
53-
}
54-
} catch (Exception e) {
55-
throw new RuntimeException(e);
56-
}
57-
});
51+
Thread thread2 =
52+
new Thread(
53+
() -> {
54+
try {
55+
threadHoldLockPhase.arriveAndAwaitAdvance();
56+
Object lock2 = getClassLoadingLockMethod.invoke(loader, className2);
57+
synchronized (lock2) {
58+
acquireLockFromMainThreadPhase.arrive();
59+
}
60+
} catch (Exception e) {
61+
throw new RuntimeException(e);
62+
}
63+
});
5864
thread2.start();
5965

6066
thread1.join();
@@ -68,23 +74,28 @@ void agentClassloaderDoesNotLockClassloadingAroundInstance() throws Exception {
6874
@Test
6975
void multiReleaseJar() throws Exception {
7076
boolean jdk8 = "1.8".equals(System.getProperty("java.specification.version"));
71-
Class<?> mrJarClass = Class.forName("io.opentelemetry.instrumentation.resources.internal.ProcessArguments");
77+
Class<?> mrJarClass =
78+
Class.forName("io.opentelemetry.instrumentation.resources.internal.ProcessArguments");
7279
// sdk is a multi release jar
7380
URL multiReleaseJar = mrJarClass.getProtectionDomain().getCodeSource().getLocation();
7481

75-
try (AgentClassLoader loader = new AgentClassLoader(new File(multiReleaseJar.toURI())) {
76-
@Override
77-
protected String getClassSuffix() {
78-
return "";
79-
}
80-
}) {
81-
URL url = loader.findResource("io/opentelemetry/instrumentation/resources/internal/ProcessArguments.class");
82+
try (AgentClassLoader loader =
83+
new AgentClassLoader(new File(multiReleaseJar.toURI())) {
84+
@Override
85+
protected String getClassSuffix() {
86+
return "";
87+
}
88+
}) {
89+
URL url =
90+
loader.findResource(
91+
"io/opentelemetry/instrumentation/resources/internal/ProcessArguments.class");
8292

8393
assertThat(url).isNotNull();
8494
// versioned resource is found when not running on jdk 8
8595
assertThat(url.toString().contains("META-INF/versions/9/")).isNotEqualTo(jdk8);
8696

87-
Class<?> clazz = loader.loadClass("io.opentelemetry.instrumentation.resources.internal.ProcessArguments");
97+
Class<?> clazz =
98+
loader.loadClass("io.opentelemetry.instrumentation.resources.internal.ProcessArguments");
8899
// class was loaded by agent loader used in this test
89100
assertThat(clazz.getClassLoader()).isEqualTo(loader);
90101
Method method = clazz.getDeclaredMethod("getProcessArguments");

0 commit comments

Comments
 (0)