CORDA-2802 use eventually to wait (#4932)

* CORDA-2802 use eventually to wait

* Catch Exception, not Throwable
This commit is contained in:
Dominic Fox
2019-03-26 16:01:06 +00:00
committed by josecoll
parent 3e4a5976d8
commit c2ad64ccde
7 changed files with 153 additions and 101 deletions

View File

@ -1,25 +0,0 @@
package net.corda.nodeapi
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")
}

View File

@ -3,8 +3,8 @@ package net.corda.nodeapi.internal.network
import net.corda.core.internal.div
import net.corda.core.internal.list
import net.corda.core.internal.write
import net.corda.nodeapi.eventually
import net.corda.core.internal.NODE_INFO_DIRECTORY
import net.corda.testing.common.internal.eventually
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Rule
@ -63,7 +63,7 @@ class NodeInfoFilesCopierTest {
nodeInfoFilesCopier.addConfig(node2RootPath)
advanceTime()
eventually<AssertionError, Unit>(Duration.ofMinutes(1)) {
eventually(Duration.ofMinutes(1)) {
// Check only one file is copied.
checkDirectoryContainsSingleFile(node2AdditionalNodeInfoPath, GOOD_NODE_INFO_NAME)
}
@ -81,7 +81,7 @@ class NodeInfoFilesCopierTest {
(node2RootPath / BAD_NODE_INFO_NAME).write(content)
advanceTime()
eventually<AssertionError, Unit>(Duration.ofMinutes(1)) {
eventually(Duration.ofMinutes(1)) {
// Check only one file is copied to the other node.
checkDirectoryContainsSingleFile(node1AdditionalNodeInfoPath, GOOD_NODE_INFO_NAME)
}
@ -105,7 +105,7 @@ class NodeInfoFilesCopierTest {
(node2RootPath / GOOD_NODE_INFO_NAME).write(content)
advanceTime()
eventually<AssertionError, Unit>(Duration.ofMinutes(1)) {
eventually(Duration.ofMinutes(1)) {
// Check only one file is copied to the other node.
checkDirectoryContainsSingleFile(node1AdditionalNodeInfoPath, GOOD_NODE_INFO_NAME)
}
@ -124,8 +124,9 @@ class NodeInfoFilesCopierTest {
(node2RootPath / GOOD_NODE_INFO_NAME_2).write(content)
// Give some time to the filesystem to report the change.
Thread.sleep(100)
assertThat(node1AdditionalNodeInfoPath.list()).isEmpty()
eventually {
assertThat(node1AdditionalNodeInfoPath.list()).isEmpty()
}
}
private fun advanceTime() {