Skip to content

Commit

Permalink
Fix all WeavePackageManagerTest cache size assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed May 5, 2021
1 parent 970329f commit 648f3c1
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.newrelic.weave.weavepackage;

import com.github.benmanes.caffeine.cache.Cache;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void testTwoWeaves() throws IOException {
wpm.validPackages.getIfPresent(Thread.currentThread().getContextClassLoader()).values()) {
WeaveTestUtils.expectViolations(res);
}
Assert.assertEquals(1, wpm.validPackages.asMap().size());
Assert.assertEquals(1, getCacheSize(wpm.validPackages));
Assert.assertEquals(2, wpm.validPackages.getIfPresent(Thread.currentThread().getContextClassLoader()).size());

Assert.assertNotNull(compositeBytes);
Expand Down Expand Up @@ -124,8 +125,8 @@ public void testBootstrapWeave() throws IOException {
WeaveTestUtils.getClassBytes(targetClassName));
Assert.assertTrue(bsPackage.hasMatcher(targetInternalName, superNames, interfaceNames,
Collections.<String>emptySet(), Collections.<String>emptySet(), null));
Assert.assertEquals(0, wpm.invalidPackages.asMap().size());
Assert.assertEquals(1, wpm.validPackages.asMap().size());
Assert.assertEquals(0, getCacheSize(wpm.invalidPackages));
Assert.assertEquals(1, getCacheSize(wpm.validPackages));
Assert.assertNotNull(wpm.validPackages.getIfPresent(BootstrapLoader.PLACEHOLDER));
Assert.assertNotNull(wpm.validPackages.getIfPresent(BootstrapLoader.PLACEHOLDER).get(bsPackage));

Expand All @@ -147,7 +148,7 @@ public void testMaxValidClassLoaders() throws IOException {
byte[] compositeBytes = WeaveTestUtils.getClassBytes(className);
Assert.assertNotNull(compositeBytes);
compositeBytes = wpm.weave(originalClassLoader, internalName, compositeBytes);
Assert.assertEquals(1, wpm.validPackages.asMap().size());
Assert.assertEquals(1, getCacheSize(wpm.validPackages));
Assert.assertEquals(1, wpm.validPackages.getIfPresent(originalClassLoader).size());

Assert.assertNotNull(compositeBytes);
Expand All @@ -169,7 +170,7 @@ public void testMaxValidClassLoaders() throws IOException {
wpm.weave(cl, "com/newrelic/weave/weavepackage/testclasses/MyOriginalBase",
WeaveTestUtils.getClassBytes("com.newrelic.weave.weavepackage.testclasses.MyOriginalBase"));
}
Assert.assertTrue(wpm.validPackages.asMap().size() <= WeavePackageManager.MAX_VALID_PACKAGE_CACHE);
Assert.assertTrue(getCacheSize(wpm.validPackages) <= WeavePackageManager.MAX_VALID_PACKAGE_CACHE);

// 3) load the original class and a new class with the original classloader and verify they load + weave

Expand All @@ -187,7 +188,7 @@ public void testMaxValidClassLoaders() throws IOException {
byte[] newCompositeBytes = WeaveTestUtils.getClassBytes(newClassName);
Assert.assertNotNull(newCompositeBytes);
newCompositeBytes = wpm.weave(originalClassLoader, newInternalName, newCompositeBytes);
Assert.assertTrue(wpm.validPackages.asMap().size() <= WeavePackageManager.MAX_VALID_PACKAGE_CACHE);
Assert.assertTrue(getCacheSize(wpm.validPackages) <= WeavePackageManager.MAX_VALID_PACKAGE_CACHE);
Assert.assertEquals(1, wpm.validPackages.getIfPresent(originalClassLoader).size());

Assert.assertNotNull(newCompositeBytes);
Expand Down Expand Up @@ -218,7 +219,7 @@ public void testMaxInvalidClassLoaders() throws IOException {
byte[] compositeBytes = WeaveTestUtils.getClassBytes(className);
Assert.assertNotNull(compositeBytes);
compositeBytes = wpm.weave(originalClassLoader, internalName, compositeBytes);
Assert.assertEquals(1, wpm.invalidPackages.asMap().size());
Assert.assertEquals(1, getCacheSize(wpm.invalidPackages));
Assert.assertEquals(1, wpm.invalidPackages.getIfPresent(originalClassLoader).size());

Assert.assertNull(compositeBytes);
Expand All @@ -239,8 +240,7 @@ public void testMaxInvalidClassLoaders() throws IOException {
wpm.weave(cl, "com/newrelic/weave/weavepackage/WeavePackageManagerTest$NewNoMatchClass",
WeaveTestUtils.getClassBytes("com.newrelic.weave.weavepackage.WeavePackageManagerTest$NewNoMatchClass"));
}
wpm.invalidPackages.cleanUp();
Assert.assertTrue(WeavePackageManager.MAX_INVALID_PACKAGE_CACHE >= wpm.invalidPackages.asMap().size());
Assert.assertTrue(WeavePackageManager.MAX_INVALID_PACKAGE_CACHE >= getCacheSize(wpm.invalidPackages));

// 3) load with the original classloader again and verify that class loads but doesn't weave (pushes to invalid)
compositeBytes = WeaveTestUtils.getClassBytes(className);
Expand Down Expand Up @@ -270,7 +270,7 @@ public void testWeakClassLoaders() throws IOException {
wpm.weave(cl, "com/newrelic/weave/weavepackage/testclasses/MyOriginalBase",
WeaveTestUtils.getClassBytes("com.newrelic.weave.weavepackage.testclasses.MyOriginalBase"));
}
Assert.assertEquals(numClassLoaders, wpm.validPackages.asMap().size());
Assert.assertEquals(numClassLoaders, getCacheSize(wpm.validPackages));

for (int i = 0; i < 5; ++i) {
System.gc();
Expand All @@ -279,7 +279,7 @@ public void testWeakClassLoaders() throws IOException {
wpm.weave(cl, "com/newrelic/weave/weavepackage/testclasses/MyOriginalBase",
WeaveTestUtils.getClassBytes("com.newrelic.weave.weavepackage.testclasses.MyOriginalBase"));
}
Assert.assertTrue(wpm.validPackages.asMap().size() >= numClassLoaders);
Assert.assertTrue(getCacheSize(wpm.validPackages) >= numClassLoaders);

classloaders.clear();
for (int i = 0; i < 10; ++i) {
Expand All @@ -292,7 +292,7 @@ public void testWeakClassLoaders() throws IOException {
wpm.validPackages.invalidate(cl);
}

Assert.assertTrue(wpm.validPackages.asMap().size() < numClassLoaders);
Assert.assertTrue(getCacheSize(wpm.validPackages) < numClassLoaders);
}

/**
Expand Down Expand Up @@ -437,6 +437,12 @@ public void testListener() throws IOException {
Assert.assertTrue(expectedInvokeCount == listener.invokeCount);
}

private static int getCacheSize(Cache<?, ?> cache) {
// Trigger cache cleanup to evict expired entries.
cache.cleanUp();
return cache.asMap().size();
}

private static class TestListener implements WeavePackageLifetimeListener, ClassWeavedListener {
public int invokeCount = 0;

Expand Down

0 comments on commit 648f3c1

Please sign in to comment.