Showcase of closures registered as flows. (#2162)

This commit is contained in:
Michele Sollecito 2017-12-01 14:31:52 +00:00 committed by GitHub
parent 9a9eb72179
commit 1f0571306b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<Any>() {
@Suspendable
override fun call(): Any {
val session = initiateFlow(counterParty)
return session.sendAndReceive<Any>(message).unwrap { it }
}
}
nodes[1].registerInitiatedFlow(initiatingFlow::class) { session ->
object : FlowLogic<Unit>() {
@Suspendable
override fun call() {
// this is a closure, meaning you can access variables outside its scope e.g., `answer`.
val receivedMessage = session.receive<String>().unwrap { it }
logger.info("Got message from counterParty: $receivedMessage.")
assertThat(receivedMessage).isEqualTo(message)
session.send(answer)
}
} as FlowLogic<Unit>
}
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