brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rich...@apache.org
Subject [1/8] git commit: Policy rebind tests
Date Tue, 27 May 2014 21:35:13 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 22bb13113 -> ea88352da


Policy rebind tests


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

Branch: refs/heads/master
Commit: 2f7f0e79c526f12df90c22f47391518e13b8bf24
Parents: 22bb131
Author: Aled Sage <aled.sage@gmail.com>
Authored: Fri May 23 18:29:26 2014 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Tue May 27 20:49:33 2014 +0100

----------------------------------------------------------------------
 .../entity/rebind/RebindPolicyTest.java         |  52 ++++++-
 .../autoscaling/AutoScalerPolicyRebindTest.java | 106 ++++++++++++++
 .../brooklyn/policy/ha/HaPolicyRebindTest.java  | 145 +++++++++++++++++++
 .../policy/ha/ServiceFailureDetectorTest.java   |  24 +--
 4 files changed, 304 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2f7f0e79/core/src/test/java/brooklyn/entity/rebind/RebindPolicyTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/rebind/RebindPolicyTest.java b/core/src/test/java/brooklyn/entity/rebind/RebindPolicyTest.java
index bf6e943..5db6d03 100644
--- a/core/src/test/java/brooklyn/entity/rebind/RebindPolicyTest.java
+++ b/core/src/test/java/brooklyn/entity/rebind/RebindPolicyTest.java
@@ -1,13 +1,16 @@
 package brooklyn.entity.rebind;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
 
-import java.util.Collection;
 import java.util.Map;
 
 import org.testng.annotations.Test;
 
-import brooklyn.policy.Policy;
+import brooklyn.config.ConfigKey;
+import brooklyn.entity.basic.ConfigKeys;
+import brooklyn.policy.PolicySpec;
 import brooklyn.policy.basic.AbstractPolicy;
 import brooklyn.test.entity.TestApplication;
 import brooklyn.util.collections.MutableMap;
@@ -26,30 +29,65 @@ public class RebindPolicyTest extends RebindTestFixtureWithApp {
      * See RebindManagerImpl.CheckpointingChangeListener.onChanged(Entity) and
      * MementosGenerator.newEntityMementoBuilder()
      */
+    
     @Test(enabled=false)
-    public void testRestoresSimplePolicy() throws Exception {
-        MyPolicy origPolicy = new MyPolicy(MutableMap.of("myfield", "myval"));
+    public void testRestoresSimplePolicyFromConstructor() throws Exception {
+        MyPolicy origPolicy = new MyPolicy(MutableMap.of("myfield", "myval", "myconfigkey",
"myConfigVal"));
         origApp.addPolicy(origPolicy);
+    }
+
+    @Test(enabled=false)
+    public void testRestoresSimplePolicyFromPolicySpec() throws Exception {
+        origApp.addPolicy(PolicySpec.create(MyPolicy.class)
+                .configure("myfield", "myFieldVal")
+                .configure(MyPolicy.MY_CONFIG, "myConfigVal"));
+    }
+    
+    protected void runRestoresSimplePolicy() throws Exception {
+        MyPolicy origPolicy = (MyPolicy) Iterables.getOnlyElement(origApp.getPolicies());
+        assertTrue(origPolicy.isRunning());
+        assertTrue(origPolicy.initCalled);
+        assertFalse(origPolicy.rebindCalled);
         
         TestApplication newApp = rebind();
-        Collection<Policy> policies = newApp.getPolicies();
-        MyPolicy newPolicy = (MyPolicy) Iterables.get(policies, 0);
+        MyPolicy newPolicy = (MyPolicy) Iterables.getOnlyElement(newApp.getPolicies());
         
-        assertEquals(newPolicy.myfield, origPolicy.myfield);
+        assertEquals(newPolicy.myfield, "myFieldVal");
+        assertEquals(newPolicy.myfield, "myConfigVal");
+        assertTrue(newPolicy.isRunning());
+        assertFalse(origPolicy.initCalled);
+        assertTrue(origPolicy.rebindCalled);
     }
 
     public static class MyPolicy extends AbstractPolicy {
+        public static final ConfigKey<String> MY_CONFIG = ConfigKeys.newStringConfigKey("myconfigkey");
+        
         @SetFromFlag
         String myfield;
 
         @SuppressWarnings("unused")
         private final Object dummy = new Object(); // so not serializable
         
+        public volatile boolean initCalled;
+        public volatile boolean rebindCalled;
+        
         public MyPolicy() {
         }
         
         public MyPolicy(Map<?,?> flags) {
             super(flags);
         }
+        
+        @Override
+        public void init() {
+            super.init();
+            initCalled = true;
+        }
+        
+        // TODO When AbstractPolicy declares rebind; @Override
+        public void rebind() {
+            // TODO super.rebind();
+            rebindCalled = true;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2f7f0e79/policy/src/test/java/brooklyn/policy/autoscaling/AutoScalerPolicyRebindTest.java
----------------------------------------------------------------------
diff --git a/policy/src/test/java/brooklyn/policy/autoscaling/AutoScalerPolicyRebindTest.java
b/policy/src/test/java/brooklyn/policy/autoscaling/AutoScalerPolicyRebindTest.java
new file mode 100644
index 0000000..647533a
--- /dev/null
+++ b/policy/src/test/java/brooklyn/policy/autoscaling/AutoScalerPolicyRebindTest.java
@@ -0,0 +1,106 @@
+package brooklyn.policy.autoscaling;
+
+import static org.testng.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.testng.annotations.Test;
+
+import brooklyn.entity.basic.EntityInternal;
+import brooklyn.entity.group.DynamicCluster;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.rebind.RebindTestFixtureWithApp;
+import brooklyn.event.AttributeSensor;
+import brooklyn.event.basic.BasicNotificationSensor;
+import brooklyn.event.basic.Sensors;
+import brooklyn.test.EntityTestUtils;
+import brooklyn.test.entity.TestApplication;
+import brooklyn.test.entity.TestEntity;
+
+import com.google.common.collect.Iterables;
+
+public class AutoScalerPolicyRebindTest extends RebindTestFixtureWithApp {
+
+    public static BasicNotificationSensor<Map> POOL_HOT_SENSOR = new BasicNotificationSensor<Map>(
+            Map.class, "AutoScalerPolicyRebindTest.resizablepool.hot", "Pool is over-utilized;
it has insufficient resource for current workload");
+    public static BasicNotificationSensor<Map> POOL_COLD_SENSOR = new BasicNotificationSensor<Map>(
+            Map.class, "AutoScalerPolicyRebindTest.resizablepool.cold", "Pool is under-utilized;
it has too much resource for current workload");
+    public static BasicNotificationSensor<Map> POOL_OK_SENSOR = new BasicNotificationSensor<Map>(
+            Map.class, "AutoScalerPolicyRebindTest.resizablepool.cold", "Pool utilization
is ok; the available resources are fine for the current workload");
+    public static BasicNotificationSensor<MaxPoolSizeReachedEvent> MAX_SIZE_REACHED_SENSOR
= new BasicNotificationSensor<MaxPoolSizeReachedEvent>(
+            MaxPoolSizeReachedEvent.class, "AutoScalerPolicyRebindTest.maxSizeReached");
+    public static AttributeSensor<Integer> METRIC_SENSOR = Sensors.newIntegerSensor("AutoScalerPolicyRebindTest.metric");
+            
+    private DynamicCluster cluster;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        cluster = origApp.createAndManageChild(EntitySpec.create(DynamicCluster.class)
+                .configure("memberSpec", EntitySpec.create(TestEntity.class)));
+    }
+    
+    @Test(enabled=false)
+    public void testRestoresAutoScalerConfig() throws Exception {
+        cluster.addPolicy(AutoScalerPolicy.builder()
+                .name("myname")
+                .metric(METRIC_SENSOR)
+                .entityWithMetric(cluster)
+                .metricUpperBound(1)
+                .metricLowerBound(2)
+                .minPoolSize(0)
+                .maxPoolSize(3)
+                .minPeriodBetweenExecs(4)
+                .resizeUpStabilizationDelay(5)
+                .resizeDownStabilizationDelay(6)
+                .poolHotSensor(POOL_HOT_SENSOR)
+                .poolColdSensor(POOL_COLD_SENSOR)
+                .poolOkSensor(POOL_OK_SENSOR)
+                .maxSizeReachedSensor(MAX_SIZE_REACHED_SENSOR)
+                .maxReachedNotificationDelay(7)
+                .buildSpec());
+        
+        TestApplication newApp = rebind();
+        AutoScalerPolicy newPolicy = (AutoScalerPolicy) Iterables.getOnlyElement(newApp.getPolicies());
+
+        assertEquals(newPolicy.getName(), "myname");
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.METRIC), METRIC_SENSOR);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.ENTITY_WITH_METRIC), cluster);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.METRIC_UPPER_BOUND), 1);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.METRIC_LOWER_BOUND), 2);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.MIN_POOL_SIZE), (Integer)0);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.MAX_POOL_SIZE), (Integer)3);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.MIN_PERIOD_BETWEEN_EXECS), 4);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.RESIZE_UP_STABILIZATION_DELAY),
5);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.RESIZE_DOWN_STABILIZATION_DELAY),
6);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.POOL_HOT_SENSOR), POOL_HOT_SENSOR);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.POOL_COLD_SENSOR), POOL_COLD_SENSOR);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.POOL_OK_SENSOR), POOL_OK_SENSOR);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.MAX_SIZE_REACHED_SENSOR), MAX_SIZE_REACHED_SENSOR);
+        assertEquals(newPolicy.getConfig(AutoScalerPolicy.MAX_REACHED_NOTIFICATION_DELAY),
7);
+    }
+    
+    @Test(enabled=false)
+    public void testAutoScalerResizesAfterRebind() throws Exception {
+        cluster.addPolicy(AutoScalerPolicy.builder()
+                .name("myname")
+                .metric(METRIC_SENSOR)
+                .entityWithMetric(cluster)
+                .metricUpperBound(10)
+                .metricLowerBound(100)
+                .minPoolSize(0)
+                .maxPoolSize(3)
+                .buildSpec());
+        
+        TestApplication newApp = rebind();
+        DynamicCluster newCluster = (DynamicCluster) Iterables.getOnlyElement(newApp.getChildren());
+
+        assertEquals(newCluster.getCurrentSize(), (Integer)0);
+        
+        ((EntityInternal)cluster).setAttribute(METRIC_SENSOR, 1000);
+        EntityTestUtils.assertGroupSizeEqualsEventually(newCluster, 3);
+        
+        ((EntityInternal)cluster).setAttribute(METRIC_SENSOR, 0);
+        EntityTestUtils.assertGroupSizeEqualsEventually(newCluster, 0);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2f7f0e79/policy/src/test/java/brooklyn/policy/ha/HaPolicyRebindTest.java
----------------------------------------------------------------------
diff --git a/policy/src/test/java/brooklyn/policy/ha/HaPolicyRebindTest.java b/policy/src/test/java/brooklyn/policy/ha/HaPolicyRebindTest.java
new file mode 100644
index 0000000..75e6ba0
--- /dev/null
+++ b/policy/src/test/java/brooklyn/policy/ha/HaPolicyRebindTest.java
@@ -0,0 +1,145 @@
+package brooklyn.policy.ha;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.fail;
+
+import java.util.List;
+import java.util.Set;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.Entity;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.basic.Lifecycle;
+import brooklyn.entity.group.DynamicCluster;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.entity.rebind.RebindTestFixtureWithApp;
+import brooklyn.event.Sensor;
+import brooklyn.event.SensorEvent;
+import brooklyn.event.SensorEventListener;
+import brooklyn.location.Location;
+import brooklyn.location.LocationSpec;
+import brooklyn.location.basic.SimulatedLocation;
+import brooklyn.policy.PolicySpec;
+import brooklyn.policy.ha.HASensors.FailureDescriptor;
+import brooklyn.test.Asserts;
+import brooklyn.test.entity.TestApplication;
+import brooklyn.test.entity.TestEntity;
+import brooklyn.util.collections.MutableMap;
+
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+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 com.google.common.collect.Sets;
+
+public class HaPolicyRebindTest extends RebindTestFixtureWithApp {
+
+    private TestEntity origEntity;
+    private SensorEventListener<FailureDescriptor> eventListener;
+    private List<SensorEvent<FailureDescriptor>> events;
+    
+    @Override
+    @BeforeMethod(alwaysRun=true)
+    public void setUp() throws Exception {
+        origEntity = origApp.createAndManageChild(EntitySpec.create(TestEntity.class));
+        events = Lists.newCopyOnWriteArrayList();
+        eventListener = new SensorEventListener<FailureDescriptor>() {
+            @Override public void onEvent(SensorEvent<FailureDescriptor> event) {
+                events.add(event);
+            }
+        };
+    }
+
+    @Test(enabled=false)
+    public void testServiceRestarterWorksAfterRebind() throws Exception {
+        origEntity.addPolicy(PolicySpec.create(ServiceRestarter.class)
+                .configure(ServiceRestarter.FAILURE_SENSOR_TO_MONITOR, HASensors.ENTITY_FAILED));
+        
+        TestApplication newApp = rebind();
+        final TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class));
+        
+        newEntity.emit(HASensors.ENTITY_FAILED, new FailureDescriptor(origEntity, "simulate
failure"));
+        
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                assertEquals(newEntity.getCallHistory(), ImmutableList.of("restart"));
+            }});
+    }
+
+    public void testServiceReplacerWorksAfterRebind() throws Exception {
+        Location origLoc = origManagementContext.getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class));
+        DynamicCluster origCluster = origApp.createAndManageChild(EntitySpec.create(DynamicCluster.class)
+                .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(TestEntity.class))
+                .configure(DynamicCluster.INITIAL_SIZE, 3));
+        origApp.start(ImmutableList.<Location>of(origLoc));
+
+        origCluster.addPolicy(PolicySpec.create(ServiceReplacer.class)
+                .configure(ServiceReplacer.FAILURE_SENSOR_TO_MONITOR, HASensors.ENTITY_FAILED));
+
+        // rebind
+        TestApplication newApp = rebind();
+        final DynamicCluster newCluster = (DynamicCluster) Iterables.find(newApp.getChildren(),
Predicates.instanceOf(DynamicCluster.class));
+
+        // stimulate the policy
+        final Set<Entity> initialMembers = ImmutableSet.copyOf(newCluster.getMembers());
+        final TestEntity e1 = (TestEntity) Iterables.get(initialMembers, 1);
+        
+        newApp.getManagementContext().getSubscriptionManager().subscribe(e1, HASensors.ENTITY_FAILED,
eventListener);
+        newApp.getManagementContext().getSubscriptionManager().subscribe(e1, HASensors.ENTITY_RECOVERED,
eventListener);
+        
+        e1.emit(HASensors.ENTITY_FAILED, new FailureDescriptor(e1, "simulate failure"));
+        
+        // Expect e1 to be replaced
+        Asserts.succeedsEventually(new Runnable() {
+            @Override public void run() {
+                Set<Entity> newMembers = Sets.difference(ImmutableSet.copyOf(newCluster.getMembers()),
initialMembers);
+                Set<Entity> removedMembers = Sets.difference(initialMembers, ImmutableSet.copyOf(newCluster.getMembers()));
+                assertEquals(removedMembers, ImmutableSet.of(e1));
+                assertEquals(newMembers.size(), 1);
+                assertEquals(((TestEntity)Iterables.getOnlyElement(newMembers)).getCallHistory(),
ImmutableList.of("start"));
+                assertEquals(e1.getCallHistory(), ImmutableList.of("start", "stop"));
+                assertFalse(Entities.isManaged(e1));
+            }});
+    }
+    
+    public void testServiceFailureDetectorWorksAfterRebind() throws Exception {
+        origEntity.addPolicy(PolicySpec.create(ServiceFailureDetector.class));
+
+        // rebind
+        TestApplication newApp = rebind();
+        final TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class));
+
+        newApp.getManagementContext().getSubscriptionManager().subscribe(newEntity, HASensors.ENTITY_FAILED,
eventListener);
+
+        // stimulate the policy
+        newEntity.setAttribute(TestEntity.SERVICE_STATE, Lifecycle.RUNNING);
+        newEntity.setAttribute(TestEntity.SERVICE_UP, true);
+        newEntity.setAttribute(TestEntity.SERVICE_UP, false);
+
+        assertHasEventEventually(HASensors.ENTITY_FAILED, Predicates.<Object>equalTo(newEntity),
null);
+        assertEquals(events.size(), 1, "events="+events);
+    }
+    
+    private void assertHasEventEventually(final Sensor<?> sensor, final Predicate<Object>
componentPredicate, final Predicate<? super CharSequence> descriptionPredicate) {
+        Asserts.succeedsEventually(MutableMap.of("timeout", TIMEOUT_MS), new Runnable() {
+            @Override public void run() {
+                assertHasEvent(sensor, componentPredicate, descriptionPredicate);
+            }});
+    }
+    
+    private void assertHasEvent(Sensor<?> sensor, Predicate<Object> componentPredicate,
Predicate<? super CharSequence> descriptionPredicate) {
+        for (SensorEvent<FailureDescriptor> event : events) {
+            if (event.getSensor().equals(sensor) && 
+                    (componentPredicate == null || componentPredicate.apply(event.getValue().getComponent()))
&&
+                    (descriptionPredicate == null || descriptionPredicate.apply(event.getValue().getDescription())))
{
+                return;
+            }
+        }
+        fail("No matching "+sensor+" event found; events="+events);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2f7f0e79/policy/src/test/java/brooklyn/policy/ha/ServiceFailureDetectorTest.java
----------------------------------------------------------------------
diff --git a/policy/src/test/java/brooklyn/policy/ha/ServiceFailureDetectorTest.java b/policy/src/test/java/brooklyn/policy/ha/ServiceFailureDetectorTest.java
index 0641d87..d9ab80e 100644
--- a/policy/src/test/java/brooklyn/policy/ha/ServiceFailureDetectorTest.java
+++ b/policy/src/test/java/brooklyn/policy/ha/ServiceFailureDetectorTest.java
@@ -40,31 +40,23 @@ public class ServiceFailureDetectorTest {
     private ServiceFailureDetector policy;
     
     private List<SensorEvent<FailureDescriptor>> events;
+    private SensorEventListener<FailureDescriptor> eventListener;
     
     @BeforeMethod(alwaysRun=true)
     public void setUp() throws Exception {
         events = new CopyOnWriteArrayList<SensorEvent<FailureDescriptor>>();
+        eventListener = new SensorEventListener<FailureDescriptor>() {
+            @Override public void onEvent(SensorEvent<FailureDescriptor> event) {
+                events.add(event);
+            }
+        };
         
         managementContext = Entities.newManagementContext();
         app = ApplicationBuilder.newManagedApp(TestApplication.class, managementContext);
         e1 = app.createAndManageChild(EntitySpec.create(TestEntity.class));
         
-        app.getManagementContext().getSubscriptionManager().subscribe(
-                e1, 
-                HASensors.ENTITY_FAILED, 
-                new SensorEventListener<FailureDescriptor>() {
-                    @Override public void onEvent(SensorEvent<FailureDescriptor> event)
{
-                        events.add(event);
-                    }
-                });
-        app.getManagementContext().getSubscriptionManager().subscribe(
-                e1, 
-                HASensors.ENTITY_RECOVERED, 
-                new SensorEventListener<FailureDescriptor>() {
-                    @Override public void onEvent(SensorEvent<FailureDescriptor> event)
{
-                        events.add(event);
-                    }
-                });
+        app.getManagementContext().getSubscriptionManager().subscribe(e1, HASensors.ENTITY_FAILED,
eventListener);
+        app.getManagementContext().getSubscriptionManager().subscribe(e1, HASensors.ENTITY_RECOVERED,
eventListener);
     }
     
     @AfterMethod(alwaysRun=true)


Mime
View raw message