Skip to content

Commit

Permalink
added retryUntilOneIsDetails(int maxRetries, Call<V>... calls) in Ret…
Browse files Browse the repository at this point in the history
…ryUtils.java
  • Loading branch information
vculea committed Dec 12, 2024
1 parent 8873f5e commit a8ee17a
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/main/java/com/sdl/selenium/web/utils/RetryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.slf4j.Logger;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -149,9 +148,10 @@ public static <V> Result<V> retryUntilOneIs(int maxRetries, String prefixLog, Ca
V execute = null;
boolean notExpected = true;
int position = 0;
List<String> fields = new ArrayList<>();
StringBuilder field = new StringBuilder();
do {
count++;
field.append("\n|").append(count).append(">");
wait = count < 9 ? fibonacci(wait, fib).getResult() : wait;
Utils.sleep(wait);
int iteration = 0;
Expand All @@ -164,11 +164,11 @@ public static <V> Result<V> retryUntilOneIs(int maxRetries, String prefixLog, Ca
if (!notExpected) {
position = iteration;
long millis = getDuringMillis(startMsCall);
fields.add(position + ":" + millis);
field.append(position).append(":").append(millis);
break;
}
long millis = getDuringMillis(startMsCall);
fields.add(iteration + ":" + millis);
field.append(iteration).append(":").append(millis).append(",");
}
} catch (Exception | AssertionError e) {
if (count >= maxRetries) {
Expand All @@ -182,7 +182,7 @@ public static <V> Result<V> retryUntilOneIs(int maxRetries, String prefixLog, Ca
long duringMs = getDuringMillis(startMs);
log.info((Strings.isNullOrEmpty(prefixLog) ? "" : prefixLog + ":") + "Retry {} and wait {} milliseconds", count, duringMs);
}
return new Result<>(execute, position, count == maxRetries, String.join(",", fields));
return new Result<>(execute, position, count == maxRetries, field.toString());
}

@SafeVarargs
Expand All @@ -194,9 +194,10 @@ public static <V> Result<V> retryUntilOneIsDetails(int maxRetries, String prefix
V execute = null;
boolean notExpected = true;
int position = 0;
List<String> fields = new ArrayList<>();
StringBuilder field = new StringBuilder();
do {
count++;
field.append("\n|").append(count).append(">");
wait = count < 9 ? fibonacci(wait, fib).getResult() : wait;
Utils.sleep(wait);
int iteration = 0;
Expand All @@ -209,11 +210,11 @@ public static <V> Result<V> retryUntilOneIsDetails(int maxRetries, String prefix
if (!notExpected) {
position = iteration;
long millis = getDuringMillis(startMsCall);
fields.add(call.name() + ":" + millis);
field.append(call.name()).append(":").append(millis);
break;
}
long millis = getDuringMillis(startMsCall);
fields.add(call.name() + ":" + millis);
field.append(call.name()).append(":").append(millis).append(",");
}
} catch (Exception | AssertionError e) {
if (count >= maxRetries) {
Expand All @@ -227,7 +228,7 @@ public static <V> Result<V> retryUntilOneIsDetails(int maxRetries, String prefix
long duringMs = getDuringMillis(startMs);
log.info((Strings.isNullOrEmpty(prefixLog) ? "" : prefixLog + ":") + "Retry {} and wait {} milliseconds", count, duringMs);
}
return new Result<>(execute, position, count == maxRetries, String.join(",", fields));
return new Result<>(execute, position, count == maxRetries, field.toString());
}

@SafeVarargs
Expand All @@ -246,9 +247,10 @@ public static <V> Result<V> retryUntilOneIs(Duration duration, String prefixLog,
V execute = null;
boolean notExpected = true;
int position = 0;
List<String> fields = new ArrayList<>();
StringBuilder field = new StringBuilder();
do {
count++;
field.append("\n|").append(count).append(">");
wait = fibonacciSinusoidal(wait, fib).getResult();
Utils.sleep(wait);
int iteration = 0;
Expand All @@ -261,11 +263,11 @@ public static <V> Result<V> retryUntilOneIs(Duration duration, String prefixLog,
if (!notExpected) {
position = iteration;
long millis = getDuringMillis(startMsCall);
fields.add(position + ":" + millis);
field.append(position).append(":").append(millis);
break;
}
long millis = getDuringMillis(startMsCall);
fields.add(iteration + ":" + millis);
field.append(iteration).append(":").append(millis).append(",");
}
} catch (Exception | AssertionError e) {
if (timeIsOver(startMillis, duration)) {
Expand All @@ -279,7 +281,7 @@ public static <V> Result<V> retryUntilOneIs(Duration duration, String prefixLog,
long duringMs = getDuringMillis(startMs);
log.info((Strings.isNullOrEmpty(prefixLog) ? "" : prefixLog + ":") + "Retry {} and wait {} milliseconds", count, duringMs);
}
return new Result<>(execute, position, timeIsOver(startMillis, duration), String.join(",", fields));
return new Result<>(execute, position, timeIsOver(startMillis, duration), field.toString());
}

@SafeVarargs
Expand All @@ -298,9 +300,10 @@ public static <V> Result<V> retryUntilOneIsDetails(Duration duration, String pre
V execute = null;
boolean notExpected = true;
int position = 0;
List<String> fields = new ArrayList<>();
StringBuilder field = new StringBuilder();
do {
count++;
field.append("\n|").append(count).append(">");
wait = fibonacciSinusoidal(wait, fib).getResult();
Utils.sleep(wait);
int iteration = 0;
Expand All @@ -313,11 +316,11 @@ public static <V> Result<V> retryUntilOneIsDetails(Duration duration, String pre
if (!notExpected) {
position = iteration;
long millis = getDuringMillis(startMsCall);
fields.add(call.name() + ":" + millis);
field.append(call.name()).append(":").append(millis);
break;
}
long millis = getDuringMillis(startMsCall);
fields.add(call.name() + ":" + millis);
field.append(call.name()).append(":").append(millis).append(",");
}
} catch (Exception | AssertionError e) {
if (timeIsOver(startMillis, duration)) {
Expand All @@ -331,7 +334,7 @@ public static <V> Result<V> retryUntilOneIsDetails(Duration duration, String pre
long duringMs = getDuringMillis(startMs);
log.info((Strings.isNullOrEmpty(prefixLog) ? "" : prefixLog + ":") + "Retry {} and wait {} milliseconds", count, duringMs);
}
return new Result<>(execute, position, timeIsOver(startMillis, duration), String.join(",", fields));
return new Result<>(execute, position, timeIsOver(startMillis, duration), field.toString());
}

private static <V> V retry(Duration duration, String prefixLog, Callable<V> call, boolean safe) {
Expand Down

0 comments on commit a8ee17a

Please sign in to comment.