polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject [1/2] zest-java git commit: Introduce JUnit Retry Rule
Date Wed, 15 Jun 2016 17:04:46 GMT
Repository: zest-java
Updated Branches:
  refs/heads/develop 7862b9cc3 -> 9bd8a764b


Introduce JUnit Retry Rule


Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/119bf53d
Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/119bf53d
Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/119bf53d

Branch: refs/heads/develop
Commit: 119bf53da1ba659de30feec4a5b0764189d84b18
Parents: 7862b9c
Author: Paul Merlin <paulmerlin@apache.org>
Authored: Wed Jun 15 19:03:33 2016 +0200
Committer: Paul Merlin <paulmerlin@apache.org>
Committed: Wed Jun 15 19:03:33 2016 +0200

----------------------------------------------------------------------
 .../org/apache/zest/test/util/RetryRule.java    | 55 ++++++++++++++++++++
 .../apache/zest/test/cache/RetryRuleTest.java   | 22 ++++++++
 2 files changed, 77 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zest-java/blob/119bf53d/core/testsupport/src/main/java/org/apache/zest/test/util/RetryRule.java
----------------------------------------------------------------------
diff --git a/core/testsupport/src/main/java/org/apache/zest/test/util/RetryRule.java b/core/testsupport/src/main/java/org/apache/zest/test/util/RetryRule.java
new file mode 100644
index 0000000..bd8436a
--- /dev/null
+++ b/core/testsupport/src/main/java/org/apache/zest/test/util/RetryRule.java
@@ -0,0 +1,55 @@
+package org.apache.zest.test.util;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * JUnit Rule to retry tests.
+ * Useful for flaky tests.
+ */
+public class RetryRule implements TestRule
+{
+    private static final int MAX_DEFAULT = 3;
+
+    private final int max;
+    private int attempts = 0;
+
+    public RetryRule()
+    {
+        this( MAX_DEFAULT );
+    }
+
+    public RetryRule( int max )
+    {
+        this.max = max;
+    }
+
+    @Override
+    public Statement apply( Statement base, Description description )
+    {
+        return new Statement()
+        {
+            @Override
+            public void evaluate() throws Throwable
+            {
+                attempts++;
+                try
+                {
+                    base.evaluate();
+                }
+                catch( Throwable ex )
+                {
+                    if( attempts <= max )
+                    {
+                        evaluate();
+                    }
+                    else
+                    {
+                        throw ex;
+                    }
+                }
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/zest-java/blob/119bf53d/core/testsupport/src/test/java/org/apache/zest/test/cache/RetryRuleTest.java
----------------------------------------------------------------------
diff --git a/core/testsupport/src/test/java/org/apache/zest/test/cache/RetryRuleTest.java
b/core/testsupport/src/test/java/org/apache/zest/test/cache/RetryRuleTest.java
new file mode 100644
index 0000000..9979709
--- /dev/null
+++ b/core/testsupport/src/test/java/org/apache/zest/test/cache/RetryRuleTest.java
@@ -0,0 +1,22 @@
+package org.apache.zest.test.cache;
+
+import org.apache.zest.test.util.RetryRule;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class RetryRuleTest
+{
+    @Rule public RetryRule retry = new RetryRule( 3 );
+
+    static int count = 0;
+
+    @Test
+    public void test()
+    {
+        count++;
+        assertThat( count, is( 3 ) );
+    }
+}


Mime
View raw message