diff --git a/docs/source/api-testing.rst b/docs/source/api-testing.rst index 286e15cb70..78bf5e828f 100644 --- a/docs/source/api-testing.rst +++ b/docs/source/api-testing.rst @@ -201,19 +201,22 @@ CorDapps specific to their role in the network. Running the network ^^^^^^^^^^^^^^^^^^^ +When using a ``MockNetwork``, you must be careful to ensure that all the nodes have processed all the relevant messages +before making assertions about the result of performing some action. For example, if you start a flow to update the ledger +but don't wait until all the nodes involved have processed all the resulting messages, your nodes' vaults may not be in +the state you expect. -Regular Corda nodes automatically process received messages. When using a ``MockNetwork`` with -``networkSendManuallyPumped`` set to ``false``, you must manually initiate the processing of received messages. - +When ``networkSendManuallyPumped`` is set to ``false``, you must manually initiate the processing of received messages. You manually process received messages as follows: -* ``StartedMockNode.pumpReceive`` to process a single message from the node's queue - -* ``MockNetwork.runNetwork`` to process all the messages in every node's queue. This may generate additional messages - that must in turn be processed - - * ``network.runNetwork(-1)`` (the default in Kotlin) will exchange messages until there are no further messages to +* ``StartedMockNode.pumpReceive()`` processes a single message from the node's queue +* ``MockNetwork.runNetwork()`` processes all the messages in every node's queue until there are no further messages to process + +When ``networkSendManuallyPumped`` is set to ``true``, nodes will automatically process the messages they receive. You +can block until all messages have been processed using ``MockNetwork.waitQuiescent()``. + +.. warning:: If ``threadPerNode`` is set to ``true``, ``networkSendManuallyPumped`` must also be set to ``true``. Running flows ^^^^^^^^^^^^^ diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt index d81fc4c529..61b0198c2a 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt @@ -71,7 +71,7 @@ data class MockNodeParameters constructor( * Immutable builder for configuring a [MockNetwork]. Kotlin users can also use named parameters to the constructor * of [MockNetwork], which is more convenient. * - * @property networkSendManuallyPumped If true then messages will not be routed from sender to receiver until you use + * @property 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. @@ -280,7 +280,7 @@ inline fun > StartedMockNode.registerResponderFlow( * @property cordappPackages A [List] of cordapp packages to scan for any cordapp code, e.g. contract verification code, flows and services. * @property defaultParameters A [MockNetworkParameters] object which contains the same parameters as the constructor, provided * as a convenience for Java users. - * @property networkSendManuallyPumped If true then messages will not be routed from sender to receiver until you use + * @property 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. diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt index 454d0e3ca1..3985bf713a 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt @@ -506,7 +506,8 @@ open class InternalMockNetwork(defaultParameters: MockNetworkParameters = MockNe */ @JvmOverloads fun runNetwork(rounds: Int = -1) { - check(!networkSendManuallyPumped) + check(!networkSendManuallyPumped) { "MockNetwork.runNetwork() should only be used when networkSendManuallyPumped == false. " + + "You can use MockNetwork.waitQuiescent() to wait for all the nodes to process all the messages on their queues instead." } fun pumpAll() = messagingNetwork.endpoints.map { it.pumpReceive(false) } if (rounds == -1) {