You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2021/08/26 22:18:19 UTC

[sling-org-apache-sling-testing-osgi-mock] branch master updated: SLING-10757 make sure services are only restarted once while checking for static greedy references during registering of new services - otherwise the new "already registered" mail fail the operation

This is an automated email from the ASF dual-hosted git repository.

sseifert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git


The following commit(s) were added to refs/heads/master by this push:
     new e8eb264  SLING-10757 make sure services are only restarted once while checking for static greedy references during registering of new services - otherwise the new "already registered" mail fail the operation
e8eb264 is described below

commit e8eb264576ca0e81e8548fba9d192a7477409057
Author: Stefan Seifert <st...@users.noreply.github.com>
AuthorDate: Fri Aug 27 00:14:42 2021 +0200

    SLING-10757 make sure services are only restarted once while checking for static greedy references during registering of new services - otherwise the new "already registered" mail fail the operation
---
 .../sling/testing/mock/osgi/MockBundleContext.java |  9 +++++++--
 ...ockBundleContextStaticGreedyReferencesTest.java | 22 +++++++++++++++++++++-
 .../osgiserviceutil/Service3StaticGreedy.java      |  2 ++
 ...rvice3StaticGreedyConstructorInjectionImpl.java |  5 +++++
 .../osgiserviceutil/Service3StaticGreedyImpl.java  |  9 +++++++++
 5 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
index 3949585..f465ef2 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
@@ -28,6 +28,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Dictionary;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Queue;
@@ -172,6 +173,7 @@ class MockBundleContext implements BundleContext {
 
         // handle STATIC+GREEDY references to this registration
         List<ReferenceInfo<?>> affectedStaticGreedyReferences = OsgiServiceUtil.getMatchingStaticGreedyReferences(registeredServices, registration);
+        Set<MockServiceRegistration<?>> servicesToRestart = new HashSet<>();
         for (ReferenceInfo<?> referenceInfo : affectedStaticGreedyReferences) {
             Reference reference = referenceInfo.getReference();
             switch (reference.getCardinality()) {
@@ -181,12 +183,13 @@ class MockBundleContext implements BundleContext {
             case MANDATORY_MULTIPLE:
             case OPTIONAL_MULTIPLE:
             case OPTIONAL_UNARY:
-                restartService(referenceInfo.getServiceRegistration());
+                servicesToRestart.add(referenceInfo.getServiceRegistration());
                 break;
             default:
                 throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
             }
         }
+        servicesToRestart.forEach(this::restartService);
     }
 
     void unregisterService(MockServiceRegistration<?> registration) {
@@ -247,6 +250,7 @@ class MockBundleContext implements BundleContext {
 
         // handle STATIC+GREEDY references to this registration
         List<ReferenceInfo<?>> affectedStaticGreedyReferences = OsgiServiceUtil.getMatchingStaticGreedyReferences(registeredServices, registration);
+        Set<MockServiceRegistration<?>> servicesToRestart = new HashSet<>();
         for (ReferenceInfo<?> referenceInfo : affectedStaticGreedyReferences) {
             Reference reference = referenceInfo.getReference();
             switch (reference.getCardinality()) {
@@ -254,12 +258,13 @@ class MockBundleContext implements BundleContext {
             case MANDATORY_MULTIPLE:
             case OPTIONAL_MULTIPLE:
             case OPTIONAL_UNARY:
-                restartService(referenceInfo.getServiceRegistration());
+                servicesToRestart.add(referenceInfo.getServiceRegistration());
                 break;
             default:
                 throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
             }
         }
+        servicesToRestart.forEach(this::restartService);
     }
 
     @SuppressWarnings("unchecked")
diff --git a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyReferencesTest.java b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyReferencesTest.java
index a9a6bd3..a99afb1 100644
--- a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyReferencesTest.java
+++ b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextStaticGreedyReferencesTest.java
@@ -38,6 +38,7 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -148,6 +149,19 @@ public class MockBundleContextStaticGreedyReferencesTest {
         reg2a.unregister();
     }
 
+    @Test
+    public void testReferenceWithTargetFilter() {
+        assertDependencies3Filtered();
+
+        bundleContext.registerService(ServiceInterface3.class.getName(), dependency3a,
+                MapUtil.toDictionary(ImmutableMap.<String, Object>of("prop1", "abc")));
+
+        bundleContext.registerService(ServiceInterface3.class.getName(), dependency3b,
+                MapUtil.toDictionary(ImmutableMap.<String, Object>of("prop1", "def")));
+
+        assertDependencies3Filtered(dependency3a);
+    }
+
     private void assertDependency1(ServiceInterface1 instance) {
         Service3StaticGreedy service = getService();
         if (instance == null) {
@@ -175,7 +189,7 @@ public class MockBundleContextStaticGreedyReferencesTest {
     }
 
     private void assertDependencies3(ServiceSuperInterface3... instances) {
-        Service3StaticGreedy service =getService();
+        Service3StaticGreedy service = getService();
         assertEquals(ImmutableSet.<ServiceSuperInterface3>copyOf(instances),
                 ImmutableSet.<ServiceSuperInterface3>copyOf(service.getReferences3()));
     }
@@ -185,4 +199,10 @@ public class MockBundleContextStaticGreedyReferencesTest {
         return (Service3StaticGreedy)bundleContext.getService(serviceRef);
     }
 
+    private void assertDependencies3Filtered(ServiceSuperInterface3... instances) {
+        Service3StaticGreedy service = getService();
+        assertEquals(ImmutableSet.<ServiceSuperInterface3>copyOf(instances),
+                ImmutableSet.<ServiceSuperInterface3>copyOf(service.getReferences3Filtered()));
+    }
+
 }
diff --git a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedy.java b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedy.java
index 0f30599..9a60e5d 100644
--- a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedy.java
+++ b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedy.java
@@ -33,4 +33,6 @@ public interface Service3StaticGreedy {
 
     List<Map<String, Object>> getReference3Configs();
 
+    List<ServiceSuperInterface3> getReferences3Filtered();
+
 }
diff --git a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedyConstructorInjectionImpl.java b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedyConstructorInjectionImpl.java
index 677582e..8131912 100644
--- a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedyConstructorInjectionImpl.java
+++ b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedyConstructorInjectionImpl.java
@@ -122,6 +122,11 @@ public class Service3StaticGreedyConstructorInjectionImpl implements Service3Sta
         return null;
     }
 
+    @Override
+    public List<ServiceSuperInterface3> getReferences3Filtered() {
+        return null;
+    }
+
     public ComponentContext getComponentContext() {
         return this.componentContext;
     }
diff --git a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedyImpl.java b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedyImpl.java
index 01f08a2..6beb5d8 100644
--- a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedyImpl.java
+++ b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service3StaticGreedyImpl.java
@@ -57,6 +57,10 @@ public class Service3StaticGreedyImpl implements Service3StaticGreedy {
     private List<ServiceSuperInterface3> references3 = new ArrayList<>();
     private List<Map<String, Object>> reference3Configs = new ArrayList<>();
 
+    @Reference(service = ServiceInterface3.class, cardinality = ReferenceCardinality.MULTIPLE, target="(prop1=abc)",
+            policy = ReferencePolicy.STATIC, policyOption = ReferencePolicyOption.GREEDY)
+    private List<ServiceSuperInterface3> references3Filtered;
+
     private ComponentContext componentContext;
     private Map<String, Object> config;
 
@@ -112,6 +116,11 @@ public class Service3StaticGreedyImpl implements Service3StaticGreedy {
         return this.reference3Configs;
     }
 
+    @Override
+    public List<ServiceSuperInterface3> getReferences3Filtered() {
+        return this.references3Filtered;
+    }
+
     public ComponentContext getComponentContext() {
         return this.componentContext;
     }