Removed thread waits from tests instead relying on a lock passed to the demo environment.

This commit is contained in:
Clinton Alexander 2016-06-08 15:55:25 +01:00 committed by Andras Slemmer
parent 929b752b42
commit a7ac54f280
5 changed files with 43 additions and 45 deletions

View File

@ -10,6 +10,7 @@ import com.r3corda.node.services.config.NodeConfiguration
import com.r3corda.node.services.network.InMemoryMessagingNetwork import com.r3corda.node.services.network.InMemoryMessagingNetwork
import java.nio.file.Path import java.nio.file.Path
import java.time.Clock import java.time.Clock
import java.util.concurrent.CountDownLatch
val messageNetwork = InMemoryMessagingNetwork() val messageNetwork = InMemoryMessagingNetwork()
@ -25,3 +26,8 @@ class DemoNode(messagingService: MessagingService, dir: Path, p2pAddr: HostAndPo
override fun startMessagingService() = Unit override fun startMessagingService() = Unit
} }
class DemoConfig(useInMemoryMessaging: Boolean = false) {
val inMemory = useInMemoryMessaging
val nodeReady = CountDownLatch(1)
}

View File

@ -4,7 +4,6 @@ import com.google.common.net.HostAndPort
import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigFactory
import com.r3corda.core.crypto.Party import com.r3corda.core.crypto.Party
import com.r3corda.core.logElapsedTime import com.r3corda.core.logElapsedTime
import com.r3corda.core.messaging.MessagingService
import com.r3corda.node.internal.Node import com.r3corda.node.internal.Node
import com.r3corda.node.services.config.NodeConfiguration import com.r3corda.node.services.config.NodeConfiguration
import com.r3corda.node.services.config.NodeConfigurationFromConfig import com.r3corda.node.services.config.NodeConfigurationFromConfig
@ -35,7 +34,6 @@ import java.net.URL
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
import java.nio.file.Paths import java.nio.file.Paths
import java.time.Clock
import java.util.* import java.util.*
import kotlin.concurrent.fixedRateTimer import kotlin.concurrent.fixedRateTimer
import kotlin.system.exitProcess import kotlin.system.exitProcess
@ -90,7 +88,7 @@ fun main(args: Array<String>) {
exitProcess(runIRSDemo(args)) exitProcess(runIRSDemo(args))
} }
fun runIRSDemo(args: Array<String>, useInMemoryMessaging: Boolean = false): Int { fun runIRSDemo(args: Array<String>, demoNodeConfig: DemoConfig = DemoConfig()): Int {
val parser = OptionParser() val parser = OptionParser()
val demoArgs = setupArgs(parser) val demoArgs = setupArgs(parser)
val options = try { val options = try {
@ -154,7 +152,7 @@ fun runIRSDemo(args: Array<String>, useInMemoryMessaging: Boolean = false): Int
} }
try { try {
runNode(configureNodeParams(role, demoArgs, options), useInMemoryMessaging) runNode(configureNodeParams(role, demoArgs, options), demoNodeConfig)
} catch (e: NotSetupException) { } catch (e: NotSetupException) {
println(e.message) println(e.message)
return 1 return 1
@ -241,8 +239,8 @@ private fun configureNodeParams(role: IRSDemoRole, args: DemoArgs, options: Opti
return nodeParams return nodeParams
} }
private fun runNode(nodeParams : NodeParams, useInMemoryMessaging: Boolean) : Unit { private fun runNode(nodeParams : NodeParams, demoNodeConfig: DemoConfig) : Unit {
val node = when(useInMemoryMessaging) { val node = when(demoNodeConfig.inMemory) {
true -> startDemoNode(nodeParams) true -> startDemoNode(nodeParams)
false -> startNode(nodeParams) false -> startNode(nodeParams)
} }
@ -255,6 +253,7 @@ private fun runNode(nodeParams : NodeParams, useInMemoryMessaging: Boolean) : Un
runUploadRates("http://localhost:31341") runUploadRates("http://localhost:31341")
} }
demoNodeConfig.nodeReady.countDown()
try { try {
while (true) Thread.sleep(Long.MAX_VALUE) while (true) Thread.sleep(Long.MAX_VALUE)
} catch(e: InterruptedException) { } catch(e: InterruptedException) {

View File

@ -32,7 +32,6 @@ import com.r3corda.protocols.NotaryProtocol
import com.r3corda.protocols.TwoPartyTradeProtocol import com.r3corda.protocols.TwoPartyTradeProtocol
import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigFactory
import joptsimple.OptionParser import joptsimple.OptionParser
import joptsimple.OptionSet
import java.io.Serializable import java.io.Serializable
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
@ -78,7 +77,7 @@ fun main(args: Array<String>) {
exitProcess(runTraderDemo(args)) exitProcess(runTraderDemo(args))
} }
fun runTraderDemo(args: Array<String>, useInMemoryMessaging: Boolean = false): Int { fun runTraderDemo(args: Array<String>, demoNodeConfig: DemoConfig = DemoConfig()): Int {
val cashIssuerKey = generateKeyPair() val cashIssuerKey = generateKeyPair()
val cashIssuer = Party("Trusted cash issuer", cashIssuerKey.public) val cashIssuer = Party("Trusted cash issuer", cashIssuerKey.public)
val amount = 1000.DOLLARS `issued by` cashIssuer.ref(1) val amount = 1000.DOLLARS `issued by` cashIssuer.ref(1)
@ -163,7 +162,7 @@ fun runTraderDemo(args: Array<String>, useInMemoryMessaging: Boolean = false): I
cashIssuer = party cashIssuer = party
NodeInfo(ArtemisMessagingService.makeRecipient(theirNetAddr), party, setOf(NetworkMapService.Type)) NodeInfo(ArtemisMessagingService.makeRecipient(theirNetAddr), party, setOf(NetworkMapService.Type))
if(useInMemoryMessaging) { if(demoNodeConfig.inMemory) {
val handle = InMemoryMessagingNetwork.Handle(peerId, "Other Node") val handle = InMemoryMessagingNetwork.Handle(peerId, "Other Node")
NodeInfo(handle, party, setOf(NetworkMapService.Type)) NodeInfo(handle, party, setOf(NetworkMapService.Type))
} else { } else {
@ -173,7 +172,11 @@ fun runTraderDemo(args: Array<String>, useInMemoryMessaging: Boolean = false): I
val messageService = messageNetwork.createNodeWithID(false, id).start().get() val messageService = messageNetwork.createNodeWithID(false, id).start().get()
// And now construct then start the node object. It takes a little while. // And now construct then start the node object. It takes a little while.
val node = logElapsedTime("Node startup") { val node = logElapsedTime("Node startup") {
Node(directory, myNetAddr, config, networkMapId, advertisedServices).setup().start() if(demoNodeConfig.inMemory) {
DemoNode(messageService, directory, myNetAddr, config, networkMapId, advertisedServices).setup().start()
} else {
Node(directory, myNetAddr, config, networkMapId, advertisedServices).setup().start()
}
} }
// TODO: Replace with a separate trusted cash issuer // TODO: Replace with a separate trusted cash issuer
@ -190,7 +193,7 @@ fun runTraderDemo(args: Array<String>, useInMemoryMessaging: Boolean = false): I
val dest: Destination val dest: Destination
val recipient: SingleMessageRecipient val recipient: SingleMessageRecipient
if(useInMemoryMessaging) { if(demoNodeConfig.inMemory) {
recipient = InMemoryMessagingNetwork.Handle(peerId, "Other Node") recipient = InMemoryMessagingNetwork.Handle(peerId, "Other Node")
dest = Destination(InMemoryMessagingNetwork.Handle(id, role.toString()), true) dest = Destination(InMemoryMessagingNetwork.Handle(id, role.toString()), true)
} else { } else {

View File

@ -1,5 +1,6 @@
package com.r3corda.core.testing package com.r3corda.core.testing
import com.r3corda.demos.DemoConfig
import com.r3corda.demos.runIRSDemo import com.r3corda.demos.runIRSDemo
import kotlin.concurrent.thread import kotlin.concurrent.thread
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -12,14 +13,12 @@ class IRSDemoTest {
val dirA = Paths.get("./nodeA") val dirA = Paths.get("./nodeA")
val dirB = Paths.get("./nodeB") val dirB = Paths.get("./nodeB")
try { try {
setupNodeA(dirA) setupNode(dirA, "NodeA")
setupNodeB(dirB) setupNode(dirB, "NodeB")
startNodeA(dirA) startNode(dirA, "NodeA")
startNodeB(dirB) startNode(dirB, "NodeB")
runTrade() runTrade()
runDateChange() runDateChange()
stopNodeA()
stopNodeB()
} finally { } finally {
cleanup(dirA) cleanup(dirA)
cleanup(dirB) cleanup(dirB)
@ -27,30 +26,21 @@ class IRSDemoTest {
} }
} }
private fun setupNodeA(dir: Path) { private fun setupNode(dir: Path, nodeType: String) {
runIRSDemo(arrayOf("--role", "SetupNodeA", "--dir", dir.toString())) runIRSDemo(arrayOf("--role", "Setup" + nodeType, "--dir", dir.toString()))
} }
private fun setupNodeB(dir: Path) { private fun startNode(dir: Path, nodeType: String) {
runIRSDemo(arrayOf("--role", "SetupNodeB", "--dir", dir.toString())) val config = DemoConfig(true)
} thread(true, false, null, nodeType, -1, {
try {
private fun startNodeA(dir: Path) { runIRSDemo(arrayOf("--role", nodeType, "--dir", dir.toString()), config)
thread(true, false, null, "NodeA", -1, { runIRSDemo(arrayOf("--role", "NodeA", "--dir", dir.toString()), true) }) } finally {
Thread.sleep(15000) // Will only reach here during error or after node is stopped, so ensure lock is unlocked.
} config.nodeReady.countDown()
}
private fun startNodeB(dir: Path) { })
thread(true, false, null, "NodeB", -1, { runIRSDemo(arrayOf("--role", "NodeB", "--dir", dir.toString()), true) }) config.nodeReady.await()
Thread.sleep(15000)
}
private fun stopNodeA() {
}
private fun stopNodeB() {
} }
private fun runTrade() { private fun runTrade() {

View File

@ -1,10 +1,9 @@
package com.r3corda.core.testing package com.r3corda.core.testing
import com.r3corda.demos.DemoConfig
import com.r3corda.demos.runTraderDemo import com.r3corda.demos.runTraderDemo
import org.junit.Test import org.junit.Test
import java.nio.file.Path
import java.nio.file.Paths import java.nio.file.Paths
import kotlin.concurrent.thread
import kotlin.test.assertEquals import kotlin.test.assertEquals
class TraderDemoTest { class TraderDemoTest {
@ -19,13 +18,14 @@ class TraderDemoTest {
} }
private fun runBuyer() { private fun runBuyer() {
thread(true, false, null, "Buyer", -1, { runTraderDemo(arrayOf("--role", "BUYER"), true) }) val config = DemoConfig(true)
Thread.sleep(15000) runTraderDemo(arrayOf("--role", "BUYER"), config)
config.nodeReady.await()
} }
private fun runSeller() { private fun runSeller() {
println("Running Seller") val config = DemoConfig(true)
assertEquals(runTraderDemo(arrayOf("--role", "SELLER"), true), 0) assertEquals(runTraderDemo(arrayOf("--role", "SELLER"), config), 0)
} }
private fun cleanup() { private fun cleanup() {