Author: csierra
Date: Wed May 24 21:43:52 2017
New Revision: 1796108

URL: http://svn.apache.org/viewvc?rev=1796108&view=rev
Log:
Test implementation with highest service ranking

Added:
    
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/HighestRankingRouter.java
Modified:
    
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/DSLTest.java

Modified: 
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/DSLTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/DSLTest.java?rev=1796108&r1=1796107&r2=1796108&view=diff
==============================================================================
--- 
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/DSLTest.java
 (original)
+++ 
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/DSLTest.java
 Wed May 24 21:43:52 2017
@@ -47,6 +47,7 @@ import static org.apache.aries.osgi.func
 import static org.apache.aries.osgi.functional.OSGi.register;
 import static org.apache.aries.osgi.functional.OSGi.serviceReferences;
 import static org.apache.aries.osgi.functional.OSGi.services;
+import static 
org.apache.aries.osgi.functional.test.HighestRankingRouter.highest;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -490,6 +491,58 @@ public class DSLTest {
             serviceRegistrationTwo.unregister();
             serviceRegistrationThree.unregister();
         }
+
+    }
+
+    @Test
+    public void testHighestRankingOnly() {
+        AtomicReference<ServiceReference<Service>> current =
+            new AtomicReference<>();
+
+        OSGi<Void> program =
+            highest(Service.class).
+            foreach(current::set, sr -> current.set(null));
+
+        assertNull(current.get());
+
+        try (OSGiResult<Void> result = program.run(bundleContext)) {
+            ServiceRegistration<Service> serviceRegistrationOne =
+                bundleContext.registerService(
+                    Service.class, new Service(),
+                    new Hashtable<String, Object>() {{
+                        put("service.ranking", 0);
+                    }});
+
+            assertEquals(serviceRegistrationOne.getReference(), current.get());
+
+            ServiceRegistration<Service> serviceRegistrationTwo =
+                bundleContext.registerService(
+                    Service.class, new Service(),
+                    new Hashtable<String, Object>() {{
+                        put("service.ranking", 1);
+                    }});
+
+            assertEquals(serviceRegistrationTwo.getReference(), current.get());
+
+            ServiceRegistration<Service> serviceRegistrationMinusOne =
+                bundleContext.registerService(
+                    Service.class, new Service(),
+                    new Hashtable<String, Object>() {{
+                        put("service.ranking", -1);
+                    }});
+
+            assertEquals(serviceRegistrationTwo.getReference(), current.get());
+
+            serviceRegistrationTwo.unregister();
+
+            assertEquals(serviceRegistrationOne.getReference(), current.get());
+
+            serviceRegistrationOne.unregister();
+
+            assertEquals(
+                serviceRegistrationMinusOne.getReference(), current.get());
+
+            serviceRegistrationMinusOne.unregister();
         }
 
     }

Added: 
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/HighestRankingRouter.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/HighestRankingRouter.java?rev=1796108&view=auto
==============================================================================
--- 
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/HighestRankingRouter.java
 (added)
+++ 
aries/trunk/component-dsl/itests/src/main/java/org/apache/aries/osgi/functional/test/HighestRankingRouter.java
 Wed May 24 21:43:52 2017
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.aries.osgi.functional.test;
+
+import org.apache.aries.osgi.functional.Event;
+import org.apache.aries.osgi.functional.OSGi;
+import org.osgi.framework.ServiceReference;
+
+import java.util.Comparator;
+import java.util.PriorityQueue;
+import java.util.function.Consumer;
+
+import static org.apache.aries.osgi.functional.OSGi.serviceReferences;
+
+/**
+ * @author Carlos Sierra Andrés
+ */
+public class HighestRankingRouter<T extends Comparable<? super T>>
+    implements Consumer<OSGi.Router<T>> {
+
+    private PriorityQueue<Event<T>> _instances;
+
+    public static <T> OSGi<ServiceReference<T>> highest(Class<T> clazz) {
+        return serviceReferences(clazz).route(new HighestRankingRouter<>());
+    }
+
+    @Override
+    public void accept(OSGi.Router<T> router) {
+        router.onIncoming(sr -> {
+            Event<T> old = _instances.peek();
+
+            _instances.add(sr);
+
+            if (_instances.peek() == sr) {
+                if (old != null) {
+                    router.signalLeave(old);
+                }
+
+                router.signalAdd(sr);
+            }
+
+        });
+
+        router.onLeaving(sr -> {
+            Event<T> old = _instances.peek();
+
+            _instances.remove(sr);
+
+            Event<T> current = _instances.peek();
+
+            if (current != old) {
+                router.signalLeave(old);
+
+                if (current != null) {
+                    router.signalAdd(current);
+                }
+            }
+        });
+
+        router.onStart(
+            () -> _instances = new PriorityQueue<>(
+                Comparator.<Event<T>, T>comparing(Event::getContent).
+                    reversed()));
+
+        router.onClose(() -> {
+            _instances.forEach(router::signalLeave);
+
+            _instances.clear();
+        });
+    }
+}


Reply via email to