Skip to content
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

fix: add error message parameter to Wait.until function #12

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/main/kotlin/io/iohk/atala/automation/utils/Wait.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.iohk.atala.automation.utils

import org.awaitility.Awaitility
import org.awaitility.core.ConditionTimeoutException
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
Expand All @@ -12,27 +13,37 @@ object Wait {
*
* Usage example:
* ```
* Utils.waitUntil() {
* Wait.until() {
* customRequest().statusCode == HttpStatus.SC_OK
* }
* ```
*
* @param timeout maximum time to wait
* @param pollInterval polling interval
* @param condition lambda expression to run the condition
* @param errorMessage error message to throw if the condition is not met
*/
fun until(
timeout: Duration = 5.seconds,
pollInterval: Duration = 500.milliseconds,
condition: () -> Boolean
condition: () -> Boolean,
errorMessage: String?,
) {
Awaitility.await()
.pollInSameThread()
.with()
.pollInterval(pollInterval.toJavaDuration())
.atMost(timeout.toJavaDuration())
.until {
condition()
try {
Awaitility.await()
.pollInSameThread()
.with()
.pollInterval(pollInterval.toJavaDuration())
.atMost(timeout.toJavaDuration())
.until {
condition()
}
} catch (err: ConditionTimeoutException) {
if (errorMessage != null) {
throw ConditionTimeoutException(errorMessage)
} else {
throw err
}
}
}
}
Loading