corda / net.corda.testing.node / MockNetwork

MockNetwork

open class MockNetwork

A mock node brings up a suite of in-memory services in a fast manner suitable for unit testing. Components that do IO are either swapped out for mocks, or pointed to a Jimfs in memory filesystem or an in memory H2 database instance.

Java users can use the constructor that takes an (optional) MockNetworkParameters builder, which may be more convenient than specifying all the defaults by hand. Please see MockNetworkParameters for the documentation of each parameter.

Mock network nodes require manual pumping by default: they will not run asynchronous. This means that for message exchanges to take place (and associated handlers to run), you must call the runNetwork method. If you want messages to flow automatically, use automatic pumping with a thread per node but watch out for code running parallel to your unit tests: you will need to use futures correctly to ensure race-free results.

You can get a printout of every message sent by using code like:

    LogHelper.setLevel("+messages")

By default a single notary node is automatically started, which forms part of the network parameters for all the nodes. This node is available by calling defaultNotaryNode.

Constructors

<init>

MockNetwork(cordappPackages: List<String>, parameters: MockNetworkParameters = MockNetworkParameters())MockNetwork(cordappPackages: List<String>, defaultParameters: MockNetworkParameters = MockNetworkParameters(), networkSendManuallyPumped: Boolean = defaultParameters.networkSendManuallyPumped, threadPerNode: Boolean = defaultParameters.threadPerNode, servicePeerAllocationStrategy: ServicePeerAllocationStrategy = defaultParameters.servicePeerAllocationStrategy, notarySpecs: List<MockNetworkNotarySpec> = defaultParameters.notarySpecs, networkParameters: NetworkParameters = defaultParameters.networkParameters)

A mock node brings up a suite of in-memory services in a fast manner suitable for unit testing. Components that do IO are either swapped out for mocks, or pointed to a Jimfs in memory filesystem or an in memory H2 database instance.

Properties

cordappPackages

val cordappPackages: List<String>

A List of cordapp packages to scan for any cordapp code, e.g. contract verification code, flows and services.

defaultNotaryIdentity

val defaultNotaryIdentity: Party

Return the identity of the default notary node.

defaultNotaryNode

val defaultNotaryNode: StartedMockNode

Returns the single notary node on the network. Throws an exception if there are none or more than one.

defaultParameters

val defaultParameters: MockNetworkParameters

A MockNetworkParameters object which contains the same parameters as the constructor, provided as a convenience for Java users.

networkParameters

val networkParameters: NetworkParameters

The network parameters to be used by all the nodes. NetworkParameters.notaries must be empty as notaries are defined by notarySpecs.

networkSendManuallyPumped

val networkSendManuallyPumped: Boolean

If true then messages will not be routed from sender to receiver until you use the MockNetwork.runNetwork method. This is useful for writing single-threaded unit test code that can examine the state of the mock network before and after a message is sent, without races and without the receiving node immediately sending a response. The default is false, so you must call runNetwork.

nextNodeId

val nextNodeId: Int

In a mock network, nodes have an incrementing integer ID. Real networks do not have this. Returns the next ID that will be used.

notaryNodes

val notaryNodes: List<StartedMockNode>

Returns the list of notary nodes started by the network.

notarySpecs

val notarySpecs: List<MockNetworkNotarySpec>

The notaries to use in the mock network. By default you get one mock notary and that is usually sufficient.

servicePeerAllocationStrategy

val servicePeerAllocationStrategy: ServicePeerAllocationStrategy

How messages are load balanced in the case where a single compound identity is used by multiple nodes. You rarely if ever need to change that, it's primarily of interest to people testing notary code.

threadPerNode

val threadPerNode: Boolean

If true then each node will be run in its own thread. This can result in race conditions in your code if not carefully written, but is more realistic and may help if you have flows in your app that do long blocking operations. The default is false.

Functions

baseDirectory

fun baseDirectory(nodeId: Int): Path

Get the base directory for the given node id.

createNode

fun createNode(parameters: MockNodeParameters = MockNodeParameters()): StartedMockNode
fun createNode(legalName: CordaX500Name? = null, forcedID: Int? = null, entropyRoot: BigInteger = BigInteger.valueOf(random63BitValue()), configOverrides: (NodeConfiguration) -> Any? = {}, extraCordappPackages: List<String> = emptyList()): StartedMockNode

Create a started node with the given parameters.

createPartyNode

fun createPartyNode(legalName: CordaX500Name? = null): StartedMockNode

Create a started node with the given identity.

createUnstartedNode

fun createUnstartedNode(parameters: MockNodeParameters = MockNodeParameters()): UnstartedMockNode
fun createUnstartedNode(legalName: CordaX500Name? = null, forcedID: Int? = null, entropyRoot: BigInteger = BigInteger.valueOf(random63BitValue()), configOverrides: (NodeConfiguration) -> Any? = {}, extraCordappPackages: List<String> = emptyList()): UnstartedMockNode

Create an unstarted node with the given parameters.

runNetwork

fun runNetwork(rounds: Int = -1): Unit

Asks every node in order to process any queued up inbound messages. This may in turn result in nodes sending more messages to each other, thus, a typical usage is to call runNetwork with the rounds parameter set to -1 (the default) which simply runs as many rounds as necessary to result in network stability (no nodes sent any messages in the last round).

startNodes

fun startNodes(): Unit

Start all nodes that aren't already started.

stopNodes

fun stopNodes(): Unit

Stop all nodes.

waitQuiescent

fun waitQuiescent(): Unit

Block until all scheduled activity, active flows and network activity has ceased.

Extension Functions

contextLogger

fun Any.contextLogger(): Logger

When called from a companion object, returns the logger for the enclosing class.