brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [5/6] git commit: Convert BrooklynMetricsTest from groovy to java
Date Wed, 25 Jun 2014 15:05:52 GMT
Convert BrooklynMetricsTest from groovy to java


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

Branch: refs/heads/master
Commit: b8622eba31544795414ed532ceac766cfe937914
Parents: c309976
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Jun 18 10:22:14 2014 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Jun 18 10:25:46 2014 +0100

----------------------------------------------------------------------
 .../brooklyn/event/SensorEventListener.java     |   8 ++
 .../entity/brooklyn/BrooklynMetricsTest.groovy  |  96 -----------------
 .../entity/brooklyn/BrooklynMetricsTest.java    | 103 +++++++++++++++++++
 3 files changed, 111 insertions(+), 96 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b8622eba/api/src/main/java/brooklyn/event/SensorEventListener.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/event/SensorEventListener.java b/api/src/main/java/brooklyn/event/SensorEventListener.java
index 63b71f4..394b466 100644
--- a/api/src/main/java/brooklyn/event/SensorEventListener.java
+++ b/api/src/main/java/brooklyn/event/SensorEventListener.java
@@ -1,9 +1,17 @@
 package brooklyn.event;
 
+import brooklyn.entity.Entity;
+
 /**
  * A listener for {@link SensorEvent}s on an {@link Entity}.
  */
 public interface SensorEventListener<T> {
+    
+    public static final SensorEventListener<Object> NOOP = new SensorEventListener<Object>()
{
+        @Override public void onEvent(SensorEvent<Object> event) {
+        }
+    };
+    
     /**
      * The {@link SensorEvent} handler method.
      */

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b8622eba/policy/src/test/java/brooklyn/entity/brooklyn/BrooklynMetricsTest.groovy
----------------------------------------------------------------------
diff --git a/policy/src/test/java/brooklyn/entity/brooklyn/BrooklynMetricsTest.groovy b/policy/src/test/java/brooklyn/entity/brooklyn/BrooklynMetricsTest.groovy
deleted file mode 100644
index 2cf37b9..0000000
--- a/policy/src/test/java/brooklyn/entity/brooklyn/BrooklynMetricsTest.groovy
+++ /dev/null
@@ -1,96 +0,0 @@
-package brooklyn.entity.brooklyn
-
-import static brooklyn.test.TestUtils.*
-import static org.testng.Assert.*
-
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod
-import org.testng.annotations.Test
-
-import brooklyn.entity.Entity
-import brooklyn.entity.basic.ApplicationBuilder
-import brooklyn.entity.basic.Entities
-import brooklyn.entity.proxying.EntitySpec
-import brooklyn.event.AttributeSensor
-import brooklyn.event.SensorEventListener
-import brooklyn.location.basic.SimulatedLocation
-import brooklyn.test.entity.TestApplication
-import brooklyn.test.entity.TestEntity
-
-class BrooklynMetricsTest {
-
-    private static final long TIMEOUT_MS = 2*1000;
-    
-    TestApplication app
-    SimulatedLocation loc
-    BrooklynMetrics brooklynMetrics
-    
-    @BeforeMethod(alwaysRun=true)
-    public void setUp() {
-        loc = new SimulatedLocation()
-        app = ApplicationBuilder.newManagedApp(TestApplication.class);
-        brooklynMetrics = app.createAndManageChild(EntitySpec.create(BrooklynMetrics.class).configure("updatePeriod",
10L));
-        Entities.manage(brooklynMetrics);
-    }
-    
-    @AfterMethod(alwaysRun=true)
-    public void tearDown() throws Exception {
-        if (app != null) Entities.destroyAll(app.getManagementContext());
-    }
-
-    @Test
-    public void testInitialBrooklynMetrics() {
-        app.start([loc])
-
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EFFECTORS_INVOKED),
1)
-            assertTrue(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_TASKS_SUBMITTED)
> 0)
-            assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.NUM_INCOMPLETE_TASKS),
0)
-            assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.NUM_ACTIVE_TASKS),
0)
-            assertTrue(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EVENTS_PUBLISHED)
> 0)
-            assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EVENTS_DELIVERED),
0)
-            assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.NUM_SUBSCRIPTIONS),
0)
-        }
-    }
-    
-    @Test
-    public void testBrooklynMetricsIncremented() {
-        TestEntity e = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-        app.start([loc])
-
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EFFECTORS_INVOKED),
2) // for app and testEntity's start
-        }
-
-        // Note if attribute has not yet been set, the value returned could be null
-        long effsInvoked = getAttribute(brooklynMetrics, BrooklynMetrics.TOTAL_EFFECTORS_INVOKED,
0);
-        long tasksSubmitted = getAttribute(brooklynMetrics, BrooklynMetrics.TOTAL_TASKS_SUBMITTED,
0);
-        long eventsPublished = getAttribute(brooklynMetrics, BrooklynMetrics.TOTAL_EVENTS_PUBLISHED,
0);
-        long eventsDelivered = getAttribute(brooklynMetrics, BrooklynMetrics.TOTAL_EVENTS_DELIVERED,
0);
-        long subscriptions = getAttribute(brooklynMetrics, BrooklynMetrics.NUM_SUBSCRIPTIONS,
0);
-
-        // Invoking an effector increments effector/task count
-        e.myEffector()
-        
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EFFECTORS_INVOKED),
effsInvoked+1)
-            assertTrue(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_TASKS_SUBMITTED)
> tasksSubmitted)
-        }
-        
-        // Setting attribute causes event to be published and delivered to the subscriber
-        // Note that the brooklyn metrics entity itself is also publishing sensors
-        app.subscribe(e, TestEntity.SEQUENCE, {} as SensorEventListener)
-        e.setAttribute(TestEntity.SEQUENCE, 1)
-        
-        executeUntilSucceeds(timeout:TIMEOUT_MS) {
-            assertTrue(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EVENTS_PUBLISHED)
> eventsPublished)
-            assertTrue(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EVENTS_DELIVERED)
> eventsDelivered)
-            assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.NUM_SUBSCRIPTIONS),
1)
-        }
-    }
-    
-    private long getAttribute(Entity entity, AttributeSensor<Long> attribute, long
defaultVal) {
-        Long result = entity.getAttribute(attribute);
-        return (result != null) ? result : defaultVal;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/b8622eba/policy/src/test/java/brooklyn/entity/brooklyn/BrooklynMetricsTest.java
----------------------------------------------------------------------
diff --git a/policy/src/test/java/brooklyn/entity/brooklyn/BrooklynMetricsTest.java b/policy/src/test/java/brooklyn/entity/brooklyn/BrooklynMetricsTest.java
new file mode 100644
index 0000000..90e2a24
--- /dev/null
+++ b/policy/src/test/java/brooklyn/entity/brooklyn/BrooklynMetricsTest.java
@@ -0,0 +1,103 @@
+package brooklyn.entity.brooklyn;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import brooklyn.entity.Entity;
+import brooklyn.entity.basic.ApplicationBuilder;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.event.AttributeSensor;
+import brooklyn.event.SensorEventListener;
+import brooklyn.location.basic.SimulatedLocation;
+import brooklyn.test.Asserts;
+import brooklyn.test.entity.TestApplication;
+import brooklyn.test.entity.TestEntity;
+
+import com.google.common.collect.ImmutableList;
+
+public class BrooklynMetricsTest {
+
+    private static final long TIMEOUT_MS = 2*1000;
+    
+    TestApplication app;
+    SimulatedLocation loc;
+    BrooklynMetrics brooklynMetrics;
+    
+    @BeforeMethod(alwaysRun=true)
+    public void setUp() {
+        loc = new SimulatedLocation();
+        app = ApplicationBuilder.newManagedApp(TestApplication.class);
+        brooklynMetrics = app.createAndManageChild(EntitySpec.create(BrooklynMetrics.class).configure("updatePeriod",
10L));
+        Entities.manage(brooklynMetrics);
+    }
+    
+    @AfterMethod(alwaysRun=true)
+    public void tearDown() throws Exception {
+        if (app != null) Entities.destroyAll(app.getManagementContext());
+    }
+
+    @Test
+    public void testInitialBrooklynMetrics() {
+        app.start(ImmutableList.of(loc));
+
+        Asserts.succeedsEventually(new Runnable() {
+            public void run() {
+                assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EFFECTORS_INVOKED),
(Long)1L);
+                assertTrue(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_TASKS_SUBMITTED)
> 0);
+                assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.NUM_INCOMPLETE_TASKS),
(Long)0L);
+                assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.NUM_ACTIVE_TASKS),
(Long)0L);
+                assertTrue(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EVENTS_PUBLISHED)
> 0);
+                assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EVENTS_DELIVERED),
(Long)0L);
+                assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.NUM_SUBSCRIPTIONS),
(Long)0L);
+            }});
+    }
+    
+    @Test
+    public void testBrooklynMetricsIncremented() {
+        TestEntity e = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+        app.start(ImmutableList.of(loc));
+
+        Asserts.succeedsEventually(new Runnable() {
+            public void run() {
+                assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EFFECTORS_INVOKED),
(Long)2L); // for app and testEntity's start
+            }});
+
+        // Note if attribute has not yet been set, the value returned could be null
+        final long effsInvoked = getAttribute(brooklynMetrics, BrooklynMetrics.TOTAL_EFFECTORS_INVOKED,
0);
+        final long tasksSubmitted = getAttribute(brooklynMetrics, BrooklynMetrics.TOTAL_TASKS_SUBMITTED,
0);
+        final long eventsPublished = getAttribute(brooklynMetrics, BrooklynMetrics.TOTAL_EVENTS_PUBLISHED,
0);
+        final long eventsDelivered = getAttribute(brooklynMetrics, BrooklynMetrics.TOTAL_EVENTS_DELIVERED,
0);
+        final long subscriptions = getAttribute(brooklynMetrics, BrooklynMetrics.NUM_SUBSCRIPTIONS,
0);
+
+        // Invoking an effector increments effector/task count
+        e.myEffector();
+        
+        Asserts.succeedsEventually(new Runnable() {
+            public void run() {
+                assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EFFECTORS_INVOKED),
(Long)(effsInvoked+1));
+                assertTrue(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_TASKS_SUBMITTED)
> tasksSubmitted);
+            }});
+        
+        // Setting attribute causes event to be published and delivered to the subscriber
+        // Note that the brooklyn metrics entity itself is also publishing sensors
+        app.subscribe(e, TestEntity.SEQUENCE, SensorEventListener.NOOP);
+        e.setAttribute(TestEntity.SEQUENCE, 1);
+        
+        Asserts.succeedsEventually(new Runnable() {
+            public void run() {
+                assertTrue(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EVENTS_PUBLISHED)
> eventsPublished);
+                assertTrue(brooklynMetrics.getAttribute(BrooklynMetrics.TOTAL_EVENTS_DELIVERED)
> eventsDelivered);
+                assertEquals(brooklynMetrics.getAttribute(BrooklynMetrics.NUM_SUBSCRIPTIONS),
(Long)1L);
+            }});
+    }
+    
+    private long getAttribute(Entity entity, AttributeSensor<Long> attribute, long
defaultVal) {
+        Long result = entity.getAttribute(attribute);
+        return (result != null) ? result : defaultVal;
+    }
+}


Mime
View raw message