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
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.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 @@
 
         // 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 @@
             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 @@
 
         // 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 @@
             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.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 @@
         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 @@
     }
 
     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 @@
         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 @@
 
     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 @@
         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 @@
     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 @@
         return this.reference3Configs;
     }
 
+    @Override
+    public List<ServiceSuperInterface3> getReferences3Filtered() {
+        return this.references3Filtered;
+    }
+
     public ComponentContext getComponentContext() {
         return this.componentContext;
     }