-
Notifications
You must be signed in to change notification settings - Fork 566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[3.x] Add robustness to some of the timing-susceptible metrics tests; add util matcher with retry #5047
Merged
tjquinno
merged 1 commit into
helidon-io:helidon-3.x
from
tjquinno:intermittent-metrics-tests-3.x
Oct 4, 2022
Merged
[3.x] Add robustness to some of the timing-susceptible metrics tests; add util matcher with retry #5047
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
config/testing/src/main/java/io/helidon/config/testing/MatcherWithRetry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.config.testing; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.function.Supplier; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.StringDescription; | ||
|
||
/** | ||
* Hamcrest matcher capable of configured retries before failing the assertion, plus more generic retry processing. | ||
*/ | ||
public class MatcherWithRetry { | ||
|
||
private static final int RETRY_COUNT = Integer.getInteger("io.helidon.test.retryCount", 10); | ||
private static final int RETRY_DELAY_MS = Integer.getInteger("io.helidon.test.retryDelayMs", 500); | ||
|
||
private MatcherWithRetry() { | ||
} | ||
|
||
/** | ||
* Checks the matcher, possibly multiple times after configured delays, invoking the supplier of the matched value each time, | ||
* until either the matcher passes or the maximum retry expires. | ||
* @param reason explanation of the assertion | ||
* @param actualSupplier {@code Supplier} that furnishes the value to submit to the matcher | ||
* @param matcher Hamcrest matcher which evaluates the supplied value | ||
* @return the supplied value | ||
* @param <T> type of the supplied value | ||
* @throws InterruptedException if the sleep is interrupted | ||
*/ | ||
public static <T> T assertThatWithRetry(String reason, Supplier<T> actualSupplier, Matcher<? super T> matcher) | ||
throws InterruptedException { | ||
|
||
T actual = null; | ||
for (int i = 0; i < RETRY_COUNT; i++) { | ||
actual = actualSupplier.get(); | ||
if (matcher.matches(actual)) { | ||
return actual; | ||
} | ||
Thread.sleep(RETRY_DELAY_MS); | ||
} | ||
|
||
Description description = new StringDescription(); | ||
description.appendText(reason) | ||
.appendText("\nExpected: ") | ||
.appendDescriptionOf(matcher) | ||
.appendText("\n but: "); | ||
matcher.describeMismatch(actual, description); | ||
|
||
throw new AssertionError(description.toString()); | ||
} | ||
|
||
/** | ||
* Retries the specified work until successful or retry limit is exceeded. | ||
* | ||
* @param work the work to perform; returned boolean indicates if the work was successful | ||
* @throws Exception exception from the work or from the sleep being interrupted | ||
*/ | ||
public static void retry(Callable<Boolean> work) throws Exception { | ||
for (int i = 0; i < RETRY_COUNT; i++) { | ||
if (work.call()) { | ||
return; | ||
} | ||
Thread.sleep(RETRY_DELAY_MS); | ||
} | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tjquinno I don't think this actually retries anything since only usage always returns true and this thing doesn't catch exceptions. We do have really fancy fault tolerance Retry which works really well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I'd intended for the
retry
method to repeat if the work returnedfalse
(and you are correct, so far the only use returnstrue
) or if the work threw an exception (such as theasserts
in the lambda which is passed toretry
in its only use).