From a7ac54f28058b277dc6f8be18bd38e90511ba86f Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Wed, 8 Jun 2016 15:55:25 +0100 Subject: [PATCH] Removed thread waits from tests instead relying on a lock passed to the demo environment. --- src/main/kotlin/com/r3corda/demos/DemoNode.kt | 6 +++ src/main/kotlin/com/r3corda/demos/IRSDemo.kt | 11 ++--- .../kotlin/com/r3corda/demos/TraderDemo.kt | 13 ++++-- .../com/r3corda/core/testing/IRSDemoTest.kt | 46 ++++++++----------- .../com/r3corda/core/testing/TradeDemoTest.kt | 12 ++--- 5 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/main/kotlin/com/r3corda/demos/DemoNode.kt b/src/main/kotlin/com/r3corda/demos/DemoNode.kt index 6f04fbabea..508b2feb03 100644 --- a/src/main/kotlin/com/r3corda/demos/DemoNode.kt +++ b/src/main/kotlin/com/r3corda/demos/DemoNode.kt @@ -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) } \ No newline at end of file diff --git a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt index f06f54097d..0dacb98323 100644 --- a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt @@ -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) { exitProcess(runIRSDemo(args)) } -fun runIRSDemo(args: Array, useInMemoryMessaging: Boolean = false): Int { +fun runIRSDemo(args: Array, demoNodeConfig: DemoConfig = DemoConfig()): Int { val parser = OptionParser() val demoArgs = setupArgs(parser) val options = try { @@ -154,7 +152,7 @@ fun runIRSDemo(args: Array, 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) { diff --git a/src/main/kotlin/com/r3corda/demos/TraderDemo.kt b/src/main/kotlin/com/r3corda/demos/TraderDemo.kt index 918b49a72b..a3effd7b68 100644 --- a/src/main/kotlin/com/r3corda/demos/TraderDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/TraderDemo.kt @@ -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) { exitProcess(runTraderDemo(args)) } -fun runTraderDemo(args: Array, useInMemoryMessaging: Boolean = false): Int { +fun runTraderDemo(args: Array, 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, 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, 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, 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 { diff --git a/src/test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt b/src/test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt index 5534ad7bb0..33a0a389f4 100644 --- a/src/test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt +++ b/src/test/kotlin/com/r3corda/core/testing/IRSDemoTest.kt @@ -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() { diff --git a/src/test/kotlin/com/r3corda/core/testing/TradeDemoTest.kt b/src/test/kotlin/com/r3corda/core/testing/TradeDemoTest.kt index 4575cfc909..ae75523736 100644 --- a/src/test/kotlin/com/r3corda/core/testing/TradeDemoTest.kt +++ b/src/test/kotlin/com/r3corda/core/testing/TradeDemoTest.kt @@ -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() {