brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/8] brooklyn-server git commit: Convert integration tests to unit tests and fix timing related failures
Date Fri, 11 Nov 2016 09:50:18 GMT
Convert integration tests to unit tests and fix timing related failures


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

Branch: refs/heads/master
Commit: 89646d5d80d88ec05583e2d9d7c7fdec5036c36c
Parents: b714516
Author: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Authored: Tue Nov 8 14:21:41 2016 +0200
Committer: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Committed: Thu Nov 10 18:06:50 2016 +0200

----------------------------------------------------------------------
 .../brooklyn/core/entity/EntityAssertsTest.java | 203 +++++++++++--------
 .../brooklyn/enricher/stock/EnrichersTest.java  |  12 +-
 2 files changed, 125 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/89646d5d/core/src/test/java/org/apache/brooklyn/core/entity/EntityAssertsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/EntityAssertsTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/EntityAssertsTest.java
index 8ea2fd0..bfdac3c 100644
--- a/core/src/test/java/org/apache/brooklyn/core/entity/EntityAssertsTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/entity/EntityAssertsTest.java
@@ -18,19 +18,18 @@
  */
 package org.apache.brooklyn.core.entity;
 
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
 
 import org.apache.brooklyn.api.entity.EntitySpec;
-import org.apache.brooklyn.api.location.LocationSpec;
-import org.apache.brooklyn.api.sensor.AttributeSensor;
-import org.apache.brooklyn.core.location.SimulatedLocation;
+import org.apache.brooklyn.api.location.Location;
+import org.apache.brooklyn.api.mgmt.Task;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.entity.group.DynamicGroup;
+import org.apache.brooklyn.util.repeat.Repeater;
 import org.apache.brooklyn.util.time.Duration;
-import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
@@ -46,35 +45,17 @@ public class EntityAssertsTest extends BrooklynAppUnitTestSupport {
 
     private static final String STOOGE = "stooge";
 
-    private SimulatedLocation loc;
     private TestEntity entity;
-    private ScheduledExecutorService executor;
     private DynamicGroup stooges;
 
     @BeforeMethod(alwaysRun=true)
     @Override
     public void setUp() throws Exception {
         super.setUp();
-        loc = app.getManagementContext().getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class));
         entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
-        stooges = app.createAndManageChild(EntitySpec.create(DynamicGroup.class));
-        final EntitySpec<TestEntity> stooge =
-                EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, STOOGE);
-        app.createAndManageChild(stooge);
-        app.createAndManageChild(stooge);
-        app.createAndManageChild(stooge);
-        app.start(ImmutableList.of(loc));
-        executor = Executors.newScheduledThreadPool(3);
-    }
-
-    @AfterMethod(alwaysRun=true)
-    public void tearDown() throws Exception {
-        if (executor != null) executor.shutdownNow();
-        if (app != null) Entities.destroyAll(app.getManagementContext());
-        super.tearDown();
+        app.start(ImmutableList.<Location>of());
     }
 
-
     @Test
     public void shouldAssertAttributeEquals() {
         final String myName = "myname";
@@ -99,47 +80,59 @@ public class EntityAssertsTest extends BrooklynAppUnitTestSupport {
         EntityAsserts.assertConfigEquals(entity, TestEntity.CONF_NAME, "bogus");
     }
 
-    @Test(groups="Integration")
-    public void shouldAssertAttributeEqualsEventually() {
+    @Test
+    public void shouldAssertAttributeEqualsEventually() throws Exception {
         entity.sensors().set(TestEntity.NAME, "before");
         final String after = "after";
-        setSensorValueLater(TestEntity.NAME, after, Duration.seconds(2));
-        EntityAsserts.assertAttributeEqualsEventually(entity, TestEntity.NAME, after);
+
+        Task<?> assertValue = entity.getExecutionContext().submit(new Runnable() {
+            @Override
+            public void run() {
+                EntityAsserts.assertAttributeEqualsEventually(entity, TestEntity.NAME, after);
+            }
+        });
+        entity.sensors().set(TestEntity.NAME, after);
+        assertValue.get();
     }
 
-    @Test(groups="Integration", expectedExceptions = AssertionError.class)
+    @Test(expectedExceptions = AssertionError.class)
     public void shouldFailToAssertAttributeEqualsEventually() {
         entity.sensors().set(TestEntity.NAME, "before");
         final String after = "after";
-        setSensorValueLater(TestEntity.NAME, after, Duration.seconds(2));
-        EntityAsserts.assertAttributeEqualsEventually(ImmutableMap.of("timeout", "1s"), entity,
TestEntity.NAME, after);
+        EntityAsserts.assertAttributeEqualsEventually(ImmutableMap.of("timeout", "100ms"),
entity, TestEntity.NAME, after);
     }
 
-    private <T> void setSensorValueLater(final AttributeSensor<T> sensor, final
T value, final Duration delay) {
-        executor.schedule(new Runnable() {
+    @Test
+    public void shouldAssertAttributeEventuallyNonNull() throws Exception {
+        EntityAsserts.assertAttributeEquals(entity, TestEntity.NAME, null);
+        Task<?> assertValue = entity.getExecutionContext().submit(new Runnable() {
             @Override
             public void run() {
-                entity.sensors().set(sensor, value);
+                EntityAsserts.assertAttributeEventuallyNonNull(entity, TestEntity.NAME);
             }
-        }, delay.toUnit(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
-    }
-
-    @Test(groups="Integration")
-    public void shouldAssertAttributeEventuallyNonNull() {
-        EntityAsserts.assertAttributeEquals(entity, TestEntity.NAME, null);
-        setSensorValueLater(TestEntity.NAME, "something", Duration.seconds(1));
-        EntityAsserts.assertAttributeEventuallyNonNull(entity, TestEntity.NAME);
+        });
+        entity.sensors().set(TestEntity.NAME, "something");
+        assertValue.get();
     }
 
-    @Test(groups="Integration")
-    public void shouldAssertAttributeEventually() {
-        setSensorValueLater(TestEntity.NAME, "testing testing 123", Duration.seconds(1));
-        EntityAsserts.assertAttributeEventually(entity, TestEntity.NAME, new Predicate<String>()
{
+    @Test
+    public void shouldAssertAttributeEventually() throws Exception {
+        final CountDownLatch eventuallyEntered = new CountDownLatch(2);
+        Task<?> assertValue = entity.getExecutionContext().submit(new Runnable() {
             @Override
-            public boolean apply(String input) {
-                return input.matches(".*\\d+");
+            public void run() {
+                EntityAsserts.assertAttributeEventually(entity, TestEntity.NAME, new Predicate<String>()
{
+                    @Override
+                    public boolean apply(String input) {
+                        eventuallyEntered.countDown();
+                        return input.matches(".*\\d+");
+                    }
+                });
             }
         });
+        eventuallyEntered.await();
+        entity.sensors().set(TestEntity.NAME, "testing testing 123");
+        assertValue.get();
     }
 
     @Test
@@ -149,68 +142,110 @@ public class EntityAssertsTest extends BrooklynAppUnitTestSupport {
         EntityAsserts.assertAttribute(entity, TestEntity.NAME, Predicates.equalTo(before));
     }
 
-    @Test(groups="Integration")
-    public void shouldAssertPredicateEventuallyTrue() {
+    @Test
+    public void shouldAssertPredicateEventuallyTrue() throws Exception {
         final int testVal = 987654321;
-        executor.schedule(new Runnable() {
+        final CountDownLatch eventuallyEntered = new CountDownLatch(2);
+        Task<?> assertValue = entity.getExecutionContext().submit(new Runnable() {
             @Override
             public void run() {
-                entity.setSequenceValue(testVal);
-            }
-        }, 1, TimeUnit.SECONDS);
-        EntityAsserts.assertPredicateEventuallyTrue(entity, new Predicate<TestEntity>()
{
-            @Override
-            public boolean apply(TestEntity input) {
-                return testVal == input.getSequenceValue() ;
+                EntityAsserts.assertPredicateEventuallyTrue(entity, new Predicate<TestEntity>()
{
+                    @Override
+                    public boolean apply(TestEntity input) {
+                        eventuallyEntered.countDown();
+                        return testVal == input.getSequenceValue();
+                    }
+                });
             }
         });
+        eventuallyEntered.await();
+        entity.setSequenceValue(testVal);
+        assertValue.get();
     }
 
-    @Test(groups="Integration")
+    @Test
     public void shouldAssertAttributeEqualsContinually() {
         final String myName = "myname";
         entity.sensors().set(TestEntity.NAME, myName);
         EntityAsserts.assertAttributeEqualsContinually(
-                ImmutableMap.of("timeout", "2s"), entity, TestEntity.NAME, myName);
+                ImmutableMap.of("timeout", "100ms"), entity, TestEntity.NAME, myName);
     }
 
-    @Test(groups="Integration", expectedExceptions = AssertionError.class)
-    public void shouldFailAssertAttributeEqualsContinually() {
+    @Test(expectedExceptions = AssertionError.class)
+    public void shouldFailAssertAttributeEqualsContinually() throws Throwable {
         final String myName = "myname";
         entity.sensors().set(TestEntity.NAME, myName);
-        setSensorValueLater(TestEntity.NAME, "something", Duration.seconds(1));
-        EntityAsserts.assertAttributeEqualsContinually(
-                ImmutableMap.of("timeout", "2s"), entity, TestEntity.NAME, myName);
+        Task<?> assertValue = entity.getExecutionContext().submit(new Runnable() {
+            @Override
+            public void run() {
+                EntityAsserts.assertAttributeEqualsContinually(entity, TestEntity.NAME, myName);
+            }
+        });
+        entity.sensors().set(TestEntity.NAME, "something");
+        try {
+            assertValue.get();
+        } catch (ExecutionException e) {
+            //strip wrapper exception
+            throw e.getCause();
+        }
     }
 
-    @Test(groups="Integration")
-    public void shouldAssertGroupSizeEqualsEventually() {
-        setGroupFilterLater(STOOGE, 1);
-        EntityAsserts.assertGroupSizeEqualsEventually(ImmutableMap.of("timeout", "2s"), stooges,
3);
-        setGroupFilterLater("Marx Brother", 1);
-        EntityAsserts.assertGroupSizeEqualsEventually(stooges, 0);
-    }
+    @Test
+    public void shouldAssertGroupSizeEqualsEventually() throws Exception {
+        stooges = app.createAndManageChild(EntitySpec.create(DynamicGroup.class));
+        final EntitySpec<TestEntity> stooge =
+                EntitySpec.create(TestEntity.class).configure(TestEntity.CONF_NAME, STOOGE);
+        app.createAndManageChild(stooge);
+        app.createAndManageChild(stooge);
+        app.createAndManageChild(stooge);
 
-    private void setGroupFilterLater(final String conf, long delaySeconds) {
-        executor.schedule(new Runnable() {
+        Task<?> assertValue1 = entity.getExecutionContext().submit(new Runnable() {
             @Override
             public void run() {
-                stooges.setEntityFilter(EntityPredicates.configEqualTo(TestEntity.CONF_NAME,
conf));
+                EntityAsserts.assertGroupSizeEqualsEventually(ImmutableMap.of("timeout",
"2s"), stooges, 3);
             }
-        }, delaySeconds, TimeUnit.SECONDS);
+        });
+        stooges.setEntityFilter(EntityPredicates.configEqualTo(TestEntity.CONF_NAME, STOOGE));
+        assertValue1.get();
+        Task<?> assertValue2 = entity.getExecutionContext().submit(new Runnable() {
+            @Override
+            public void run() {
+                EntityAsserts.assertGroupSizeEqualsEventually(stooges, 0);
+            }
+        });
+        stooges.setEntityFilter(EntityPredicates.configEqualTo(TestEntity.CONF_NAME, "Marx
Brother"));
+        assertValue2.get();
     }
 
-    @Test(groups="Integration")
-    public void shouldAssertAttributeChangesEventually () {
+    @Test
+    public void shouldAssertAttributeChangesEventually () throws Exception{
         entity.sensors().set(TestEntity.NAME, "before");
-        setSensorValueLater(TestEntity.NAME, "after", Duration.seconds(2));
-        EntityAsserts.assertAttributeChangesEventually(entity, TestEntity.NAME);
+        final Task<?> assertValue = entity.getExecutionContext().submit(new Runnable()
{
+            @Override
+            public void run() {
+                EntityAsserts.assertAttributeChangesEventually(entity, TestEntity.NAME);
+            }
+        });
+        Repeater.create()
+            .repeat(new Runnable() {
+                @Override
+                public void run() {
+                    entity.sensors().set(TestEntity.NAME, "after" + System.currentTimeMillis());
+                }
+            }).until(new Callable<Boolean>() {
+                @Override
+                public Boolean call() throws Exception {
+                    return assertValue.isDone();
+                }
+            }).every(Duration.millis(10))
+            .run();
+        assertValue.get();
     }
 
-    @Test(groups="Integration")
+    @Test
     public void shouldAssertAttributeNever() {
         entity.sensors().set(TestEntity.NAME, "ever");
-        EntityAsserts.assertAttributeContinuallyNotEqualTo(ImmutableMap.of("timeout", "5s"),
entity, TestEntity.NAME, "after");
+        EntityAsserts.assertAttributeContinuallyNotEqualTo(ImmutableMap.of("timeout", "100ms"),
entity, TestEntity.NAME, "after");
     }
 
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/89646d5d/core/src/test/java/org/apache/brooklyn/enricher/stock/EnrichersTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/enricher/stock/EnrichersTest.java b/core/src/test/java/org/apache/brooklyn/enricher/stock/EnrichersTest.java
index 4e6c87e..19503e7 100644
--- a/core/src/test/java/org/apache/brooklyn/enricher/stock/EnrichersTest.java
+++ b/core/src/test/java/org/apache/brooklyn/enricher/stock/EnrichersTest.java
@@ -112,7 +112,7 @@ public class EnrichersTest extends BrooklynAppUnitTestSupport {
         EntityAsserts.assertAttributeEqualsEventually(entity, NUM3, 1);
     }
     
-    @Test(groups="Integration") // because takes a second
+    @Test
     public void testCombiningRespectsUnchanged() {
         entity.enrichers().add(Enrichers.builder()
                 .combining(NUM1, NUM2)
@@ -132,7 +132,7 @@ public class EnrichersTest extends BrooklynAppUnitTestSupport {
         EntityAsserts.assertAttributeEqualsEventually(entity, NUM3, 126);
         
         entity.sensors().set(NUM1, 2);
-        EntityAsserts.assertAttributeEqualsContinually(entity, NUM3, 126);
+        EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", "100ms"),
entity, NUM3, 126);
     }
     
     @Test
@@ -187,7 +187,7 @@ public class EnrichersTest extends BrooklynAppUnitTestSupport {
         EntityAsserts.assertAttributeEqualsEventually(entity, STR2, "myvalmysuffix");
     }
 
-    @Test(groups="Integration") // because takes a second
+    @Test
     public void testTransformingRespectsUnchangedButWillRepublish() throws Exception {
         RecordingSensorEventListener<String> record = new RecordingSensorEventListener<>();
         app.getManagementContext().getSubscriptionManager().subscribe(entity, STR2, record);
@@ -211,7 +211,7 @@ public class EnrichersTest extends BrooklynAppUnitTestSupport {
         EntityAsserts.assertAttributeEqualsEventually(entity, STR2, "myval");
 
         entity.sensors().set(STR1, "ignoredval");
-        EntityAsserts.assertAttributeEqualsContinually(entity, STR2, "myval");
+        EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", "100ms"),
entity, STR2, "myval");
 
         entity.sensors().set(STR1, "myval2");
         Asserts.eventually(Suppliers.ofInstance(record), CollectionFunctionals.sizeEquals(3));
@@ -378,7 +378,7 @@ public class EnrichersTest extends BrooklynAppUnitTestSupport {
         EntityAsserts.assertAttributeEqualsEventually(group, LONG1, Long.valueOf(1));
     }
     
-    @Test(groups="Integration") // because takes a second
+    @Test
     public void testAggregatingRespectsUnchanged() {
         group.addMember(entity);
         group.enrichers().add(Enrichers.builder()
@@ -399,7 +399,7 @@ public class EnrichersTest extends BrooklynAppUnitTestSupport {
         EntityAsserts.assertAttributeEqualsEventually(group, LONG1, Long.valueOf(123));
         
         entity.sensors().set(NUM1, 987654);
-        EntityAsserts.assertAttributeEqualsContinually(group, LONG1, Long.valueOf(123));
+        EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", "100ms"),
group, LONG1, Long.valueOf(123));
     }
 
     @Test


Mime
View raw message