diff --git a/core/src/test/kotlin/net/corda/core/flows/ReceiveAllFlowTests.kt b/core/src/test/kotlin/net/corda/core/flows/ReceiveAllFlowTests.kt index dd2dbab17f..2fc6be8955 100644 --- a/core/src/test/kotlin/net/corda/core/flows/ReceiveAllFlowTests.kt +++ b/core/src/test/kotlin/net/corda/core/flows/ReceiveAllFlowTests.kt @@ -20,6 +20,42 @@ class ReceiveMultipleFlowTests { mockNet.stopNodes() } + @Test + fun showcase_flows_as_closures() { + + val answer = 10.0 + val message = "Hello Ivan" + + val counterParty = nodes[1].info.singleIdentity() + + val initiatingFlow = @InitiatingFlow object : FlowLogic() { + + @Suspendable + override fun call(): Any { + val session = initiateFlow(counterParty) + return session.sendAndReceive(message).unwrap { it } + } + } + + nodes[1].registerInitiatedFlow(initiatingFlow::class) { session -> + object : FlowLogic() { + @Suspendable + override fun call() { + // this is a closure, meaning you can access variables outside its scope e.g., `answer`. + val receivedMessage = session.receive().unwrap { it } + logger.info("Got message from counterParty: $receivedMessage.") + assertThat(receivedMessage).isEqualTo(message) + session.send(answer) + } + } as FlowLogic + } + + val flow = nodes[0].services.startFlow(initiatingFlow) + mockNet.runNetwork() + val receivedAnswer = flow.resultFuture.getOrThrow() + assertThat(receivedAnswer).isEqualTo(answer) + } + @Test fun `receive all messages in parallel using map style`() { val doubleValue = 5.0