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

View File

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

View File

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

View File

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