MockNetwork - corda

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.

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(parameters: MockNetworkParameters)` 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. `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)`

### Properties

#### cordappPackages | A List of cordapp packages to scan for any cordapp code, e.g. contract verification code, flows and services. `val cordappPackages: List<String>`
#### defaultNotaryIdentity | Return the identity of the default notary node. `val defaultNotaryIdentity: Party`
#### defaultNotaryNode | Returns the single notary node on the network. Throws an exception if there are none or more than one. `val defaultNotaryNode: StartedMockNode`
#### defaultParameters | The default parameters for the network. If any of the remaining constructor parameters are specified then their values are taken instead of the corresponding value in defaultParameters. `val defaultParameters: MockNetworkParameters`
#### networkParameters | The network parameters to be used by all the nodes. NetworkParameters.notaries must be empty as notaries are defined by notarySpecs. `val networkParameters: NetworkParameters`
#### networkSendManuallyPumped | If false 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. `val networkSendManuallyPumped: Boolean`
#### nextNodeId | In a mock network, nodes have an incrementing integer ID. Real networks do not have this. Returns the next ID that will be used. `val nextNodeId: Int`
#### notaryNodes | Returns the list of notary nodes started by the network. `val notaryNodes: List<StartedMockNode>`
#### notarySpecs | The notaries to use in the mock network. By default you get one mock notary and that is usually sufficient. `val notarySpecs: List<MockNetworkNotarySpec>`
#### 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. `val servicePeerAllocationStrategy: ServicePeerAllocationStrategy`
#### threadPerNode | 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. `val threadPerNode: Boolean`

### Functions

#### baseDirectory | Get the base directory for the given node id. `fun baseDirectory(nodeId: Int): Path`
#### createNode | Create a started node with the given parameters. `fun createNode(parameters: MockNodeParameters): StartedMockNode` `fun createNode(legalName: CordaX500Name? = null, forcedID: Int? = null, entropyRoot: BigInteger = BigInteger.valueOf(random63BitValue()), configOverrides: MockNodeConfigOverrides? = null): StartedMockNode`
#### createPartyNode | Create a started node with the given identity. `fun createPartyNode(legalName: CordaX500Name? = null): StartedMockNode`
#### createUnstartedNode | Create an unstarted node with the given parameters. `fun createUnstartedNode(parameters: MockNodeParameters = MockNodeParameters()): UnstartedMockNode` `fun createUnstartedNode(legalName: CordaX500Name? = null, forcedID: Int? = null, entropyRoot: BigInteger = BigInteger.valueOf(random63BitValue()), configOverrides: MockNodeConfigOverrides? = null): UnstartedMockNode`
#### runNetwork | 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). `fun runNetwork(rounds: Int = -1): Unit`
#### startNodes | Start all nodes that aren't already started. `fun startNodes(): Unit`
#### stopNodes | Stop all nodes. `fun stopNodes(): Unit`
#### waitQuiescent | Block until all scheduled activity, active flows and network activity has ceased. `fun waitQuiescent(): Unit`