Ports now randomised during demo tests.

This commit is contained in:
Clinton Alexander 2016-06-21 21:32:38 +01:00 committed by Andras Slemmer
parent b52f344eb3
commit 4900c7eb26
5 changed files with 40 additions and 30 deletions

View File

@ -18,8 +18,8 @@ class IRSDemoTest {
try {
setupNode(dirA, "NodeA")
setupNode(dirB, "NodeB")
procA = startNode(dirA, "NodeA", nodeAddrA)
procB = startNode(dirB, "NodeB", freeLocalHostAndPort())
procA = startNode(dirA, "NodeA", nodeAddrA, nodeAddrA)
procB = startNode(dirB, "NodeB", freeLocalHostAndPort(), nodeAddrA)
runTrade(apiAddrA)
runDateChange(apiAddrA)
} finally {
@ -33,16 +33,20 @@ 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)
val proc = spawn("com.r3corda.demos.IRSDemoKt", args, "IRSDemoSetup$nodeType")
assertExitOrKill(proc)
assertEquals(proc.exitValue(), 0)
}
private fun startNode(dir: Path, nodeType: String, nodeAddr: HostAndPort): Process {
private fun startNode(dir: Path, nodeType: String, nodeAddr: HostAndPort, networkMapAddr: 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)
val args = listOf(
"--role", nodeType,
"--dir", dir.toString(),
"--network-address", nodeAddr.toString(),
"--network-map-address", networkMapAddr.toString())
val proc = spawn("com.r3corda.demos.IRSDemoKt", args, "IRSDemo$nodeType")
ensureNodeStartsOrKill(proc, nodeAddr)
return proc
}
@ -50,7 +54,7 @@ private fun startNode(dir: Path, nodeType: String, nodeAddr: HostAndPort): Proce
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)
val proc = spawn("com.r3corda.demos.IRSDemoKt", args, "IRSDemoTrade")
assertExitOrKill(proc)
assertEquals(proc.exitValue(), 0)
}
@ -58,7 +62,7 @@ private fun runTrade(nodeAddr: HostAndPort) {
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)
val proc = spawn("com.r3corda.demos.IRSDemoKt", args, "IRSDemoDate")
assertExitOrKill(proc)
assertEquals(proc.exitValue(), 0)
}

View File

@ -1,5 +1,6 @@
package com.r3corda.core.testing
import com.google.common.net.HostAndPort
import com.r3corda.core.testing.utilities.assertExitOrKill
import com.r3corda.core.testing.utilities.ensureNodeStartsOrKill
import com.r3corda.core.testing.utilities.spawn
@ -9,11 +10,12 @@ import kotlin.test.assertEquals
class TraderDemoTest {
@Test fun `runs trader demo`() {
val buyerAddr = freeLocalHostAndPort()
var nodeProc: Process? = null
try {
cleanupFiles()
nodeProc = runBuyer()
runSeller()
nodeProc = runBuyer(buyerAddr)
runSeller(buyerAddr)
} finally {
nodeProc?.destroy()
cleanupFiles()
@ -21,18 +23,22 @@ class TraderDemoTest {
}
}
private fun runBuyer(): Process {
private fun runBuyer(buyerAddr: HostAndPort): Process {
println("Running Buyer")
val args = listOf("--role", "BUYER")
val proc = spawn("com.r3corda.demos.TraderDemoKt", args)
ensureNodeStartsOrKill(proc, freeLocalHostAndPort())
val args = listOf("--role", "BUYER", "--network-address", buyerAddr.toString())
val proc = spawn("com.r3corda.demos.TraderDemoKt", args, "TradeDemoBuyer")
ensureNodeStartsOrKill(proc, buyerAddr)
return proc
}
private fun runSeller() {
private fun runSeller(buyerAddr: HostAndPort) {
println("Running Seller")
val args = listOf("--role", "SELLER")
val proc = spawn("com.r3corda.demos.TraderDemoKt", args)
val sellerAddr = freeLocalHostAndPort()
val args = listOf(
"--role", "SELLER",
"--network-address", sellerAddr.toString(),
"--other-network-address", buyerAddr.toString())
val proc = spawn("com.r3corda.demos.TraderDemoKt", args, "TradeDemoSeller")
assertExitOrKill(proc);
assertEquals(proc.exitValue(), 0)
}

View File

@ -4,11 +4,11 @@ import java.nio.file.Paths
import java.util.concurrent.TimeUnit
import kotlin.test.assertEquals
fun spawn(className: String, args: List<String>): Process {
fun spawn(className: String, args: List<String>, appName: String): Process {
val separator = System.getProperty("file.separator")
val classpath = System.getProperty("java.class.path")
val path = System.getProperty("java.home") + separator + "bin" + separator + "java"
val javaArgs = listOf(path, "-javaagent:lib/quasar.jar", "-cp", classpath, className)
val javaArgs = listOf(path, "-Dname=$appName", "-javaagent:lib/quasar.jar", "-cp", classpath, className)
val builder = ProcessBuilder(javaArgs + args)
builder.redirectError(Paths.get("error.$className.log").toFile())
builder.inheritIO()

View File

@ -9,7 +9,7 @@ import java.net.HttpURLConnection
import java.net.URL
import kotlin.test.assertEquals
class NodeDidNotStartException: Throwable {
class NodeDidNotStartException: Exception {
constructor(message: String): super(message) {}
}
@ -17,7 +17,7 @@ fun ensureNodeStartsOrKill(proc: Process, nodeAddr: HostAndPort) {
try {
assertEquals(proc.isAlive, true)
waitForNodeStartup(nodeAddr)
} catch (e: Exception) {
} catch (e: Throwable) {
println("Forcibly killing node process")
proc.destroyForcibly()
throw e
@ -47,7 +47,7 @@ private fun waitForNodeStartup(nodeAddr: HostAndPort) {
"IOException: ${e.toString()}"
}
if(retries > 50) {
if(retries > 25) {
throw NodeDidNotStartException("The node did not start: " + err)
}
} while (respCode != 200)

View File

@ -61,7 +61,7 @@ enum class IRSDemoRole {
private class NodeParams() {
var dir : Path = Paths.get("")
var address : String = ""
var address : HostAndPort = HostAndPort.fromString("localhost").withDefaultPort(Node.DEFAULT_PORT)
var mapAddress: String = ""
var identityFile: Path = Paths.get("")
var tradeWithAddrs: List<String> = listOf()
@ -235,7 +235,7 @@ private fun configureNodeParams(role: IRSDemoRole, args: DemoArgs, options: Opti
nodeParams.dir = Paths.get(options.valueOf(args.dirArg))
}
if (options.has(args.networkAddressArg)) {
nodeParams.address = options.valueOf(args.networkAddressArg)
nodeParams.address = HostAndPort.fromString(options.valueOf(args.networkAddressArg)).withDefaultPort(Node.DEFAULT_PORT)
}
nodeParams.identityFile = if (options.has(args.networkMapIdentityFile)) {
Paths.get(options.valueOf(args.networkMapIdentityFile))
@ -265,7 +265,8 @@ private fun runNode(nodeParams: NodeParams) : Unit {
ExitServerProtocol.Handler.register(node)
if(nodeParams.uploadRates) {
runUploadRates("http://localhost:31341")
val apiAddr = "http://${nodeParams.address.hostText}:${nodeParams.address.port + 1}";
runUploadRates(apiAddr)
}
try {
@ -283,8 +284,7 @@ private fun createRecipient(addr: String) : SingleMessageRecipient {
private fun startNode(params : NodeParams, networkMap: SingleMessageRecipient, recipients: List<SingleMessageRecipient>) : Node {
val config = getNodeConfig(params)
val advertisedServices: Set<ServiceType>
val myNetAddr = HostAndPort.fromString(params.address).withDefaultPort(Node.DEFAULT_PORT)
val networkMapId = if (params.mapAddress.equals(params.address)) {
val networkMapId = if (params.mapAddress.equals(params.address.toString())) {
// This node provides network map and notary services
advertisedServices = setOf(NetworkMapService.Type, SimpleNotaryService.Type)
null
@ -293,7 +293,7 @@ private fun startNode(params : NodeParams, networkMap: SingleMessageRecipient, r
nodeInfo(networkMap, params.identityFile, setOf(NetworkMapService.Type, SimpleNotaryService.Type))
}
val node = logElapsedTime("Node startup") { Node(params.dir, myNetAddr, config, networkMapId,
val node = logElapsedTime("Node startup") { Node(params.dir, params.address, config, networkMapId,
advertisedServices, DemoClock(),
listOf(InterestRateSwapAPI::class.java)).start() }
@ -415,7 +415,7 @@ private fun uploadFile(url: URL, file: String) : Boolean {
private fun createNodeAParams() : NodeParams {
val params = NodeParams()
params.dir = Paths.get("nodeA")
params.address = "localhost"
params.address = HostAndPort.fromString("localhost")
params.tradeWithAddrs = listOf("localhost:31340")
params.tradeWithIdentities = listOf(getRoleDir(IRSDemoRole.NodeB).resolve(AbstractNode.PUBLIC_IDENTITY_FILE_NAME))
params.defaultLegalName = "Bank A"
@ -425,7 +425,7 @@ private fun createNodeAParams() : NodeParams {
private fun createNodeBParams() : NodeParams {
val params = NodeParams()
params.dir = Paths.get("nodeB")
params.address = "localhost:31340"
params.address = HostAndPort.fromString("localhost:31340")
params.tradeWithAddrs = listOf("localhost")
params.tradeWithIdentities = listOf(getRoleDir(IRSDemoRole.NodeA).resolve(AbstractNode.PUBLIC_IDENTITY_FILE_NAME))
params.defaultLegalName = "Bank B"