-
Notifications
You must be signed in to change notification settings - Fork 882
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
86 deletions.
There are no files selected for viewing
53 changes: 0 additions & 53 deletions
53
...nal-class-loader/javaagent-integration-tests/src/test/groovy/ResourceInjectionTest.groovy
This file was deleted.
Oops, something went wrong.
13 changes: 9 additions & 4 deletions
13
.../java/io/opentelemetry/javaagent/instrumentation/internal/classloader/RegressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
package io.opentelemetry.javaagent.instrumentation.internal.classloader; | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import org.junit.jupiter.api.Test; | ||
package io.opentelemetry.javaagent.instrumentation.internal.classloader; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class RegressionTest { | ||
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/5155 | ||
// loading a class that is extended/implemented by a helper class causes | ||
// java.lang.LinkageError: loader 'app' (instance of jdk.internal.loader.ClassLoaders$AppClassLoader) | ||
// java.lang.LinkageError: loader 'app' (instance of | ||
// jdk.internal.loader.ClassLoaders$AppClassLoader) | ||
// attempted duplicate interface definition for org.apache.commons.lang3.function.FailableCallable | ||
// this test verifies that the duplicate class definition LinkageError is not thrown into | ||
// application code | ||
@Test | ||
void noDuplicateClassDefinition() throws ClassNotFoundException { | ||
assertThat(Class.forName("org.apache.commons.lang3.function.FailableCallable")).isNotNull(); | ||
} | ||
|
||
} |
62 changes: 33 additions & 29 deletions
62
...o/opentelemetry/javaagent/instrumentation/internal/classloader/ResourceInjectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,68 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.internal.classloader; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import java.io.IOException; | ||
import static io.opentelemetry.instrumentation.test.utils.GcUtils.awaitGc; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.lang.ref.WeakReference; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.nio.charset.Charset; | ||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static io.opentelemetry.instrumentation.test.utils.GcUtils.awaitGc; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import org.apache.commons.lang3.SystemUtils; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ResourceInjectionTest { | ||
|
||
private static String trimStream(URL url) throws Exception { | ||
try (BufferedReader reader = | ||
new BufferedReader(new InputStreamReader(url.openStream(), Charset.defaultCharset()))) { | ||
return reader.readLine().trim(); | ||
} | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("UnnecessaryAsync") | ||
void resourcesInjectedToNonDelegatingClassLoader() | ||
throws IOException, ClassNotFoundException, InterruptedException, TimeoutException { | ||
void resourcesInjectedToNonDelegatingClassLoader() throws Exception { | ||
|
||
String resourceName = "test-resources/test-resource.txt"; | ||
URL[] urls = {ResourceInjectionTest.class.getProtectionDomain().getCodeSource().getLocation()}; | ||
AtomicReference<URLClassLoader> emptyLoader = new AtomicReference<>(new URLClassLoader(urls, null)); | ||
URL[] urls = {SystemUtils.class.getProtectionDomain().getCodeSource().getLocation()}; | ||
AtomicReference<URLClassLoader> emptyLoader = | ||
new AtomicReference<>(new URLClassLoader(urls, null)); | ||
|
||
Enumeration<URL> resourceUrls = emptyLoader.get().getResources(resourceName); | ||
assertThat(resourceUrls.hasMoreElements()).isFalse(); | ||
|
||
URLClassLoader notInjectedLoader = new URLClassLoader(urls, null); | ||
|
||
// this triggers resource injection | ||
emptyLoader.get().loadClass(ResourceInjectionTest.class.getName()); | ||
emptyLoader.get().loadClass(SystemUtils.class.getName()); | ||
|
||
for (int i = 0; i < 2; i++) { | ||
|
||
URL test = ( resourceUrls.asIterator(); | ||
if (i == 0) { | ||
assertThat(test).isEqualTo("Hello world!"); | ||
} else { | ||
assertThat(test).isEqualTo("Hello there"); | ||
} | ||
} | ||
assertThat(resourceUrls.hasMoreElements()).isFalse(); | ||
List<URL> resourceList = Collections.list(emptyLoader.get().getResources(resourceName)); | ||
|
||
|
||
// resourceUrls = (Enumeration<URL>) Collections.list(emptyLoader.get().getResources(resourceName)); | ||
// assertThat(resourceUrls.).isEqualTo(2); | ||
// assertThat(list.get(0).openStream().toString().trim()).isEqualTo("Hello world!"); | ||
// assertThat(list.get(1).openStream().toString().trim()).isEqualTo("Hello there"); | ||
assertThat(resourceList.size()).isEqualTo(2); | ||
assertThat(trimStream(resourceList.get(0))).isEqualTo("Hello world!"); | ||
assertThat(trimStream(resourceList.get(1))).isEqualTo("Hello there"); | ||
|
||
assertThat(notInjectedLoader.getResources(resourceName).hasMoreElements()).isFalse(); | ||
|
||
// references to emptyloader are gone | ||
emptyLoader.get().close(); | ||
emptyLoader.get().close(); // cleanup | ||
WeakReference<URLClassLoader> ref = new WeakReference<>(emptyLoader.get()); | ||
emptyLoader.set(null); | ||
|
||
awaitGc(ref, Duration.ofSeconds(10)); | ||
|
||
assertThat(ref.get()).isNull(); | ||
} | ||
|
||
} |