Introducing Observable.toFuture() extension method

This commit is contained in:
Shams Asari
2017-01-17 11:40:33 +00:00
parent 429fbb3b97
commit c4e3b258c7
10 changed files with 85 additions and 68 deletions

View File

@ -1,10 +1,10 @@
package net.corda.docs
import com.google.common.util.concurrent.SettableFuture
import net.corda.core.contracts.*
import net.corda.core.getOrThrow
import net.corda.core.node.services.ServiceInfo
import net.corda.core.serialization.OpaqueBytes
import net.corda.core.toFuture
import net.corda.core.utilities.DUMMY_NOTARY
import net.corda.core.utilities.DUMMY_NOTARY_KEY
import net.corda.flows.CashCommand
@ -66,14 +66,9 @@ class FxTransactionBuildTutorialTest {
printBalances()
// Setup some futures on the vaults to await the arrival of the exchanged funds at both nodes
val done2 = SettableFuture.create<Unit>()
val done3 = SettableFuture.create<Unit>()
val subs2 = nodeA.services.vaultService.updates.subscribe {
done2.set(Unit)
}
val subs3 = nodeB.services.vaultService.updates.subscribe {
done3.set(Unit)
}
val nodeAVaultUpdate = nodeA.services.vaultService.updates.toFuture()
val nodeBVaultUpdate = nodeB.services.vaultService.updates.toFuture()
// Now run the actual Fx exchange
val doIt = nodeA.services.startFlow(ForeignExchangeFlow("trade1",
POUNDS(100).issuedBy(nodeB.info.legalIdentity.ref(0x01)),
@ -83,16 +78,14 @@ class FxTransactionBuildTutorialTest {
// wait for the flow to finish and the vault updates to be done
doIt.resultFuture.getOrThrow()
// Get the balances when the vault updates
done2.get()
nodeAVaultUpdate.get()
val balancesA = databaseTransaction(nodeA.database) {
nodeA.services.vaultService.cashBalances
}
done3.get()
nodeBVaultUpdate.get()
val balancesB = databaseTransaction(nodeB.database) {
nodeB.services.vaultService.cashBalances
}
subs2.unsubscribe()
subs3.unsubscribe()
println("BalanceA\n" + balancesA)
println("BalanceB\n" + balancesB)
// Verify the transfers occurred as expected

View File

@ -1,6 +1,5 @@
package net.corda.docs
import com.google.common.util.concurrent.SettableFuture
import net.corda.core.contracts.LinearState
import net.corda.core.contracts.StateAndRef
import net.corda.core.contracts.StateRef
@ -8,6 +7,7 @@ import net.corda.core.getOrThrow
import net.corda.core.node.ServiceHub
import net.corda.core.node.services.ServiceInfo
import net.corda.core.node.services.linearHeadsOfType
import net.corda.core.toFuture
import net.corda.core.utilities.DUMMY_NOTARY
import net.corda.core.utilities.DUMMY_NOTARY_KEY
import net.corda.node.services.network.NetworkMapService
@ -56,17 +56,13 @@ class WorkflowTransactionBuildTutorialTest {
@Test
fun `Run workflow to completion`() {
// Setup a vault subscriber to wait for successful upload of the proposal to NodeB
val done1 = SettableFuture.create<Unit>()
val subs1 = nodeB.services.vaultService.updates.subscribe {
done1.set(Unit)
}
val nodeBVaultUpdate = nodeB.services.vaultService.updates.toFuture()
// Kick of the proposal flow
val flow1 = nodeA.services.startFlow(SubmitTradeApprovalFlow("1234", nodeB.info.legalIdentity))
// Wait for the flow to finish
val proposalRef = flow1.resultFuture.getOrThrow()
// Wait for NodeB to include it's copy in the vault
done1.get()
subs1.unsubscribe()
nodeBVaultUpdate.get()
// Fetch the latest copy of the state from both nodes
val latestFromA = databaseTransaction(nodeA.database) {
nodeA.services.latest<TradeApprovalContract.State>(proposalRef.ref)
@ -82,23 +78,15 @@ class WorkflowTransactionBuildTutorialTest {
assertEquals(proposalRef, latestFromA)
assertEquals(proposalRef, latestFromB)
// Setup a vault subscriber to pause until the final update is in NodeA and NodeB
val done2 = SettableFuture.create<Unit>()
val subs2 = nodeA.services.vaultService.updates.subscribe {
done2.set(Unit)
}
val done3 = SettableFuture.create<Unit>()
val subs3 = nodeB.services.vaultService.updates.subscribe {
done3.set(Unit)
}
val nodeAVaultUpdate = nodeA.services.vaultService.updates.toFuture()
val secondNodeBVaultUpdate = nodeB.services.vaultService.updates.toFuture()
// Run the manual completion flow from NodeB
val flow2 = nodeB.services.startFlow(SubmitCompletionFlow(latestFromB.ref, WorkflowState.APPROVED))
// wait for the flow to end
val completedRef = flow2.resultFuture.getOrThrow()
// wait for the vault updates to stabilise
done2.get()
done3.get()
subs2.unsubscribe()
subs3.unsubscribe()
nodeAVaultUpdate.get()
secondNodeBVaultUpdate.get()
// Fetch the latest copies from the vault
val finalFromA = databaseTransaction(nodeA.database) {
nodeA.services.latest<TradeApprovalContract.State>(proposalRef.ref)