Enable RPC start of Flows that return nothing (eg. Void). (#1374)

* Enable RPC start of Flows that return nothing (eg. Void).

* Fix blocking test (caused by not running mockNetwork).
Improve execution times by moving redundant setup() initialisation to only tests that use it.
This commit is contained in:
josecoll
2017-09-01 15:44:53 +01:00
committed by GitHub
parent 954ed69102
commit a286f7553b
4 changed files with 49 additions and 26 deletions

View File

@ -127,7 +127,7 @@ class CordaRPCOpsImpl(
}
}
override fun <T : Any> startTrackedFlowDynamic(logicType: Class<out FlowLogic<T>>, vararg args: Any?): FlowProgressHandle<T> {
override fun <T> startTrackedFlowDynamic(logicType: Class<out FlowLogic<T>>, vararg args: Any?): FlowProgressHandle<T> {
val stateMachine = startFlow(logicType, args)
return FlowProgressHandleImpl(
id = stateMachine.id,
@ -136,12 +136,12 @@ class CordaRPCOpsImpl(
)
}
override fun <T : Any> startFlowDynamic(logicType: Class<out FlowLogic<T>>, vararg args: Any?): FlowHandle<T> {
override fun <T> startFlowDynamic(logicType: Class<out FlowLogic<T>>, vararg args: Any?): FlowHandle<T> {
val stateMachine = startFlow(logicType, args)
return FlowHandleImpl(id = stateMachine.id, returnValue = stateMachine.resultFuture)
}
private fun <T : Any> startFlow(logicType: Class<out FlowLogic<T>>, args: Array<out Any?>): FlowStateMachineImpl<T> {
private fun <T> startFlow(logicType: Class<out FlowLogic<T>>, args: Array<out Any?>): FlowStateMachineImpl<T> {
require(logicType.isAnnotationPresent(StartableByRPC::class.java)) { "${logicType.name} was not designed for RPC" }
val rpcContext = getRpcContext()
rpcContext.requirePermission(startFlowPermission(logicType))

View File

@ -126,7 +126,7 @@ interface ServiceHubInternal : PluginServiceHub {
* @throws net.corda.core.flows.IllegalFlowLogicException or IllegalArgumentException if there are problems with the
* [logicType] or [args].
*/
fun <T : Any> invokeFlowAsync(
fun <T> invokeFlowAsync(
logicType: Class<out FlowLogic<T>>,
flowInitiator: FlowInitiator,
vararg args: Any?): FlowStateMachineImpl<T> {

View File

@ -7,6 +7,7 @@ import net.corda.core.contracts.Issued
import net.corda.core.crypto.isFulfilledBy
import net.corda.core.crypto.keys
import net.corda.core.flows.FlowLogic
import net.corda.core.flows.StartableByRPC
import net.corda.core.flows.StateMachineRunId
import net.corda.core.messaging.*
import net.corda.core.node.services.ServiceInfo
@ -44,6 +45,7 @@ import rx.Observable
import java.io.ByteArrayOutputStream
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue
class CordaRPCOpsImplTest {
@ -71,12 +73,6 @@ class CordaRPCOpsImplTest {
startFlowPermission<CashIssueFlow>(),
startFlowPermission<CashPaymentFlow>()
))))
aliceNode.database.transaction {
stateMachineUpdates = rpc.stateMachinesFeed().updates
transactions = rpc.verifiedTransactionsFeed().updates
vaultTrackCash = rpc.vaultTrackBy<Cash.State>().updates
}
}
@After
@ -86,6 +82,11 @@ class CordaRPCOpsImplTest {
@Test
fun `cash issue accepted`() {
aliceNode.database.transaction {
stateMachineUpdates = rpc.stateMachinesFeed().updates
vaultTrackCash = rpc.vaultTrackBy<Cash.State>().updates
}
val quantity = 1000L
val ref = OpaqueBytes(ByteArray(1) { 1 })
@ -131,6 +132,12 @@ class CordaRPCOpsImplTest {
@Test
fun `issue and move`() {
aliceNode.database.transaction {
stateMachineUpdates = rpc.stateMachinesFeed().updates
transactions = rpc.verifiedTransactionsFeed().updates
vaultTrackCash = rpc.vaultTrackBy<Cash.State>().updates
}
val anonymous = false
val result = rpc.startFlow(::CashIssueFlow,
100.DOLLARS,
@ -248,4 +255,20 @@ class CordaRPCOpsImplTest {
@Suspendable
override fun call() = Unit
}
@Test
fun `attempt to start RPC flow with void return`() {
CURRENT_RPC_CONTEXT.set(RpcContext(User("user", "pwd", permissions = setOf(
startFlowPermission<VoidRPCFlow>()
))))
val result = rpc.startFlow(::VoidRPCFlow)
mockNet.runNetwork()
assertNull(result.returnValue.getOrThrow())
}
@StartableByRPC
class VoidRPCFlow : FlowLogic<Void?>() {
@Suspendable
override fun call() : Void? = null
}
}