You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by ri...@apache.org on 2014/12/12 11:15:52 UTC

[1/2] incubator-brooklyn git commit: Emit location_added/removed event on entities

Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 39935baca -> 6c46ac438


Emit location_added/removed event on entities

Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/9c44be31
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/9c44be31
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/9c44be31

Branch: refs/heads/master
Commit: 9c44be31ef25764dfeacff2e7378a1bd5a3de189
Parents: 39935ba
Author: Aled Sage <al...@gmail.com>
Authored: Thu Dec 11 21:15:35 2014 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Thu Dec 11 21:15:35 2014 +0000

----------------------------------------------------------------------
 .../brooklyn/entity/basic/AbstractEntity.java   | 15 ++++
 .../entity/basic/EntityLocationsTest.java       | 88 +++++++++++++++++++-
 2 files changed, 101 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9c44be31/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java b/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
index de28236..9b0399b 100644
--- a/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
+++ b/core/src/main/java/brooklyn/entity/basic/AbstractEntity.java
@@ -102,6 +102,7 @@ import com.google.common.base.Objects;
 import com.google.common.base.Objects.ToStringHelper;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -144,6 +145,11 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
     
     static { BrooklynLanguageExtensions.init(); }
     
+    public static final BasicNotificationSensor<Location> LOCATION_ADDED = new BasicNotificationSensor<Location>(
+            Location.class, "entity.Location.added", "Location dynamically added to entity");
+    public static final BasicNotificationSensor<Location> LOCATION_REMOVED = new BasicNotificationSensor<Location>(
+            Location.class, "entity.Location.removed", "Location dynamically removed from entity");
+
     public static final BasicNotificationSensor<Sensor> SENSOR_ADDED = new BasicNotificationSensor<Sensor>(Sensor.class,
             "entity.sensor.added", "Sensor dynamically added to entity");
     public static final BasicNotificationSensor<Sensor> SENSOR_REMOVED = new BasicNotificationSensor<Sensor>(Sensor.class,
@@ -740,6 +746,10 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
             if (truelyNewLocations.size() > 0) {
                 locations.set(ImmutableList.<Location>builder().addAll(oldLocations).addAll(truelyNewLocations).build());
             }
+            
+            for (Location loc : truelyNewLocations) {
+                emit(AbstractEntity.LOCATION_ADDED, loc);
+            }
         }
         
         if (getManagementSupport().isDeployed()) {
@@ -756,7 +766,12 @@ public abstract class AbstractEntity extends AbstractBrooklynObject implements E
     public void removeLocations(Collection<? extends Location> removedLocations) {
         synchronized (locations) {
             List<Location> oldLocations = locations.get();
+            Set<Location> truelyRemovedLocations = Sets.intersection(ImmutableSet.copyOf(removedLocations), ImmutableSet.copyOf(oldLocations));
             locations.set(MutableList.<Location>builder().addAll(oldLocations).removeAll(removedLocations).buildImmutable());
+            
+            for (Location loc : truelyRemovedLocations) {
+                emit(AbstractEntity.LOCATION_REMOVED, loc);
+            }
         }
         
         // TODO Not calling `Entities.unmanage(removedLocation)` because this location might be shared with other entities.

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9c44be31/core/src/test/java/brooklyn/entity/basic/EntityLocationsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/EntityLocationsTest.java b/core/src/test/java/brooklyn/entity/basic/EntityLocationsTest.java
index f77a745..5207256 100644
--- a/core/src/test/java/brooklyn/entity/basic/EntityLocationsTest.java
+++ b/core/src/test/java/brooklyn/entity/basic/EntityLocationsTest.java
@@ -18,23 +18,107 @@
  */
 package brooklyn.entity.basic;
 
+import static org.testng.Assert.assertEquals;
+
 import java.util.Arrays;
+import java.util.List;
 
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
 import brooklyn.entity.BrooklynAppUnitTestSupport;
+import brooklyn.entity.basic.EntitySubscriptionTest.RecordingSensorEventListener;
 import brooklyn.location.Location;
-import brooklyn.location.basic.SimulatedLocation;
+import brooklyn.test.Asserts;
+
+import com.google.common.collect.ImmutableList;
 
 public class EntityLocationsTest extends BrooklynAppUnitTestSupport {
 
     @Test
     public void testDuplicateLocationOnlyAddedOnce() {
-        Location l = new SimulatedLocation();
+        Location l = app.newSimulatedLocation();
         app.addLocations(Arrays.asList(l, l));
         app.addLocations(Arrays.asList(l, l));
         Assert.assertEquals(app.getLocations().size(), 1);
     }
     
+    @Test
+    public void testNotifiedOfAddAndRemoveLocations() throws Exception {
+        final Location l = app.newSimulatedLocation();
+        final Location l2 = app.newSimulatedLocation();
+        
+        final RecordingSensorEventListener addedEvents = new RecordingSensorEventListener();
+        final RecordingSensorEventListener removedEvents = new RecordingSensorEventListener();
+        app.subscribe(app, AbstractEntity.LOCATION_ADDED, addedEvents);
+        app.subscribe(app, AbstractEntity.LOCATION_REMOVED, removedEvents);
+
+        // Add first location
+        app.addLocations(ImmutableList.of(l));
+        
+        assertEventValuesEqualsEventually(addedEvents, ImmutableList.of(l));
+        assertEventValuesEquals(removedEvents, ImmutableList.of());
+
+        // Add second location
+        app.addLocations(ImmutableList.of(l2));
+
+        assertEventValuesEqualsEventually(addedEvents, ImmutableList.of(l, l2));
+        assertEventValuesEquals(removedEvents, ImmutableList.of());
+
+        // Remove first location
+        app.removeLocations(ImmutableList.of(l));
+        
+        assertEventValuesEqualsEventually(removedEvents, ImmutableList.of(l));
+        assertEventValuesEquals(addedEvents, ImmutableList.of(l, l2));
+
+        // Remove second location
+        app.removeLocations(ImmutableList.of(l2));
+        
+        assertEventValuesEqualsEventually(removedEvents, ImmutableList.of(l, l2));
+        assertEventValuesEquals(addedEvents, ImmutableList.of(l, l2));
+    }
+    
+    @Test(groups="Integration") // because takes a second
+    public void testNotNotifiedDuplicateAddedLocations() throws Exception {
+        final Location l = app.newSimulatedLocation();
+        
+        final RecordingSensorEventListener addedEvents = new RecordingSensorEventListener();
+        final RecordingSensorEventListener removedEvents = new RecordingSensorEventListener();
+        app.subscribe(app, AbstractEntity.LOCATION_ADDED, addedEvents);
+        app.subscribe(app, AbstractEntity.LOCATION_REMOVED, removedEvents);
+
+        // Add first location
+        app.addLocations(ImmutableList.of(l, l));
+        
+        assertEventValuesEqualsEventually(addedEvents, ImmutableList.of(l));
+        assertEventValuesEquals(removedEvents, ImmutableList.of());
+
+        // Add second location
+        app.addLocations(ImmutableList.of(l));
+
+        assertEventValuesEqualsContinually(addedEvents, ImmutableList.of(l));
+        assertEventValuesEquals(removedEvents, ImmutableList.of());
+    }
+    
+    private void assertEventValuesEqualsEventually(final RecordingSensorEventListener listener, final List<?> expectedVals) {
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEventValuesEquals(listener, expectedVals);
+            }});
+    }
+    
+    private void assertEventValuesEqualsContinually(final RecordingSensorEventListener listener, final List<?> expectedVals) {
+        Asserts.succeedsContinually(new Runnable() {
+            @Override public void run() {
+                assertEventValuesEquals(listener, expectedVals);
+            }});
+    }
+    
+    private void assertEventValuesEquals(final RecordingSensorEventListener listener, final List<?> expectedVals) {
+        assertEquals(listener.events.size(), expectedVals.size(), "events="+listener.events);
+        for (int i = 0; i < expectedVals.size(); i++) {
+            Object expectedVal = expectedVals.get(i);
+            assertEquals(listener.events.get(i).getValue(), expectedVal, "events="+listener.events);
+        }
+    }
 }


[2/2] incubator-brooklyn git commit: Merge and close #384

Posted by ri...@apache.org.
Merge and close #384


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/6c46ac43
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/6c46ac43
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/6c46ac43

Branch: refs/heads/master
Commit: 6c46ac4380eb9867a4de3a99a29c2a5550262923
Parents: 39935ba 9c44be3
Author: Richard Downer <ri...@apache.org>
Authored: Fri Dec 12 10:15:29 2014 +0000
Committer: Richard Downer <ri...@apache.org>
Committed: Fri Dec 12 10:15:29 2014 +0000

----------------------------------------------------------------------
 .../brooklyn/entity/basic/AbstractEntity.java   | 15 ++++
 .../entity/basic/EntityLocationsTest.java       | 88 +++++++++++++++++++-
 2 files changed, 101 insertions(+), 2 deletions(-)
----------------------------------------------------------------------