You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2023/05/17 16:13:47 UTC
[shardingsphere] branch master updated: Fix sonar issues on RetryExecutor (#25745)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new c2bf3da0633 Fix sonar issues on RetryExecutor (#25745)
c2bf3da0633 is described below
commit c2bf3da0633196c3955f267682e81e11f5276035
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu May 18 00:13:34 2023 +0800
Fix sonar issues on RetryExecutor (#25745)
* Fix sonar issues on RetryExecutor
---
.../infra/util/retry/RetryExecutor.java | 38 ++++----------
.../infra/util/retry/RetryExecutorTest.java | 48 ++++++-----------
.../util/retry/fixture/RetryFunctionFixture.java | 60 ----------------------
3 files changed, 25 insertions(+), 121 deletions(-)
diff --git a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/retry/RetryExecutor.java b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/retry/RetryExecutor.java
index 23c34927b49..c1f69df898a 100644
--- a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/retry/RetryExecutor.java
+++ b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/retry/RetryExecutor.java
@@ -20,8 +20,7 @@ package org.apache.shardingsphere.infra.util.retry;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
-import java.util.function.BiFunction;
-import java.util.function.Function;
+import java.util.function.Predicate;
/**
* Retry executor.
@@ -33,38 +32,19 @@ public final class RetryExecutor {
private final long intervalMillis;
- private long expendMillis;
+ private long elapsedMillis;
/**
* Execute and retry.
*
- * @param function function to be executed
+ * @param predicate predicate to be executed
* @param arg argument
* @param <T> argument type
- * @return execute result
+ * @return execute result success or not
*/
- public <T> boolean execute(final Function<T, Boolean> function, final T arg) {
+ public <T> boolean execute(final Predicate<T> predicate, final T arg) {
do {
- if (function.apply(arg)) {
- return true;
- }
- } while (!isTimeout());
- return false;
- }
-
- /**
- * Execute and retry.
- *
- * @param function function to be executed
- * @param arg1 the first argument
- * @param arg2 the second argument
- * @param <T> the first argument type
- * @param <U> the second argument type
- * @return execute result
- */
- public <T, U> boolean execute(final BiFunction<T, U, Boolean> function, final T arg1, final U arg2) {
- do {
- if (function.apply(arg1, arg2)) {
+ if (predicate.test(arg)) {
return true;
}
} while (!isTimeout());
@@ -74,10 +54,10 @@ public final class RetryExecutor {
@SneakyThrows(InterruptedException.class)
private boolean isTimeout() {
Thread.sleep(intervalMillis);
- if (-1L == timeoutMillis) {
+ if (timeoutMillis < 0L) {
return false;
}
- expendMillis += intervalMillis;
- return expendMillis > timeoutMillis;
+ elapsedMillis += intervalMillis;
+ return elapsedMillis > timeoutMillis;
}
}
diff --git a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/retry/RetryExecutorTest.java b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/retry/RetryExecutorTest.java
index 19dba6fc853..234eace517e 100644
--- a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/retry/RetryExecutorTest.java
+++ b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/retry/RetryExecutorTest.java
@@ -17,51 +17,35 @@
package org.apache.shardingsphere.infra.util.retry;
-import org.apache.shardingsphere.infra.util.retry.fixture.RetryFunctionFixture;
import org.junit.jupiter.api.Test;
+import java.util.function.Predicate;
+
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class RetryExecutorTest {
@Test
- void assertExecuteForOnce() {
- long timeout = 5L;
- long target = 10L;
- RetryFunctionFixture withFunc = new RetryFunctionFixture(target);
- assertTrue(new RetryExecutor(timeout, 2L).execute(withFunc.moveAdd(), 12L));
- RetryFunctionFixture withBiFunc = new RetryFunctionFixture(target);
- assertTrue(new RetryExecutor(timeout, 2L).execute(withBiFunc.moveMulti(), 4L, 3L));
- }
-
- @Test
- void assertExecuteForInProgress() {
- long timeout = 5L;
- long target = 20L;
- RetryFunctionFixture withFunc = new RetryFunctionFixture(target);
- assertTrue(new RetryExecutor(timeout, 2L).execute(withFunc.moveAdd(), 12L));
- RetryFunctionFixture withBiFunc = new RetryFunctionFixture(target);
- assertTrue(new RetryExecutor(timeout, 2L).execute(withBiFunc.moveMulti(), 4L, 3L));
+ void assertExecute() {
+ assertTrue(new RetryExecutor(5L, 2L).execute(value -> value > 0, 1));
}
@Test
- void assertExecuteForTimeout() {
- long timeout = 5L;
- long target = 100L;
- RetryFunctionFixture withFunc = new RetryFunctionFixture(target);
- assertFalse(new RetryExecutor(timeout, 2L).execute(withFunc.moveAdd(), 12L));
- RetryFunctionFixture withBiFunc = new RetryFunctionFixture(target);
- assertFalse(new RetryExecutor(timeout, 2L).execute(withBiFunc.moveMulti(), 4L, 3L));
+ void assertExecuteTimeout() {
+ assertFalse(new RetryExecutor(5L, 2L).execute(value -> false, -1));
}
@Test
- void assertExecuteForWait() {
- long timeout = -1L;
- long target = 100L;
- RetryFunctionFixture withFunc = new RetryFunctionFixture(target);
- assertTrue(new RetryExecutor(timeout, 2L).execute(withFunc.moveAdd(), 12L));
- RetryFunctionFixture withBiFunc = new RetryFunctionFixture(target);
- assertTrue(new RetryExecutor(timeout, 2L).execute(withBiFunc.moveMulti(), 4L, 3L));
+ void assertExecuteWithNeverTimeout() {
+ assertTrue(new RetryExecutor(-1L, 2L).execute(new Predicate<Integer>() {
+
+ private int currentCount;
+
+ @Override
+ public boolean test(final Integer value) {
+ return ++currentCount > 10;
+ }
+ }, null));
}
}
diff --git a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/retry/fixture/RetryFunctionFixture.java b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/retry/fixture/RetryFunctionFixture.java
deleted file mode 100644
index e8ebecbf78d..00000000000
--- a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/retry/fixture/RetryFunctionFixture.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * 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.shardingsphere.infra.util.retry.fixture;
-
-import java.util.function.BiFunction;
-import java.util.function.Function;
-
-public final class RetryFunctionFixture {
-
- private long target;
-
- private long step;
-
- private final Function<? super Long, Boolean> add = arg -> {
- step += arg;
- return step > target;
- };
-
- private final BiFunction<? super Long, ? super Long, Boolean> multi = (arg1, arg2) -> {
- step += arg1 * arg2;
- return step > target;
- };
-
- public RetryFunctionFixture(final long target) {
- this.target = target;
- }
-
- /**
- * Function of add.
- *
- * @return execute Function
- */
- public Function<? super Long, Boolean> moveAdd() {
- return add;
- }
-
- /**
- * BiFunction of multi.
- *
- * @return execute BiFunction
- */
- public BiFunction<? super Long, ? super Long, Boolean> moveMulti() {
- return multi;
- }
-}