public 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) class MockNetworkParameters
builder, which may be more
convenient than specifying all the defaults by hand. Please see class 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 MockNetwork.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 MockNetwork.getDefaultNotaryNode
.
Constructor and Description |
---|
MockNetwork(java.util.List<java.lang.String> cordappPackages,
MockNetworkParameters defaultParameters,
boolean networkSendManuallyPumped,
boolean threadPerNode,
InMemoryMessagingNetwork.ServicePeerAllocationStrategy servicePeerAllocationStrategy,
java.util.List<net.corda.testing.node.MockNetworkNotarySpec> notarySpecs,
NetworkParameters 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.
|
MockNetwork(java.util.List<java.lang.String> cordappPackages,
MockNetworkParameters parameters)
Deprecated.
|
MockNetwork(MockNetworkParameters parameters) |
Modifier and Type | Method and Description |
---|---|
java.nio.file.Path |
baseDirectory(int nodeId)
Get the base directory for the given node id.
|
StartedMockNode |
createNode(MockNodeParameters parameters)
Create a started node with the given parameters.
|
StartedMockNode |
createNode(CordaX500Name legalName,
java.lang.Integer forcedID,
java.math.BigInteger entropyRoot,
MockNodeConfigOverrides configOverrides)
Create a started node with the given parameters.
|
StartedMockNode |
createPartyNode(CordaX500Name legalName)
Create a started node with the given identity.
|
UnstartedMockNode |
createUnstartedNode(MockNodeParameters parameters)
Create an unstarted node with the given parameters.
|
UnstartedMockNode |
createUnstartedNode(CordaX500Name legalName,
java.lang.Integer forcedID,
java.math.BigInteger entropyRoot,
MockNodeConfigOverrides configOverrides)
Create an unstarted node with the given parameters.
|
java.util.List<java.lang.String> |
getCordappPackages()
Deprecated.
|
Party |
getDefaultNotaryIdentity()
Return the identity of the default notary node.
|
StartedMockNode |
getDefaultNotaryNode()
Returns the single notary node on the network. Throws an exception if there are none or more than one.
|
MockNetworkParameters |
getDefaultParameters()
The default parameters for the network.
|
NetworkParameters |
getNetworkParameters()
The network parameters to be used by all the nodes.
|
boolean |
getNetworkSendManuallyPumped()
If false then messages will not be routed from sender to receiver until you use
|
int |
getNextNodeId()
In a mock network, nodes have an incrementing integer ID. Real networks do not have this. Returns the next ID that will be used.
|
java.util.List<net.corda.testing.node.StartedMockNode> |
getNotaryNodes()
Returns the list of notary nodes started by the network.
|
java.util.List<net.corda.testing.node.MockNetworkNotarySpec> |
getNotarySpecs()
The notaries to use in the mock network.
|
InMemoryMessagingNetwork.ServicePeerAllocationStrategy |
getServicePeerAllocationStrategy()
How messages are load balanced in the case where a single compound identity
|
boolean |
getThreadPerNode()
If true then each node will be run in its own thread.
|
void |
runNetwork(int rounds)
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).
|
void |
startNodes()
Start all nodes that aren't already started.
|
void |
stopNodes()
Stop all nodes.
|
void |
waitQuiescent()
Block until all scheduled activity, active flows and network activity has ceased.
|
public MockNetwork(java.util.List<java.lang.String> cordappPackages, MockNetworkParameters defaultParameters, boolean networkSendManuallyPumped, boolean threadPerNode, InMemoryMessagingNetwork.ServicePeerAllocationStrategy servicePeerAllocationStrategy, java.util.List<net.corda.testing.node.MockNetworkNotarySpec> notarySpecs, NetworkParameters 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.
Java users can use the constructor that takes an (optional) class MockNetworkParameters
builder, which may be more
convenient than specifying all the defaults by hand. Please see class 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 MockNetwork.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 MockNetwork.getDefaultNotaryNode
.
cordappPackages
- A List of cordapp packages to scan for any cordapp code, e.g. contract verification code, flows and services.defaultParameters
- The default parameters for the network. If any of the remaining constructor parameters are specified thentheir values are taken instead of the corresponding value in defaultParameters.networkSendManuallyPumped
- If false then messages will not be routed from sender to receiver until you usethe MockNetwork.runNetwork
method. This is useful for writing single-threaded unit test code that can examine thestate of the mock network before and after a message is sent, without races and without the receiving node immediatelysending a response. The default is false, so you must call runNetwork.threadPerNode
- If true then each node will be run in its own thread. This can result in race conditions inyour code if not carefully written, but is more realistic and may help if you have flows in your app that do longblocking operations. The default is false.servicePeerAllocationStrategy
- How messages are load balanced in the case where a single compound identityis used by multiple nodes. You rarely if ever need to change that, it's primarily of interest to people testingnotary code.notarySpecs
- The notaries to use in the mock network. By default you get one mock notary and that is usually sufficient.networkParameters
- The network parameters to be used by all the nodes. NetworkParameters.notaries must beempty as notaries are defined by notarySpecs.class MockNetworkParameters
,
class MockNetworkParameters
,
MockNetwork.runNetwork
,
MockNetwork.getDefaultNotaryNode
public MockNetwork(java.util.List<java.lang.String> cordappPackages, MockNetworkParameters parameters)
public MockNetwork(MockNetworkParameters parameters)
public int getNextNodeId()
In a mock network, nodes have an incrementing integer ID. Real networks do not have this. Returns the next ID that will be used.
public StartedMockNode getDefaultNotaryNode()
Returns the single notary node on the network. Throws an exception if there are none or more than one.
MockNetwork.getNotaryNodes
public Party getDefaultNotaryIdentity()
Return the identity of the default notary node.
MockNetwork.getDefaultNotaryNode
public java.util.List<net.corda.testing.node.StartedMockNode> getNotaryNodes()
Returns the list of notary nodes started by the network.
public StartedMockNode createPartyNode(CordaX500Name legalName)
Create a started node with the given identity.
public StartedMockNode createNode(MockNodeParameters parameters)
Create a started node with the given parameters.
public StartedMockNode createNode(CordaX500Name legalName, java.lang.Integer forcedID, java.math.BigInteger entropyRoot, MockNodeConfigOverrides configOverrides)
Create a started node with the given parameters.
legalName
- The node's legal name.forcedID
- A unique identifier for the node.entropyRoot
- The initial entropy value to use when generating keys. Defaults to an (insecure) random value,
but can be overridden to cause nodes to have stable or colliding identity/service keys.configOverrides
- Add/override the default configuration/behaviour of the nodepublic UnstartedMockNode createUnstartedNode(MockNodeParameters parameters)
Create an unstarted node with the given parameters.
public UnstartedMockNode createUnstartedNode(CordaX500Name legalName, java.lang.Integer forcedID, java.math.BigInteger entropyRoot, MockNodeConfigOverrides configOverrides)
Create an unstarted node with the given parameters.
legalName
- The node's legal name.forcedID
- A unique identifier for the node.entropyRoot
- The initial entropy value to use when generating keys. Defaults to an (insecure) random value,
but can be overridden to cause nodes to have stable or colliding identity/service keys.configOverrides
- Add/override behaviour of the NodeConfiguration mock object.public void startNodes()
Start all nodes that aren't already started.
public void stopNodes()
Stop all nodes.
public void waitQuiescent()
Block until all scheduled activity, active flows and network activity has ceased.
public void runNetwork(int rounds)
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).
public java.nio.file.Path baseDirectory(int nodeId)
Get the base directory for the given node id.
public java.util.List<java.lang.String> getCordappPackages()
A
List of cordapp packages to scan for any cordapp code, e.g. contract verification code, flows and services.
public MockNetworkParameters getDefaultParameters()
The default parameters for the network.
If any of the remaining constructor parameters are specified thentheir values are taken instead of the corresponding value in defaultParameters.
public boolean getNetworkSendManuallyPumped()
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 thestate of the mock network before and after a message is sent, without races and without the receiving node immediatelysending a response. The default is false, so you must call runNetwork.
MockNetwork.runNetwork
public boolean getThreadPerNode()
If true then each node will be run in its own thread.
This can result in race conditions inyour code if not carefully written, but is more realistic and may help if you have flows in your app that do longblocking operations. The default is false.
public InMemoryMessagingNetwork.ServicePeerAllocationStrategy getServicePeerAllocationStrategy()
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 testingnotary code.
public java.util.List<net.corda.testing.node.MockNetworkNotarySpec> getNotarySpecs()
The notaries to use in the mock network.
By default you get one mock notary and that is usually sufficient.
public NetworkParameters getNetworkParameters()
The network parameters to be used by all the nodes.
NetworkParameters.notaries must beempty as notaries are defined by notarySpecs.