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.

This commit is contained in:
Clinton Alexander 2016-06-17 19:17:27 +01:00 committed by Andras Slemmer
parent 9d4f75f241
commit 5bf5e37572
5 changed files with 53 additions and 18 deletions

1
.idea/modules.xml generated
View File

@ -22,7 +22,6 @@
<module fileurl="file://$PROJECT_DIR$/.idea/modules/node/node_test.iml" filepath="$PROJECT_DIR$/.idea/modules/node/node_test.iml" group="node" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/r3prototyping.iml" filepath="$PROJECT_DIR$/.idea/modules/r3prototyping.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/r3prototyping_integrationTest.iml" filepath="$PROJECT_DIR$/.idea/modules/r3prototyping_integrationTest.iml" group="r3prototyping" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/r3prototyping_integrationTest.iml" filepath="$PROJECT_DIR$/.idea/modules/r3prototyping_integrationTest.iml" group="r3prototyping" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/r3prototyping_main.iml" filepath="$PROJECT_DIR$/.idea/modules/r3prototyping_main.iml" group="r3prototyping" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/r3prototyping_test.iml" filepath="$PROJECT_DIR$/.idea/modules/r3prototyping_test.iml" group="r3prototyping" />
</modules>

View File

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

View File

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

View File

@ -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<String>): Process {
val separator = System.getProperty("file.separator")
@ -9,7 +11,23 @@ fun spawn(className: String, args: List<String>): 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()
}
}

View File

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