Return-Path: X-Original-To: apmail-curator-commits-archive@minotaur.apache.org Delivered-To: apmail-curator-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1A8341745F for ; Mon, 31 Aug 2015 14:20:04 +0000 (UTC) Received: (qmail 28311 invoked by uid 500); 31 Aug 2015 14:20:04 -0000 Delivered-To: apmail-curator-commits-archive@curator.apache.org Received: (qmail 28257 invoked by uid 500); 31 Aug 2015 14:20:04 -0000 Mailing-List: contact commits-help@curator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@curator.apache.org Delivered-To: mailing list commits@curator.apache.org Received: (qmail 28235 invoked by uid 99); 31 Aug 2015 14:20:03 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 31 Aug 2015 14:20:03 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D04B4DFE20; Mon, 31 Aug 2015 14:20:03 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: randgalt@apache.org To: commits@curator.apache.org Date: Mon, 31 Aug 2015 14:20:03 -0000 Message-Id: <31c8b4d35f9b4b3aa3451355ce3a1896@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/8] curator git commit: CURATOR-239 - Adding RetryForever retry policy Repository: curator Updated Branches: refs/heads/CURATOR-3.0 fed0bee84 -> d01eabe5b 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-3.0 Commit: 6e8c10847d4b453a1a1aeafcb6c58f51889f2d1f Parents: 870b4d5 Author: Leandro Nunes Authored: Fri Jul 24 00:06:26 2015 +0100 Committer: Leandro Nunes 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 allowsRetry. + */ +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); + } + } }