From 38876e3984fc39011a85b71cb5dd978c4748aa46 Mon Sep 17 00:00:00 2001 From: Shams Asari Date: Mon, 28 Nov 2016 10:47:59 +0000 Subject: [PATCH] Introducing Future.getOrThrow() which throws the cause of the ExecutionException --- .../net/corda/client/CordaRPCClientTest.kt | 3 +- .../net/corda/client/NodeMonitorModelTest.kt | 7 +-- core/src/main/kotlin/net/corda/core/Utils.kt | 43 +++++++++++-------- .../core/flows/ResolveTransactionsFlowTest.kt | 18 +++----- .../net/corda/node/driver/DriverTests.kt | 16 +++---- .../node/services/DistributedNotaryTests.kt | 12 +++--- .../corda/node/messaging/AttachmentTests.kt | 12 +++--- .../messaging/TwoPartyTradeProtocolTests.kt | 27 +++++------- .../services/DistributedImmutableMapTests.kt | 13 +++--- .../services/InMemoryNetworkMapCacheTest.kt | 12 ++---- .../services/InMemoryNetworkMapServiceTest.kt | 17 ++++---- .../corda/node/services/NotaryChangeTests.kt | 13 +++--- .../corda/node/services/NotaryServiceTests.kt | 17 ++++---- .../services/ValidatingNotaryServiceTests.kt | 13 +++--- .../statemachine/StateMachineManagerTests.kt | 9 ++-- .../corda/node/utilities/ClockUtilsTest.kt | 5 ++- .../net/corda/node/utilities/FiberBoxTest.kt | 15 ++----- .../attachmentdemo/AttachmentDemoTest.kt | 6 +-- .../kotlin/net/corda/irs/IRSDemoTest.kt | 13 +++--- .../corda/irs/testing/IRSSimulationTest.kt | 8 +--- .../irs/testing/NodeInterestRatesTest.kt | 3 +- .../net/corda/traderdemo/TraderDemoTest.kt | 5 ++- .../kotlin/net/corda/testing/CoreTestUtils.kt | 10 ----- .../main/kotlin/net/corda/testing/Expect.kt | 3 +- .../testing/node/InMemoryMessagingNetwork.kt | 3 +- .../kotlin/net/corda/testing/node/MockNode.kt | 9 ++-- 26 files changed, 141 insertions(+), 171 deletions(-) diff --git a/client/src/integration-test/kotlin/net/corda/client/CordaRPCClientTest.kt b/client/src/integration-test/kotlin/net/corda/client/CordaRPCClientTest.kt index 487bfa04fa..06ce13c99b 100644 --- a/client/src/integration-test/kotlin/net/corda/client/CordaRPCClientTest.kt +++ b/client/src/integration-test/kotlin/net/corda/client/CordaRPCClientTest.kt @@ -1,6 +1,7 @@ package net.corda.client import net.corda.core.contracts.DOLLARS +import net.corda.core.getOrThrow import net.corda.core.node.services.ServiceInfo import net.corda.core.random63BitValue import net.corda.core.serialization.OpaqueBytes @@ -35,7 +36,7 @@ class CordaRPCClientTest { val driverStarted = CountDownLatch(1) driverThread = thread { driver(isDebug = true) { - driverInfo = startNode(rpcUsers = listOf(rpcUser), advertisedServices = setOf(ServiceInfo(ValidatingNotaryService.type))).get() + driverInfo = startNode(rpcUsers = listOf(rpcUser), advertisedServices = setOf(ServiceInfo(ValidatingNotaryService.type))).getOrThrow() client = CordaRPCClient(toHostAndPort(driverInfo.nodeInfo.address), configureTestSSL()) driverStarted.countDown() stopDriver.await() diff --git a/client/src/integration-test/kotlin/net/corda/client/NodeMonitorModelTest.kt b/client/src/integration-test/kotlin/net/corda/client/NodeMonitorModelTest.kt index 303ab00583..215ad570a4 100644 --- a/client/src/integration-test/kotlin/net/corda/client/NodeMonitorModelTest.kt +++ b/client/src/integration-test/kotlin/net/corda/client/NodeMonitorModelTest.kt @@ -8,6 +8,7 @@ import net.corda.core.contracts.Issued import net.corda.core.contracts.PartyAndReference import net.corda.core.contracts.USD import net.corda.core.flows.StateMachineRunId +import net.corda.core.getOrThrow import net.corda.core.node.NodeInfo import net.corda.core.node.services.NetworkMapCache import net.corda.core.node.services.ServiceInfo @@ -60,9 +61,9 @@ class NodeMonitorModelTest { val aliceNodeFuture = startNode("Alice", rpcUsers = listOf(cashUser)) val notaryNodeFuture = startNode("Notary", advertisedServices = setOf(ServiceInfo(SimpleNotaryService.type))) - aliceNode = aliceNodeFuture.get().nodeInfo - notaryNode = notaryNodeFuture.get().nodeInfo - newNode = { nodeName -> startNode(nodeName).get().nodeInfo } + aliceNode = aliceNodeFuture.getOrThrow().nodeInfo + notaryNode = notaryNodeFuture.getOrThrow().nodeInfo + newNode = { nodeName -> startNode(nodeName).getOrThrow().nodeInfo } val monitor = NodeMonitorModel() stateMachineTransactionMapping = monitor.stateMachineTransactionMapping.bufferUntilSubscribed() diff --git a/core/src/main/kotlin/net/corda/core/Utils.kt b/core/src/main/kotlin/net/corda/core/Utils.kt index 2575923158..847535ab2d 100644 --- a/core/src/main/kotlin/net/corda/core/Utils.kt +++ b/core/src/main/kotlin/net/corda/core/Utils.kt @@ -27,6 +27,7 @@ import java.util.concurrent.ExecutionException import java.util.concurrent.Executor import java.util.concurrent.Future import java.util.concurrent.locks.ReentrantLock +import java.util.function.BiConsumer import java.util.stream.Stream import java.util.zip.ZipInputStream import kotlin.concurrent.withLock @@ -60,8 +61,22 @@ infix fun Long.checkedAdd(b: Long) = Math.addExact(this, b) */ fun random63BitValue(): Long = Math.abs(newSecureRandom().nextLong()) -// TODO Convert the CompletableFuture into a ListenableFuture -fun future(block: () -> T): Future = CompletableFuture.supplyAsync(block) +/** Same as [Future.get] but with a more descriptive name, and doesn't throw [ExecutionException], instead throwing its cause */ +fun Future.getOrThrow(): T { + try { + return get() + } catch (e: ExecutionException) { + throw e.cause!! + } +} + +fun future(block: () -> T): ListenableFuture = CompletableToListenable(CompletableFuture.supplyAsync(block)) + +private class CompletableToListenable(private val base: CompletableFuture) : Future by base, ListenableFuture { + override fun addListener(listener: Runnable, executor: Executor) { + base.whenCompleteAsync(BiConsumer { result, exception -> listener.run() }, executor) + } +} // Some utilities for working with Guava listenable futures. fun ListenableFuture.then(executor: Executor, body: () -> Unit) = addListener(Runnable(body), executor) @@ -77,9 +92,7 @@ fun ListenableFuture.success(executor: Executor, body: (T) -> Unit) = the fun ListenableFuture.failure(executor: Executor, body: (Throwable) -> Unit) = then(executor) { try { - get() - } catch (e: ExecutionException) { - body(e.cause!!) + getOrThrow() } catch (t: Throwable) { body(t) } @@ -101,15 +114,11 @@ inline fun SettableFuture.catch(block: () -> T) { fun ListenableFuture.toObservable(): Observable { return Observable.create { subscriber -> - then { - try { - subscriber.onNext(get()) - subscriber.onCompleted() - } catch (e: ExecutionException) { - subscriber.onError(e.cause!!) - } catch (t: Throwable) { - subscriber.onError(t) - } + success { + subscriber.onNext(it) + subscriber.onCompleted() + } failure { + subscriber.onError(it) } } } @@ -186,7 +195,7 @@ fun List.randomOrNull(): T? { fun List.randomOrNull(predicate: (T) -> Boolean) = filter(predicate).randomOrNull() // An alias that can sometimes make code clearer to read. -val RunOnCallerThread = MoreExecutors.directExecutor() +val RunOnCallerThread: Executor = MoreExecutors.directExecutor() // TODO: Add inline back when a new Kotlin version is released and check if the java.lang.VerifyError // returns in the IRSSimulationTest. If not, commit the inline back. @@ -342,6 +351,4 @@ fun Observable.bufferUntilSubscribed(): Observable { } /** Allows summing big decimals that are in iterable collections */ -fun Iterable.sum(): BigDecimal { - return this.fold(BigDecimal.valueOf(0)) { a: BigDecimal, b: BigDecimal -> a + b } -} +fun Iterable.sum(): BigDecimal = fold(BigDecimal.ZERO) { a, b -> a + b } diff --git a/core/src/test/kotlin/net/corda/core/flows/ResolveTransactionsFlowTest.kt b/core/src/test/kotlin/net/corda/core/flows/ResolveTransactionsFlowTest.kt index eb8c60575e..a2c6aeadbb 100644 --- a/core/src/test/kotlin/net/corda/core/flows/ResolveTransactionsFlowTest.kt +++ b/core/src/test/kotlin/net/corda/core/flows/ResolveTransactionsFlowTest.kt @@ -4,6 +4,7 @@ import net.corda.core.contracts.DummyContract import net.corda.core.crypto.NullSignature import net.corda.core.crypto.Party import net.corda.core.crypto.SecureHash +import net.corda.core.getOrThrow import net.corda.core.node.recordTransactions import net.corda.core.serialization.opaque import net.corda.core.transactions.SignedTransaction @@ -14,7 +15,6 @@ import net.corda.testing.MEGA_CORP import net.corda.testing.MEGA_CORP_KEY import net.corda.testing.MINI_CORP_PUBKEY import net.corda.testing.node.MockNetwork -import net.corda.testing.rootCauseExceptions import org.junit.After import org.junit.Before import org.junit.Test @@ -52,7 +52,7 @@ class ResolveTransactionsFlowTest { val p = ResolveTransactionsFlow(setOf(stx2.id), a.info.legalIdentity) val future = b.services.startFlow(p).resultFuture net.runNetwork() - val results = future.get() + val results = future.getOrThrow() assertEquals(listOf(stx1.id, stx2.id), results.map { it.id }) databaseTransaction(b.database) { assertEquals(stx1, b.storage.validatedTransactions.getTransaction(stx1.id)) @@ -67,9 +67,7 @@ class ResolveTransactionsFlowTest { val p = ResolveTransactionsFlow(setOf(stx.id), a.info.legalIdentity) val future = b.services.startFlow(p).resultFuture net.runNetwork() - assertFailsWith(SignatureException::class) { - rootCauseExceptions { future.get() } - } + assertFailsWith(SignatureException::class) { future.getOrThrow() } } @Test @@ -78,7 +76,7 @@ class ResolveTransactionsFlowTest { val p = ResolveTransactionsFlow(stx2, a.info.legalIdentity) val future = b.services.startFlow(p).resultFuture net.runNetwork() - future.get() + future.getOrThrow() databaseTransaction(b.database) { assertEquals(stx1, b.storage.validatedTransactions.getTransaction(stx1.id)) // But stx2 wasn't inserted, just stx1. @@ -105,9 +103,7 @@ class ResolveTransactionsFlowTest { p.transactionCountLimit = 40 val future = b.services.startFlow(p).resultFuture net.runNetwork() - assertFailsWith { - rootCauseExceptions { future.get() } - } + assertFailsWith { future.getOrThrow() } } @Test @@ -133,7 +129,7 @@ class ResolveTransactionsFlowTest { val p = ResolveTransactionsFlow(setOf(stx3.id), a.info.legalIdentity) val future = b.services.startFlow(p).resultFuture net.runNetwork() - future.get() + future.getOrThrow() } @Test @@ -143,7 +139,7 @@ class ResolveTransactionsFlowTest { val p = ResolveTransactionsFlow(stx2, a.info.legalIdentity) val future = b.services.startFlow(p).resultFuture net.runNetwork() - future.get() + future.getOrThrow() assertNotNull(b.services.storageService.attachments.openAttachment(id)) } diff --git a/node/src/integration-test/kotlin/net/corda/node/driver/DriverTests.kt b/node/src/integration-test/kotlin/net/corda/node/driver/DriverTests.kt index aaa6dce494..4209904d32 100644 --- a/node/src/integration-test/kotlin/net/corda/node/driver/DriverTests.kt +++ b/node/src/integration-test/kotlin/net/corda/node/driver/DriverTests.kt @@ -1,7 +1,7 @@ package net.corda.node.driver +import net.corda.core.getOrThrow import net.corda.core.node.NodeInfo -import net.corda.core.node.services.NetworkMapCache import net.corda.core.node.services.ServiceInfo import net.corda.node.services.api.RegulatorService import net.corda.node.services.messaging.ArtemisMessagingComponent @@ -30,9 +30,9 @@ class DriverTests { val notary = startNode("TestNotary", setOf(ServiceInfo(SimpleNotaryService.type))) val regulator = startNode("Regulator", setOf(ServiceInfo(RegulatorService.type))) - nodeMustBeUp(notary.get().nodeInfo, "TestNotary") - nodeMustBeUp(regulator.get().nodeInfo, "Regulator") - Pair(notary.get(), regulator.get()) + nodeMustBeUp(notary.getOrThrow().nodeInfo, "TestNotary") + nodeMustBeUp(regulator.getOrThrow().nodeInfo, "Regulator") + Pair(notary.getOrThrow(), regulator.getOrThrow()) } nodeMustBeDown(notary.nodeInfo) nodeMustBeDown(regulator.nodeInfo) @@ -42,8 +42,8 @@ class DriverTests { fun startingNodeWithNoServicesWorks() { val noService = driver { val noService = startNode("NoService") - nodeMustBeUp(noService.get().nodeInfo, "NoService") - noService.get() + nodeMustBeUp(noService.getOrThrow().nodeInfo, "NoService") + noService.getOrThrow() } nodeMustBeDown(noService.nodeInfo) } @@ -52,8 +52,8 @@ class DriverTests { fun randomFreePortAllocationWorks() { val nodeInfo = driver(portAllocation = PortAllocation.RandomFree()) { val nodeInfo = startNode("NoService") - nodeMustBeUp(nodeInfo.get().nodeInfo, "NoService") - nodeInfo.get() + nodeMustBeUp(nodeInfo.getOrThrow().nodeInfo, "NoService") + nodeInfo.getOrThrow() } nodeMustBeDown(nodeInfo.nodeInfo) } diff --git a/node/src/integration-test/kotlin/net/corda/node/services/DistributedNotaryTests.kt b/node/src/integration-test/kotlin/net/corda/node/services/DistributedNotaryTests.kt index 66a108ccde..8f743b5983 100644 --- a/node/src/integration-test/kotlin/net/corda/node/services/DistributedNotaryTests.kt +++ b/node/src/integration-test/kotlin/net/corda/node/services/DistributedNotaryTests.kt @@ -9,6 +9,7 @@ import net.corda.core.crypto.CompositeKey import net.corda.core.crypto.Party import net.corda.core.crypto.composite import net.corda.core.crypto.generateKeyPair +import net.corda.core.getOrThrow import net.corda.core.messaging.SingleMessageRecipient import net.corda.core.node.services.ServiceInfo import net.corda.core.random63BitValue @@ -34,7 +35,6 @@ import java.nio.file.Path import java.nio.file.Paths import java.security.KeyPair import java.util.* -import java.util.concurrent.ExecutionException import kotlin.concurrent.thread import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -79,12 +79,12 @@ class DistributedNotaryTests { val buildFlow = { NotaryFlow.Client(stx) } val firstSpend = alice.services.startFlow(buildFlow()) - firstSpend.resultFuture.get() + firstSpend.resultFuture.getOrThrow() val secondSpend = alice.services.startFlow(buildFlow()) - val ex = assertFailsWith(ExecutionException::class) { secondSpend.resultFuture.get() } - val error = (ex.cause as NotaryException).error as NotaryError.Conflict + val ex = assertFailsWith(NotaryException::class) { secondSpend.resultFuture.getOrThrow() } + val error = ex.error as NotaryError.Conflict assertEquals(error.tx, stx.tx) } @@ -133,7 +133,7 @@ class DistributedNotaryTests { notaryNode.setup().start() thread { notaryNode.run() } - notaryNode.networkMapRegistrationFuture.get() + notaryNode.networkMapRegistrationFuture.getOrThrow() return notaryNode } @@ -145,7 +145,7 @@ class DistributedNotaryTests { networkMapAddress = networkMapAddress) alice.setup().start() thread { alice.run() } - alice.networkMapRegistrationFuture.get() + alice.networkMapRegistrationFuture.getOrThrow() return alice } diff --git a/node/src/test/kotlin/net/corda/node/messaging/AttachmentTests.kt b/node/src/test/kotlin/net/corda/node/messaging/AttachmentTests.kt index e2d1ffb6e1..94c4856486 100644 --- a/node/src/test/kotlin/net/corda/node/messaging/AttachmentTests.kt +++ b/node/src/test/kotlin/net/corda/node/messaging/AttachmentTests.kt @@ -3,6 +3,7 @@ package net.corda.node.messaging import net.corda.core.contracts.Attachment import net.corda.core.crypto.SecureHash import net.corda.core.crypto.sha256 +import net.corda.core.getOrThrow import net.corda.core.messaging.SingleMessageRecipient import net.corda.core.node.services.ServiceInfo import net.corda.core.write @@ -13,7 +14,6 @@ import net.corda.node.services.network.NetworkMapService import net.corda.node.services.persistence.NodeAttachmentService import net.corda.node.services.transactions.SimpleNotaryService import net.corda.testing.node.MockNetwork -import net.corda.testing.rootCauseExceptions import org.junit.Before import org.junit.Test import java.io.ByteArrayInputStream @@ -53,7 +53,7 @@ class AttachmentTests { network.runNetwork() val f1 = n1.services.startFlow(FetchAttachmentsFlow(setOf(id), n0.info.legalIdentity)) network.runNetwork() - assertEquals(0, f1.resultFuture.get().fromDisk.size) + assertEquals(0, f1.resultFuture.getOrThrow().fromDisk.size) // Verify it was inserted into node one's store. val attachment = n1.storage.attachments.openAttachment(id)!! @@ -62,7 +62,7 @@ class AttachmentTests { // Shut down node zero and ensure node one can still resolve the attachment. n0.stop() - val response: FetchDataFlow.Result = n1.services.startFlow(FetchAttachmentsFlow(setOf(id), n0.info.legalIdentity)).resultFuture.get() + val response: FetchDataFlow.Result = n1.services.startFlow(FetchAttachmentsFlow(setOf(id), n0.info.legalIdentity)).resultFuture.getOrThrow() assertEquals(attachment, response.fromDisk[0]) } @@ -75,7 +75,7 @@ class AttachmentTests { network.runNetwork() val f1 = n1.services.startFlow(FetchAttachmentsFlow(setOf(hash), n0.info.legalIdentity)) network.runNetwork() - val e = assertFailsWith { rootCauseExceptions { f1.resultFuture.get() } } + val e = assertFailsWith { f1.resultFuture.getOrThrow() } assertEquals(hash, e.requested) } @@ -106,8 +106,6 @@ class AttachmentTests { network.runNetwork() val f1 = n1.services.startFlow(FetchAttachmentsFlow(setOf(id), n0.info.legalIdentity)) network.runNetwork() - assertFailsWith { - rootCauseExceptions { f1.resultFuture.get() } - } + assertFailsWith { f1.resultFuture.getOrThrow() } } } diff --git a/node/src/test/kotlin/net/corda/node/messaging/TwoPartyTradeProtocolTests.kt b/node/src/test/kotlin/net/corda/node/messaging/TwoPartyTradeProtocolTests.kt index 6e5c6bcd1f..61a811f133 100644 --- a/node/src/test/kotlin/net/corda/node/messaging/TwoPartyTradeProtocolTests.kt +++ b/node/src/test/kotlin/net/corda/node/messaging/TwoPartyTradeProtocolTests.kt @@ -11,9 +11,11 @@ import net.corda.core.crypto.composite import net.corda.core.days import net.corda.core.flows.FlowStateMachine import net.corda.core.flows.StateMachineRunId +import net.corda.core.getOrThrow import net.corda.core.map import net.corda.core.messaging.SingleMessageRecipient import net.corda.core.node.services.* +import net.corda.core.rootCause import net.corda.core.transactions.SignedTransaction import net.corda.core.transactions.TransactionBuilder import net.corda.core.transactions.WireTransaction @@ -43,13 +45,11 @@ import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.security.KeyPair import java.util.* -import java.util.concurrent.ExecutionException import java.util.concurrent.Future import java.util.jar.JarOutputStream import java.util.zip.ZipEntry import kotlin.test.assertEquals import kotlin.test.assertFailsWith -import kotlin.test.assertNotNull import kotlin.test.assertTrue /** @@ -102,11 +102,11 @@ class TwoPartyTradeFlowTests { insertFakeTransactions(alicesFakePaper, aliceNode, aliceKey, notaryKey) - val (bobPsm, aliceResult) = runBuyerAndSeller("alice's paper".outputStateAndRef()) + val (bobStateMachine, aliceResult) = runBuyerAndSeller("alice's paper".outputStateAndRef()) // TODO: Verify that the result was inserted into the transaction database. // assertEquals(bobResult.get(), aliceNode.storage.validatedTransactions[aliceResult.get().id]) - assertEquals(aliceResult.get(), bobPsm.get().resultFuture.get()) + assertEquals(aliceResult.getOrThrow(), bobStateMachine.getOrThrow().resultFuture.getOrThrow()) aliceNode.stop() bobNode.stop() @@ -192,7 +192,7 @@ class TwoPartyTradeFlowTests { net.runNetwork() // Bob is now finished and has the same transaction as Alice. - assertThat(bobFuture.get()).isEqualTo(aliceFuture.get()) + assertThat(bobFuture.getOrThrow()).isEqualTo(aliceFuture.getOrThrow()) assertThat(bobNode.smm.findStateMachines(Buyer::class.java)).isEmpty() databaseTransaction(bobNode.database) { @@ -443,23 +443,18 @@ class TwoPartyTradeFlowTests { net.runNetwork() // Clear network map registration messages - val (bobPsm, aliceResult) = runBuyerAndSeller("alice's paper".outputStateAndRef()) + val (bobStateMachine, aliceResult) = runBuyerAndSeller("alice's paper".outputStateAndRef()) net.runNetwork() - val e = assertFailsWith { + val e = assertFailsWith { if (bobError) - aliceResult.get() + aliceResult.getOrThrow() else - bobPsm.get().resultFuture.get() + bobStateMachine.getOrThrow().resultFuture.getOrThrow() } - assertTrue(e.cause is TransactionVerificationException) - assertNotNull(e.cause!!.cause) - assertNotNull(e.cause!!.cause!!.message) - val underlyingMessage = e.cause!!.cause!!.message!! - if (underlyingMessage.contains(expectedMessageSubstring)) { - assertTrue(underlyingMessage.contains(expectedMessageSubstring)) - } else { + val underlyingMessage = e.rootCause.message!! + if (expectedMessageSubstring !in underlyingMessage) { assertEquals(expectedMessageSubstring, underlyingMessage) } } diff --git a/node/src/test/kotlin/net/corda/node/services/DistributedImmutableMapTests.kt b/node/src/test/kotlin/net/corda/node/services/DistributedImmutableMapTests.kt index a25d1b7858..112b6e4939 100644 --- a/node/src/test/kotlin/net/corda/node/services/DistributedImmutableMapTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/DistributedImmutableMapTests.kt @@ -6,6 +6,7 @@ import io.atomix.copycat.client.CopycatClient import io.atomix.copycat.server.CopycatServer import io.atomix.copycat.server.storage.Storage import io.atomix.copycat.server.storage.StorageLevel +import net.corda.core.getOrThrow import net.corda.core.utilities.LogHelper import net.corda.node.services.network.NetworkMapService import net.corda.node.services.transactions.DistributedImmutableMap @@ -57,14 +58,14 @@ class DistributedImmutableMapTests { val entries = mapOf("key1" to "value1", "key2" to "value2") - val conflict = client.submit(DistributedImmutableMap.Commands.PutAll(entries)).get() + val conflict = client.submit(DistributedImmutableMap.Commands.PutAll(entries)).getOrThrow() assertTrue { conflict.isEmpty() } val value1 = client.submit(DistributedImmutableMap.Commands.Get("key1")) val value2 = client.submit(DistributedImmutableMap.Commands.Get("key2")) - assertEquals(value1.get(), "value1") - assertEquals(value2.get(), "value2") + assertEquals(value1.getOrThrow(), "value1") + assertEquals(value2.getOrThrow(), "value2") } @Test @@ -73,9 +74,9 @@ class DistributedImmutableMapTests { val entries = mapOf("key1" to "value1", "key2" to "value2") - var conflict = client.submit(DistributedImmutableMap.Commands.PutAll(entries)).get() + var conflict = client.submit(DistributedImmutableMap.Commands.PutAll(entries)).getOrThrow() assertTrue { conflict.isEmpty() } - conflict = client.submit(DistributedImmutableMap.Commands.PutAll(entries)).get() + conflict = client.submit(DistributedImmutableMap.Commands.PutAll(entries)).getOrThrow() assertTrue { conflict == entries } } @@ -83,7 +84,7 @@ class DistributedImmutableMapTests { val clusterAddress = freeLocalHostAndPort() val cluster = mutableListOf(createReplica(clusterAddress)) for (i in 1..nodeCount) cluster.add(createReplica(freeLocalHostAndPort(), clusterAddress)) - return cluster.map { it.get() } + return cluster.map { it.getOrThrow() } } private fun createReplica(myAddress: HostAndPort, clusterAddress: HostAndPort? = null): CompletableFuture { diff --git a/node/src/test/kotlin/net/corda/node/services/InMemoryNetworkMapCacheTest.kt b/node/src/test/kotlin/net/corda/node/services/InMemoryNetworkMapCacheTest.kt index 6c8d03d312..de5d6b4e96 100644 --- a/node/src/test/kotlin/net/corda/node/services/InMemoryNetworkMapCacheTest.kt +++ b/node/src/test/kotlin/net/corda/node/services/InMemoryNetworkMapCacheTest.kt @@ -2,29 +2,23 @@ package net.corda.node.services import net.corda.core.crypto.composite import net.corda.core.crypto.generateKeyPair +import net.corda.core.getOrThrow import net.corda.core.node.services.ServiceInfo import net.corda.node.services.network.NetworkMapService import net.corda.testing.expect import net.corda.testing.node.MockNetwork -import org.junit.Before import org.junit.Test import kotlin.test.assertEquals class InMemoryNetworkMapCacheTest { - lateinit var network: MockNetwork - - @Before - fun setup() { - network = MockNetwork() - } + private val network = MockNetwork() @Test fun registerWithNetwork() { val (n0, n1) = network.createTwoNodes() - val future = n1.services.networkMapCache.addMapService(n1.net, n0.info.address, false, null) network.runNetwork() - future.get() + future.getOrThrow() } @Test diff --git a/node/src/test/kotlin/net/corda/node/services/InMemoryNetworkMapServiceTest.kt b/node/src/test/kotlin/net/corda/node/services/InMemoryNetworkMapServiceTest.kt index 8713c66eaa..0e0ab1fe4c 100644 --- a/node/src/test/kotlin/net/corda/node/services/InMemoryNetworkMapServiceTest.kt +++ b/node/src/test/kotlin/net/corda/node/services/InMemoryNetworkMapServiceTest.kt @@ -1,6 +1,7 @@ package net.corda.node.services import com.google.common.util.concurrent.ListenableFuture +import net.corda.core.getOrThrow import net.corda.core.map import net.corda.core.messaging.send import net.corda.core.node.services.DEFAULT_SESSION_ID @@ -83,25 +84,25 @@ abstract class AbstractNetworkMapServiceTest { // Confirm all nodes have registered themselves network.runNetwork() - var fetchPsm = registerNode.fetchMap(mapServiceNode, false) + var fetchResult = registerNode.fetchMap(mapServiceNode, false) network.runNetwork() - assertEquals(2, fetchPsm.get()?.count()) + assertEquals(2, fetchResult.getOrThrow()?.count()) // Forcibly deregister the second node val nodeKey = registerNode.services.legalIdentityKey val instant = Instant.now() val expires = instant + NetworkMapService.DEFAULT_EXPIRATION_PERIOD val reg = NodeRegistration(registerNode.info, instant.toEpochMilli()+1, AddOrRemove.REMOVE, expires) - val registerPsm = registerNode.registration(mapServiceNode, reg, nodeKey.private) + val registerResult = registerNode.registration(mapServiceNode, reg, nodeKey.private) network.runNetwork() - assertTrue(registerPsm.get().success) + assertTrue(registerResult.getOrThrow().success) swizzle() // Now only map service node should be registered - fetchPsm = registerNode.fetchMap(mapServiceNode, false) + fetchResult = registerNode.fetchMap(mapServiceNode, false) network.runNetwork() - assertEquals(mapServiceNode.info, fetchPsm.get()?.filter { it.type == AddOrRemove.ADD }?.map { it.node }?.single()) + assertEquals(mapServiceNode.info, fetchResult.getOrThrow()?.filter { it.type == AddOrRemove.ADD }?.map { it.node }?.single()) } protected fun `subscribe with network`(network: MockNetwork, @@ -114,9 +115,9 @@ abstract class AbstractNetworkMapServiceTest { // Test subscribing to updates network.runNetwork() - val subscribePsm = registerNode.subscribe(mapServiceNode, true) + val subscribeResult = registerNode.subscribe(mapServiceNode, true) network.runNetwork() - subscribePsm.get() + subscribeResult.getOrThrow() swizzle() diff --git a/node/src/test/kotlin/net/corda/node/services/NotaryChangeTests.kt b/node/src/test/kotlin/net/corda/node/services/NotaryChangeTests.kt index 2835a8fcc7..24601d5355 100644 --- a/node/src/test/kotlin/net/corda/node/services/NotaryChangeTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/NotaryChangeTests.kt @@ -3,6 +3,7 @@ package net.corda.node.services import net.corda.core.contracts.* import net.corda.core.crypto.Party import net.corda.core.crypto.generateKeyPair +import net.corda.core.getOrThrow import net.corda.core.node.services.ServiceInfo import net.corda.core.seconds import net.corda.core.utilities.DUMMY_NOTARY @@ -14,14 +15,13 @@ import net.corda.node.internal.AbstractNode import net.corda.node.services.network.NetworkMapService import net.corda.node.services.transactions.SimpleNotaryService import net.corda.testing.node.MockNetwork +import org.assertj.core.api.Assertions.assertThat import org.junit.Before import org.junit.Test import java.time.Instant import java.util.* -import java.util.concurrent.ExecutionException import kotlin.test.assertEquals import kotlin.test.assertFailsWith -import kotlin.test.assertTrue class NotaryChangeTests { lateinit var net: MockNetwork @@ -53,7 +53,7 @@ class NotaryChangeTests { net.runNetwork() - val newState = future.resultFuture.get() + val newState = future.resultFuture.getOrThrow() assertEquals(newState.state.notary, newNotary) } @@ -66,7 +66,7 @@ class NotaryChangeTests { net.runNetwork() - val newState = future.resultFuture.get() + val newState = future.resultFuture.getOrThrow() assertEquals(newState.state.notary, newNotary) val loadedStateA = clientNodeA.services.loadState(newState.ref) val loadedStateB = clientNodeB.services.loadState(newState.ref) @@ -82,9 +82,8 @@ class NotaryChangeTests { net.runNetwork() - val ex = assertFailsWith(ExecutionException::class) { future.resultFuture.get() } - val error = (ex.cause as StateReplacementException).error - assertTrue(error is StateReplacementRefused) + val ex = assertFailsWith(StateReplacementException::class) { future.resultFuture.getOrThrow() } + assertThat(ex.error).isInstanceOf(StateReplacementRefused::class.java) } // TODO: Add more test cases once we have a general flow/service exception handling mechanism: diff --git a/node/src/test/kotlin/net/corda/node/services/NotaryServiceTests.kt b/node/src/test/kotlin/net/corda/node/services/NotaryServiceTests.kt index 8b7b6d63d7..474402e172 100644 --- a/node/src/test/kotlin/net/corda/node/services/NotaryServiceTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/NotaryServiceTests.kt @@ -6,6 +6,7 @@ import net.corda.core.contracts.StateAndRef import net.corda.core.contracts.StateRef import net.corda.core.contracts.TransactionType import net.corda.core.crypto.DigitalSignature +import net.corda.core.getOrThrow import net.corda.core.node.services.ServiceInfo import net.corda.core.seconds import net.corda.core.transactions.SignedTransaction @@ -19,14 +20,13 @@ import net.corda.node.services.network.NetworkMapService import net.corda.node.services.transactions.SimpleNotaryService import net.corda.testing.MINI_CORP_KEY import net.corda.testing.node.MockNetwork +import org.assertj.core.api.Assertions.assertThat import org.junit.Before import org.junit.Test import java.time.Instant import java.util.* -import java.util.concurrent.ExecutionException import kotlin.test.assertEquals import kotlin.test.assertFailsWith -import kotlin.test.assertTrue class NotaryServiceTests { lateinit var net: MockNetwork @@ -53,7 +53,7 @@ class NotaryServiceTests { } val future = runNotaryClient(stx) - val signature = future.get() + val signature = future.getOrThrow() signature.verifyWithECDSA(stx.id) } @@ -66,7 +66,7 @@ class NotaryServiceTests { } val future = runNotaryClient(stx) - val signature = future.get() + val signature = future.getOrThrow() signature.verifyWithECDSA(stx.id) } @@ -81,9 +81,8 @@ class NotaryServiceTests { val future = runNotaryClient(stx) - val ex = assertFailsWith(ExecutionException::class) { future.get() } - val error = (ex.cause as NotaryException).error - assertTrue(error is NotaryError.TimestampInvalid) + val ex = assertFailsWith(NotaryException::class) { future.getOrThrow() } + assertThat(ex.error).isInstanceOf(NotaryError.TimestampInvalid::class.java) } @Test fun `should report conflict for a duplicate transaction`() { @@ -101,8 +100,8 @@ class NotaryServiceTests { net.runNetwork() - val ex = assertFailsWith(ExecutionException::class) { future.resultFuture.get() } - val notaryError = (ex.cause as NotaryException).error as NotaryError.Conflict + val ex = assertFailsWith(NotaryException::class) { future.resultFuture.getOrThrow() } + val notaryError = ex.error as NotaryError.Conflict assertEquals(notaryError.tx, stx.tx) notaryError.conflict.verified() } diff --git a/node/src/test/kotlin/net/corda/node/services/ValidatingNotaryServiceTests.kt b/node/src/test/kotlin/net/corda/node/services/ValidatingNotaryServiceTests.kt index e364ea9f5a..daddcb47bf 100644 --- a/node/src/test/kotlin/net/corda/node/services/ValidatingNotaryServiceTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/ValidatingNotaryServiceTests.kt @@ -4,6 +4,7 @@ import com.google.common.util.concurrent.ListenableFuture import net.corda.core.contracts.* import net.corda.core.crypto.DigitalSignature import net.corda.core.crypto.composite +import net.corda.core.getOrThrow import net.corda.core.node.services.ServiceInfo import net.corda.core.transactions.SignedTransaction import net.corda.core.utilities.DUMMY_NOTARY @@ -21,7 +22,6 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.Before import org.junit.Test import java.util.* -import java.util.concurrent.ExecutionException import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -51,9 +51,8 @@ class ValidatingNotaryServiceTests { val future = runClient(stx) - val ex = assertFailsWith(ExecutionException::class) { future.get() } - val notaryError = (ex.cause as NotaryException).error - assertThat(notaryError).isInstanceOf(NotaryError.TransactionInvalid::class.java) + val ex = assertFailsWith(NotaryException::class) { future.getOrThrow() } + assertThat(ex.error).isInstanceOf(NotaryError.TransactionInvalid::class.java) } @Test fun `should report error for missing signatures`() { @@ -67,11 +66,11 @@ class ValidatingNotaryServiceTests { tx.toSignedTransaction(false) } - val ex = assertFailsWith(ExecutionException::class) { + val ex = assertFailsWith(NotaryException::class) { val future = runClient(stx) - future.get() + future.getOrThrow() } - val notaryError = (ex.cause as NotaryException).error + val notaryError = ex.error assertThat(notaryError).isInstanceOf(NotaryError.SignaturesMissing::class.java) val missingKeys = (notaryError as NotaryError.SignaturesMissing).missingSigners diff --git a/node/src/test/kotlin/net/corda/node/services/statemachine/StateMachineManagerTests.kt b/node/src/test/kotlin/net/corda/node/services/statemachine/StateMachineManagerTests.kt index 1af9d24a32..c54f08597f 100644 --- a/node/src/test/kotlin/net/corda/node/services/statemachine/StateMachineManagerTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/statemachine/StateMachineManagerTests.kt @@ -6,6 +6,7 @@ import com.google.common.util.concurrent.ListenableFuture import net.corda.core.crypto.Party import net.corda.core.flows.FlowLogic import net.corda.core.flows.FlowSessionException +import net.corda.core.getOrThrow import net.corda.core.random63BitValue import net.corda.core.serialization.deserialize import net.corda.node.services.persistence.checkpoints @@ -162,7 +163,7 @@ class StateMachineManagerTests { // Run the network which will also fire up the second flow. First message should get deduped. So message data stays in sync. net.runNetwork() node2b.smm.executor.flush() - fut1.get() + fut1.getOrThrow() val receivedCount = sessionTransfers.count { it.isPayloadTransfer } // Check flows completed cleanly and didn't get out of phase @@ -177,8 +178,8 @@ class StateMachineManagerTests { } assertEquals(payload2, firstAgain.receivedPayload, "Received payload does not match the first value on Node 3") assertEquals(payload2 + 1, firstAgain.receivedPayload2, "Received payload does not match the expected second value on Node 3") - assertEquals(payload, secondFlow.get().receivedPayload, "Received payload does not match the (restarted) first value on Node 2") - assertEquals(payload + 1, secondFlow.get().receivedPayload2, "Received payload does not match the expected second value on Node 2") + assertEquals(payload, secondFlow.getOrThrow().receivedPayload, "Received payload does not match the (restarted) first value on Node 2") + assertEquals(payload + 1, secondFlow.getOrThrow().receivedPayload2, "Received payload does not match the expected second value on Node 2") } @Test @@ -264,7 +265,7 @@ class StateMachineManagerTests { node2.services.registerFlowInitiator(ReceiveThenSuspendFlow::class) { ExceptionFlow } val future = node1.smm.add(ReceiveThenSuspendFlow(node2.info.legalIdentity)).resultFuture net.runNetwork() - assertThatThrownBy { future.get() }.hasCauseInstanceOf(FlowSessionException::class.java) + assertThatThrownBy { future.getOrThrow() }.isInstanceOf(FlowSessionException::class.java) assertSessionTransfers( node1 sent sessionInit(node1, ReceiveThenSuspendFlow::class) to node2, node2 sent sessionConfirm() to node1, diff --git a/node/src/test/kotlin/net/corda/node/utilities/ClockUtilsTest.kt b/node/src/test/kotlin/net/corda/node/utilities/ClockUtilsTest.kt index 5f625b0b10..38a5c91d67 100644 --- a/node/src/test/kotlin/net/corda/node/utilities/ClockUtilsTest.kt +++ b/node/src/test/kotlin/net/corda/node/utilities/ClockUtilsTest.kt @@ -5,6 +5,7 @@ import co.paralleluniverse.fibers.FiberExecutorScheduler import co.paralleluniverse.fibers.Suspendable import co.paralleluniverse.strands.Strand import com.google.common.util.concurrent.SettableFuture +import net.corda.core.getOrThrow import net.corda.testing.node.TestClock import org.junit.After import org.junit.Before @@ -153,7 +154,7 @@ class ClockUtilsTest { testClock.advanceBy(Duration.ofMinutes(10)) }).start() } - assertFalse(future.get(), "Should have reached deadline") + assertFalse(future.getOrThrow(), "Should have reached deadline") } @Test @@ -175,7 +176,7 @@ class ClockUtilsTest { testClock.advanceBy(Duration.ofMinutes(10)) }).start() } - assertFalse(future.get(), "Should have reached deadline") + assertFalse(future.getOrThrow(), "Should have reached deadline") } @Suspendable diff --git a/node/src/test/kotlin/net/corda/node/utilities/FiberBoxTest.kt b/node/src/test/kotlin/net/corda/node/utilities/FiberBoxTest.kt index aabb2fb5a6..80211f61aa 100644 --- a/node/src/test/kotlin/net/corda/node/utilities/FiberBoxTest.kt +++ b/node/src/test/kotlin/net/corda/node/utilities/FiberBoxTest.kt @@ -1,10 +1,10 @@ package net.corda.node.utilities - import co.paralleluniverse.fibers.FiberExecutorScheduler import co.paralleluniverse.fibers.Suspendable import co.paralleluniverse.strands.Strand import net.corda.core.RetryableException +import net.corda.core.getOrThrow import net.corda.testing.node.TestClock import org.junit.After import org.junit.Before @@ -12,7 +12,6 @@ import org.junit.Test import java.time.Clock import java.time.Duration import java.util.concurrent.CompletableFuture -import java.util.concurrent.ExecutionException import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import kotlin.test.assertEquals @@ -120,11 +119,7 @@ class FiberBoxTest { testClock.advanceBy(Duration.ofMinutes(10)) }).start() } - try { - assertEquals(2, future.get()) - } catch(e: ExecutionException) { - throw e.cause!! - } + assertEquals(2, future.getOrThrow()) } /** @@ -155,11 +150,7 @@ class FiberBoxTest { } mutex.write { integer = 1 } }).start() - try { - assertEquals(1, future.get()) - } catch(e: ExecutionException) { - throw e.cause!! - } + assertEquals(1, future.getOrThrow()) } private fun backgroundWrite() { diff --git a/samples/attachment-demo/src/integration-test/kotlin/net/corda/attachmentdemo/AttachmentDemoTest.kt b/samples/attachment-demo/src/integration-test/kotlin/net/corda/attachmentdemo/AttachmentDemoTest.kt index 069241e91d..75d2083896 100644 --- a/samples/attachment-demo/src/integration-test/kotlin/net/corda/attachmentdemo/AttachmentDemoTest.kt +++ b/samples/attachment-demo/src/integration-test/kotlin/net/corda/attachmentdemo/AttachmentDemoTest.kt @@ -1,6 +1,6 @@ package net.corda.attachmentdemo -import net.corda.core.crypto.toBase58String +import net.corda.core.getOrThrow import net.corda.core.node.services.ServiceInfo import net.corda.node.driver.driver import net.corda.node.services.transactions.SimpleNotaryService @@ -12,9 +12,9 @@ class AttachmentDemoTest { @Test fun `runs attachment demo`() { driver(dsl = { startNode("Notary", setOf(ServiceInfo(SimpleNotaryService.Companion.type))) - val nodeA = startNode("Bank A").get() + val nodeA = startNode("Bank A").getOrThrow() val nodeAApiAddr = nodeA.config.getHostAndPort("webAddress") - val nodeBApiAddr = startNode("Bank B").get().config.getHostAndPort("webAddress") + val nodeBApiAddr = startNode("Bank B").getOrThrow().config.getHostAndPort("webAddress") var recipientReturn: Boolean? = null var senderReturn: Boolean? = null diff --git a/samples/irs-demo/src/integration-test/kotlin/net/corda/irs/IRSDemoTest.kt b/samples/irs-demo/src/integration-test/kotlin/net/corda/irs/IRSDemoTest.kt index 579a3c2295..357132b05d 100644 --- a/samples/irs-demo/src/integration-test/kotlin/net/corda/irs/IRSDemoTest.kt +++ b/samples/irs-demo/src/integration-test/kotlin/net/corda/irs/IRSDemoTest.kt @@ -1,8 +1,8 @@ package net.corda.irs import com.google.common.net.HostAndPort -import com.google.common.util.concurrent.ListenableFuture -import com.google.common.util.concurrent.SettableFuture +import com.typesafe.config.Config +import net.corda.core.getOrThrow import net.corda.core.node.services.ServiceInfo import net.corda.irs.api.NodeInterestRates import net.corda.irs.utilities.postJson @@ -10,8 +10,7 @@ import net.corda.irs.utilities.putJson import net.corda.irs.utilities.uploadFile import net.corda.node.driver.driver import net.corda.node.services.transactions.SimpleNotaryService -import net.corda.testing.* -import com.typesafe.config.Config +import net.corda.testing.IntegrationTestCategory import org.apache.commons.io.IOUtils import org.junit.Test import java.net.URL @@ -21,9 +20,9 @@ class IRSDemoTest: IntegrationTestCategory { @Test fun `runs IRS demo`() { driver(dsl = { - val controller = startNode("Notary", setOf(ServiceInfo(SimpleNotaryService.type), ServiceInfo(NodeInterestRates.type))).get() - val nodeA = startNode("Bank A").get() - val nodeB = startNode("Bank B").get() + val controller = startNode("Notary", setOf(ServiceInfo(SimpleNotaryService.type), ServiceInfo(NodeInterestRates.type))).getOrThrow() + val nodeA = startNode("Bank A").getOrThrow() + val nodeB = startNode("Bank B").getOrThrow() runUploadRates(controller.config.getHostAndPort("webAddress")) runTrade(nodeA.config.getHostAndPort("webAddress")) runDateChange(nodeB.config.getHostAndPort("webAddress")) diff --git a/samples/irs-demo/src/test/kotlin/net/corda/irs/testing/IRSSimulationTest.kt b/samples/irs-demo/src/test/kotlin/net/corda/irs/testing/IRSSimulationTest.kt index f15d91c5b7..0c3d1fc05c 100644 --- a/samples/irs-demo/src/test/kotlin/net/corda/irs/testing/IRSSimulationTest.kt +++ b/samples/irs-demo/src/test/kotlin/net/corda/irs/testing/IRSSimulationTest.kt @@ -1,6 +1,6 @@ package net.corda.irs.testing -import com.google.common.base.Throwables +import net.corda.core.getOrThrow import net.corda.core.utilities.LogHelper import net.corda.simulation.IRSSimulation import org.junit.Test @@ -13,10 +13,6 @@ class IRSSimulationTest { val sim = IRSSimulation(false, false, null) val future = sim.start() while (!future.isDone) sim.iterate() - try { - future.get() - } catch(e: Throwable) { - throw Throwables.getRootCause(e) - } + future.getOrThrow() } } diff --git a/samples/irs-demo/src/test/kotlin/net/corda/irs/testing/NodeInterestRatesTest.kt b/samples/irs-demo/src/test/kotlin/net/corda/irs/testing/NodeInterestRatesTest.kt index 14742aeb4d..52fdaa57f1 100644 --- a/samples/irs-demo/src/test/kotlin/net/corda/irs/testing/NodeInterestRatesTest.kt +++ b/samples/irs-demo/src/test/kotlin/net/corda/irs/testing/NodeInterestRatesTest.kt @@ -9,6 +9,7 @@ import net.corda.core.contracts.* import net.corda.core.crypto.MerkleTreeException import net.corda.core.crypto.Party import net.corda.core.crypto.generateKeyPair +import net.corda.core.getOrThrow import net.corda.core.node.services.ServiceInfo import net.corda.core.transactions.FilterFuns import net.corda.core.transactions.FilteredTransaction @@ -206,7 +207,7 @@ class NodeInterestRatesTest { net.runNetwork() val future = n1.services.startFlow(flow).resultFuture net.runNetwork() - future.get() + future.getOrThrow() // We should now have a valid signature over our tx from the oracle. val fix = tx.toSignedTransaction(true).tx.commands.map { it.value as Fix }.first() assertEquals(fixOf, fix.of) diff --git a/samples/trader-demo/src/integration-test/kotlin/net/corda/traderdemo/TraderDemoTest.kt b/samples/trader-demo/src/integration-test/kotlin/net/corda/traderdemo/TraderDemoTest.kt index 16e1473327..e994c9bb30 100644 --- a/samples/trader-demo/src/integration-test/kotlin/net/corda/traderdemo/TraderDemoTest.kt +++ b/samples/trader-demo/src/integration-test/kotlin/net/corda/traderdemo/TraderDemoTest.kt @@ -1,5 +1,6 @@ package net.corda.traderdemo +import net.corda.core.getOrThrow import net.corda.core.node.services.ServiceInfo import net.corda.node.driver.driver import net.corda.node.services.transactions.SimpleNotaryService @@ -10,9 +11,9 @@ class TraderDemoTest { @Test fun `runs trader demo`() { driver(dsl = { startNode("Notary", setOf(ServiceInfo(SimpleNotaryService.type))) - val nodeA = startNode("Bank A").get() + val nodeA = startNode("Bank A").getOrThrow() val nodeAApiAddr = nodeA.config.getHostAndPort("webAddress") - val nodeBApiAddr = startNode("Bank B").get().config.getHostAndPort("webAddress") + val nodeBApiAddr = startNode("Bank B").getOrThrow().config.getHostAndPort("webAddress") assert(TraderDemoClientApi(nodeAApiAddr).runBuyer()) assert(TraderDemoClientApi(nodeBApiAddr).runSeller(counterparty = nodeA.nodeInfo.legalIdentity.name)) diff --git a/test-utils/src/main/kotlin/net/corda/testing/CoreTestUtils.kt b/test-utils/src/main/kotlin/net/corda/testing/CoreTestUtils.kt index 63a60cf107..970296da68 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/CoreTestUtils.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/CoreTestUtils.kt @@ -2,7 +2,6 @@ @file:JvmName("CoreTestUtils") package net.corda.testing -import com.google.common.base.Throwables import com.google.common.net.HostAndPort import com.google.common.util.concurrent.ListenableFuture import com.google.common.util.concurrent.SettableFuture @@ -74,15 +73,6 @@ val MOCK_IDENTITY_SERVICE: MockIdentityService get() = MockIdentityService(listO fun generateStateRef() = StateRef(SecureHash.randomSHA256(), 0) -/** If an exception is thrown by the body, rethrows the root cause exception. */ -inline fun rootCauseExceptions(body: () -> R): R { - try { - return body() - } catch(e: Exception) { - throw Throwables.getRootCause(e) - } -} - /** * Returns a free port. * diff --git a/test-utils/src/main/kotlin/net/corda/testing/Expect.kt b/test-utils/src/main/kotlin/net/corda/testing/Expect.kt index e1ba96198f..4da88de05d 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/Expect.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/Expect.kt @@ -1,6 +1,7 @@ package net.corda.testing import com.google.common.util.concurrent.SettableFuture +import net.corda.core.getOrThrow import org.slf4j.Logger import org.slf4j.LoggerFactory import rx.Observable @@ -175,7 +176,7 @@ fun S.genericExpectEvents( } } } - finishFuture.get() + finishFuture.getOrThrow() } sealed class ExpectCompose { diff --git a/test-utils/src/main/kotlin/net/corda/testing/node/InMemoryMessagingNetwork.kt b/test-utils/src/main/kotlin/net/corda/testing/node/InMemoryMessagingNetwork.kt index 72b04c5726..50a2650aab 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/node/InMemoryMessagingNetwork.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/node/InMemoryMessagingNetwork.kt @@ -4,6 +4,7 @@ import com.google.common.util.concurrent.Futures import com.google.common.util.concurrent.ListenableFuture import com.google.common.util.concurrent.SettableFuture import net.corda.core.ThreadBox +import net.corda.core.getOrThrow import net.corda.core.messaging.* import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.utilities.trace @@ -176,7 +177,7 @@ class InMemoryMessagingNetwork(val sendManuallyPumped: Boolean) : SingletonSeria messageSent.set(Unit) } if (block) { - messageSent.get() + messageSent.getOrThrow() } } else { pumpSendInternal(transfer) diff --git a/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt b/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt index 3ea85a82b3..10712eb980 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt @@ -3,14 +3,11 @@ package net.corda.testing.node import com.google.common.jimfs.Configuration.unix import com.google.common.jimfs.Jimfs import com.google.common.util.concurrent.Futures -import net.corda.core.createDirectories -import net.corda.core.createDirectory +import net.corda.core.* import net.corda.core.crypto.Party -import net.corda.core.div import net.corda.core.messaging.SingleMessageRecipient import net.corda.core.node.PhysicalLocation import net.corda.core.node.services.* -import net.corda.core.random63BitValue import net.corda.core.utilities.DUMMY_NOTARY_KEY import net.corda.core.utilities.loggerFor import net.corda.node.internal.AbstractNode @@ -120,7 +117,7 @@ class MockNetwork(private val networkSendManuallyPumped: Boolean = false, // through the java.nio API which we are already mocking via Jimfs. override fun makeMessagingService(): MessagingServiceInternal { require(id >= 0) { "Node ID must be zero or positive, was passed: " + id } - return mockNet.messagingNetwork.createNodeWithID(!mockNet.threadPerNode, id, serverThread, configuration.myLegalName, database).start().get() + return mockNet.messagingNetwork.createNodeWithID(!mockNet.threadPerNode, id, serverThread, configuration.myLegalName, database).start().getOrThrow() } override fun makeIdentityService() = MockIdentityService(mockNet.identities) @@ -203,7 +200,7 @@ class MockNetwork(private val networkSendManuallyPumped: Boolean = false, if (start) { node.setup().start() if (threadPerNode && networkMapAddress != null) - node.networkMapRegistrationFuture.get() // Block and wait for the node to register in the net map. + node.networkMapRegistrationFuture.getOrThrow() // Block and wait for the node to register in the net map. } _nodes.add(node) return node