brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [4/5] incubator-brooklyn git commit: DynamicMultiGroup: fix+test removing empty bucket
Date Mon, 17 Nov 2014 15:47:09 GMT
DynamicMultiGroup: fix+test removing empty bucket

- Previously gave ConcurrentModificationException when removing,
  iterating over Sets.difference, because that set is a live-view
  rather than a copy.


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

Branch: refs/heads/master
Commit: 7ef7b092305fdfb1e66dec62160902d99192f0a4
Parents: 984fbc4
Author: Aled Sage <aled.sage@gmail.com>
Authored: Sat Nov 15 23:48:00 2014 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Mon Nov 17 15:46:05 2014 +0000

----------------------------------------------------------------------
 .../entity/group/DynamicMultiGroupImpl.java     |  3 +-
 .../entity/group/DynamicMultiGroupTest.java     | 40 +++++++++++++++++++-
 2 files changed, 41 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7ef7b092/core/src/main/java/brooklyn/entity/group/DynamicMultiGroupImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/group/DynamicMultiGroupImpl.java b/core/src/main/java/brooklyn/entity/group/DynamicMultiGroupImpl.java
index 17a9a81..afb51e0 100644
--- a/core/src/main/java/brooklyn/entity/group/DynamicMultiGroupImpl.java
+++ b/core/src/main/java/brooklyn/entity/group/DynamicMultiGroupImpl.java
@@ -37,6 +37,7 @@ import brooklyn.event.feed.function.FunctionPollConfig;
 
 import com.google.common.base.Function;
 import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
@@ -161,7 +162,7 @@ public class DynamicMultiGroupImpl extends DynamicGroupImpl implements
DynamicMu
             }
 
             // Remove any now-empty buckets
-            Set<String> empty = Sets.difference(buckets.keySet(), entityMapping.keySet());
+            Set<String> empty = ImmutableSet.copyOf(Sets.difference(buckets.keySet(),
entityMapping.keySet()));
             for (String name : empty) {
                 Group removed = buckets.remove(name);
                 removeChild(removed);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7ef7b092/core/src/test/java/brooklyn/entity/group/DynamicMultiGroupTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/group/DynamicMultiGroupTest.java b/core/src/test/java/brooklyn/entity/group/DynamicMultiGroupTest.java
index c2ee1ab..ec52636 100644
--- a/core/src/test/java/brooklyn/entity/group/DynamicMultiGroupTest.java
+++ b/core/src/test/java/brooklyn/entity/group/DynamicMultiGroupTest.java
@@ -25,7 +25,9 @@ import static brooklyn.entity.group.DynamicMultiGroup.RESCAN_INTERVAL;
 import static brooklyn.entity.group.DynamicMultiGroupImpl.bucketFromAttribute;
 import static com.google.common.base.Predicates.instanceOf;
 import static com.google.common.collect.Iterables.find;
-import static org.testng.Assert.*;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
 
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
@@ -48,6 +50,7 @@ import brooklyn.test.entity.TestEntity;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
 
 public class DynamicMultiGroupTest {
 
@@ -108,6 +111,41 @@ public class DynamicMultiGroupTest {
         checkDistribution(group, dmg, childSpec, child1, child2);
     }
 
+    @Test
+    public void testRemovesEmptyBuckets() {
+        Group group = app.createAndManageChild(EntitySpec.create(BasicGroup.class));
+        final DynamicMultiGroup dmg = app.createAndManageChild(
+                EntitySpec.create(DynamicMultiGroup.class)
+                        .configure(ENTITY_FILTER, instanceOf(TestEntity.class))
+                        .configure(BUCKET_FUNCTION, bucketFromAttribute(SENSOR))
+        );
+        app.subscribeToChildren(group, SENSOR, new SensorEventListener<String>() {
+            public void onEvent(SensorEvent<String> event) { dmg.rescanEntities();
}
+        });
+
+        EntitySpec<TestEntity> childSpec = EntitySpec.create(TestEntity.class);
+        TestEntity child1 = app.createAndManageChild(EntitySpec.create(childSpec).displayName("child1"));
+        TestEntity child2 = app.createAndManageChild(EntitySpec.create(childSpec).displayName("child2"));
+
+        // Expect two buckets: bucketA and bucketB 
+        child1.setAttribute(SENSOR, "bucketA");
+        child2.setAttribute(SENSOR, "bucketB");
+        dmg.rescanEntities();
+        Group bucketA = (Group) find(dmg.getChildren(), displayNameEqualTo("bucketA"), null);
+        Group bucketB = (Group) find(dmg.getChildren(), displayNameEqualTo("bucketB"), null);
+        assertNotNull(bucketA);
+        assertNotNull(bucketB);
+        
+        // Expect second bucket to be removed when empty 
+        child1.setAttribute(SENSOR, "bucketA");
+        child2.setAttribute(SENSOR, "bucketA");
+        dmg.rescanEntities();
+        bucketA = (Group) find(dmg.getChildren(), displayNameEqualTo("bucketA"), null);
+        bucketB = (Group) find(dmg.getChildren(), displayNameEqualTo("bucketB"), null);
+        assertNotNull(bucketA);
+        assertNull(bucketB);
+    }
+
     private void checkDistribution(final Group group, final DynamicMultiGroup dmg, final
EntitySpec<TestEntity> childSpec, final TestEntity child1, final TestEntity child2)
{
         // Start with both children in bucket A; there is no bucket B
         child1.setAttribute(SENSOR, "bucketA");


Mime
View raw message