Ensured that nodes are killed by process.destroyForcibly. Added random port numbers to test.

This commit is contained in:
Clinton Alexander 2016-06-21 16:48:43 +01:00 committed by Andras Slemmer
parent 68867d21bb
commit b52f344eb3
4 changed files with 34 additions and 18 deletions

View File

@ -1,5 +1,6 @@
package com.r3corda.core.testing
import com.google.common.net.HostAndPort
import com.r3corda.core.testing.utilities.*
import kotlin.test.assertEquals
import org.junit.Test
@ -8,6 +9,8 @@ import java.nio.file.Paths
class IRSDemoTest {
@Test fun `runs IRS demo`() {
val nodeAddrA = freeLocalHostAndPort()
val apiAddrA = HostAndPort.fromString("${nodeAddrA.hostText}:${nodeAddrA.port + 1}")
val dirA = Paths.get("./nodeA")
val dirB = Paths.get("./nodeB")
var procA: Process? = null
@ -15,10 +18,10 @@ class IRSDemoTest {
try {
setupNode(dirA, "NodeA")
setupNode(dirB, "NodeB")
procA = startNode(dirA, "NodeA", "http://localhost:31338")
procB = startNode(dirB, "NodeB", "http://localhost:31341")
runTrade()
runDateChange()
procA = startNode(dirA, "NodeA", nodeAddrA)
procB = startNode(dirB, "NodeB", freeLocalHostAndPort())
runTrade(apiAddrA)
runDateChange(apiAddrA)
} finally {
stopNode(procA)
stopNode(procB)
@ -28,28 +31,33 @@ class IRSDemoTest {
}
}
private fun setupNode(dir: Path, nodeType: String) {
println("Running setup for $nodeType")
val args = listOf("--role", "Setup" + nodeType, "--dir", dir.toString())
val proc = spawn("com.r3corda.demos.IRSDemoKt", args)
assertExitOrKill(proc)
assertEquals(proc.exitValue(), 0)
}
private fun startNode(dir: Path, nodeType: String, nodeAddr: String): Process {
val args = listOf("--role", nodeType, "--dir", dir.toString())
private fun startNode(dir: Path, nodeType: String, nodeAddr: HostAndPort): Process {
println("Running node $nodeType")
println("Node addr: ${nodeAddr.toString()}")
val args = listOf("--role", nodeType, "--dir", dir.toString(), "--network-address", nodeAddr.toString())
val proc = spawn("com.r3corda.demos.IRSDemoKt", args)
ensureNodeStartsOrKill(proc, nodeAddr)
return proc
}
private fun runTrade() {
val args = listOf("--role", "Trade", "trade1")
private fun runTrade(nodeAddr: HostAndPort) {
println("Running trade")
val args = listOf("--role", "Trade", "trade1", "--network-address", "http://" + nodeAddr.toString())
val proc = spawn("com.r3corda.demos.IRSDemoKt", args)
assertExitOrKill(proc)
assertEquals(proc.exitValue(), 0)
}
private fun runDateChange() {
val args = listOf("--role", "Date", "2017-01-02")
private fun runDateChange(nodeAddr: HostAndPort) {
println("Running date change")
val args = listOf("--role", "Date", "2017-01-02", "--network-address", "http://" + nodeAddr.toString())
val proc = spawn("com.r3corda.demos.IRSDemoKt", args)
assertExitOrKill(proc)
assertEquals(proc.exitValue(), 0)
@ -57,6 +65,7 @@ private fun runDateChange() {
private fun stopNode(nodeProc: Process?) {
if(nodeProc != null) {
println("Stopping node")
assertAliveAndKill(nodeProc)
}
}

View File

@ -25,7 +25,7 @@ private fun runBuyer(): Process {
println("Running Buyer")
val args = listOf("--role", "BUYER")
val proc = spawn("com.r3corda.demos.TraderDemoKt", args)
ensureNodeStartsOrKill(proc, "http://localhost:31338")
ensureNodeStartsOrKill(proc, freeLocalHostAndPort())
return proc
}

View File

@ -11,6 +11,7 @@ fun spawn(className: String, args: List<String>): Process {
val javaArgs = listOf(path, "-javaagent:lib/quasar.jar", "-cp", classpath, className)
val builder = ProcessBuilder(javaArgs + args)
builder.redirectError(Paths.get("error.$className.log").toFile())
builder.inheritIO()
val process = builder.start();
return process
}
@ -19,7 +20,7 @@ fun assertExitOrKill(proc: Process) {
try {
assertEquals(proc.waitFor(2, TimeUnit.MINUTES), true)
} catch (e: Throwable) {
proc.destroy()
proc.destroyForcibly()
throw e
}
}
@ -28,6 +29,6 @@ fun assertAliveAndKill(proc: Process) {
try {
assertEquals(proc.isAlive, true)
} finally {
proc.destroy()
proc.destroyForcibly()
}
}

View File

@ -1,5 +1,7 @@
package com.r3corda.core.testing.utilities
import com.google.common.net.HostAndPort
import java.io.IOException
import java.io.InputStreamReader
import java.net.ConnectException
import java.net.SocketException
@ -11,22 +13,23 @@ class NodeDidNotStartException: Throwable {
constructor(message: String): super(message) {}
}
fun ensureNodeStartsOrKill(proc: Process, nodeAddr: String) {
fun ensureNodeStartsOrKill(proc: Process, nodeAddr: HostAndPort) {
try {
assertEquals(proc.isAlive, true)
waitForNodeStartup(nodeAddr)
} catch (e: Exception) {
proc.destroy()
println("Forcibly killing node process")
proc.destroyForcibly()
throw e
}
}
private fun waitForNodeStartup(nodeAddr: String) {
private fun waitForNodeStartup(nodeAddr: HostAndPort) {
val url = URL("http://${nodeAddr.hostText}:${nodeAddr.port + 1}/api/status")
var retries = 0
var respCode: Int
do {
retries++
val url = URL(nodeAddr + "/api/status")
val err = try {
val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "GET"
@ -39,9 +42,12 @@ private fun waitForNodeStartup(nodeAddr: String) {
} catch(e: SocketException) {
respCode = -1
"Could not connect: ${e.toString()}"
} catch (e: IOException) {
respCode = -1
"IOException: ${e.toString()}"
}
if(retries > 200) {
if(retries > 50) {
throw NodeDidNotStartException("The node did not start: " + err)
}
} while (respCode != 200)