node-driver: Polling doesn't timeout, just prints warning

This commit is contained in:
Andras Slemmer 2016-08-18 11:24:26 +01:00
parent 9cbdf001fb
commit 5f33bedc13
2 changed files with 11 additions and 11 deletions

View File

@ -154,7 +154,7 @@ private fun getTimestampAsDirectoryName(): String {
} }
fun addressMustBeBound(hostAndPort: HostAndPort) { fun addressMustBeBound(hostAndPort: HostAndPort) {
poll { poll("address $hostAndPort to bind") {
try { try {
Socket(hostAndPort.hostText, hostAndPort.port).close() Socket(hostAndPort.hostText, hostAndPort.port).close()
Unit Unit
@ -165,7 +165,7 @@ fun addressMustBeBound(hostAndPort: HostAndPort) {
} }
fun addressMustNotBeBound(hostAndPort: HostAndPort) { fun addressMustNotBeBound(hostAndPort: HostAndPort) {
poll { poll("address $hostAndPort to unbind") {
try { try {
Socket(hostAndPort.hostText, hostAndPort.port).close() Socket(hostAndPort.hostText, hostAndPort.port).close()
null null
@ -175,16 +175,16 @@ fun addressMustNotBeBound(hostAndPort: HostAndPort) {
} }
} }
fun <A> poll(f: () -> A?): A { fun <A> poll(pollName: String, pollIntervalMs: Long = 500, warnCount: Int = 120, f: () -> A?): A {
var counter = 0 var counter = 0
var result = f() var result = f()
while (result == null && counter < 120) { while (result == null) {
counter++ if (counter == warnCount) {
Thread.sleep(500) log.warn("Been polling $pollName for ${pollIntervalMs * warnCount / 1000.0} seconds...")
result = f()
} }
if (result == null) { counter = (counter % warnCount) + 1
throw Exception("Poll timed out") Thread.sleep(pollIntervalMs)
result = f()
} }
return result return result
} }
@ -326,7 +326,7 @@ class DriverDSL(
advertisedServices = setOf(NetworkMapService.Type) advertisedServices = setOf(NetworkMapService.Type)
) )
networkMapCache.addMapService(messagingService, fakeNodeInfo, true) networkMapCache.addMapService(messagingService, fakeNodeInfo, true)
networkMapNodeInfo = poll { networkMapNodeInfo = poll("network map cache for $networkMapName") {
networkMapCache.partyNodes.forEach { networkMapCache.partyNodes.forEach {
if (it.identity.name == networkMapName) { if (it.identity.name == networkMapName) {
return@poll it return@poll it

View File

@ -13,7 +13,7 @@ class DriverTests {
fun nodeMustBeUp(networkMapCache: NetworkMapCache, nodeInfo: NodeInfo, nodeName: String) { fun nodeMustBeUp(networkMapCache: NetworkMapCache, nodeInfo: NodeInfo, nodeName: String) {
val address = nodeInfo.address as ArtemisMessagingComponent.Address val address = nodeInfo.address as ArtemisMessagingComponent.Address
// Check that the node is registered in the network map // Check that the node is registered in the network map
poll { poll("network map cache for $nodeName") {
networkMapCache.get().firstOrNull { networkMapCache.get().firstOrNull {
it.identity.name == nodeName it.identity.name == nodeName
} }