brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From drigod...@apache.org
Subject [3/5] brooklyn-server git commit: Fix testWarnOnlyOnceOnRepeatedCoercionException
Date Wed, 07 Feb 2018 10:27:45 GMT
Fix testWarnOnlyOnceOnRepeatedCoercionException


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

Branch: refs/heads/master
Commit: 440ae7adff9beed1706fcd8eb35b56df1e106452
Parents: 3ad3045
Author: Aled Sage <aled.sage@gmail.com>
Authored: Tue Feb 6 18:19:18 2018 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Tue Feb 6 18:35:37 2018 +0000

----------------------------------------------------------------------
 .../camp/brooklyn/FunctionSensorYamlTest.java   | 29 +++++++++++++++++---
 .../org/apache/brooklyn/test/LogWatcher.java    | 22 +++++++++++++++
 2 files changed, 47 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/440ae7ad/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/FunctionSensorYamlTest.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/FunctionSensorYamlTest.java
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/FunctionSensorYamlTest.java
index 33102f0..b6a15a9 100644
--- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/FunctionSensorYamlTest.java
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/FunctionSensorYamlTest.java
@@ -18,9 +18,9 @@
  */
 package org.apache.brooklyn.camp.brooklyn;
 
-
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
 
 import java.util.List;
 import java.util.concurrent.Callable;
@@ -189,10 +189,31 @@ public class FunctionSensorYamlTest extends AbstractYamlRebindTest {
             assertTrue(Iterables.tryFind(warnEvents, EventPredicates.containsMessages("Read
of", "gave exception", "Cannot coerce ")).isPresent(), "warnEvents="+warnEvents);
             assertEquals(Iterables.size(warnEvents), 1, "warnEvents="+warnEvents);
 
-            // Ensure we log the stacktrace only once
+            // Ensure exception logging is acceptable
             Iterable<ILoggingEvent> exceptionEvents = Iterables.filter(watcher.getEvents(),
EventPredicates.containsException());
-            assertTrue(Iterables.tryFind(exceptionEvents, EventPredicates.containsExceptionMessage("Cannot
coerce ")).isPresent(), "exceptionEvents="+exceptionEvents);
-            assertEquals(Iterables.size(exceptionEvents), 1, "exceptionEvents="+exceptionEvents);
+
+            // Expect exactly one stacktrace in normal feed execution
+            // e.g. [DEBUG] Trace for exception reading TestEntityImpl{id=xfnctsdr8k}->Sensor:
mysensor (java.lang.Integer): org.apache.brooklyn.util.javalang.coerce.ClassCoercionException:
Cannot coerce "my-not-a-number" to java.lang.Integer (my-not-a-number): adapting failed,
+            Iterable<ILoggingEvent> activeExceptionEvents = Iterables.filter(exceptionEvents,
EventPredicates.containsMessage("Trace for exception reading "));
+            assertTrue(Iterables.tryFind(activeExceptionEvents, EventPredicates.containsExceptionMessage("Cannot
coerce ")).isPresent(), "exceptionEvents="+exceptionEvents);
+            assertEquals(Iterables.size(activeExceptionEvents), 1, "activeExceptionEvents="+activeExceptionEvents);
+            
+            // After stop, can have the stacktrace logged again (when entity is "inactive")
+            // e.g. [DEBUG] unable to compute TestEntityImpl{id=xfnctsdr8k}->Sensor: mysensor
(java.lang.Integer); exception=org.apache.brooklyn.util.javalang.coerce.ClassCoercionException:
Cannot coerce "my-not-a-number" to java.lang.Integer (my-not-a-number): adapting failed (when
inactive)
+            // This could be for "interrupted", or potentially the "Cannot coerce" again.
+            Iterable<ILoggingEvent> inactiveExceptionEvents = Iterables.filter(watcher.getEvents(),
EventPredicates.containsMessage("when inactive"));
+            Iterable<ILoggingEvent> inactiveCoercionExceptionEvents = Iterables.filter(inactiveExceptionEvents,
EventPredicates.containsExceptionMessage("Cannot coerce "));
+            Iterable<ILoggingEvent> inactiveInterruptExceptionEvents = Iterables.filter(inactiveExceptionEvents,
EventPredicates.containsExceptionClassname("InterruptedException"));
+            if (!Iterables.isEmpty(inactiveCoercionExceptionEvents)) {
+                assertTrue(Iterables.size(inactiveCoercionExceptionEvents) <= 1, "inactiveCoercionExceptionEvents="+inactiveCoercionExceptionEvents);
+            }
+
+            // But no other exceptions should be logged
+            int numAllowedExceptions = Iterables.size(Iterables.concat(activeExceptionEvents,
inactiveCoercionExceptionEvents, inactiveInterruptExceptionEvents));
+            if (Iterables.size(exceptionEvents) != numAllowedExceptions) {
+                fail("exceptionEvents="+watcher.printEventsToString(exceptionEvents));
+            }
+
         } finally {
             watcher.close();
         }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/440ae7ad/test-support/src/main/java/org/apache/brooklyn/test/LogWatcher.java
----------------------------------------------------------------------
diff --git a/test-support/src/main/java/org/apache/brooklyn/test/LogWatcher.java b/test-support/src/main/java/org/apache/brooklyn/test/LogWatcher.java
index 15fd245..90b87e4 100644
--- a/test-support/src/main/java/org/apache/brooklyn/test/LogWatcher.java
+++ b/test-support/src/main/java/org/apache/brooklyn/test/LogWatcher.java
@@ -22,6 +22,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static org.testng.Assert.assertFalse;
 
+import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.PrintStream;
 import java.util.Collections;
@@ -123,6 +124,17 @@ public class LogWatcher implements Closeable {
                 }
             };
         }
+
+        public static Predicate<ILoggingEvent> containsExceptionClassname(final String
expected) {
+            return new Predicate<ILoggingEvent>() {
+                @Override public boolean apply(ILoggingEvent input) {
+                    IThrowableProxy throwable = (input != null) ? input.getThrowableProxy()
: null;
+                    String classname = (throwable != null) ? throwable.getClassName() : null;
+                    if (classname == null) return false;
+                    return classname.contains(expected);
+                }
+            };
+        }
         
         public static Predicate<ILoggingEvent> levelGeaterOrEqual(final Level expectedLevel)
{
             return new Predicate<ILoggingEvent>() {
@@ -241,6 +253,16 @@ public class LogWatcher implements Closeable {
         printEvents(System.out, getEvents());
     }
     
+    public String printEventsToString() {
+        return printEventsToString(getEvents());
+    }
+    
+    public String printEventsToString(Iterable<? extends ILoggingEvent> events) {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        printEvents(new PrintStream(baos), events);
+        return new String(baos.toByteArray());
+    }
+    
     public void printEvents(PrintStream stream, Iterable<? extends ILoggingEvent> events)
{
         for (ILoggingEvent event : events) {
             stream.println(Time.makeDateString(event.getTimeStamp()) + ": " + event.getThreadName()



Mime
View raw message