Skip to content

Commit

Permalink
Add assertConsistently
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinsbd authored and ebyhr committed Dec 12, 2024
1 parent 9499dc8 commit 9b24b74
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ public static <E extends Exception> void assertEventually(Duration timeout, Dura
}
}

public static <E extends Exception> void assertConsistently(Duration timeout, Duration retryFrequency, Assert.CheckedRunnable<E> assertion)
throws E
{
long start = System.nanoTime();
while (!Thread.currentThread().isInterrupted()) {
assertion.run();

if (Duration.nanosSince(start).compareTo(timeout) > 0) {
return;
}

try {
Thread.sleep(retryFrequency.toMillis());
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
}

public interface CheckedRunnable<E extends Exception>
{
void run()
Expand Down

0 comments on commit 9b24b74

Please sign in to comment.