Corda - 自 Corda 4 升级以来,测试接收方流程失败

3
以下流程测试在 Corda 3 中有效...
@Test
fun `can ping counterparties`() {

    val acceptorFlowFutures = listOf(nodeB, nodeC).map {
        it.registerInitiatedFlow(SendPingAcceptorFlow::class.java).toFuture()
    }

    nodeA.startFlow(SendPingInitiatorFlow("Ping!"))
    network.runNetwork()

    acceptorFlowFutures.forEach {
        val result = it
            .getOrThrow(Duration.ofMinutes(1)) // Timeout failure here
            .stateMachine
            .resultFuture
            .getOrThrow(Duration.ofMinutes(1)) as String

        assertEquals("Ping!", result)
    }
}

自从更新为使用Corda 4后,现在出现以下异常:

java.util.concurrent.TimeoutException

这是什么原因造成的?
1个回答

1

我已经在 Corda 4 上成功测试了以下代码,符合给定的情景。

import co.paralleluniverse.fibers.Suspendable
import net.corda.core.flows.*
import net.corda.core.identity.Party
import net.corda.core.toFuture
import net.corda.core.utilities.getOrThrow
import net.corda.core.utilities.unwrap
import net.corda.testing.internal.chooseIdentity
import net.corda.testing.node.MockNetwork
import net.corda.testing.node.StartedMockNode
import org.junit.After
import org.junit.Before
import org.junit.Test
import java.time.Duration
import kotlin.test.assertEquals

class DummyFlowUnitTests2 {
    lateinit var network: MockNetwork
    lateinit var nodeA: StartedMockNode
    lateinit var nodeB: StartedMockNode
    lateinit var nodeC: StartedMockNode

    lateinit var partyA: Party
    lateinit var partyB: Party
    lateinit var partyC: Party

    @Before
    fun setup() {
        network = MockNetwork(listOf("com.corda.cordapp"))

        //Create nodes.
        nodeA = network.createNode()
        nodeB = network.createNode()
        nodeC = network.createNode()

        partyA = nodeA.info.chooseIdentity()
        partyB = nodeB.info.chooseIdentity()
        partyC = nodeC.info.chooseIdentity()

        nodeB.registerInitiatedFlow(SendPingAcceptorFlow::class.java)
        nodeC.registerInitiatedFlow(SendPingAcceptorFlow::class.java)
    }

    @After
    fun tearDown() {
        network.stopNodes()
    }

    @Test
    fun `can ping counterparties`() {

        val acceptorFlowFutures = listOf(nodeB, nodeC).map {
            it.registerInitiatedFlow(SendPingAcceptorFlow::class.java).toFuture()
        }

        nodeA.startFlow(SendPingInitiatorFlow("Ping!", listOf(partyB, partyC)))
        network.runNetwork()

        acceptorFlowFutures.forEach {
            val result = it
                    .getOrThrow(Duration.ofMinutes(1)) // Timeout failure here
                    .stateMachine
                    .resultFuture
                    .getOrThrow(Duration.ofMinutes(1)) as String

            assertEquals("Ping!", result)
        }
    }

    @InitiatingFlow
    @StartableByRPC
    data class SendPingInitiatorFlow(val msg: String, val parties: List<Party>) : FlowLogic<Unit>() {

        @Suspendable
        override fun call() {
            parties.map { initiateFlow(it) }.forEach {
                it.send(msg)
            }
        }
    }

    @InitiatedBy(SendPingInitiatorFlow::class)
    data class SendPingAcceptorFlow(val othersideSession: FlowSession) : FlowLogic<String>() {

        @Suspendable
        override fun call(): String {
            return othersideSession.receive<String>().unwrap { it }
        }
    }
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接