From 9973f8a33e9e4ade7d6e488f679c83b04f8d01aa Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Fri, 10 Jun 2016 09:48:48 +0100 Subject: [PATCH] Moved integration test utilities into a utility folder. --- .../com/r3corda/core/testing/IRSDemoTest.kt | 36 ++----------------- .../r3corda/core/testing/TraderDemoTest.kt | 4 ++- .../testing/{ => utilities}/JVMSpawner.kt | 2 +- .../r3corda/core/testing/utilities/NodeApi.kt | 33 +++++++++++++++++ 4 files changed, 40 insertions(+), 35 deletions(-) rename src/integration-test/kotlin/com/r3corda/core/testing/{ => utilities}/JVMSpawner.kt (93%) create mode 100644 src/integration-test/kotlin/com/r3corda/core/testing/utilities/NodeApi.kt 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 c0e45c3bc0..e55ffc01b2 100644 --- a/src/integration-test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt +++ b/src/integration-test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt @@ -1,18 +1,12 @@ package com.r3corda.core.testing +import com.r3corda.core.testing.utilities.spawn +import com.r3corda.core.testing.utilities.waitForNodeStartup import kotlin.test.assertEquals import org.junit.Test -import java.io.InputStreamReader -import java.net.ConnectException -import java.net.HttpURLConnection -import java.net.URL import java.nio.file.Path import java.nio.file.Paths -private class NodeDidNotStartException: Throwable { - constructor(message: String): super(message) {} -} - class IRSDemoTest { @Test fun `runs IRS demo`() { val dirA = Paths.get("./nodeA") @@ -44,34 +38,10 @@ private fun setupNode(dir: Path, nodeType: String) { 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) - waitForNode(nodeAddr) + waitForNodeStartup(nodeAddr) return proc } -// Todo: Move this to a library and use it in the trade demo -private fun waitForNode(nodeAddr: String) { - var retries = 0 - var respCode: Int = 404 - do { - retries++ - val url = URL(nodeAddr + "/api/status") - 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" - } - - if(retries > 200) { - throw NodeDidNotStartException("The node did not start: " + err) - } - } while (respCode != 200) -} - private fun runTrade() { val args = listOf("--role", "Trade", "trade1") val proc = spawn("com.r3corda.demos.IRSDemoKt", args) 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 576ab9d0be..d74c97532c 100644 --- a/src/integration-test/kotlin/com/r3corda/core/testing/TraderDemoTest.kt +++ b/src/integration-test/kotlin/com/r3corda/core/testing/TraderDemoTest.kt @@ -1,5 +1,7 @@ package com.r3corda.core.testing +import com.r3corda.core.testing.utilities.spawn +import com.r3corda.core.testing.utilities.waitForNodeStartup import org.junit.Test import java.nio.file.Paths import kotlin.test.assertEquals @@ -20,7 +22,7 @@ class TraderDemoTest { private fun runBuyer(): Process { val args = listOf("--role", "BUYER") val proc = spawn("com.r3corda.demos.TraderDemoKt", args) - Thread.sleep(15000) + waitForNodeStartup("http://localhost:31338") return proc } diff --git a/src/integration-test/kotlin/com/r3corda/core/testing/JVMSpawner.kt b/src/integration-test/kotlin/com/r3corda/core/testing/utilities/JVMSpawner.kt similarity index 93% rename from src/integration-test/kotlin/com/r3corda/core/testing/JVMSpawner.kt rename to src/integration-test/kotlin/com/r3corda/core/testing/utilities/JVMSpawner.kt index 7ddf11a160..f88d55131b 100644 --- a/src/integration-test/kotlin/com/r3corda/core/testing/JVMSpawner.kt +++ b/src/integration-test/kotlin/com/r3corda/core/testing/utilities/JVMSpawner.kt @@ -1,4 +1,4 @@ -package com.r3corda.core.testing +package com.r3corda.core.testing.utilities import java.nio.file.Paths 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 new file mode 100644 index 0000000000..95ef74c27c --- /dev/null +++ b/src/integration-test/kotlin/com/r3corda/core/testing/utilities/NodeApi.kt @@ -0,0 +1,33 @@ +package com.r3corda.core.testing.utilities + +import java.io.InputStreamReader +import java.net.ConnectException +import java.net.HttpURLConnection +import java.net.URL + +class NodeDidNotStartException: Throwable { + constructor(message: String): super(message) {} +} + +fun waitForNodeStartup(nodeAddr: String) { + 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" + 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" + } + + if(retries > 200) { + throw NodeDidNotStartException("The node did not start: " + err) + } + } while (respCode != 200) +} \ No newline at end of file