GERONIMO-6674 ensure @Retry(jitter=0) passes
diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardExtension.java b/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardExtension.java
index 0f7568a..c3a85f9 100644
--- a/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardExtension.java
+++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardExtension.java
@@ -1,3 +1,21 @@
+/*
+ * 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.safeguard.impl.cdi;
import static java.util.Collections.emptyMap;
diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/retry/BaseRetryInterceptor.java b/safeguard-impl/src/main/java/org/apache/safeguard/impl/retry/BaseRetryInterceptor.java
index ffd2af4..61c7ba8 100644
--- a/safeguard-impl/src/main/java/org/apache/safeguard/impl/retry/BaseRetryInterceptor.java
+++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/retry/BaseRetryInterceptor.java
@@ -117,7 +117,10 @@
throw error;
}
modelRef.retries.inc();
- Thread.sleep(modelRef.nextPause());
+ final long pause = modelRef.nextPause();
+ if (pause > 0) {
+ Thread.sleep(pause);
+ }
return ctx;
}
@@ -195,7 +198,7 @@
private long nextPause() {
final ThreadLocalRandom random = ThreadLocalRandom.current();
return TimeUnit.NANOSECONDS
- .toMillis(min(maxDuration, max(0, ((random.nextBoolean() ? 1 : -1) * delay) + random.nextLong(jitter))));
+ .toMillis(min(maxDuration, max(0, ((random.nextBoolean() ? 1 : -1) * delay) + (jitter == 0 ? 0 : random.nextLong(jitter)))));
}
}
diff --git a/safeguard-impl/src/test/java/org/apache/safeguard/impl/tck/RetryZeroJitterTest.java b/safeguard-impl/src/test/java/org/apache/safeguard/impl/tck/RetryZeroJitterTest.java
new file mode 100644
index 0000000..c228c31
--- /dev/null
+++ b/safeguard-impl/src/test/java/org/apache/safeguard/impl/tck/RetryZeroJitterTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.safeguard.impl.tck;
+
+import javax.inject.Inject;
+
+import org.apache.safeguard.impl.tck.retry.ZeoJitterBean;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.testng.Arquillian;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.testng.annotations.Test;
+
+public class RetryZeroJitterTest extends Arquillian {
+ @Inject
+ private ZeoJitterBean bean;
+
+ @Deployment
+ public static WebArchive archive() {
+ return ShrinkWrap
+ .create(WebArchive.class, "RetryZeroJitterTest.war")
+ .addAsLibrary(ShrinkWrap
+ .create(JavaArchive.class, "RetryZeroJitterTest.jar")
+ .addClass(ZeoJitterBean.class)
+ .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
+ .as(JavaArchive.class));
+ }
+
+ @Test
+ public void testJitterAtZero() {
+ bean.execute(); // just ensure it passes
+ }
+}
diff --git a/safeguard-impl/src/test/java/org/apache/safeguard/impl/tck/retry/ZeoJitterBean.java b/safeguard-impl/src/test/java/org/apache/safeguard/impl/tck/retry/ZeoJitterBean.java
new file mode 100644
index 0000000..837684a
--- /dev/null
+++ b/safeguard-impl/src/test/java/org/apache/safeguard/impl/tck/retry/ZeoJitterBean.java
@@ -0,0 +1,36 @@
+/*
+ * 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.safeguard.impl.tck.retry;
+
+import javax.enterprise.context.ApplicationScoped;
+
+import org.eclipse.microprofile.faulttolerance.Retry;
+
+@ApplicationScoped
+public class ZeoJitterBean {
+ private int remaining = 0;
+
+ @Retry(jitter = 0, maxRetries = 1)
+ public void execute() {
+ if (remaining-- == 0) {
+ return;
+ }
+ throw new IllegalStateException("will succeed later");
+ }
+}