curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [01/14] curator git commit: CURATOR-239 - Adding RetryForever retry policy
Date Tue, 01 Sep 2015 02:48:18 GMT
Repository: curator
Updated Branches:
  refs/heads/CURATOR-247 b8240a1df -> 667b1d2c7


CURATOR-239 - Adding RetryForever retry policy


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/6e8c1084
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/6e8c1084
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/6e8c1084

Branch: refs/heads/CURATOR-247
Commit: 6e8c10847d4b453a1a1aeafcb6c58f51889f2d1f
Parents: 870b4d5
Author: Leandro Nunes <leandro.nunes@blip.pt>
Authored: Fri Jul 24 00:06:26 2015 +0100
Committer: Leandro Nunes <leandro.nunes@blip.pt>
Committed: Fri Jul 24 00:06:26 2015 +0100

----------------------------------------------------------------------
 .../org/apache/curator/retry/RetryForever.java  | 58 ++++++++++++++++++++
 .../java/org/apache/curator/TestRetryLoop.java  | 20 +++++++
 2 files changed, 78 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/6e8c1084/curator-client/src/main/java/org/apache/curator/retry/RetryForever.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/retry/RetryForever.java b/curator-client/src/main/java/org/apache/curator/retry/RetryForever.java
new file mode 100644
index 0000000..27444b9
--- /dev/null
+++ b/curator-client/src/main/java/org/apache/curator/retry/RetryForever.java
@@ -0,0 +1,58 @@
+/**
+ * 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.curator.retry;
+
+import org.apache.curator.RetryPolicy;
+import org.apache.curator.RetrySleeper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.TimeUnit;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * {@link RetryPolicy} implementation that always <i>allowsRetry</i>.
+ */
+public class RetryForever implements RetryPolicy
+{
+    private static final Logger log = LoggerFactory.getLogger(RetryForever.class);
+
+    private final int retryIntervalMs;
+
+    public RetryForever(int retryIntervalMs)
+    {
+        checkArgument(retryIntervalMs > 0);
+        this.retryIntervalMs = retryIntervalMs;
+    }
+
+    @Override
+    public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper)
+    {
+        try
+        {
+            sleeper.sleepFor(retryIntervalMs, TimeUnit.MILLISECONDS);
+        }
+        catch (InterruptedException e)
+        {
+            log.warn("Error occurred while sleeping", e);
+        }
+        return true;
+    }
+}

http://git-wip-us.apache.org/repos/asf/curator/blob/6e8c1084/curator-client/src/test/java/org/apache/curator/TestRetryLoop.java
----------------------------------------------------------------------
diff --git a/curator-client/src/test/java/org/apache/curator/TestRetryLoop.java b/curator-client/src/test/java/org/apache/curator/TestRetryLoop.java
index 0fa9020..17bb91e 100644
--- a/curator-client/src/test/java/org/apache/curator/TestRetryLoop.java
+++ b/curator-client/src/test/java/org/apache/curator/TestRetryLoop.java
@@ -19,14 +19,19 @@
 package org.apache.curator;
 
 import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.curator.retry.RetryForever;
 import org.apache.curator.retry.RetryOneTime;
 import org.apache.curator.test.BaseClassForTests;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.ZooDefs;
+import org.mockito.Mockito;
 import org.testng.Assert;
 import org.testng.annotations.Test;
+
 import java.util.concurrent.TimeUnit;
 
+import static org.mockito.Mockito.times;
+
 public class TestRetryLoop extends BaseClassForTests
 {
     @Test
@@ -142,4 +147,19 @@ public class TestRetryLoop extends BaseClassForTests
             client.close();
         }
     }
+
+    @Test
+    public void     testRetryForever() throws Exception
+    {
+        int retryIntervalMs = 1;
+        RetrySleeper sleeper = Mockito.mock(RetrySleeper.class);
+        RetryForever retryForever = new RetryForever(retryIntervalMs);
+
+        for (int i = 0; i < 10; i++)
+        {
+            boolean allowed = retryForever.allowRetry(i, 0, sleeper);
+            Assert.assertTrue(allowed);
+            Mockito.verify(sleeper, times(i + 1)).sleepFor(retryIntervalMs, TimeUnit.MILLISECONDS);
+        }
+    }
 }


Mime
View raw message