|
| 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 | + |
| 19 | + @Test |
| 20 | + void agentClassloaderDoesNotLockClassloadingAroundInstance() throws Exception { |
| 21 | + String className1 = "some/class/Name1"; |
| 22 | + String className2 = "some/class/Name2"; |
| 23 | + // any jar would do, use opentelemety sdk |
| 24 | + URL testJarLocation = OpenTelemetrySdk.class.getProtectionDomain().getCodeSource().getLocation(); |
| 25 | + |
| 26 | + try (AgentClassLoader loader = new AgentClassLoader(new File(testJarLocation.toURI()))) { |
| 27 | + Phaser threadHoldLockPhase = new Phaser(2); |
| 28 | + Phaser acquireLockFromMainThreadPhase = new Phaser(2); |
| 29 | + |
| 30 | + // Use reflection to access protected getClassLoadingLock method |
| 31 | + Method getClassLoadingLockMethod = ClassLoader.class.getDeclaredMethod("getClassLoadingLock", String.class); |
| 32 | + getClassLoadingLockMethod.setAccessible(true); |
| 33 | + |
| 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 | + }); |
| 45 | + thread1.start(); |
| 46 | + |
| 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 | + }); |
| 58 | + thread2.start(); |
| 59 | + |
| 60 | + thread1.join(); |
| 61 | + thread2.join(); |
| 62 | + boolean applicationDidNotDeadlock = true; |
| 63 | + |
| 64 | + assertThat(applicationDidNotDeadlock).isTrue(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void multiReleaseJar() throws Exception { |
| 70 | + boolean jdk8 = "1.8".equals(System.getProperty("java.specification.version")); |
| 71 | + Class<?> mrJarClass = Class.forName("io.opentelemetry.instrumentation.resources.internal.ProcessArguments"); |
| 72 | + // sdk is a multi release jar |
| 73 | + URL multiReleaseJar = mrJarClass.getProtectionDomain().getCodeSource().getLocation(); |
| 74 | + |
| 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 | + |
| 83 | + assertThat(url).isNotNull(); |
| 84 | + // versioned resource is found when not running on jdk 8 |
| 85 | + assertThat(url.toString().contains("META-INF/versions/9/")).isNotEqualTo(jdk8); |
| 86 | + |
| 87 | + Class<?> clazz = loader.loadClass("io.opentelemetry.instrumentation.resources.internal.ProcessArguments"); |
| 88 | + // class was loaded by agent loader used in this test |
| 89 | + assertThat(clazz.getClassLoader()).isEqualTo(loader); |
| 90 | + Method method = clazz.getDeclaredMethod("getProcessArguments"); |
| 91 | + method.setAccessible(true); |
| 92 | + String[] result = (String[]) method.invoke(null); |
| 93 | + // jdk8 versions returns empty array, jdk9 version does not |
| 94 | + assertThat(result.length > 0).isNotEqualTo(jdk8); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments