Created
August 8, 2020 03:05
-
-
Save automationhacks/0e49f624579999c94100f967b55ed306 to your computer and use it in GitHub Desktop.
Kotlin function to wait till a passed in lambda evaluates to true or times out
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
package experiments | |
import org.joda.time.DateTime | |
import org.testng.annotations.Test | |
object Wait { | |
fun until(function: () -> Boolean, timeout: Int = 30, retryAfter: Int = 2): Boolean { | |
val endAt = DateTime.now().plus(timeout.toLong() * 1000) | |
var current = DateTime.now() | |
while (current < endAt) { | |
val result = function() | |
if (result) { | |
println("Evaluated to true. Exiting wait polling loop") | |
return result | |
} | |
println("Evaluated to false.. Waiting for $retryAfter secs ") | |
Thread.sleep(retryAfter.toLong() * 1000) | |
current = DateTime.now() | |
} | |
println("Wait function hit timeout limit of $timeout secs") | |
return function() | |
} | |
} | |
class WaitTests { | |
@Test | |
fun testFunctionWaitsUntilTimeout() { | |
val counter = 0 | |
Wait.until({ counter == 1 }) | |
} | |
@Test | |
fun testFunctionReturnsTrueBeforeTimeout() { | |
var counter = 0 | |
val incrementCounterTillLimit = { | |
if (counter > 2) { | |
true | |
} else { | |
++counter | |
false | |
} | |
} | |
Wait.until(incrementCounterTillLimit) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment