mirror of
https://github.com/corda/corda.git
synced 2025-06-22 17:09:00 +00:00
Support for moving TestClock to just beyond a specific instant (helpful for schedule related testing)
Added TimeWindow to PR Review feedback Review feedback
This commit is contained in:
@ -0,0 +1,12 @@
|
|||||||
|
package com.r3corda.core.utilities
|
||||||
|
|
||||||
|
import java.time.Duration
|
||||||
|
import java.time.Instant
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class representing a window in time from a particular instant, lasting a specified duration.
|
||||||
|
*/
|
||||||
|
data class TimeWindow(val start: Instant, val duration: Duration) {
|
||||||
|
val end: Instant
|
||||||
|
get() = start + duration
|
||||||
|
}
|
@ -12,7 +12,6 @@ import com.r3corda.core.node.services.ServiceType
|
|||||||
import com.r3corda.core.node.services.testing.MockIdentityService
|
import com.r3corda.core.node.services.testing.MockIdentityService
|
||||||
import com.r3corda.core.utilities.loggerFor
|
import com.r3corda.core.utilities.loggerFor
|
||||||
import com.r3corda.node.internal.AbstractNode
|
import com.r3corda.node.internal.AbstractNode
|
||||||
import com.r3corda.node.serialization.NodeClock
|
|
||||||
import com.r3corda.node.services.config.NodeConfiguration
|
import com.r3corda.node.services.config.NodeConfiguration
|
||||||
import com.r3corda.node.services.network.InMemoryMessagingNetwork
|
import com.r3corda.node.services.network.InMemoryMessagingNetwork
|
||||||
import com.r3corda.node.services.network.NetworkMapService
|
import com.r3corda.node.services.network.NetworkMapService
|
||||||
@ -67,7 +66,7 @@ class MockNetwork(private val networkSendManuallyPumped: Boolean = false,
|
|||||||
}
|
}
|
||||||
|
|
||||||
open class MockNode(dir: Path, config: NodeConfiguration, val mockNet: MockNetwork, networkMapAddr: NodeInfo?,
|
open class MockNode(dir: Path, config: NodeConfiguration, val mockNet: MockNetwork, networkMapAddr: NodeInfo?,
|
||||||
advertisedServices: Set<ServiceType>, val id: Int, val keyPair: KeyPair?) : AbstractNode(dir, config, networkMapAddr, advertisedServices, NodeClock()) {
|
advertisedServices: Set<ServiceType>, val id: Int, val keyPair: KeyPair?) : AbstractNode(dir, config, networkMapAddr, advertisedServices, TestClock()) {
|
||||||
override val log: Logger = loggerFor<MockNode>()
|
override val log: Logger = loggerFor<MockNode>()
|
||||||
override val serverThread: AffinityExecutor =
|
override val serverThread: AffinityExecutor =
|
||||||
if (mockNet.threadPerNode)
|
if (mockNet.threadPerNode)
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package com.r3corda.node.internal.testing
|
package com.r3corda.node.internal.testing
|
||||||
|
|
||||||
|
import com.r3corda.core.serialization.SerializeAsToken
|
||||||
|
import com.r3corda.core.serialization.SerializeAsTokenContext
|
||||||
|
import com.r3corda.core.serialization.SingletonSerializationToken
|
||||||
import com.r3corda.node.utilities.MutableClock
|
import com.r3corda.node.utilities.MutableClock
|
||||||
import java.time.Clock
|
import java.time.Clock
|
||||||
import java.time.Duration
|
import java.time.Duration
|
||||||
@ -12,24 +15,34 @@ import javax.annotation.concurrent.ThreadSafe
|
|||||||
* A [Clock] that can have the time advanced for use in testing
|
* A [Clock] that can have the time advanced for use in testing
|
||||||
*/
|
*/
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
class TestClock(private var delegateClock: Clock = Clock.systemUTC()) : MutableClock() {
|
class TestClock(private var delegateClock: Clock = Clock.systemUTC()) : MutableClock(), SerializeAsToken {
|
||||||
|
|
||||||
@Synchronized fun advanceBy(duration: Duration): Boolean {
|
private val token = SingletonSerializationToken(this)
|
||||||
if (!duration.isNegative) {
|
|
||||||
// It's ok to increment
|
override fun toToken(context: SerializeAsTokenContext) = SingletonSerializationToken.registerWithContext(token, this, context)
|
||||||
delegateClock = offset(delegateClock, duration)
|
|
||||||
notifyMutationObservers()
|
/**
|
||||||
return true
|
* Advance this [Clock] by the specified [Duration] for testing purposes.
|
||||||
}
|
*/
|
||||||
return false
|
@Synchronized fun advanceBy(duration: Duration) {
|
||||||
|
delegateClock = offset(delegateClock, duration)
|
||||||
|
notifyMutationObservers()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move this [Clock] to the specified [Instant] for testing purposes.
|
||||||
|
*
|
||||||
|
* This will only be approximate due to the time ticking away, but will be some time shortly after the requested [Instant].
|
||||||
|
*/
|
||||||
|
@Synchronized fun setTo(newInstant: Instant) = advanceBy(Duration.between(instant(), newInstant))
|
||||||
|
|
||||||
@Synchronized override fun instant(): Instant {
|
@Synchronized override fun instant(): Instant {
|
||||||
return delegateClock.instant()
|
return delegateClock.instant()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized override fun withZone(zone: ZoneId): Clock {
|
@Deprecated("Do not use this. Instead seek to use ZonedDateTime methods.", level = DeprecationLevel.ERROR)
|
||||||
return TestClock(delegateClock.withZone(zone))
|
override fun withZone(zone: ZoneId): Clock {
|
||||||
|
throw UnsupportedOperationException("Tokenized clock does not support withZone()")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized override fun getZone(): ZoneId {
|
@Synchronized override fun getZone(): ZoneId {
|
||||||
|
Reference in New Issue
Block a user