Setup TraderDemo test. Moved DemoNode to a common file. Modified TraderDemo to be tested.

This commit is contained in:
Clinton Alexander 2016-06-07 18:25:11 +01:00 committed by Andras Slemmer
parent de27b1e8de
commit b61b362891
4 changed files with 87 additions and 28 deletions

View File

@ -0,0 +1,27 @@
package com.r3corda.demos
import com.google.common.net.HostAndPort
import com.r3corda.core.messaging.MessagingService
import com.r3corda.core.node.NodeInfo
import com.r3corda.core.node.services.ServiceType
import com.r3corda.node.internal.Node
import com.r3corda.node.serialization.NodeClock
import com.r3corda.node.services.config.NodeConfiguration
import com.r3corda.node.services.network.InMemoryMessagingNetwork
import java.nio.file.Path
import java.time.Clock
val messageNetwork = InMemoryMessagingNetwork()
class DemoNode(messagingService: MessagingService, dir: Path, p2pAddr: HostAndPort, config: NodeConfiguration,
networkMapAddress: NodeInfo?, advertisedServices: Set<ServiceType>,
clock: Clock = NodeClock(), clientAPIs: List<Class<*>> = listOf())
: Node(dir, p2pAddr, config, networkMapAddress, advertisedServices, clock, clientAPIs) {
val messagingService = messagingService
override fun makeMessagingService(): MessagingService {
return messagingService
}
override fun startMessagingService() = Unit
}

View File

@ -86,21 +86,6 @@ private class NotSetupException: Throwable {
constructor(message: String): super(message) {} constructor(message: String): super(message) {}
} }
val messageNetwork = InMemoryMessagingNetwork()
class DemoNode(messagingService: MessagingService, dir: Path, p2pAddr: HostAndPort, config: NodeConfiguration,
networkMapAddress: NodeInfo?, advertisedServices: Set<ServiceType>,
clock: Clock, clientAPIs: List<Class<*>> = listOf())
: Node(dir, p2pAddr, config, networkMapAddress, advertisedServices, clock, clientAPIs) {
val messagingService = messagingService
override fun makeMessagingService(): MessagingService {
return messagingService
}
override fun startMessagingService() = Unit
}
fun main(args: Array<String>) { fun main(args: Array<String>) {
exitProcess(runIRSDemo(args)) exitProcess(runIRSDemo(args))
} }

View File

@ -68,13 +68,27 @@ enum class Role {
val DIRNAME = "trader-demo" val DIRNAME = "trader-demo"
fun main(args: Array<String>) { fun main(args: Array<String>) {
exitProcess(runTraderDemo(args))
}
fun runTraderDemo(args: Array<String>, useInMemoryMessaging: Boolean = false): Int {
val cashIssuerKey = generateKeyPair()
val cashIssuer = Party("Trusted cash issuer", cashIssuerKey.public)
val amount = 1000.DOLLARS `issued by` cashIssuer.ref(1)
val parser = OptionParser() val parser = OptionParser()
val roleArg = parser.accepts("role").withRequiredArg().ofType(Role::class.java).required() val roleArg = parser.accepts("role").withRequiredArg().ofType(Role::class.java).required()
val myNetworkAddress = parser.accepts("network-address").withRequiredArg().defaultsTo("localhost") val myNetworkAddress = parser.accepts("network-address").withRequiredArg().defaultsTo("localhost")
val theirNetworkAddress = parser.accepts("other-network-address").withRequiredArg().defaultsTo("localhost") val theirNetworkAddress = parser.accepts("other-network-address").withRequiredArg().defaultsTo("localhost")
val options = parseOptions(args, parser) val options = try {
parser.parse(*args)
} catch (e: Exception) {
println(e.message)
println("Please refer to the documentation in docs/build/index.html to learn how to run the demo.")
return 1
}
val role = options.valueOf(roleArg)!! val role = options.valueOf(roleArg)!!
val myNetAddr = HostAndPort.fromString(options.valueOf(myNetworkAddress)).withDefaultPort( val myNetAddr = HostAndPort.fromString(options.valueOf(myNetworkAddress)).withDefaultPort(
@ -130,6 +144,11 @@ fun main(args: Array<String>) {
NodeInfo(ArtemisMessagingService.makeRecipient(theirNetAddr), party, setOf(NetworkMapService.Type)) NodeInfo(ArtemisMessagingService.makeRecipient(theirNetAddr), party, setOf(NetworkMapService.Type))
} }
val id = when(role) {
Role.BUYER -> 0
Role.SELLER -> 1
}
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() Node(directory, myNetAddr, config, networkMapId, advertisedServices).setup().start()
@ -148,19 +167,13 @@ fun main(args: Array<String>) {
} else { } else {
runSeller(myNetAddr, node, theirNetAddr, amount) runSeller(myNetAddr, node, theirNetAddr, amount)
} }
return 0
} }
fun parseOptions(args: Array<String>, parser: OptionParser): OptionSet {
try {
return parser.parse(*args)
} catch (e: Exception) {
println(e.message)
println("Please refer to the documentation in docs/build/index.html to learn how to run the demo.")
exitProcess(1)
}
} }
fun runSeller(myNetAddr: HostAndPort, node: Node, theirNetAddr: HostAndPort, amount: Amount<Issued<Currency>>) { private fun runSeller(myNetAddr: HostAndPort, node: Node, theirNetAddr: HostAndPort) {
// The seller will sell some commercial paper to the buyer, who will pay with (self issued) cash. // The seller will sell some commercial paper to the buyer, who will pay with (self issued) cash.
// //
// The CP sale transaction comes with a prospectus PDF, which will tag along for the ride in an // The CP sale transaction comes with a prospectus PDF, which will tag along for the ride in an
@ -187,7 +200,7 @@ fun runSeller(myNetAddr: HostAndPort, node: Node, theirNetAddr: HostAndPort, amo
node.stop() node.stop()
} }
fun runBuyer(node: Node, amount: Amount<Issued<Currency>>) { private fun runBuyer(node: Node, amount: Amount<Issued<Currency>>) {
// Buyer will fetch the attachment from the seller automatically when it resolves the transaction. // Buyer will fetch the attachment from the seller automatically when it resolves the transaction.
// For demo purposes just extract attachment jars when saved to disk, so the user can explore them. // For demo purposes just extract attachment jars when saved to disk, so the user can explore them.
val attachmentsPath = (node.storage.attachments as NodeAttachmentService).let { val attachmentsPath = (node.storage.attachments as NodeAttachmentService).let {
@ -211,7 +224,7 @@ fun runBuyer(node: Node, amount: Amount<Issued<Currency>>) {
val DEMO_TOPIC = "initiate.demo.trade" val DEMO_TOPIC = "initiate.demo.trade"
class TraderDemoProtocolBuyer(private val attachmentsPath: Path, private class TraderDemoProtocolBuyer(private val attachmentsPath: Path,
val notary: Party, val notary: Party,
val amount: Amount<Issued<Currency>>) : ProtocolLogic<Unit>() { val amount: Amount<Issued<Currency>>) : ProtocolLogic<Unit>() {
companion object { companion object {
@ -289,7 +302,7 @@ ${Emoji.renderIfSupported(cpIssuance)}""")
} }
} }
class TraderDemoProtocolSeller(val myAddress: HostAndPort, private class TraderDemoProtocolSeller(val myAddress: HostAndPort,
val otherSide: SingleMessageRecipient, val otherSide: SingleMessageRecipient,
val amount: Amount<Issued<Currency>>, val amount: Amount<Issued<Currency>>,
override val progressTracker: ProgressTracker = TraderDemoProtocolSeller.tracker()) : ProtocolLogic<Unit>() { override val progressTracker: ProgressTracker = TraderDemoProtocolSeller.tracker()) : ProtocolLogic<Unit>() {

View File

@ -0,0 +1,34 @@
package com.r3corda.core.testing
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 {
@Test fun `runs trader demo`() {
try {
runBuyer()
runSeller()
} finally {
cleanup()
}
}
}
private fun runBuyer() {
thread(true, false, null, "Buyer", -1, { runTraderDemo(arrayOf("--role", "BUYER"), true) })
Thread.sleep(5000)
}
private fun runSeller() {
assertEquals(runTraderDemo(arrayOf("--role", "SELLER"), true), 0)
}
private fun cleanup() {
val dir = Paths.get("trader-demo")
println("Erasing " + dir)
dir.toFile().deleteRecursively()
}