Moved integration test utilities into a utility folder.

This commit is contained in:
Clinton Alexander 2016-06-10 09:48:48 +01:00 committed by Andras Slemmer
parent 9638ea5b9e
commit 9973f8a33e
4 changed files with 40 additions and 35 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -1,4 +1,4 @@
package com.r3corda.core.testing
package com.r3corda.core.testing.utilities
import java.nio.file.Paths

View File

@ -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)
}