From 5bf5e37572aa7d643a0bc445762cac1d9cfc4a18 Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Fri, 17 Jun 2016 19:17:27 +0100 Subject: [PATCH] Demos now fully handle process managment except in the case where the process is killed with something like pkill or the JVM being ended by task manager. --- .idea/modules.xml | 1 - .../com/r3corda/core/testing/IRSDemoTest.kt | 17 ++++++++-------- .../r3corda/core/testing/TraderDemoTest.kt | 16 +++++++++------ .../core/testing/utilities/JVMSpawner.kt | 20 ++++++++++++++++++- .../r3corda/core/testing/utilities/NodeApi.kt | 17 +++++++++++++++- 5 files changed, 53 insertions(+), 18 deletions(-) diff --git a/.idea/modules.xml b/.idea/modules.xml index e38ea5ee5e..8a1bd10f31 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -22,7 +22,6 @@ - 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 a81a63a6f0..2a0e485a7d 100644 --- a/src/integration-test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt +++ b/src/integration-test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt @@ -1,12 +1,10 @@ package com.r3corda.core.testing -import com.r3corda.core.testing.utilities.spawn -import com.r3corda.core.testing.utilities.waitForNodeStartup +import com.r3corda.core.testing.utilities.* import kotlin.test.assertEquals import org.junit.Test import java.nio.file.Path import java.nio.file.Paths -import java.util.concurrent.TimeUnit class IRSDemoTest { @Test fun `runs IRS demo`() { @@ -32,34 +30,35 @@ class IRSDemoTest { private fun setupNode(dir: Path, nodeType: String) { val args = listOf("--role", "Setup" + nodeType, "--dir", dir.toString()) val proc = spawn("com.r3corda.demos.IRSDemoKt", args) - assertEquals(proc.waitFor(2, TimeUnit.MINUTES), true); + assertExitOrKill(proc) assertEquals(proc.exitValue(), 0) } private fun startNode(dir: Path, nodeType: String, nodeAddr: String): Process { val args = listOf("--role", nodeType, "--dir", dir.toString()) val proc = spawn("com.r3corda.demos.IRSDemoKt", args) - waitForNodeStartup(nodeAddr) + ensureNodeStartsOrKill(proc, nodeAddr) return proc } private fun runTrade() { val args = listOf("--role", "Trade", "trade1") val proc = spawn("com.r3corda.demos.IRSDemoKt", args) - assertEquals(proc.waitFor(2, TimeUnit.MINUTES), true); + assertExitOrKill(proc) assertEquals(proc.exitValue(), 0) } private fun runDateChange() { val args = listOf("--role", "Date", "2017-01-02") val proc = spawn("com.r3corda.demos.IRSDemoKt", args) - assertEquals(proc.waitFor(2, TimeUnit.MINUTES), true); + assertExitOrKill(proc) assertEquals(proc.exitValue(), 0) } private fun stopNode(nodeProc: Process?) { - assertEquals(nodeProc?.isAlive, true) - nodeProc?.destroy() + if(nodeProc != null) { + assertAliveAndKill(nodeProc) + } } private fun cleanup(dir: Path) { 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 e9073d6c5f..2e492f5fd3 100644 --- a/src/integration-test/kotlin/com/r3corda/core/testing/TraderDemoTest.kt +++ b/src/integration-test/kotlin/com/r3corda/core/testing/TraderDemoTest.kt @@ -1,40 +1,44 @@ package com.r3corda.core.testing +import com.r3corda.core.testing.utilities.assertExitOrKill +import com.r3corda.core.testing.utilities.ensureNodeStartsOrKill import com.r3corda.core.testing.utilities.spawn -import com.r3corda.core.testing.utilities.waitForNodeStartup import org.junit.Test import java.nio.file.Paths -import java.util.concurrent.TimeUnit import kotlin.test.assertEquals class TraderDemoTest { @Test fun `runs trader demo`() { var nodeProc: Process? = null try { + cleanupFiles() nodeProc = runBuyer() runSeller() } finally { nodeProc?.destroy() - cleanup() + cleanupFiles() } } } private fun runBuyer(): Process { + println("Running Buyer") val args = listOf("--role", "BUYER") val proc = spawn("com.r3corda.demos.TraderDemoKt", args) - waitForNodeStartup("http://localhost:31338") + ensureNodeStartsOrKill(proc, "http://localhost:31338") return proc } private fun runSeller() { + println("Running Seller") val args = listOf("--role", "SELLER") val proc = spawn("com.r3corda.demos.TraderDemoKt", args) - assertEquals(proc.waitFor(30, TimeUnit.SECONDS), true); + assertExitOrKill(proc); assertEquals(proc.exitValue(), 0) } -private fun cleanup() { +private fun cleanupFiles() { + println("Cleaning up TraderDemoTest files") val dir = Paths.get("trader-demo") println("Erasing " + dir) dir.toFile().deleteRecursively() 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 f88d55131b..b1495e3512 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 @@ -1,6 +1,8 @@ package com.r3corda.core.testing.utilities import java.nio.file.Paths +import java.util.concurrent.TimeUnit +import kotlin.test.assertEquals fun spawn(className: String, args: List): Process { val separator = System.getProperty("file.separator") @@ -9,7 +11,23 @@ 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 } + +fun assertExitOrKill(proc: Process) { + try { + assertEquals(proc.waitFor(2, TimeUnit.MINUTES), true) + } catch (e: Throwable) { + proc.destroy() + throw e + } +} + +fun assertAliveAndKill(proc: Process) { + try { + assertEquals(proc.isAlive, true) + } finally { + proc.destroy() + } +} \ 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 95ef74c27c..e206bb25bb 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 @@ -2,14 +2,26 @@ package com.r3corda.core.testing.utilities import java.io.InputStreamReader import java.net.ConnectException +import java.net.SocketException import java.net.HttpURLConnection import java.net.URL +import kotlin.test.assertEquals class NodeDidNotStartException: Throwable { constructor(message: String): super(message) {} } -fun waitForNodeStartup(nodeAddr: String) { +fun ensureNodeStartsOrKill(proc: Process, nodeAddr: String) { + try { + assertEquals(proc.isAlive, true) + waitForNodeStartup(nodeAddr) + } catch (e: Exception) { + proc.destroy() + throw e + } +} + +private fun waitForNodeStartup(nodeAddr: String) { var retries = 0 var respCode: Int do { @@ -24,6 +36,9 @@ fun waitForNodeStartup(nodeAddr: String) { // 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()}" } if(retries > 200) {