You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2013/10/29 11:28:40 UTC

svn commit: r1536640 - in /sling/trunk/bundles/commons/testing/src: main/java/org/apache/sling/commons/testing/junit/ test/java/org/apache/sling/commons/testing/junit/

Author: bdelacretaz
Date: Tue Oct 29 10:28:40 2013
New Revision: 1536640

URL: http://svn.apache.org/r1536640
Log:
SLING-3212 - JUnit RetryRule implementation

Added:
    sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/
    sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/Retry.java
    sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/RetryRule.java
    sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/junit/
    sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/junit/RetryRuleTest.java

Added: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/Retry.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/Retry.java?rev=1536640&view=auto
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/Retry.java (added)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/Retry.java Tue Oct 29 10:28:40 2013
@@ -0,0 +1,33 @@
+/*
+ * 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 ASF 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.commons.testing.junit;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/** Used to annotate JUnit tests as being retryable */
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Retry {
+    
+    /** Retries for at most this many milliseconds */ 
+    int timeoutMsec() default -1;
+    
+    /** Wait this many milliseconds between retries */
+    int intervalMsec() default -1;
+}	

Added: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/RetryRule.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/RetryRule.java?rev=1536640&view=auto
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/RetryRule.java (added)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/junit/RetryRule.java Tue Oct 29 10:28:40 2013
@@ -0,0 +1,112 @@
+/*
+ * 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 ASF 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.commons.testing.junit;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** JUnit Rule that implements retries, see tests for usage example */
+public class RetryRule implements TestRule {
+    
+    public static final int DEFAULT_DEFAULT_TIMEOUT_MSEC = 5000;
+    public static final int DEFAULT_DEFAULT_INTERVAL_MSEC = 500;
+    
+    private static final Logger log = LoggerFactory.getLogger(RetryRule.class);
+    
+    private final long defaultTimeout;
+    private final long defaultInterval;
+
+    /** Create a RetryRule with default values for default timeout and interval */
+    public RetryRule() {
+        this(DEFAULT_DEFAULT_TIMEOUT_MSEC, DEFAULT_DEFAULT_INTERVAL_MSEC);
+    }
+    
+    /** Create a RetryRule with specific values for default timeout and interval */
+    public RetryRule(long defaultTimeout, long defaultInterval) {
+        this.defaultTimeout = defaultTimeout;
+        this.defaultInterval = defaultInterval;
+    }
+    
+    @Override
+    public String toString() {
+        return new StringBuilder()
+        .append(getClass().getSimpleName())
+        .append(", default interval=")
+        .append(defaultInterval)
+        .append(" msec, default timeout=")
+        .append(defaultTimeout)
+        .append(" msec")
+        .toString();
+    }
+    
+    public Statement apply(final Statement statement, final Description description) {
+        return new Statement() {
+            
+            private Throwable eval(Statement stmt) {
+                try {
+                    stmt.evaluate();
+                } catch(Throwable t) {
+                    return t;
+                }
+                return null;
+            }
+            
+            @Override
+            public void evaluate() throws Throwable {
+                int retries = 0;
+                Throwable t = eval(statement);
+                if(t != null) {
+                    final Retry r = description.getAnnotation(Retry.class);
+                    if(r != null) {
+                        final long timeout = System.currentTimeMillis() + getTimeout(r.timeoutMsec());
+                        while(System.currentTimeMillis() < timeout) {
+                            retries++;
+                            t = eval(statement);
+                            if(t == null) {
+                                break;
+                            }
+                            Thread.sleep(getInterval(r.intervalMsec()));
+                        }
+                    }
+                }
+                if(t != null) {
+                    if(retries > 0) {
+                        log.debug("{} fails after retrying {} time(s)", statement, retries);
+                    }
+                    throw t;
+                }
+                if(retries > 0) {
+                    log.debug("{} succeeds after retrying {} time(s)", statement, retries);
+                }
+            }
+        };
+    }
+    
+    long getTimeout(long ruleTimeout) {
+        return ruleTimeout > 0 ? ruleTimeout : defaultTimeout;
+    }
+    
+    long getInterval(long ruleInterval) {
+        return ruleInterval > 0 ? ruleInterval : defaultInterval;
+    }
+    
+}
\ No newline at end of file

Added: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/junit/RetryRuleTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/junit/RetryRuleTest.java?rev=1536640&view=auto
==============================================================================
--- sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/junit/RetryRuleTest.java (added)
+++ sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/junit/RetryRuleTest.java Tue Oct 29 10:28:40 2013
@@ -0,0 +1,110 @@
+/*
+ * 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 ASF 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.commons.testing.junit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class RetryRuleTest {
+    private int setupCounter;
+    private int callCount = 0;
+    private long callTime = -1;
+
+    @Rule
+    public final RetryRule retryRule = new RetryRule();
+    
+    @Before
+    public void setup() {
+        setupCounter = 0;
+    }
+    
+    @Retry
+    @Test
+    public void testDefaultParameters() {
+        callCount++;
+        setupCounter++;
+        
+        if(callTime > 0) {
+            final long delta = System.currentTimeMillis();
+            assertTrue("Expecting at least 500 msec between calls", delta >= 500);
+        }
+        callTime = System.currentTimeMillis();
+        
+        assertTrue("Expecting to be called several times before passing", callCount > 3);
+        assertEquals("Expecting setup() to be called before every retry", 1, setupCounter);
+
+        // Once we pass, reset counters for other tests
+        callTime = -1;
+        callCount = 0;
+    }
+    
+    @Retry
+    @Test
+    public void testRetryOnException() throws Exception {
+        callCount++;
+        setupCounter++;
+
+        if(callCount <= 3) {
+            throw new Exception("Expecting to be called several times before passing");
+        }
+        
+        assertEquals("Expecting setup() to be called before every retry", 1, setupCounter);
+
+        // Once we pass, reset counters for other tests
+        callTime = -1;
+        callCount = 0;
+    }
+    
+    @Retry(timeoutMsec=500, intervalMsec=1)
+    @Test
+    public void testCustomParameters() {
+        callCount++;
+        setupCounter++;
+        
+        assertTrue("Expecting to be called many times before passing", callCount > 100);
+        assertEquals("Expecting setup() to be called before every retry", 1, setupCounter);
+        
+        // Once we pass, reset callTime for other tests
+        callTime = -1;
+        callCount = 0;
+    }
+    
+    @Test
+    public void testDefaultDefaultTimings() {
+        final RetryRule r = new RetryRule();
+        assertEquals("Expecting default default timeout", RetryRule.DEFAULT_DEFAULT_TIMEOUT_MSEC, r.getTimeout(-1));
+        assertEquals("Expecting default default interval", RetryRule.DEFAULT_DEFAULT_INTERVAL_MSEC, r.getInterval(-1));
+    }
+    
+    @Test
+    public void testSpecifiedDefaultTimings() {
+        final RetryRule r = new RetryRule(12, 42);
+        assertEquals("Expecting specified default timeout", 12, r.getTimeout(-1));
+        assertEquals("Expecting specified default interval", 42, r.getInterval(-1));
+    }
+    
+    @Test
+    public void testRuleTimings() {
+        final RetryRule r = new RetryRule(12, 42);
+        assertEquals("Expecting timeout from Rule", 1012, r.getTimeout(1012));
+        assertEquals("Expecting interval from Rule", 1024, r.getInterval(1024));
+    }
+}