This is an automated email from the ASF dual-hosted git repository.
reschke pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git
The following commit(s) were added to refs/heads/master by this push:
new ba7588b SLING-12636: Resource Resolver: add test coverage for
background init (#136)
ba7588b is described below
commit ba7588bc5cf5bda1aae55c3e5022659fcc440380
Author: Julian Reschke <[email protected]>
AuthorDate: Mon Feb 3 12:30:45 2025 +0100
SLING-12636: Resource Resolver: add test coverage for background init (#136)
* SLING-12636: Resource Resolver: add test coverage for background init
* SLING-12636: Resource Resolver: add test coverage for background init -
fix MapEntries internal state when loadVanityPaths is called a second time
---
.../resourceresolver/impl/mapping/MapEntries.java | 2 +
.../impl/mapping/VanityPathMapEntriesTest.java | 70 ++++++++++++++++------
2 files changed, 53 insertions(+), 19 deletions(-)
diff --git
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
index 95a4164..80556f0 100644
---
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
+++
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
@@ -1166,8 +1166,10 @@ public class MapEntries implements
this.initializing.lock();
try {
if (this.factory.isVanityPathEnabled()) {
+ vanityPathsProcessed.set(false);
this.vanityBloomFilter = createVanityBloomFilter();
VanityPathInitializer vpi = new
VanityPathInitializer(this.factory);
+
if (this.factory.isVanityPathCacheInitInBackground()) {
this.log.debug("bg init starting");
Thread vpinit = new Thread(vpi, "VanityPathInitializer");
diff --git
a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java
b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java
index 218db15..d0da9df 100644
---
a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java
+++
b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java
@@ -90,23 +90,26 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
@Mock
private EventAdmin eventAdmin;
- private int pageSize;
- private boolean isMaxCachedVanityPathEntriesStartup;
+ private final int pageSize;
+ private final boolean isMaxCachedVanityPathEntriesStartup;
+ private final boolean isVanityPathCacheInitInBackground;
private int prevPageSize = 1000;
- @Parameters(name="{0} {1}")
+ @Parameters(name="{0} {1} {2}")
public static Collection<Object[]> data() {
return List.of(new Object[][] {
- {1000, false},
- {1, true},
- {1000, true}}
+ {1000, false, true},
+ {1, true, false},
+ {1000, true, false}}
);
}
- public VanityPathMapEntriesTest(int pageSize, boolean
isMaxCachedVanityPathEntriesStartup) {
+ public VanityPathMapEntriesTest(int pageSize, boolean
isMaxCachedVanityPathEntriesStartup,
+ boolean isVanityPathCacheInitInBackground)
{
this.pageSize = pageSize;
this.isMaxCachedVanityPathEntriesStartup =
isMaxCachedVanityPathEntriesStartup;
+ this.isVanityPathCacheInitInBackground =
isVanityPathCacheInitInBackground;
}
@Override
@@ -133,6 +136,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
Collections.sort(configs);
when(bundle.getSymbolicName()).thenReturn("TESTBUNDLE");
when(bundleContext.getBundle()).thenReturn(bundle);
+
when(resourceResolverFactory.getServiceUserAuthenticationInfo(anyString())).thenReturn(Map.of());
when(resourceResolverFactory.getServiceResourceResolver(any(Map.class))).thenReturn(resourceResolver);
when(resourceResolverFactory.isVanityPathEnabled()).thenReturn(true);
when(resourceResolverFactory.getVanityPathConfig()).thenReturn(configs);
@@ -140,6 +144,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(resourceResolverFactory.getMapRoot()).thenReturn(MapEntries.DEFAULT_MAP_ROOT);
when(resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(-1L);
when(resourceResolverFactory.isMaxCachedVanityPathEntriesStartup()).thenReturn(isMaxCachedVanityPathEntriesStartup);
+
when(resourceResolverFactory.isVanityPathCacheInitInBackground()).thenReturn(isVanityPathCacheInitInBackground);
when(resourceResolver.findResources(anyString(),
eq("sql"))).thenReturn(
Collections.emptyIterator());
when(resourceResolver.findResources(anyString(),
eq("JCR-SQL2"))).thenReturn(
@@ -149,6 +154,35 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
Optional<ResourceResolverMetrics> metrics = Optional.empty();
mapEntries = new MapEntries(resourceResolverFactory, bundleContext,
eventAdmin, stringInterpolationProvider, metrics);
+ waitForBgInit();
+ }
+
+ // get internal flag that signals completion of background task
+ private AtomicBoolean getVanityPathsProcessed() {
+ try {
+ Field field =
MapEntries.class.getDeclaredField("vanityPathsProcessed");
+ field.setAccessible(true);
+ return (AtomicBoolean) field.get(mapEntries);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ // wait for background thread to complete
+ private void waitForBgInit() {
+ while (!getVanityPathsProcessed().get()) {
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ // ignored
+ }
+ }
+ }
+
+ // get vanity paths (after waiting for bg init to complete)
+ private void initializeVanityPaths() throws IOException {
+ mapEntries.initializeVanityPaths();
+ waitForBgInit();
}
@Override
@@ -166,8 +200,9 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
String oneMore = "one-more";
prepareMapEntriesForVanityPath(false, false, containerName,
childName, oneMore, vanityPath);
- mapEntries.doInit();
- mapEntries.initializeVanityPaths();
+
+ initializeVanityPaths();
+
Map<String, List<String>> vanityMap =
mapEntries.getVanityPathMappings();
assertNotNull(vanityMap);
assertEquals(vanityPath, vanityMap.get("/" + containerName + "/" +
childName).get(0));
@@ -184,8 +219,9 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
String oneMore = "one-more";
prepareMapEntriesForVanityPath(true, true, containerName,
childName, oneMore, vanityPath);
- mapEntries.doInit();
- mapEntries.initializeVanityPaths();
+
+ initializeVanityPaths();
+
Map<String, List<String>> vanityMap =
mapEntries.getVanityPathMappings();
assertNotNull(vanityMap);
// not present
@@ -299,8 +335,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
}
});
- mapEntries.doInit();
- mapEntries.initializeVanityPaths();
+ initializeVanityPaths();
List<MapEntry> entries = mapEntries.getResolveMaps();
@@ -346,8 +381,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(resourceResolver.findResources(anyString(),
eq("JCR-SQL2"))).thenAnswer((Answer<Iterator<Resource>>) invocation ->
Collections.emptyIterator());
- mapEntries.doInit();
- mapEntries.initializeVanityPaths();
+ initializeVanityPaths();
// map entries should have no alias atm
assertTrue( mapEntries.getResolveMaps().isEmpty());
@@ -460,7 +494,6 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
resources.add(getVanityPathResource(val));
}
-
when(resourceResolver.findResources(anyString(),
eq("JCR-SQL2"))).thenAnswer((Answer<Iterator<Resource>>) invocation -> {
String query = invocation.getArguments()[0].toString();
if (matchesPagedQuery(query)) {
@@ -475,8 +508,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
}
});
- mapEntries.doInit();
- mapEntries.initializeVanityPaths();
+ initializeVanityPaths();
List<MapEntry> entries = mapEntries.getResolveMaps();
// each valid resource results in 2 entries
@@ -1254,7 +1286,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
return vp.length == 0 ? "": vp[0];
}
- private Comparator<Resource> vanityResourceComparator = (o1, o2) -> {
+ private final Comparator<Resource> vanityResourceComparator = (o1, o2) -> {
String s1 = getFirstVanityPath(o1);
String s2 = getFirstVanityPath(o2);
return s1.compareTo(s2);