diff --git a/src/main/kotlin/io/iohk/atala/automation/utils/Wait.kt b/src/main/kotlin/io/iohk/atala/automation/utils/Wait.kt index e880117..cd86eb8 100644 --- a/src/main/kotlin/io/iohk/atala/automation/utils/Wait.kt +++ b/src/main/kotlin/io/iohk/atala/automation/utils/Wait.kt @@ -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 @@ -12,7 +13,7 @@ object Wait { * * Usage example: * ``` - * Utils.waitUntil() { + * Wait.until() { * customRequest().statusCode == HttpStatus.SC_OK * } * ``` @@ -20,19 +21,29 @@ object Wait { * @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 } + } } }