Skip to content

Instantly share code, notes, and snippets.

@witoldsz
Created January 8, 2017 13:01
Show Gist options
  • Save witoldsz/fd357ac6135cdf34768c52470805bcf9 to your computer and use it in GitHub Desktop.
Save witoldsz/fd357ac6135cdf34768c52470805bcf9 to your computer and use it in GitHub Desktop.
package e2e.util;
import java.util.Date;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Consequence;
import net.serenitybdd.screenplay.EventualConsequence;
import net.serenitybdd.screenplay.Performable;
/**
*
* @author witoldsz
* @param <T>
*/
public class ForceRetryConsequence<T> implements Consequence<T> {
private final EventualConsequence<T> delegate;
protected ForceRetryConsequence(EventualConsequence<T> delegate) {
this.delegate = delegate;
}
public static <T> Consequence<T> eventually(Consequence<T> consequenceThatMightTakeSomeTime) {
return new ForceRetryConsequence<>(new EventualConsequence<>(consequenceThatMightTakeSomeTime));
}
@Override
public void evaluateFor(Actor actor) {
evaluateFor(actor, true);
}
private void evaluateFor(Actor actor, boolean shouldRetry) {
try {
delegate.evaluateFor(actor);
} catch (Throwable e) {
if (shouldRetry) evaluateFor(actor, false);
else throw e;
}
}
@Override
public String toString() {
return delegate.toString();
}
@Override
public Consequence<T> orComplainWith(Class<? extends Error> complaintType) {
return new ForceRetryConsequence<>((EventualConsequence<T>) delegate.orComplainWith(complaintType));
}
@Override
public Consequence<T> orComplainWith(Class<? extends Error> complaintType, String complaintDetails) {
return new ForceRetryConsequence<>((EventualConsequence<T>) delegate.orComplainWith(complaintType, complaintDetails));
}
@Override
public Consequence<T> whenAttemptingTo(Performable p) {
return new ForceRetryConsequence<>((EventualConsequence<T>) delegate.whenAttemptingTo(p));
}
@Override
public Consequence<T> because(String string) {
return new ForceRetryConsequence<>((EventualConsequence<T>) delegate.because(string));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment