You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by re...@apache.org on 2025/03/05 08:54:03 UTC

(sling-org-apache-sling-resourceresolver) branch master updated: SLING-12686: add test coverage for events during VP bg init (#155)

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 8ed46c7e SLING-12686: add test coverage for events during VP bg init (#155)
8ed46c7e is described below

commit 8ed46c7e51995f22e3aa22e42424de7df49949f2
Author: Julian Reschke <ju...@gmx.de>
AuthorDate: Wed Mar 5 09:53:59 2025 +0100

    SLING-12686: add test coverage for events during VP bg init (#155)
    
    * SLING-12686: add test coverage for events during VP bg init
    
    * SLING-12686: add test coverage for events during VP bg init - test clarified
    
    * SLING-12686: add test coverage for events during VP bg init - test clarified
---
 .../impl/mapping/VanityPathMapEntriesTest.java     | 60 ++++++++++++++++------
 1 file changed, 44 insertions(+), 16 deletions(-)

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 e03493b6..d56061e5 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
@@ -57,7 +57,6 @@ import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -138,6 +137,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest {
         configs.add(new VanityPathConfig("/redirectingVanityPath", false));
         configs.add(new VanityPathConfig("/redirectingVanityPath301", false));
         configs.add(new VanityPathConfig("/vanityPathOnJcrContent", false));
+        configs.add(new VanityPathConfig("/eventTest", false));
 
         Collections.sort(configs);
         when(bundle.getSymbolicName()).thenReturn("TESTBUNDLE");
@@ -1213,11 +1213,11 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest {
     }
 
     @Test
-    public void test_bg_init_fallback_while_no_ready() {
+    public void test_bg_init_fallback_while_not_ready() {
         // this is only applicable when background init is enabled
         assumeTrue(this.isVanityPathCacheInitInBackground);
 
-        AtomicInteger queryCounter = new AtomicInteger();
+        List<String> queries = Collections.synchronizedList(new ArrayList<>());
 
         Lock lock = new ReentrantLock();
         lock.lock();
@@ -1235,7 +1235,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest {
             String query = invocation.getArguments()[0].toString();
             try {
                 lock.lock();
-                queryCounter.incrementAndGet();
+                queries.add(query);
                 if (matchesSpecificQueryFor(query, targetPath) || matchesPagedQuery(query)) {
                     return Collections.singleton(simpleVanityResource).iterator();
                 } else {
@@ -1246,41 +1246,69 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest {
             }
         });
 
-        // no query yet
-        assertEquals(0, queryCounter.get());
+        assertEquals("test not started, query counter should be 0, queries were: " +
+                dumpQueries(queries), 0, queries.size());
 
         mapEntries.vph.initializeVanityPaths();
 
-        // still no query
-        assertEquals(0, queryCounter.get());
+        assertEquals("test started, but query is blocked, query counter should still be 0, queries were: " +
+                dumpQueries(queries), 0, queries.size());
 
         // should not be finished until unlocked
         assertFalse("VPH should not be ready as it is locked", mapEntries.vph.isReady());
 
         // do a forced lookup while init runs
-        Iterator<MapEntry> mit = mapEntries.vph.getCurrentMapEntryForVanityPath("/foo");
+        Iterator<MapEntry> mit = mapEntries.vph.getCurrentMapEntryForVanityPath(targetPath);
         assertNotNull(mit);
-        assertEquals(1, queryCounter.get());
+        assertEquals("after lookup bypassing background init, exactly 1 query should have happened, but queries were: " +
+                dumpQueries(queries), 1, queries.size());
 
         // intermediate map does not contain vp
         Map<String, List<String>> intermediateVanityMap = mapEntries.getVanityPathMappings();
-        assertFalse(intermediateVanityMap.containsKey("/simpleVanityPath"));
+        assertFalse("while bg init is running, vanity map should not be updated yet",
+                intermediateVanityMap.containsKey("/simpleVanityPath"));
 
         // try another forced lookup, should be cached
-        mit = mapEntries.vph.getCurrentMapEntryForVanityPath("/foo");
+        mit = mapEntries.vph.getCurrentMapEntryForVanityPath(targetPath);
         assertNotNull(mit);
-        assertEquals(1, queryCounter.get());
+        assertEquals("second lookup for the same path during background init should be cached, so no query should have happened, queries were: " +
+                dumpQueries(queries), 1, queries.size());
+
+        // send a change event for a resource with vanity path;
+        // this will be queued while init is running and then processed later on
+        Resource eventTest = mock(Resource.class, "eventTest");
+        when(eventTest.getName()).thenReturn("eventTest");
+        when(eventTest.getPath()).thenReturn("/eventTest");
+        when(eventTest.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/baa"));
+
+        // target for the resource got which we sent the event
+        Resource eventTestTarget = mock(Resource.class, "eventTestTarget");
+        when(eventTestTarget.getName()).thenReturn("baa");
+        when(eventTestTarget.getPath()).thenReturn("/baa");
+
+        when(resourceResolver.getResource(eventTest.getPath())).thenReturn(eventTest);
+        when(resourceResolver.getResource(eventTestTarget.getPath())).thenReturn(eventTestTarget);
+
+        mapEntries.onChange(List.of(
+                new ResourceChange(ChangeType.ADDED, eventTest.getPath(), false)
+        ));
 
         // let initializer run, then wait until finished
         lock.unlock();
         waitForBgInit();
 
         // now one more query should have happened
-        assertEquals(2, queryCounter.get());
+        assertEquals("query done by background init, total number should now be 2, but queries were: " +
+                dumpQueries(queries), 2, queries.size());
 
-        // final map contains vp
+        // final map contains both vps (from direct lookup and from event)
         Map<String, List<String>> finalVanityMap = mapEntries.getVanityPathMappings();
-        assertTrue(finalVanityMap.get("/simpleVanityPath").contains("/foo"));
+        assertTrue(finalVanityMap.get("/simpleVanityPath").contains(targetPath));
+        assertTrue(finalVanityMap.get("/eventTest").contains("/baa"));
+    }
+
+    private static String dumpQueries(List<String> queries) {
+        return queries.size() + " (" + queries + ")";
     }
 
     // utilities for testing vanity path queries