From 941719ab8b2482c8444930d31ded44b901380d01 Mon Sep 17 00:00:00 2001 From: Florian Friemel Date: Thu, 28 Jun 2018 17:14:06 +0100 Subject: [PATCH 1/5] Use TestClock in MockServices. --- .../src/main/kotlin/net/corda/testing/node/MockServices.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt index 5b1d0c0362..ad92938f75 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt @@ -37,6 +37,7 @@ import net.corda.testing.internal.MockCordappProvider import net.corda.testing.node.internal.MockKeyManagementService import net.corda.testing.node.internal.MockTransactionStorage import net.corda.testing.services.MockAttachmentStorage +import org.junit.Test import java.security.KeyPair import java.sql.Connection import java.time.Clock @@ -230,7 +231,7 @@ open class MockServices private constructor( override val vaultService: VaultService get() = throw UnsupportedOperationException() override val contractUpgradeService: ContractUpgradeService get() = throw UnsupportedOperationException() override val networkMapCache: NetworkMapCache get() = throw UnsupportedOperationException() - override val clock: Clock get() = Clock.systemUTC() + override val clock: TestClock get() = TestClock(Clock.systemUTC()) override val myInfo: NodeInfo get() { return NodeInfo(listOf(NetworkHostAndPort("mock.node.services", 10000)), listOf(initialIdentity.identity), 1, serial = 1L) @@ -242,7 +243,7 @@ open class MockServices private constructor( protected val servicesForResolution: ServicesForResolution get() = ServicesForResolutionImpl(identityService, attachments, cordappProvider, networkParameters, validatedTransactions) internal fun makeVaultService(hibernateConfig: HibernateConfiguration, schemaService: SchemaService, database: CordaPersistence): VaultServiceInternal { - val vaultService = NodeVaultService(Clock.systemUTC(), keyManagementService, servicesForResolution, hibernateConfig, database) + val vaultService = NodeVaultService(clock, keyManagementService, servicesForResolution, hibernateConfig, database) HibernateObserver.install(vaultService.rawUpdates, hibernateConfig, schemaService) return vaultService } From 61d9d475c88cf67e163a928cb22480c657cac7e1 Mon Sep 17 00:00:00 2001 From: Florian Friemel Date: Thu, 28 Jun 2018 17:14:50 +0100 Subject: [PATCH 2/5] Advance TestClock instead of sleeping. --- .../kotlin/net/corda/node/services/vault/VaultQueryTests.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt b/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt index 980f79b29d..51c36d4d56 100644 --- a/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt @@ -1848,7 +1848,7 @@ abstract class VaultQueryTestsBase : VaultQueryParties { database.transaction { vaultFiller.fillWithSomeTestLinearStates(1, "TEST1") vaultFiller.fillWithSomeTestLinearStates(1, "TEST2") - sleep(1000) + services.clock.advanceBy(1.seconds) vaultFiller.fillWithSomeTestLinearStates(1, "TEST3") // 2 unconsumed states with same external ID From 136500d48d7d17d9aaeed9759edabffc056a1748 Mon Sep 17 00:00:00 2001 From: Florian Friemel Date: Thu, 28 Jun 2018 17:58:46 +0100 Subject: [PATCH 3/5] Advance TestClock instead of sleeping. --- .../corda/node/services/vault/VaultQueryTests.kt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt b/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt index 51c36d4d56..60a5628773 100644 --- a/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt @@ -40,7 +40,7 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.ExpectedException import org.junit.rules.ExternalResource -import java.lang.Thread.sleep +import java.time.Duration import java.time.Instant import java.time.LocalDate import java.time.ZoneOffset @@ -198,7 +198,7 @@ abstract class VaultQueryTestsBase : VaultQueryParties { vaultFiller.fillWithSomeTestLinearStates(3, "ABC") val dealStates = vaultFiller.fillWithSomeTestDeals(listOf("123", "456", "789")) // Total unconsumed states = 10 + 1 + 2 + 3 + 3 = 19 - sleep(delay) + services.clock.advanceBy(Duration.ofMillis(delay)) // consume some states vaultFiller.consumeLinearStates(linearStatesXYZ.states.toList()) @@ -1806,12 +1806,11 @@ abstract class VaultQueryTestsBase : VaultQueryParties { // specifying Query on Linear state attributes @Test fun `unconsumed linear heads for linearId between two timestamps`() { - val start = Instant.now() - val end = start.plus(1, ChronoUnit.SECONDS) - database.transaction { + val start = services.clock.instant() vaultFiller.fillWithSomeTestLinearStates(1, "TEST") - sleep(1000) + services.clock.advanceBy(1.seconds) + val end = services.clock.instant() vaultFiller.fillWithSomeTestLinearStates(1, "TEST") // 2 unconsumed states with same external ID val recordedBetweenExpression = TimeCondition(TimeInstantType.RECORDED, builder { between(start, end) }) @@ -1842,13 +1841,12 @@ abstract class VaultQueryTestsBase : VaultQueryParties { // specifying Query on Linear state attributes @Test fun `unconsumed linear heads for linearId between two timestamps for a given external id`() { - val start = Instant.now() - val end = start.plus(1, ChronoUnit.SECONDS) - database.transaction { + val start = services.clock.instant() vaultFiller.fillWithSomeTestLinearStates(1, "TEST1") vaultFiller.fillWithSomeTestLinearStates(1, "TEST2") services.clock.advanceBy(1.seconds) + val end = services.clock.instant() vaultFiller.fillWithSomeTestLinearStates(1, "TEST3") // 2 unconsumed states with same external ID From 302dde88d66c30b90e06471ee1b1b0d2caed1c4a Mon Sep 17 00:00:00 2001 From: Florian Friemel Date: Fri, 29 Jun 2018 09:50:04 +0100 Subject: [PATCH 4/5] Update api-current.txt to reflect that MockServices now uses TestClock instead of Clock. --- .ci/api-current.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci/api-current.txt b/.ci/api-current.txt index 52e9092d84..ec11b37730 100644 --- a/.ci/api-current.txt +++ b/.ci/api-current.txt @@ -6328,7 +6328,7 @@ public class net.corda.testing.node.MockServices extends java.lang.Object implem @NotNull public final net.corda.testing.services.MockAttachmentStorage getAttachments() @NotNull - public java.time.Clock getClock() + public net.corda.testing.node.TestClock getClock() @NotNull public net.corda.core.node.services.ContractUpgradeService getContractUpgradeService() @NotNull From 4c5618838364b72bb0a30ade29f8a0100500a45b Mon Sep 17 00:00:00 2001 From: Florian Friemel Date: Fri, 29 Jun 2018 09:55:34 +0100 Subject: [PATCH 5/5] Cleanup. --- .../src/main/kotlin/net/corda/testing/node/MockServices.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt index ad92938f75..c6d99267b4 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt @@ -37,7 +37,6 @@ import net.corda.testing.internal.MockCordappProvider import net.corda.testing.node.internal.MockKeyManagementService import net.corda.testing.node.internal.MockTransactionStorage import net.corda.testing.services.MockAttachmentStorage -import org.junit.Test import java.security.KeyPair import java.sql.Connection import java.time.Clock