CORDA-649: Second attempt to make PersistentNetworkMapCacheTest more stable (#1738)

This commit is contained in:
Viktor Kolomeyko
2017-10-02 10:50:39 +01:00
committed by GitHub
parent f961412396
commit 33e8105a29
4 changed files with 29 additions and 8 deletions

View File

@ -0,0 +1,24 @@
package net.corda.testing
import java.time.Duration
/**
* Ideas borrowed from "io.kotlintest" with some improvements made
* This is meant for use from Kotlin code use only mainly due to it's inline/reified nature
*/
inline fun <reified E : Throwable, R>eventually(duration: Duration, f: () -> R): R {
val end = System.nanoTime() + duration.toNanos()
var times = 0
while (System.nanoTime() < end) {
try {
return f()
} catch (e: Throwable) {
when(e) {
is E -> {}// ignore and continue
else -> throw e // unexpected exception type - rethrow
}
}
times++
}
throw AssertionError("Test failed after $duration; attempted $times times")
}