integtest: Put delay in status polling

This commit is contained in:
Andras Slemmer 2016-06-27 14:14:12 +01:00
parent e54dad9a8b
commit f2505fb504
4 changed files with 51 additions and 44 deletions

View File

@ -54,7 +54,7 @@ private fun startNode(dir: Path,
"--network-map-address", networkMapAddr.toString(),
"--api-address", apiAddr.toString())
val proc = spawn("com.r3corda.demos.IRSDemoKt", args, "IRSDemo$nodeType")
ensureNodeStartsOrKill(proc, apiAddr)
NodeApi.ensureNodeStartsOrKill(proc, apiAddr)
return proc
}

View File

@ -1,8 +1,8 @@
package com.r3corda.core.testing
import com.google.common.net.HostAndPort
import com.r3corda.core.testing.utilities.NodeApi
import com.r3corda.core.testing.utilities.assertExitOrKill
import com.r3corda.core.testing.utilities.ensureNodeStartsOrKill
import com.r3corda.core.testing.utilities.spawn
import org.junit.Test
import java.nio.file.Paths
@ -32,7 +32,7 @@ private fun runBuyer(buyerAddr: HostAndPort, buyerApiAddr: HostAndPort): Process
"--api-address", buyerApiAddr.toString()
)
val proc = spawn("com.r3corda.demos.TraderDemoKt", args, "TradeDemoBuyer")
ensureNodeStartsOrKill(proc, buyerAddr)
NodeApi.ensureNodeStartsOrKill(proc, buyerApiAddr)
return proc
}

View File

@ -31,4 +31,4 @@ fun assertAliveAndKill(proc: Process) {
} finally {
proc.destroyForcibly()
}
}
}

View File

@ -9,46 +9,53 @@ import java.net.HttpURLConnection
import java.net.URL
import kotlin.test.assertEquals
class NodeDidNotStartException: Exception {
constructor(message: String): super(message) {}
}
class NodeApi {
class NodeDidNotStartException(message: String): Exception(message)
fun ensureNodeStartsOrKill(proc: Process, nodeAddr: HostAndPort) {
try {
assertEquals(proc.isAlive, true)
waitForNodeStartup(nodeAddr)
} catch (e: Throwable) {
println("Forcibly killing node process")
proc.destroyForcibly()
throw e
companion object {
val NODE_WAIT_RETRY_COUNT: Int = 50
val NODE_WAIT_RETRY_DELAY_MS: Long = 200
fun ensureNodeStartsOrKill(proc: Process, nodeWebserverAddr: HostAndPort) {
try {
assertEquals(proc.isAlive, true)
waitForNodeStartup(nodeWebserverAddr)
} catch (e: Throwable) {
println("Forcibly killing node process")
proc.destroyForcibly()
throw e
}
}
private fun waitForNodeStartup(nodeWebserverAddr: HostAndPort) {
val url = URL("http://${nodeWebserverAddr.toString()}/api/status")
var retries = 0
var respCode: Int
do {
retries++
val err = try {
val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "GET"
respCode = conn.responseCode
InputStreamReader(conn.inputStream).readLines().joinToString { it }
} catch(e: ConnectException) {
// This is to be expected while it loads up
respCode = 404
"Node hasn't started"
} catch(e: SocketException) {
respCode = -1
"Could not connect: ${e.toString()}"
} catch (e: IOException) {
respCode = -1
"IOException: ${e.toString()}"
}
if (retries > NODE_WAIT_RETRY_COUNT) {
throw NodeDidNotStartException("The node did not start: " + err)
}
Thread.sleep(NODE_WAIT_RETRY_DELAY_MS)
} while (respCode != 200)
}
}
}
private fun waitForNodeStartup(nodeAddr: HostAndPort) {
val url = URL("http://${nodeAddr.toString()}/api/status")
var retries = 0
var respCode: Int
do {
retries++
val err = try {
val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "GET"
respCode = conn.responseCode
InputStreamReader(conn.inputStream).readLines().joinToString { it }
} catch(e: ConnectException) {
// This is to be expected while it loads up
respCode = 404
"Node hasn't started"
} catch(e: SocketException) {
respCode = -1
"Could not connect: ${e.toString()}"
} catch (e: IOException) {
respCode = -1
"IOException: ${e.toString()}"
}
if (retries > 25) {
throw NodeDidNotStartException("The node did not start: " + err)
}
} while (respCode != 200)
}