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 ecbe0e2 commit 34b119d
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/main/java/com/sdl/selenium/web/utils/RetryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 @@ -148,6 +149,7 @@ 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<>();
do {
count++;
wait = count < 9 ? fibonacci(wait, fib).getResult() : wait;
Expand All @@ -156,12 +158,17 @@ public static <V> Result<V> retryUntilOneIs(int maxRetries, String prefixLog, Ca
try {
for (Callable<V> call : calls) {
iteration++;
long startMsCall = System.currentTimeMillis();
execute = call.call();
notExpected = isNotExpected(execute);
if (!notExpected) {
position = iteration;
long millis = getDuringMillis(startMsCall);
fields.add(position + ":" + millis);
break;
}
long millis = getDuringMillis(startMsCall);
fields.add(iteration + ":" + millis);
}
} catch (Exception | AssertionError e) {
if (count >= maxRetries) {
Expand All @@ -175,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);
return new Result<>(execute, position, count == maxRetries, String.join(",", fields));
}

@SafeVarargs
Expand All @@ -187,7 +194,7 @@ public static <V> Result<V> retryUntilOneIsDetails(int maxRetries, String prefix
V execute = null;
boolean notExpected = true;
int position = 0;
String field = "";
List<String> fields = new ArrayList<>();
do {
count++;
wait = count < 9 ? fibonacci(wait, fib).getResult() : wait;
Expand All @@ -196,13 +203,17 @@ public static <V> Result<V> retryUntilOneIsDetails(int maxRetries, String prefix
try {
for (Call<V> call : calls) {
iteration++;
long startMsCall = System.currentTimeMillis();
execute = call.callable().call();
notExpected = isNotExpected(execute);
if (!notExpected) {
position = iteration;
field = call.name();
long millis = getDuringMillis(startMsCall);
fields.add(call.name() + ":" + millis);
break;
}
long millis = getDuringMillis(startMsCall);
fields.add(call.name() + ":" + millis);
}
} catch (Exception | AssertionError e) {
if (count >= maxRetries) {
Expand All @@ -216,7 +227,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, field);
return new Result<>(execute, position, count == maxRetries, String.join(",", fields));
}

@SafeVarargs
Expand All @@ -235,6 +246,7 @@ public static <V> Result<V> retryUntilOneIs(Duration duration, String prefixLog,
V execute = null;
boolean notExpected = true;
int position = 0;
List<String> fields = new ArrayList<>();
do {
count++;
wait = fibonacciSinusoidal(wait, fib).getResult();
Expand All @@ -243,12 +255,17 @@ public static <V> Result<V> retryUntilOneIs(Duration duration, String prefixLog,
try {
for (Callable<V> call : calls) {
iteration++;
long startMsCall = System.currentTimeMillis();
execute = call.call();
notExpected = isNotExpected(execute);
if (!notExpected) {
position = iteration;
long millis = getDuringMillis(startMsCall);
fields.add(position + ":" + millis);
break;
}
long millis = getDuringMillis(startMsCall);
fields.add(iteration + ":" + millis);
}
} catch (Exception | AssertionError e) {
if (timeIsOver(startMillis, duration)) {
Expand All @@ -262,7 +279,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));
return new Result<>(execute, position, timeIsOver(startMillis, duration), String.join(",", fields));
}

@SafeVarargs
Expand All @@ -281,7 +298,7 @@ public static <V> Result<V> retryUntilOneIsDetails(Duration duration, String pre
V execute = null;
boolean notExpected = true;
int position = 0;
String field = "";
List<String> fields = new ArrayList<>();
do {
count++;
wait = fibonacciSinusoidal(wait, fib).getResult();
Expand All @@ -290,13 +307,17 @@ public static <V> Result<V> retryUntilOneIsDetails(Duration duration, String pre
try {
for (Call<V> call : calls) {
iteration++;
long startMsCall = System.currentTimeMillis();
execute = call.callable().call();
notExpected = isNotExpected(execute);
if (!notExpected) {
position = iteration;
field = call.name();
long millis = getDuringMillis(startMsCall);
fields.add(call.name() + ":" + millis);
break;
}
long millis = getDuringMillis(startMsCall);
fields.add(call.name() + ":" + millis);
}
} catch (Exception | AssertionError e) {
if (timeIsOver(startMillis, duration)) {
Expand All @@ -310,7 +331,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), field);
return new Result<>(execute, position, timeIsOver(startMillis, duration), String.join(",", fields));
}

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

0 comments on commit 34b119d

Please sign in to comment.