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

View File

@ -25,7 +25,7 @@ private fun runBuyer(): Process {
println("Running Buyer") println("Running Buyer")
val args = listOf("--role", "BUYER") val args = listOf("--role", "BUYER")
val proc = spawn("com.r3corda.demos.TraderDemoKt", args) val proc = spawn("com.r3corda.demos.TraderDemoKt", args)
ensureNodeStartsOrKill(proc, "http://localhost:31338") ensureNodeStartsOrKill(proc, freeLocalHostAndPort())
return proc 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 javaArgs = listOf(path, "-javaagent:lib/quasar.jar", "-cp", classpath, className)
val builder = ProcessBuilder(javaArgs + args) val builder = ProcessBuilder(javaArgs + args)
builder.redirectError(Paths.get("error.$className.log").toFile()) builder.redirectError(Paths.get("error.$className.log").toFile())
builder.inheritIO()
val process = builder.start(); val process = builder.start();
return process return process
} }
@ -19,7 +20,7 @@ fun assertExitOrKill(proc: Process) {
try { try {
assertEquals(proc.waitFor(2, TimeUnit.MINUTES), true) assertEquals(proc.waitFor(2, TimeUnit.MINUTES), true)
} catch (e: Throwable) { } catch (e: Throwable) {
proc.destroy() proc.destroyForcibly()
throw e throw e
} }
} }
@ -28,6 +29,6 @@ fun assertAliveAndKill(proc: Process) {
try { try {
assertEquals(proc.isAlive, true) assertEquals(proc.isAlive, true)
} finally { } finally {
proc.destroy() proc.destroyForcibly()
} }
} }

View File

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