From b52f344eb3cb1ed9ba413efb7f2e4eb93f77e33c Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Tue, 21 Jun 2016 16:48:43 +0100 Subject: [PATCH] Ensured that nodes are killed by process.destroyForcibly. Added random port numbers to test. --- .../com/r3corda/core/testing/IRSDemoTest.kt | 29 ++++++++++++------- .../r3corda/core/testing/TraderDemoTest.kt | 2 +- .../core/testing/utilities/JVMSpawner.kt | 5 ++-- .../r3corda/core/testing/utilities/NodeApi.kt | 16 ++++++---- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/integration-test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt b/src/integration-test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt index 2a0e485a7d..229d74e406 100644 --- a/src/integration-test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt +++ b/src/integration-test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt @@ -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) } } diff --git a/src/integration-test/kotlin/com/r3corda/core/testing/TraderDemoTest.kt b/src/integration-test/kotlin/com/r3corda/core/testing/TraderDemoTest.kt index 2e492f5fd3..62ab740729 100644 --- a/src/integration-test/kotlin/com/r3corda/core/testing/TraderDemoTest.kt +++ b/src/integration-test/kotlin/com/r3corda/core/testing/TraderDemoTest.kt @@ -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 } diff --git a/src/integration-test/kotlin/com/r3corda/core/testing/utilities/JVMSpawner.kt b/src/integration-test/kotlin/com/r3corda/core/testing/utilities/JVMSpawner.kt index b1495e3512..bfc7b3f8f2 100644 --- a/src/integration-test/kotlin/com/r3corda/core/testing/utilities/JVMSpawner.kt +++ b/src/integration-test/kotlin/com/r3corda/core/testing/utilities/JVMSpawner.kt @@ -11,6 +11,7 @@ fun spawn(className: String, args: List): 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() } } \ No newline at end of file diff --git a/src/integration-test/kotlin/com/r3corda/core/testing/utilities/NodeApi.kt b/src/integration-test/kotlin/com/r3corda/core/testing/utilities/NodeApi.kt index e206bb25bb..f2739046dc 100644 --- a/src/integration-test/kotlin/com/r3corda/core/testing/utilities/NodeApi.kt +++ b/src/integration-test/kotlin/com/r3corda/core/testing/utilities/NodeApi.kt @@ -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)