This is an automated email from the ASF dual-hosted git repository.
nfsantos pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/trunk by this push:
new 0d39afccdc OAK-11777 - Fix flaky test. (#2353)
0d39afccdc is described below
commit 0d39afccdc09a9a35ebb1b34914a1fe41a2fa5f5
Author: Nuno Santos <[email protected]>
AuthorDate: Tue Jun 24 13:50:22 2025 +0200
OAK-11777 - Fix flaky test. (#2353)
---
.../elastic/index/ElasticsearchRetryPolicyTest.java | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git
a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticsearchRetryPolicyTest.java
b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticsearchRetryPolicyTest.java
index 5bc54edee4..0c1cc317bb 100644
---
a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticsearchRetryPolicyTest.java
+++
b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticsearchRetryPolicyTest.java
@@ -51,13 +51,17 @@ public class ElasticsearchRetryPolicyTest {
}
public void execute() throws IOException {
+ long now = System.nanoTime();
+ long elapsedTimeMillis;
if (firstExecutionTime == -1) {
- firstExecutionTime = System.nanoTime();
+ firstExecutionTime = now;
+ elapsedTimeMillis = 0;
+ } else {
+ elapsedTimeMillis = TimeUnit.NANOSECONDS.toMillis(now -
firstExecutionTime);
}
- long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()
- firstExecutionTime);
- if (elapsedTime < succeedAfterElapsedTimeMillis) {
+ if (elapsedTimeMillis < succeedAfterElapsedTimeMillis) {
// Simulate a failure until the elapsed time is reached
- throw new IOException("Simulated failure. Elapsed time: " +
elapsedTime + "ms, will succeed after " + succeedAfterElapsedTimeMillis +
"ms.");
+ throw new IOException("Simulated failure. Elapsed time: " +
elapsedTimeMillis + "ms, will succeed after " + succeedAfterElapsedTimeMillis +
"ms.");
}
}
}
@@ -80,9 +84,12 @@ public class ElasticsearchRetryPolicyTest {
retryPolicy.withRetries(new PassAfterElapsedTime(20));
assertTrue(System.nanoTime() - startTime >=
TimeUnit.MILLISECONDS.toNanos(20));
- // This task will fail for 60 ms, so the retry policy will also fail
because it is set to wait for 50 ms.
+ // This check verifies that a retry policy will stop trying after the
maximum amount of time has passed,
+ // 40 ms in this case. We use a task that will only succeed very far
into the future, to avoid the case where the
+ // 40 ms have elapsed but the thread running the test is suspended
until enough time passes that the task would
+ // succeed.
try {
- retryPolicy.withRetries(new PassAfterElapsedTime(60));
+ retryPolicy.withRetries(new
PassAfterElapsedTime(Integer.MAX_VALUE));
fail("Expected IOException not thrown");
} catch (IOException e) {
//pass