Ian
05/07/2017, 4:19 PMeventually
contains a “busy” while loop, was thinking this might be a better implementation:
val Duration.millis get() = this.timeUnit.toMillis(amount)
fun <T> eventually(maximumTime: io.kotlintest.Duration, pollEvery : Duration = 100.milliseconds, f: () -> T): T {
val end = System.nanoTime() + maximumTime.nanoseconds
var times = 0
while (System.nanoTime() < end) {
try {
return f()
} catch (e: Exception) {
e.printStackTrace()
}
Thread.sleep(pollEvery.millis)
times++
}
throw AssertionError("Test failed after ${maximumTime.amount} ${maximumTime.timeUnit}; attempted $times times")
}