SOLVED: A simple InitiatingFlow with send function gets called two times in a test case


(Nehal Shah) #1

I have a simple sender , receiver flow. The sender flow sends a string message using send function to receiver. When I try to test it using MockNetwork my sender flow gets called two times. Code is below

FLOW CODE

InitiatingFlow
StartableByRPC
class SenderFlow(val message: String,
val otherParty: Party
, override val progressTracker: ProgressTracker = tracker() )
: FlowLogic(){

companion object {

    object FINDING_CP_FROM_VAULT: ProgressTracker.Step("Finding CP from valult")
    object SENDING_PROPOSAL: ProgressTracker.Step("Sending proposal to buyer")
    object VERIFYING_AND_SIGNING : ProgressTracker.Step("Verifying and singing transaction") {
        override fun childProgressTracker() = SignTransactionFlow.tracker()
    }

    object TRADING: ProgressTracker.Step("Trading commerical paper")

    fun tracker() = ProgressTracker(FINDING_CP_FROM_VAULT, SENDING_PROPOSAL, VERIFYING_AND_SIGNING)
}

override fun call(): TestData {



    progressTracker.currentStep = SENDING_PROPOSAL
    println("=============${progressTracker.currentStep}=============")
    //Send the proposal
    var data = TestData(message)
    send(otherParty,data)


    progressTracker.currentStep = VERIFYING_AND_SIGNING
    println("=============${progressTracker.currentStep}=============")


    return data

}

}

@CordaSerializable
data class TestData(val data: String)

@InitiatedBy(SenderFlow::class)
class AcceptorFlow(val otherParty: Party) : FlowLogic() {

override val progressTracker: ProgressTracker = tracker()

companion object {

    val PERCENT_VARIANCE = 0.10

    object RECEIVING: ProgressTracker.Step("waiting to receive offer")
    object VERIFYING: ProgressTracker.Step("Verifying proposal")
    object SIGNING : ProgressTracker.Step("Generating and Signing transaction")
    object COLLECTING_SIGNATURES : ProgressTracker.Step("Collecting signatures from other parties") {
        override fun childProgressTracker() = CollectSignaturesFlow.tracker()
    }
    object RECORDING: ProgressTracker.Step("Recording transaction")

    fun tracker() = ProgressTracker(RECEIVING, VERIFYING, SIGNING, COLLECTING_SIGNATURES, RECORDING)
}

override fun call(): TestData {

    progressTracker.currentStep = RECEIVING
    println("=============${progressTracker.currentStep}=============")

        var proposalData = receive<TestData>(otherParty)

    progressTracker.currentStep = VERIFYING
    println("=============${progressTracker.currentStep}=============")

        var proposal = proposalData.unwrap {  }

        return TestData("hello")
}

}

TEST CODE

class FlowTests {
lateinit var net: MockNetwork
lateinit var sellerNode: MockNetwork.MockNode
lateinit var buyerNode: MockNetwork.MockNode
lateinit var bankNode: MockNetwork.MockNode
lateinit var nodes: MockNetwork.BasketOfNodes

@Before
fun setup() {
    net = MockNetwork()
    nodes = net.createSomeNodes(3)
    sellerNode = nodes.partyNodes[0]
    buyerNode = nodes.partyNodes[1]
    bankNode = nodes.partyNodes[2]
    println(nodes.notaryNode.info.legalIdentity.owningKey)
    buyerNode.registerInitiatedFlow(AcceptorFlow::class.java)

    net.runNetwork()
}

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

@Test
fun `sender acceptor flow`(){

    val senderFlow = SenderFlow("hi",buyerNode.info.legalIdentity)

    var f =sellerNode.services.startFlow(senderFlow).resultFuture
   // net.runNetwork()
   val result = f.getOrThrow()

}

}

LOG OUTPUT: (SEE BOLD)

[INFO ] 11:31:47,092 [Mock network] network.AbstractNetworkMapService.processRegistrationRequest - Added node [] to network map
[INFO ] 11:31:47,098 [Mock network] network.AbstractNetworkMapService.processRegistrationRequest - Added node [] to network map
[INFO ] 11:31:47,109 [Mock network] network.AbstractNetworkMapService.processRegistrationRequest - Added node [] to network map
[INFO ] 11:31:47,114 [Mock network] network.AbstractNetworkMapService.processRegistrationRequest - Added node [] to network map
[INFO ] 11:31:47,122 [Mock network] network.AbstractNetworkMapService.processRegistrationRequest - Added node [] to network map
=============com.usb.trade.flow.SenderFlow$Companion$SENDING_PROPOSAL@7385f64=============
=============com.usb.trade.flow.SenderFlow$Companion$SENDING_PROPOSAL@7385f64=============
=============com.usb.trade.flow.SenderFlow$Companion$VERIFYING_AND_SIGNING@60f6d689=============


(Nehal Shah) #2

I finally figure it out myself. I was missing @Suspendable annotation on the call method