Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public AgentTypePool typePool(
return typePool(classFileLocator, classLoader);
}

private TypePool.CacheProvider getCacheProvider(ClassLoader classLoader) {
TypePool.CacheProvider getCacheProvider(ClassLoader classLoader) {
if (classLoader == null) {
return bootstrapCacheProvider;
}
Expand All @@ -167,7 +167,7 @@ private TypePool.CacheProvider getCacheProvider(ClassLoader classLoader) {
*
* <p>The loaderHash exists to avoid calling get & strengthening the Reference.
*/
private static final class TypeCacheKey {
static final class TypeCacheKey {
private final int loaderHash;
@Nullable private final WeakReference<ClassLoader> loaderRef;
private final String className;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.muzzle;

import static org.assertj.core.api.Assertions.assertThat;

import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.URLClassLoader;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.pool.TypePool;
import org.junit.jupiter.api.Test;

class CacheProviderTest {

@Test
void keyBootstrapEquivalence() {
// ClassLoader loader = null;
int loaderHash = AgentCachingPoolStrategy.BOOTSTRAP_HASH;
WeakReference<ClassLoader> loaderRef = null;

AgentCachingPoolStrategy.TypeCacheKey key1 =
new AgentCachingPoolStrategy.TypeCacheKey(loaderHash, loaderRef, "foo");
AgentCachingPoolStrategy.TypeCacheKey key2 =
new AgentCachingPoolStrategy.TypeCacheKey(loaderHash, loaderRef, "foo");

assertThat(key1).hasSameHashCodeAs(key2);
assertThat(key1).isEqualTo(key2);
}

@Test
void keySameRefEquivalence() {
ClassLoader loader = newClassLoader();
int loaderHash = loader.hashCode();
WeakReference<ClassLoader> loaderRef = new WeakReference<>(loader);

AgentCachingPoolStrategy.TypeCacheKey key1 =
new AgentCachingPoolStrategy.TypeCacheKey(loaderHash, loaderRef, "foo");
AgentCachingPoolStrategy.TypeCacheKey key2 =
new AgentCachingPoolStrategy.TypeCacheKey(loaderHash, loaderRef, "foo");

assertThat(key1).hasSameHashCodeAs(key2);
assertThat(key1).isEqualTo(key2);
// ensures that loader isn't collected
assertThat(loader).isNotNull();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loader can not be null at this point so, as it is, this assertion doesn't really make sense

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I get it now. The intention is to use the loader at the end of the method to ensure that it isn't collected before.

}

@Test
void keyDifferentRefEquivalence() {
ClassLoader loader = newClassLoader();
int loaderHash = loader.hashCode();
WeakReference<ClassLoader> loaderRef1 = new WeakReference<>(loader);
WeakReference<ClassLoader> loaderRef2 = new WeakReference<>(loader);

AgentCachingPoolStrategy.TypeCacheKey key1 =
new AgentCachingPoolStrategy.TypeCacheKey(loaderHash, loaderRef1, "foo");
AgentCachingPoolStrategy.TypeCacheKey key2 =
new AgentCachingPoolStrategy.TypeCacheKey(loaderHash, loaderRef2, "foo");

assertThat(loaderRef1).isNotSameAs(loaderRef2);

assertThat(key1).hasSameHashCodeAs(key2);
assertThat(key1).isEqualTo(key2);
// ensures that loader isn't collected
assertThat(loader).isNotNull();
}

@Test
void keyMismatchSameLoaderDifferentName() {
ClassLoader loader = newClassLoader();
int loaderHash = loader.hashCode();
WeakReference<ClassLoader> loaderRef = new WeakReference<>(loader);
AgentCachingPoolStrategy.TypeCacheKey fooKey =
new AgentCachingPoolStrategy.TypeCacheKey(loaderHash, loaderRef, "foo");
AgentCachingPoolStrategy.TypeCacheKey barKey =
new AgentCachingPoolStrategy.TypeCacheKey(loaderHash, loaderRef, "bar");

// not strictly guaranteed -- but important for performance
assertThat(fooKey.hashCode()).isNotEqualTo(barKey.hashCode());
assertThat(fooKey).isNotEqualTo(barKey);
// ensures that loader isn't collected
assertThat(loader).isNotNull();
}

@Test
void keyMismatchSameNameDifferentLoader() {
ClassLoader loader1 = newClassLoader();
int loader1Hash = loader1.hashCode();
WeakReference<ClassLoader> loaderRef1 = new WeakReference<>(loader1);

ClassLoader loader2 = newClassLoader();
int loader2Hash = loader2.hashCode();
WeakReference<ClassLoader> loaderRef2 = new WeakReference<>(loader2);

AgentCachingPoolStrategy.TypeCacheKey fooKey1 =
new AgentCachingPoolStrategy.TypeCacheKey(loader1Hash, loaderRef1, "foo");
AgentCachingPoolStrategy.TypeCacheKey fooKey2 =
new AgentCachingPoolStrategy.TypeCacheKey(loader2Hash, loaderRef2, "foo");

// not strictly guaranteed -- but important for performance
assertThat(fooKey1.hashCode()).isNotEqualTo(fooKey2.hashCode());
assertThat(fooKey1).isNotEqualTo(fooKey2);
// ensures that loader isn't collected
assertThat(loader1).isNotNull();
assertThat(loader2).isNotNull();
}

@SuppressWarnings("deprecation") // TypeDescription.VOID is deprecated
@Test
void testBasicCaching() {
AgentCachingPoolStrategy poolStrat = new AgentCachingPoolStrategy(null);

ClassLoader loader = newClassLoader();

TypePool.CacheProvider cacheProvider = poolStrat.getCacheProvider(loader);

cacheProvider.register("foo", new TypePool.Resolution.Simple(TypeDescription.VOID));

// not strictly guaranteed, but fine for this test
assertThat(cacheProvider.find("foo")).isNotNull();
// ensures that loader isn't collected
assertThat(loader).isNotNull();
}

@Test
void testLoaderEquivalence() {
AgentCachingPoolStrategy poolStrat = new AgentCachingPoolStrategy(null);

ClassLoader loader1 = newClassLoader();

TypePool.CacheProvider cacheProvider1A = poolStrat.getCacheProvider(loader1);
TypePool.CacheProvider cacheProvider1B = poolStrat.getCacheProvider(loader1);

cacheProvider1A.register("foo", newVoid());

// not strictly guaranteed, but fine for this test
assertThat(cacheProvider1A.find("foo")).isNotNull();
assertThat(cacheProvider1B.find("foo")).isNotNull();

assertThat(cacheProvider1A.find("foo")).isSameAs(cacheProvider1B.find("foo"));

// ensures that loader isn't collected
assertThat(loader1).isNotNull();
}

@Test
void testLoaderSeparation() {
AgentCachingPoolStrategy poolStrat = new AgentCachingPoolStrategy(null);

ClassLoader loader1 = newClassLoader();
ClassLoader loader2 = newClassLoader();

TypePool.CacheProvider cacheProvider1 = poolStrat.getCacheProvider(loader1);
TypePool.CacheProvider cacheProvider2 = poolStrat.getCacheProvider(loader2);

cacheProvider1.register("foo", newVoid());
cacheProvider2.register("foo", newVoid());

// not strictly guaranteed, but fine for this test
assertThat(cacheProvider1.find("foo")).isNotNull();
assertThat(cacheProvider2.find("foo")).isNotNull();

assertThat(cacheProvider1.find("foo")).isNotSameAs(cacheProvider2.find("foo"));

// ensures that loader isn't collected
assertThat(loader1).isNotNull();
assertThat(loader2).isNotNull();
}

private static TypePool.Resolution newVoid() {
return new TypePool.Resolution.Simple(TypeDescription.ForLoadedType.of(void.class));
}

private static ClassLoader newClassLoader() {
return new URLClassLoader(new URL[0], null);
}
}
Loading