sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stefane...@apache.org
Subject svn commit: r1573182 - in /sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl: cluster/ClusterLoadTest.java setup/WithholdingAppender.java
Date Sat, 01 Mar 2014 14:36:13 GMT
Author: stefanegli
Date: Sat Mar  1 14:36:12 2014
New Revision: 1573182

URL: http://svn.apache.org/r1573182
Log:
no-jira : adding WithholdingAppender which entirely muted log output as long everything is
fine, and instead writes the withheld (ie muted) log output in case of a failure - to reduce
log output of ClusterLoadTest in success cases

Added:
    sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/setup/WithholdingAppender.java
Modified:
    sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/cluster/ClusterLoadTest.java

Modified: sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/cluster/ClusterLoadTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/cluster/ClusterLoadTest.java?rev=1573182&r1=1573181&r2=1573182&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/cluster/ClusterLoadTest.java
(original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/cluster/ClusterLoadTest.java
Sat Mar  1 14:36:12 2014
@@ -11,6 +11,7 @@ import java.util.Random;
 import org.apache.sling.discovery.impl.common.resource.EstablishedInstanceDescription;
 import org.apache.sling.discovery.impl.common.resource.IsolatedInstanceDescription;
 import org.apache.sling.discovery.impl.setup.Instance;
+import org.apache.sling.discovery.impl.setup.WithholdingAppender;
 import org.apache.sling.testing.tools.retry.RetryLoop;
 import org.junit.After;
 import org.junit.Test;
@@ -114,7 +115,29 @@ public class ClusterLoadTest {
     	doTest(7, 10);
     }
 
-	private void doTest(final int size, final int loopCnt) throws Throwable {
+    private void doTest(final int size, final int loopCnt) throws Throwable {
+        WithholdingAppender withholdingAppender = null;
+        boolean failure = true;
+        try{
+            logger.info("doTest("+size+","+loopCnt+"): muting log output...");
+            withholdingAppender = WithholdingAppender.install();
+            doDoTest(size, loopCnt);
+            failure = false;
+        } finally {
+            if (withholdingAppender!=null) {
+                if (failure) {
+                    logger.info("doTest("+size+","+loopCnt+"): writing muted log output due
to failure...");
+                }
+                withholdingAppender.release(failure);
+                if (!failure) {
+                    logger.info("doTest("+size+","+loopCnt+"): not writing muted log output
due to success...");
+                }
+            }
+            logger.info("doTest("+size+","+loopCnt+"): unmuted log output.");
+        }
+    }
+    
+	private void doDoTest(final int size, final int loopCnt) throws Throwable {
 		if (size<2) {
 			fail("can only test 2 or more instances");
 		}

Added: sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/setup/WithholdingAppender.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/setup/WithholdingAppender.java?rev=1573182&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/setup/WithholdingAppender.java
(added)
+++ sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/setup/WithholdingAppender.java
Sat Mar  1 14:36:12 2014
@@ -0,0 +1,82 @@
+/*
+ * 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 SF 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.sling.discovery.impl.setup;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.net.URL;
+
+import org.apache.log4j.Layout;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.PatternLayout;
+import org.apache.log4j.PropertyConfigurator;
+import org.apache.log4j.WriterAppender;
+
+public class WithholdingAppender extends WriterAppender {
+
+    private final ByteArrayOutputStream baos;
+    private final Writer writer;
+
+    /**
+     * Install the WithholdingAppender, essentially muting all logging 
+     * and withholding it until release() is called
+     * @return the WithholdingAppender that can be used to get the 
+     * withheld log output
+     */
+    public static WithholdingAppender install() {
+        LogManager.getRootLogger().removeAllAppenders();
+        final WithholdingAppender withholdingAppender = new WithholdingAppender(
+                new PatternLayout("%d{dd.MM.yyyy HH:mm:ss} *%-5p* [%t] %c{1}: %m\n"));
+        LogManager.getRootLogger().addAppender(withholdingAppender);
+        return withholdingAppender;
+    }
+    
+    /**
+     * Release this WithholdingAppender and optionally dump what was
+     * withheld (eg in case of an exception)
+     * @param dumpToSysout
+     */
+    public void release(boolean dumpToSysout) {
+        LogManager.resetConfiguration();
+        URL log4jPropertiesFile = getClass().getResource("/log4j.properties");
+        PropertyConfigurator.configure(log4jPropertiesFile);
+        if (dumpToSysout) {
+            String withheldLogoutput = getBuffer();
+            System.out.println(withheldLogoutput);
+        }
+    }
+    
+    public WithholdingAppender(Layout layout) {
+        this.layout = layout;
+        this.baos = new ByteArrayOutputStream();
+        this.writer = new BufferedWriter(new OutputStreamWriter(baos));
+        this.setWriter(writer);
+    }
+    
+    public String getBuffer() {
+        try{
+            writer.flush();
+        } catch(IOException e) {
+            // ignore
+        }
+        return baos.toString();
+    }
+}



Mime
View raw message