You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2015/08/06 18:33:47 UTC

[1/4] incubator-brooklyn git commit: BasicStartableImpl sets expected lifecycle states

Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 56e8c3989 -> 0efc1e351


BasicStartableImpl sets expected lifecycle states


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

Branch: refs/heads/master
Commit: 614cbb27acac4051e8314524751b731d9a7fa60e
Parents: 846143c
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Mon Jul 20 19:25:59 2015 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Thu Aug 6 16:01:55 2015 +0100

----------------------------------------------------------------------
 .../entity/basic/BasicStartableImpl.java        | 58 +++++++++++++-------
 1 file changed, 37 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/614cbb27/core/src/main/java/brooklyn/entity/basic/BasicStartableImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/BasicStartableImpl.java b/core/src/main/java/brooklyn/entity/basic/BasicStartableImpl.java
index 1e42490..0f92141 100644
--- a/core/src/main/java/brooklyn/entity/basic/BasicStartableImpl.java
+++ b/core/src/main/java/brooklyn/entity/basic/BasicStartableImpl.java
@@ -30,6 +30,7 @@ import brooklyn.entity.trait.Startable;
 import brooklyn.entity.trait.StartableMethods;
 import brooklyn.location.Location;
 import brooklyn.management.Task;
+import brooklyn.util.exceptions.Exceptions;
 
 import com.google.common.base.Predicates;
 import com.google.common.collect.Iterables;
@@ -38,38 +39,53 @@ import com.google.common.collect.Lists;
 public class BasicStartableImpl extends AbstractEntity implements BasicStartable {
 
     private static final Logger log = LoggerFactory.getLogger(BasicStartableImpl.class);
-    
-    public BasicStartableImpl() {
-        super();
-    }
 
     @Override
     public void start(Collection<? extends Location> locations) {
         log.info("Starting entity "+this+" at "+locations);
-        addLocations(locations);
-        
-        // essentially does StartableMethods.start(this, locations),
-        // but optionally filters locations for each child
-        
-        brooklyn.location.basic.Locations.LocationsFilter filter = getConfig(LOCATIONS_FILTER);
-        Iterable<Entity> startables = filterStartableManagedEntities(getChildren());
-        if (startables == null || Iterables.isEmpty(startables)) return;
+        try {
+            ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING);
+
+            addLocations(locations);
 
-        List<Task<?>> tasks = Lists.newArrayList();
-        for (final Entity entity : startables) {
-            Collection<? extends Location> l2 = locations;
-            if (filter!=null) {
-                l2 = filter.filterForContext(new ArrayList<Location>(locations), entity);
-                log.debug("Child "+entity+" of "+this+" being started in filtered location list: "+l2);
+            // essentially does StartableMethods.start(this, locations),
+            // but optionally filters locations for each child
+
+            brooklyn.location.basic.Locations.LocationsFilter filter = getConfig(LOCATIONS_FILTER);
+            Iterable<Entity> startables = filterStartableManagedEntities(getChildren());
+            if (!Iterables.isEmpty(startables)) {
+                List<Task<?>> tasks = Lists.newArrayListWithCapacity(Iterables.size(startables));
+                for (final Entity entity : startables) {
+                    Collection<? extends Location> l2 = locations;
+                    if (filter != null) {
+                        l2 = filter.filterForContext(new ArrayList<Location>(locations), entity);
+                        log.debug("Child " + entity + " of " + this + " being started in filtered location list: " + l2);
+                    }
+                    tasks.add(Entities.invokeEffectorWithArgs(this, entity, Startable.START, l2));
+                }
+                for (Task<?> t : tasks) {
+                    t.getUnchecked();
+                }
             }
-            tasks.add( Entities.invokeEffectorWithArgs(this, entity, Startable.START, l2) );
+            setAttribute(Attributes.SERVICE_UP, true);
+            ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING);
+        } catch (Throwable t) {
+            ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
+            throw Exceptions.propagate(t);
         }
-        for (Task<?> t: tasks) t.getUnchecked();
     }
 
     @Override
     public void stop() {
-        StartableMethods.stop(this);
+        ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPING);
+        setAttribute(SERVICE_UP, false);
+        try {
+            StartableMethods.stop(this);
+            ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPED);
+        } catch (Exception e) {
+            ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
+            throw Exceptions.propagate(e);
+        }
     }
 
     @Override


[3/4] incubator-brooklyn git commit: Replace many versions of RecordingSensorEventListener

Posted by al...@apache.org.
Replace many versions of RecordingSensorEventListener


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

Branch: refs/heads/master
Commit: 8439d9fde7077d30e794ede40652e89e15ddbda0
Parents: 379d6a1
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Wed Aug 5 16:04:21 2015 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Thu Aug 6 16:01:57 2015 +0100

----------------------------------------------------------------------
 .../java/brooklyn/enricher/EnrichersTest.java   | 30 +++++++--------
 .../brooklyn/entity/basic/AttributeMapTest.java | 19 ++--------
 .../entity/basic/EntityLocationsTest.java       | 25 ++++++------
 .../entity/basic/EntitySubscriptionTest.java    | 40 ++++++++------------
 .../brooklyn/entity/basic/EntityTypeTest.java   | 35 +++++++++--------
 .../entity/group/DynamicClusterTest.java        |  8 ++--
 .../policy/basic/PolicySubscriptionTest.java    | 23 +++--------
 ...lledDynamicWebAppClusterIntegrationTest.java | 13 +++----
 .../ControlledDynamicWebAppClusterTest.java     | 38 +------------------
 9 files changed, 85 insertions(+), 146 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/8439d9fd/core/src/test/java/brooklyn/enricher/EnrichersTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/enricher/EnrichersTest.java b/core/src/test/java/brooklyn/enricher/EnrichersTest.java
index 20ffab0..b20f722 100644
--- a/core/src/test/java/brooklyn/enricher/EnrichersTest.java
+++ b/core/src/test/java/brooklyn/enricher/EnrichersTest.java
@@ -30,7 +30,7 @@ import brooklyn.entity.BrooklynAppUnitTestSupport;
 import brooklyn.entity.basic.BasicGroup;
 import brooklyn.entity.basic.Entities;
 import brooklyn.entity.basic.EntityAdjuncts;
-import brooklyn.entity.basic.EntitySubscriptionTest.RecordingSensorEventListener;
+import org.apache.brooklyn.entity.basic.RecordingSensorEventListener;
 import brooklyn.entity.proxying.EntitySpec;
 import brooklyn.event.AttributeSensor;
 import brooklyn.event.SensorEvent;
@@ -190,7 +190,7 @@ public class EnrichersTest extends BrooklynAppUnitTestSupport {
 
     @Test(groups="Integration") // because takes a second
     public void testTransformingRespectsUnchangedButWillRepublish() {
-        RecordingSensorEventListener record = new RecordingSensorEventListener();
+        RecordingSensorEventListener<String> record = new RecordingSensorEventListener<>();
         app.getManagementContext().getSubscriptionManager().subscribe(entity, STR2, record);
         
         entity.addEnricher(Enrichers.builder()
@@ -201,29 +201,29 @@ public class EnrichersTest extends BrooklynAppUnitTestSupport {
                             return ("ignoredval".equals(input)) ? Entities.UNCHANGED : input;
                         }})
                 .build());
-        Asserts.assertThat(record.events, CollectionFunctionals.sizeEquals(0));
-        
+        Asserts.assertThat(record.getEvents(), CollectionFunctionals.sizeEquals(0));
+
         entity.setAttribute(STR1, "myval");
-        Asserts.eventually(Suppliers.ofInstance(record.events), CollectionFunctionals.sizeEquals(1));
+        Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(1));
         EntityTestUtils.assertAttributeEquals(entity, STR2, "myval");
-        
+
         entity.setAttribute(STR1, "ignoredval");
         EntityTestUtils.assertAttributeEqualsContinually(entity, STR2, "myval");
-        
+
         entity.setAttribute(STR1, "myval2");
-        Asserts.eventually(Suppliers.ofInstance(record.events), CollectionFunctionals.sizeEquals(2));
+        Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(2));
         EntityTestUtils.assertAttributeEquals(entity, STR2, "myval2");
-        
+
         entity.setAttribute(STR1, "myval2");
         entity.setAttribute(STR1, "myval2");
         entity.setAttribute(STR1, "myval3");
-        Asserts.eventually(Suppliers.ofInstance(record.events), CollectionFunctionals.sizeEquals(5));
+        Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(5));
     }
 
     public void testTransformingSuppressDuplicates() {
-        RecordingSensorEventListener record = new RecordingSensorEventListener();
+        RecordingSensorEventListener<String> record = new RecordingSensorEventListener<>();
         app.getManagementContext().getSubscriptionManager().subscribe(entity, STR2, record);
-        
+
         entity.addEnricher(Enrichers.builder()
                 .transforming(STR1)
                 .publishing(STR2)
@@ -232,14 +232,14 @@ public class EnrichersTest extends BrooklynAppUnitTestSupport {
                 .build());
 
         entity.setAttribute(STR1, "myval");
-        Asserts.eventually(Suppliers.ofInstance(record.events), CollectionFunctionals.sizeEquals(1));
+        Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(1));
         EntityTestUtils.assertAttributeEquals(entity, STR2, "myval");
-        
+
         entity.setAttribute(STR1, "myval2");
         entity.setAttribute(STR1, "myval2");
         entity.setAttribute(STR1, "myval3");
         EntityTestUtils.assertAttributeEqualsContinually(entity, STR2, "myval3");
-        Asserts.assertThat(record.events, CollectionFunctionals.sizeEquals(3));
+        Asserts.assertThat(record.getEvents(), CollectionFunctionals.sizeEquals(3));
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/8439d9fd/core/src/test/java/brooklyn/entity/basic/AttributeMapTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/AttributeMapTest.java b/core/src/test/java/brooklyn/entity/basic/AttributeMapTest.java
index bdbd25e..6e95706 100644
--- a/core/src/test/java/brooklyn/entity/basic/AttributeMapTest.java
+++ b/core/src/test/java/brooklyn/entity/basic/AttributeMapTest.java
@@ -29,14 +29,13 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 
+import org.apache.brooklyn.entity.basic.RecordingSensorEventListener;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import brooklyn.entity.Application;
 import brooklyn.event.AttributeSensor;
-import brooklyn.event.SensorEvent;
-import brooklyn.event.SensorEventListener;
 import brooklyn.event.basic.AttributeMap;
 import brooklyn.event.basic.Sensors;
 import brooklyn.test.Asserts;
@@ -48,6 +47,7 @@ import brooklyn.util.guava.Maybe;
 import com.google.common.base.Function;
 import com.google.common.base.Functions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 
 public class AttributeMapTest {
@@ -189,14 +189,14 @@ public class AttributeMapTest {
         AttributeSensor<Integer> sensor = Sensors.newIntegerSensor("a", "");
         AttributeSensor<Integer> childSensor = Sensors.newIntegerSensor("a.b", "");
         
-        final RecordingSensorEventListener listener = new RecordingSensorEventListener();
+        final RecordingSensorEventListener<Object> listener = new RecordingSensorEventListener<>();
         entity.subscribe(entity, sensor, listener);
         
         map.modify(childSensor, Functions.constant(Maybe.<Integer>absent()));
         
         Asserts.succeedsContinually(new Runnable() {
             @Override public void run() {
-                assertTrue(listener.getEvents().isEmpty(), "events="+listener.getEvents());
+                assertTrue(Iterables.isEmpty(listener.getEvents()), "events="+listener.getEvents());
             }});
     }
     
@@ -223,16 +223,5 @@ public class AttributeMapTest {
             }
         };
     }
-    
-    public static class RecordingSensorEventListener implements SensorEventListener<Object> {
-        private List<SensorEvent<Object>> events = Collections.synchronizedList(Lists.<SensorEvent<Object>>newArrayList());
 
-        @Override public void onEvent(SensorEvent<Object> event) {
-            events.add(event);
-        }
-        
-        public List<SensorEvent<Object>> getEvents() {
-            return ImmutableList.copyOf(events);
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/8439d9fd/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 5207256..074ff0b 100644
--- a/core/src/test/java/brooklyn/entity/basic/EntityLocationsTest.java
+++ b/core/src/test/java/brooklyn/entity/basic/EntityLocationsTest.java
@@ -23,15 +23,17 @@ import static org.testng.Assert.assertEquals;
 import java.util.Arrays;
 import java.util.List;
 
+import org.apache.brooklyn.entity.basic.RecordingSensorEventListener;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
 import brooklyn.entity.BrooklynAppUnitTestSupport;
-import brooklyn.entity.basic.EntitySubscriptionTest.RecordingSensorEventListener;
+import brooklyn.event.SensorEvent;
 import brooklyn.location.Location;
 import brooklyn.test.Asserts;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
 
 public class EntityLocationsTest extends BrooklynAppUnitTestSupport {
 
@@ -48,8 +50,8 @@ public class EntityLocationsTest extends BrooklynAppUnitTestSupport {
         final Location l = app.newSimulatedLocation();
         final Location l2 = app.newSimulatedLocation();
         
-        final RecordingSensorEventListener addedEvents = new RecordingSensorEventListener();
-        final RecordingSensorEventListener removedEvents = new RecordingSensorEventListener();
+        final RecordingSensorEventListener<Object> addedEvents = new RecordingSensorEventListener<>();
+        final RecordingSensorEventListener<Object> removedEvents = new RecordingSensorEventListener<>();
         app.subscribe(app, AbstractEntity.LOCATION_ADDED, addedEvents);
         app.subscribe(app, AbstractEntity.LOCATION_REMOVED, removedEvents);
 
@@ -82,8 +84,8 @@ public class EntityLocationsTest extends BrooklynAppUnitTestSupport {
     public void testNotNotifiedDuplicateAddedLocations() throws Exception {
         final Location l = app.newSimulatedLocation();
         
-        final RecordingSensorEventListener addedEvents = new RecordingSensorEventListener();
-        final RecordingSensorEventListener removedEvents = new RecordingSensorEventListener();
+        final RecordingSensorEventListener<Object> addedEvents = new RecordingSensorEventListener<>();
+        final RecordingSensorEventListener<Object> removedEvents = new RecordingSensorEventListener<>();
         app.subscribe(app, AbstractEntity.LOCATION_ADDED, addedEvents);
         app.subscribe(app, AbstractEntity.LOCATION_REMOVED, removedEvents);
 
@@ -100,25 +102,26 @@ public class EntityLocationsTest extends BrooklynAppUnitTestSupport {
         assertEventValuesEquals(removedEvents, ImmutableList.of());
     }
     
-    private void assertEventValuesEqualsEventually(final RecordingSensorEventListener listener, final List<?> expectedVals) {
+    private void assertEventValuesEqualsEventually(final RecordingSensorEventListener<Object> listener, final List<?> expectedVals) {
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
                 assertEventValuesEquals(listener, expectedVals);
             }});
     }
     
-    private void assertEventValuesEqualsContinually(final RecordingSensorEventListener listener, final List<?> expectedVals) {
+    private void assertEventValuesEqualsContinually(final RecordingSensorEventListener<Object> 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);
+
+    private void assertEventValuesEquals(final RecordingSensorEventListener<Object> listener, final List<?> expectedVals) {
+        Iterable<SensorEvent<Object>> events = listener.getEvents();
+        assertEquals(Iterables.size(events), expectedVals.size(), "events=" + events);
         for (int i = 0; i < expectedVals.size(); i++) {
             Object expectedVal = expectedVals.get(i);
-            assertEquals(listener.events.get(i).getValue(), expectedVal, "events="+listener.events);
+            assertEquals(Iterables.get(events, i).getValue(), expectedVal, "events=" + events);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/8439d9fd/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.java b/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.java
index fd648d4..42d705c 100644
--- a/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.java
+++ b/core/src/test/java/brooklyn/entity/basic/EntitySubscriptionTest.java
@@ -20,16 +20,12 @@ package brooklyn.entity.basic;
 
 import static org.testng.Assert.assertEquals;
 
-import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
-
+import org.apache.brooklyn.entity.basic.RecordingSensorEventListener;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import brooklyn.entity.proxying.EntitySpec;
-import brooklyn.event.SensorEvent;
-import brooklyn.event.SensorEventListener;
 import brooklyn.event.basic.BasicSensorEvent;
 import brooklyn.location.basic.SimulatedLocation;
 import brooklyn.management.SubscriptionHandle;
@@ -38,6 +34,7 @@ import brooklyn.test.entity.TestApplication;
 import brooklyn.test.entity.TestEntity;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
 
 public class EntitySubscriptionTest {
 
@@ -51,7 +48,7 @@ public class EntitySubscriptionTest {
     private TestEntity observedChildEntity;
     private TestEntity observedMemberEntity;
     private TestEntity otherEntity;
-    private RecordingSensorEventListener listener;
+    private RecordingSensorEventListener<Object> listener;
     
     @BeforeMethod(alwaysRun=true)
     public void setUp() {
@@ -67,7 +64,7 @@ public class EntitySubscriptionTest {
         
         otherEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
         
-        listener = new RecordingSensorEventListener();
+        listener = new RecordingSensorEventListener<>();
         
         app.start(ImmutableList.of(loc));
     }
@@ -90,7 +87,7 @@ public class EntitySubscriptionTest {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedEntity, 123),
                         new BasicSensorEvent<String>(TestEntity.NAME, observedEntity, "myname"),
                         new BasicSensorEvent<Integer>(TestEntity.MY_NOTIF, observedEntity, 456)));
@@ -106,7 +103,7 @@ public class EntitySubscriptionTest {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedEntity, 123),
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456)));
             }});
@@ -121,7 +118,7 @@ public class EntitySubscriptionTest {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedChildEntity, 123)));
             }});
     }
@@ -135,7 +132,7 @@ public class EntitySubscriptionTest {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedChildEntity2, 123)));
             }});
     }
@@ -149,7 +146,7 @@ public class EntitySubscriptionTest {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedMemberEntity, 123)));
             }});
     }
@@ -164,7 +161,7 @@ public class EntitySubscriptionTest {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedMemberEntity2, 123)));
             }});
     }
@@ -179,7 +176,7 @@ public class EntitySubscriptionTest {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of());
+                assertEquals(listener.getEvents(), ImmutableList.of());
             }});
     }
     
@@ -198,7 +195,7 @@ public class EntitySubscriptionTest {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456)));
             }});
     }
@@ -217,7 +214,7 @@ public class EntitySubscriptionTest {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, observedEntity, 123),
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456)));
             }});
@@ -234,18 +231,11 @@ public class EntitySubscriptionTest {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events.size(), NUM_EVENTS);
+                assertEquals(Iterables.size(listener.getEvents()), NUM_EVENTS);
                 for (int i = 0; i < NUM_EVENTS; i++) {
-                    assertEquals(listener.events.get(i).getValue(), i);
+                    assertEquals(Iterables.get(listener.getEvents(), i).getValue(), i);
                 }
             }});
     }
 
-    public static class RecordingSensorEventListener implements SensorEventListener<Object> {
-        public final List<SensorEvent<?>> events = new CopyOnWriteArrayList<SensorEvent<?>>();
-        
-        @Override public void onEvent(SensorEvent<Object> event) {
-            events.add(event);
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/8439d9fd/core/src/test/java/brooklyn/entity/basic/EntityTypeTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/EntityTypeTest.java b/core/src/test/java/brooklyn/entity/basic/EntityTypeTest.java
index 67113fe..7035ccd 100644
--- a/core/src/test/java/brooklyn/entity/basic/EntityTypeTest.java
+++ b/core/src/test/java/brooklyn/entity/basic/EntityTypeTest.java
@@ -40,6 +40,7 @@ import java.util.Set;
 
 import javax.annotation.Nullable;
 
+import org.apache.brooklyn.entity.basic.RecordingSensorEventListener;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
@@ -54,12 +55,9 @@ import brooklyn.event.basic.Sensors;
 import brooklyn.test.Asserts;
 import brooklyn.test.entity.TestEntity;
 import brooklyn.test.entity.TestEntityImpl;
-import brooklyn.util.collections.CollectionFunctionals;
 import brooklyn.util.collections.MutableSet;
 
 import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
-import com.google.common.base.Suppliers;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
@@ -67,8 +65,8 @@ import com.google.common.collect.Iterables;
 public class EntityTypeTest extends BrooklynAppUnitTestSupport {
     private static final AttributeSensor<String> TEST_SENSOR = Sensors.newStringSensor("test.sensor");
     private EntityInternal entity;
-    private EntitySubscriptionTest.RecordingSensorEventListener listener;
-    
+    private RecordingSensorEventListener<Sensor> listener;
+
     public final static Set<Sensor<?>> DEFAULT_SENSORS = ImmutableSet.<Sensor<?>>of(
             SENSOR_ADDED, SENSOR_REMOVED,
             EFFECTOR_ADDED, EFFECTOR_REMOVED, EFFECTOR_CHANGED,
@@ -83,7 +81,7 @@ public class EntityTypeTest extends BrooklynAppUnitTestSupport {
     public void setUp() throws Exception{
         super.setUp();
         entity = (EntityInternal) app.createAndManageChild(EntitySpec.create(Entity.class, EmptyEntityForTesting.class));
-        listener = new EntitySubscriptionTest.RecordingSensorEventListener();
+        listener = new RecordingSensorEventListener<>();
         app.getSubscriptionContext().subscribe(entity, SENSOR_ADDED, listener);
         app.getSubscriptionContext().subscribe(entity, SENSOR_REMOVED, listener);
     }
@@ -172,11 +170,14 @@ public class EntityTypeTest extends BrooklynAppUnitTestSupport {
         assertEquals(entity.getEntityType().getSensors(), DEFAULT_SENSORS);
     }
 
-    @SuppressWarnings("unchecked")
-    protected <T> void assertEventuallyListenerEventsEqual(List<T> sensorEvents) {
-        Asserts.eventually(
-            Suppliers.ofInstance((List<T>)listener.events), 
-            Predicates.equalTo(sensorEvents));
+    protected <T> void assertEventuallyListenerEventsEqual(final List<T> sensorEvents) {
+        final RecordingSensorEventListener listener = this.listener;
+        Asserts.succeedsEventually(new Runnable() {
+            @Override
+            public void run() {
+                assertEquals(listener.getEvents(), sensorEvents);
+            }
+        });
     }
     
     @Test
@@ -224,10 +225,14 @@ public class EntityTypeTest extends BrooklynAppUnitTestSupport {
         entity.getMutableEntityType().removeSensor(POLICY_ADDED.getName());
         assertEquals(entity.getEntityType().getSensors(), 
                 MutableSet.builder().addAll(DEFAULT_SENSORS).remove(SENSOR_ADDED).remove(POLICY_ADDED).build().asUnmodifiable());
-        
-        Asserts.eventually(
-                CollectionFunctionals.sizeSupplier(listener.events), 
-                Predicates.equalTo(2));
+
+        final RecordingSensorEventListener<?> listener = this.listener;
+        Asserts.succeedsEventually(new Runnable() {
+            @Override
+            public void run() {
+                assertEquals(Iterables.size(listener.getEvents()), 2);
+            }
+        });
         assertEventuallyListenerEventsEqual(ImmutableList.of(
             BasicSensorEvent.ofUnchecked(SENSOR_REMOVED, entity, SENSOR_ADDED),
             BasicSensorEvent.ofUnchecked(SENSOR_REMOVED, entity, POLICY_ADDED)));

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/8439d9fd/core/src/test/java/brooklyn/entity/group/DynamicClusterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/group/DynamicClusterTest.java b/core/src/test/java/brooklyn/entity/group/DynamicClusterTest.java
index 1d144a0..b8a1839 100644
--- a/core/src/test/java/brooklyn/entity/group/DynamicClusterTest.java
+++ b/core/src/test/java/brooklyn/entity/group/DynamicClusterTest.java
@@ -50,8 +50,8 @@ import brooklyn.entity.basic.BasicEntity;
 import brooklyn.entity.basic.BrooklynTaskTags;
 import brooklyn.entity.basic.Entities;
 import brooklyn.entity.basic.EntityFactory;
-import brooklyn.entity.basic.EntitySubscriptionTest.RecordingSensorEventListener;
 import brooklyn.entity.basic.Lifecycle;
+import org.apache.brooklyn.entity.basic.RecordingSensorEventListener;
 import brooklyn.entity.basic.ServiceStateLogic;
 import brooklyn.entity.proxying.EntitySpec;
 import brooklyn.entity.trait.Changeable;
@@ -212,14 +212,14 @@ public class DynamicClusterTest extends BrooklynAppUnitTestSupport {
             .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(TestEntity.class))
             .configure(DynamicCluster.INITIAL_SIZE, 1));
         
-        RecordingSensorEventListener r = new RecordingSensorEventListener();
+        RecordingSensorEventListener<Lifecycle> r = new RecordingSensorEventListener<>();
         app.subscribe(cluster, Attributes.SERVICE_STATE_ACTUAL, r);
 
         cluster.start(ImmutableList.of(loc));
         EntityTestUtils.assertAttributeEqualsEventually(cluster, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING);
-        for (SensorEvent<?> evt: r.events) {
+        for (SensorEvent<Lifecycle> evt: r.getEvents()) {
             if (evt.getValue()==Lifecycle.ON_FIRE)
-                Assert.fail("Should not have published "+Lifecycle.ON_FIRE+" during normal start up: "+r.events);
+                Assert.fail("Should not have published " + Lifecycle.ON_FIRE + " during normal start up: " + r.getEvents());
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/8439d9fd/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.java b/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.java
index 41a0736..ed2bc47 100644
--- a/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.java
+++ b/core/src/test/java/brooklyn/policy/basic/PolicySubscriptionTest.java
@@ -20,16 +20,12 @@ package brooklyn.policy.basic;
 
 import static org.testng.Assert.assertEquals;
 
-import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
-
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import brooklyn.entity.BrooklynAppUnitTestSupport;
+import org.apache.brooklyn.entity.basic.RecordingSensorEventListener;
 import brooklyn.entity.proxying.EntitySpec;
-import brooklyn.event.SensorEvent;
-import brooklyn.event.SensorEventListener;
 import brooklyn.event.basic.BasicSensorEvent;
 import brooklyn.location.basic.SimulatedLocation;
 import brooklyn.management.SubscriptionHandle;
@@ -48,7 +44,7 @@ public class PolicySubscriptionTest extends BrooklynAppUnitTestSupport {
     private TestEntity entity;
     private TestEntity otherEntity;
     private AbstractPolicy policy;
-    private RecordingSensorEventListener listener;
+    private RecordingSensorEventListener<Object> listener;
     
     @BeforeMethod(alwaysRun=true)
     @Override
@@ -57,7 +53,7 @@ public class PolicySubscriptionTest extends BrooklynAppUnitTestSupport {
         loc = app.newSimulatedLocation();
         entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
         otherEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-        listener = new RecordingSensorEventListener();
+        listener = new RecordingSensorEventListener<>();
         policy = new AbstractPolicy() {};
         entity.addPolicy(policy);
         app.start(ImmutableList.of(loc));
@@ -76,7 +72,7 @@ public class PolicySubscriptionTest extends BrooklynAppUnitTestSupport {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, entity, 123),
                         new BasicSensorEvent<String>(TestEntity.NAME, entity, "myname"),
                         new BasicSensorEvent<Integer>(TestEntity.MY_NOTIF, entity, 789)));
@@ -99,7 +95,7 @@ public class PolicySubscriptionTest extends BrooklynAppUnitTestSupport {
         Thread.sleep(SHORT_WAIT_MS);
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 789)));
             }});
     }
@@ -118,17 +114,10 @@ public class PolicySubscriptionTest extends BrooklynAppUnitTestSupport {
         
         Asserts.succeedsEventually(new Runnable() {
             @Override public void run() {
-                assertEquals(listener.events, ImmutableList.of(
+                assertEquals(listener.getEvents(), ImmutableList.of(
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, entity, 123),
                         new BasicSensorEvent<Integer>(TestEntity.SEQUENCE, otherEntity, 456)));
             }});
     }
     
-    private static class RecordingSensorEventListener implements SensorEventListener<Object> {
-        final List<SensorEvent<?>> events = new CopyOnWriteArrayList<SensorEvent<?>>();
-        
-        @Override public void onEvent(SensorEvent<Object> event) {
-            events.add(event);
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/8439d9fd/software/webapp/src/test/java/brooklyn/entity/webapp/ControlledDynamicWebAppClusterIntegrationTest.java
----------------------------------------------------------------------
diff --git a/software/webapp/src/test/java/brooklyn/entity/webapp/ControlledDynamicWebAppClusterIntegrationTest.java b/software/webapp/src/test/java/brooklyn/entity/webapp/ControlledDynamicWebAppClusterIntegrationTest.java
index 44212d8..7f9a18f 100644
--- a/software/webapp/src/test/java/brooklyn/entity/webapp/ControlledDynamicWebAppClusterIntegrationTest.java
+++ b/software/webapp/src/test/java/brooklyn/entity/webapp/ControlledDynamicWebAppClusterIntegrationTest.java
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.concurrent.Callable;
 
 import org.apache.brooklyn.test.TestResourceUnavailableException;
+import org.apache.brooklyn.entity.basic.RecordingSensorEventListener;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -39,7 +40,6 @@ import brooklyn.entity.proxy.AbstractController;
 import brooklyn.entity.proxy.LoadBalancer;
 import brooklyn.entity.proxy.nginx.NginxController;
 import brooklyn.entity.proxying.EntitySpec;
-import brooklyn.entity.webapp.ControlledDynamicWebAppClusterTest.RecordingSensorEventListener;
 import brooklyn.entity.webapp.tomcat.TomcatServer;
 import brooklyn.location.basic.LocalhostMachineProvisioningLocation;
 import brooklyn.test.Asserts;
@@ -49,7 +49,6 @@ import brooklyn.test.entity.TestJavaWebAppEntity;
 import brooklyn.util.collections.CollectionFunctionals;
 import brooklyn.util.collections.MutableMap;
 
-import com.google.common.base.Predicates;
 import com.google.common.base.Suppliers;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
@@ -146,14 +145,14 @@ public class ControlledDynamicWebAppClusterIntegrationTest extends BrooklynAppLi
         app.subscribe(cluster, Attributes.SERVICE_STATE_ACTUAL, listener);
         app.start(locs);
         
-        Asserts.eventually(Suppliers.ofInstance(listener.getValues()), CollectionFunctionals.sizeEquals(2));
-        assertEquals(listener.getValues(), ImmutableList.of(Lifecycle.STARTING, Lifecycle.RUNNING), "vals="+listener.getValues());
-        listener.getValues().clear();
+        Asserts.eventually(Suppliers.ofInstance(listener.getEventValues()), CollectionFunctionals.sizeEquals(2));
+        assertEquals(listener.getEventValues(), ImmutableList.of(Lifecycle.STARTING, Lifecycle.RUNNING), "vals="+listener.getEventValues());
+        listener.clearEvents();
         
         app.stop();
         EntityTestUtils.assertAttributeEqualsEventually(cluster, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.STOPPED);
-        Asserts.eventually(Suppliers.ofInstance(listener.getValues()), CollectionFunctionals.sizeEquals(2));
-        assertEquals(listener.getValues(), ImmutableList.of(Lifecycle.STOPPING, Lifecycle.STOPPED), "vals="+listener.getValues());
+        Asserts.eventually(Suppliers.ofInstance(listener), CollectionFunctionals.sizeEquals(2));
+        assertEquals(listener.getEventValues(), ImmutableList.of(Lifecycle.STOPPING, Lifecycle.STOPPED), "vals="+listener.getEventValues());
     }
     
     @Test(groups="Integration")

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/8439d9fd/software/webapp/src/test/java/brooklyn/entity/webapp/ControlledDynamicWebAppClusterTest.java
----------------------------------------------------------------------
diff --git a/software/webapp/src/test/java/brooklyn/entity/webapp/ControlledDynamicWebAppClusterTest.java b/software/webapp/src/test/java/brooklyn/entity/webapp/ControlledDynamicWebAppClusterTest.java
index 66bacc6..7a361f2 100644
--- a/software/webapp/src/test/java/brooklyn/entity/webapp/ControlledDynamicWebAppClusterTest.java
+++ b/software/webapp/src/test/java/brooklyn/entity/webapp/ControlledDynamicWebAppClusterTest.java
@@ -20,7 +20,6 @@ package brooklyn.entity.webapp;
 
 import static org.testng.Assert.assertEquals;
 
-import java.net.URL;
 import java.util.List;
 
 import org.apache.brooklyn.test.TestResourceUnavailableException;
@@ -38,8 +37,6 @@ import brooklyn.entity.proxy.LoadBalancer;
 import brooklyn.entity.proxy.TrackingAbstractController;
 import brooklyn.entity.proxying.EntitySpec;
 import brooklyn.entity.webapp.jboss.JBoss7Server;
-import brooklyn.event.SensorEvent;
-import brooklyn.event.SensorEventListener;
 import brooklyn.location.basic.LocalhostMachineProvisioningLocation;
 import brooklyn.test.Asserts;
 import brooklyn.test.EntityTestUtils;
@@ -47,7 +44,6 @@ import brooklyn.test.entity.TestJavaWebAppEntity;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
 
 public class ControlledDynamicWebAppClusterTest extends BrooklynAppUnitTestSupport {
     private static final Logger log = LoggerFactory.getLogger(ControlledDynamicWebAppClusterTest.class);
@@ -138,39 +134,7 @@ public class ControlledDynamicWebAppClusterTest extends BrooklynAppUnitTestSuppo
 
         assertEquals(cluster.getCluster().getDisplayName(), "mydisplayname");
     }
-    
-    public static class RecordingSensorEventListener<T> implements SensorEventListener<T> {
-        private final List<SensorEvent<T>> events = Lists.newCopyOnWriteArrayList();
-        private final List<T> values = Lists.newCopyOnWriteArrayList();
-        private boolean skipDuplicateValues;
-
-        public RecordingSensorEventListener() {
-            this(false);
-        }
-        
-        public RecordingSensorEventListener(boolean skipDuplicateValues) {
-            this.skipDuplicateValues = skipDuplicateValues;
-        }
-        
-        @Override
-        public void onEvent(SensorEvent<T> event) {
-            events.add(event);
-            if (skipDuplicateValues && !values.isEmpty() && values.get(values.size()-1).equals(event.getValue())) {
-                // skip
-            } else {
-                values.add(event.getValue());
-            }
-        }
-        
-        public List<SensorEvent<T>> getEvents() {
-            return events;
-        }
-        
-        public List<T> getValues() {
-            return values;
-        }
-    }
-    
+
     @Test
     public void testMembersReflectChildClusterMembers() {
         final ControlledDynamicWebAppCluster cluster = app.createAndManageChild(EntitySpec.create(ControlledDynamicWebAppCluster.class)


[2/4] incubator-brooklyn git commit: Test lifecycle phases of BasicStartable

Posted by al...@apache.org.
Test lifecycle phases of BasicStartable


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

Branch: refs/heads/master
Commit: 379d6a1d5908dd25cccaf82e853ff9d7693734ed
Parents: 614cbb2
Author: Sam Corbett <sa...@cloudsoftcorp.com>
Authored: Thu Aug 6 16:01:36 2015 +0100
Committer: Sam Corbett <sa...@cloudsoftcorp.com>
Committed: Thu Aug 6 16:01:57 2015 +0100

----------------------------------------------------------------------
 .../entity/basic/ServiceStateLogic.java         |   7 +-
 .../entity/basic/BasicStartableTest.java        |  33 +++++-
 .../basic/RecordingSensorEventListener.java     | 115 +++++++++++++++++++
 3 files changed, 148 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/379d6a1d/core/src/main/java/brooklyn/entity/basic/ServiceStateLogic.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/entity/basic/ServiceStateLogic.java b/core/src/main/java/brooklyn/entity/basic/ServiceStateLogic.java
index eff8e2d..3b1efbe 100644
--- a/core/src/main/java/brooklyn/entity/basic/ServiceStateLogic.java
+++ b/core/src/main/java/brooklyn/entity/basic/ServiceStateLogic.java
@@ -149,8 +149,11 @@ public class ServiceStateLogic {
             if (!Boolean.TRUE.equals(up) && !Boolean.TRUE.equals(Entities.isReadOnly(entity))) {
                 // pause briefly to allow any recent problem-clearing processing to complete
                 Stopwatch timer = Stopwatch.createStarted();
-                boolean nowUp = Repeater.create().every(ValueResolver.REAL_QUICK_PERIOD).limitTimeTo(ValueResolver.PRETTY_QUICK_WAIT).until(entity, 
-                    EntityPredicates.attributeEqualTo(Attributes.SERVICE_UP, true)).run();
+                boolean nowUp = Repeater.create()
+                        .every(ValueResolver.REAL_QUICK_PERIOD)
+                        .limitTimeTo(ValueResolver.PRETTY_QUICK_WAIT)
+                        .until(entity, EntityPredicates.attributeEqualTo(Attributes.SERVICE_UP, true))
+                        .run();
                 if (nowUp) {
                     log.debug("Had to wait "+Duration.of(timer)+" for "+entity+" "+Attributes.SERVICE_UP+" to be true before setting "+state);
                 } else {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/379d6a1d/core/src/test/java/brooklyn/entity/basic/BasicStartableTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/basic/BasicStartableTest.java b/core/src/test/java/brooklyn/entity/basic/BasicStartableTest.java
index b7d47e5..2f8db0c 100644
--- a/core/src/test/java/brooklyn/entity/basic/BasicStartableTest.java
+++ b/core/src/test/java/brooklyn/entity/basic/BasicStartableTest.java
@@ -21,13 +21,20 @@ package brooklyn.entity.basic;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicReference;
 
+import org.apache.brooklyn.entity.basic.RecordingSensorEventListener;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+
 import brooklyn.entity.Entity;
 import brooklyn.entity.proxying.EntitySpec;
 import brooklyn.location.Location;
@@ -40,11 +47,6 @@ import brooklyn.test.entity.TestApplication;
 import brooklyn.test.entity.TestEntity;
 import brooklyn.util.collections.MutableSet;
 
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
-
 public class BasicStartableTest {
 
     private ManagementContext managementContext;
@@ -141,6 +143,27 @@ public class BasicStartableTest {
         assertEqualsIgnoringOrder(entity.getLocations(), ImmutableSet.of());
         assertNull(called.get());
     }
+
+    @Test
+    public void testTransitionsThroughLifecycles() throws Exception {
+        startable = app.addChild(EntitySpec.create(BasicStartable.class));
+        RecordingSensorEventListener<Lifecycle> listener = new RecordingSensorEventListener<Lifecycle>(true);
+        managementContext.getSubscriptionContext(startable)
+                .subscribe(startable, Attributes.SERVICE_STATE_ACTUAL, listener);
+
+        Entities.startManagement(startable);
+        app.start(ImmutableList.of(loc1));
+        app.stop();
+
+        ArrayList<Lifecycle> expected = Lists.newArrayList(
+                Lifecycle.STARTING,
+                Lifecycle.RUNNING,
+                Lifecycle.STOPPING,
+                Lifecycle.STOPPED);
+        Iterable<Lifecycle> actual = listener.getEventValuesSortedByTimestamp();
+        assertEquals(actual, expected,
+                "Expected=" + Iterables.toString(expected) + ", actual=" + Iterables.toString(actual));
+    }
     
     private void assertEqualsIgnoringOrder(Iterable<? extends Object> col1, Iterable<? extends Object> col2) {
         assertEquals(Iterables.size(col1), Iterables.size(col2), "col2="+col1+"; col2="+col2);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/379d6a1d/core/src/test/java/org/apache/brooklyn/entity/basic/RecordingSensorEventListener.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/entity/basic/RecordingSensorEventListener.java b/core/src/test/java/org/apache/brooklyn/entity/basic/RecordingSensorEventListener.java
new file mode 100644
index 0000000..067b7d4
--- /dev/null
+++ b/core/src/test/java/org/apache/brooklyn/entity/basic/RecordingSensorEventListener.java
@@ -0,0 +1,115 @@
+/*
+ * 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.brooklyn.entity.basic;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+
+import com.google.common.base.Function;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.common.primitives.Longs;
+
+import brooklyn.event.SensorEvent;
+import brooklyn.event.SensorEventListener;
+
+/**
+ * An event listener that records each event and allows callers to access all values and
+ * all values sorted by event timestamp.
+ */
+public class RecordingSensorEventListener<T> implements SensorEventListener<T>, Iterable<SensorEvent<T>> {
+
+    private final List<SensorEvent<T>> events = Lists.newCopyOnWriteArrayList();
+    private final boolean suppressDuplicates;
+    private T lastValue;
+
+    public RecordingSensorEventListener() {
+        this(false);
+    }
+
+    public RecordingSensorEventListener(boolean suppressDuplicates) {
+        this.suppressDuplicates = suppressDuplicates;
+    }
+
+    @Override
+    public void onEvent(SensorEvent<T> event) {
+        if (!suppressDuplicates || events.isEmpty() || !Objects.equals(lastValue, event.getValue())) {
+            events.add(event);
+            lastValue = event.getValue();
+        }
+    }
+
+    /**
+     * @return An immutable iterable of the recorded events.
+     */
+    public Iterable<SensorEvent<T>> getEvents() {
+        return ImmutableList.copyOf(events);
+    }
+
+    /**
+     * @return A live read-only view of recorded events.
+     */
+    public Iterable<T> getEventValues() {
+        return FluentIterable.from(events)
+                .transform(new GetValueFunction<T>());
+    }
+
+    /**
+     * @return A static read-only view of event values sorted by the time at which they occurred.
+     */
+    public Iterable<T> getEventValuesSortedByTimestamp() {
+        List<SensorEvent<T>> copy = Lists.newArrayList(events);
+        Collections.sort(copy, new EventTimestampComparator());
+        return FluentIterable.from(copy)
+                .transform(new GetValueFunction<T>());
+    }
+
+    /**
+     * Clears all events recorded by the listener.
+     */
+    public void clearEvents() {
+        this.events.clear();
+        lastValue = null;
+    }
+
+    @Override
+    public Iterator<SensorEvent<T>> iterator() {
+        return getEvents().iterator();
+    }
+
+    private static class GetValueFunction<T> implements Function<SensorEvent<T>, T> {
+        @Override
+        public T apply(SensorEvent<T> input) {
+            return input.getValue();
+        }
+    }
+
+    private static class EventTimestampComparator implements Comparator<SensorEvent<?>> {
+        @Override
+        public int compare(SensorEvent<?> o1, SensorEvent<?> o2) {
+            return Longs.compare(o1.getTimestamp(), o2.getTimestamp());
+        }
+    }
+
+}


[4/4] incubator-brooklyn git commit: This closes #798

Posted by al...@apache.org.
This closes #798


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

Branch: refs/heads/master
Commit: 0efc1e35103375a702b22df46561777e4d8db326
Parents: 56e8c39 8439d9f
Author: Aled Sage <al...@gmail.com>
Authored: Thu Aug 6 17:33:33 2015 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Thu Aug 6 17:33:33 2015 +0100

----------------------------------------------------------------------
 .../entity/basic/BasicStartableImpl.java        |  58 ++++++----
 .../entity/basic/ServiceStateLogic.java         |   7 +-
 .../java/brooklyn/enricher/EnrichersTest.java   |  30 ++---
 .../brooklyn/entity/basic/AttributeMapTest.java |  19 +--
 .../entity/basic/BasicStartableTest.java        |  33 +++++-
 .../entity/basic/EntityLocationsTest.java       |  25 ++--
 .../entity/basic/EntitySubscriptionTest.java    |  40 +++----
 .../brooklyn/entity/basic/EntityTypeTest.java   |  35 +++---
 .../entity/group/DynamicClusterTest.java        |   8 +-
 .../policy/basic/PolicySubscriptionTest.java    |  23 +---
 .../basic/RecordingSensorEventListener.java     | 115 +++++++++++++++++++
 ...lledDynamicWebAppClusterIntegrationTest.java |  13 +--
 .../ControlledDynamicWebAppClusterTest.java     |  38 +-----
 13 files changed, 270 insertions(+), 174 deletions(-)
----------------------------------------------------------------------