-
Notifications
You must be signed in to change notification settings - Fork 878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert internal-class-loader tests from groovy to java #12983
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eabaeba
start converting internal tests
jaydeluca b81f295
convert, still debugging
jaydeluca 9a00da9
Merge branch 'main' of github.com:jaydeluca/opentelemetry-java-instru…
jaydeluca ba1e629
fix gc issue
jaydeluca 2e03f7f
rename method
jaydeluca 683cce4
don't rely on default charset
laurit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 0 additions & 19 deletions
19
...l/internal-class-loader/javaagent-integration-tests/src/test/groovy/RegressionTest.groovy
This file was deleted.
Oops, something went wrong.
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.
24 changes: 24 additions & 0 deletions
24
.../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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
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) | ||
// 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(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.internal.classloader; | ||
|
||
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.List; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
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 Exception { | ||
|
||
String resourceName = "test-resources/test-resource.txt"; | ||
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(); | ||
resourceUrls = null; | ||
|
||
URLClassLoader notInjectedLoader = new URLClassLoader(urls, null); | ||
|
||
// this triggers resource injection | ||
emptyLoader.get().loadClass(SystemUtils.class.getName()); | ||
|
||
List<URL> resourceList = Collections.list(emptyLoader.get().getResources(resourceName)); | ||
|
||
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(); // cleanup | ||
WeakReference<URLClassLoader> ref = new WeakReference<>(emptyLoader.get()); | ||
emptyLoader.set(null); | ||
|
||
awaitGc(ref, Duration.ofSeconds(10)); | ||
|
||
assertThat(ref.get()).isNull(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to call this method
readLine