Skip to content

Commit

Permalink
fix: add error message parameter to Wait.until function (#12)
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Baliasnikov <anton.baliasnikov@iohk.io>
  • Loading branch information
Anton Baliasnikov authored Dec 6, 2023
1 parent d29a03a commit 066b3df
Showing 1 changed file with 20 additions and 9 deletions.
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
errorMessage: String? = null,
condition: () -> Boolean,
) {
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
}
}
}
}

0 comments on commit 066b3df

Please sign in to comment.