forked from corda/corda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORDA-716 Rename one TestClock to DemoClock, and unduplicate code (co…
- Loading branch information
Showing
10 changed files
with
95 additions
and
175 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
node/src/main/kotlin/net/corda/node/internal/CordaClock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package net.corda.node.internal | ||
|
||
import net.corda.core.serialization.SerializeAsToken | ||
import net.corda.core.serialization.SerializeAsTokenContext | ||
import net.corda.core.serialization.SingletonSerializationToken | ||
import rx.Observable | ||
import rx.Subscriber | ||
import rx.subscriptions.Subscriptions | ||
import java.time.Clock | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
import java.util.concurrent.CopyOnWriteArraySet | ||
import java.util.concurrent.atomic.AtomicLong | ||
import javax.annotation.concurrent.ThreadSafe | ||
|
||
/** A [Clock] that tokenizes itself when serialized, and delegates to an underlying [Clock] implementation. */ | ||
abstract class CordaClock : Clock(), SerializeAsToken { | ||
protected abstract val delegateClock: Clock | ||
private val token = SingletonSerializationToken.singletonSerializationToken(javaClass) | ||
override fun toToken(context: SerializeAsTokenContext) = token.registerWithContext(context, this) | ||
override fun instant(): Instant = delegateClock.instant() | ||
override fun getZone(): ZoneId = delegateClock.zone | ||
@Deprecated("Do not use this. Instead seek to use ZonedDateTime methods.", level = DeprecationLevel.ERROR) | ||
override fun withZone(zone: ZoneId) = throw UnsupportedOperationException("Tokenized clock does not support withZone()") | ||
} | ||
|
||
@ThreadSafe | ||
class SimpleClock(override val delegateClock: Clock) : CordaClock() | ||
|
||
/** | ||
* An abstract class with helper methods for a type of Clock that might have it's concept of "now" adjusted externally. | ||
* e.g. for testing (so unit tests do not have to wait for timeouts in realtime) or for demos and simulations. | ||
*/ | ||
abstract class MutableClock(private var _delegateClock: Clock) : CordaClock() { | ||
override var delegateClock | ||
@Synchronized get() = _delegateClock | ||
@Synchronized set(clock) { | ||
_delegateClock = clock | ||
} | ||
private val _version = AtomicLong(0L) | ||
/** This is an observer on the mutation count of this [Clock], which reflects the occurence of mutations. */ | ||
val mutations: Observable<Long> by lazy { | ||
Observable.create { subscriber: Subscriber<in Long> -> | ||
if (!subscriber.isUnsubscribed) { | ||
mutationObservers.add(subscriber) | ||
// This is not very intuitive, but subscribing to a subscriber observes unsubscribes. | ||
subscriber.add(Subscriptions.create { mutationObservers.remove(subscriber) }) | ||
} | ||
} | ||
} | ||
private val mutationObservers = CopyOnWriteArraySet<Subscriber<in Long>>() | ||
/** Must be called by subclasses when they mutate (but not just with the passage of time as per the "wall clock"). */ | ||
protected fun notifyMutationObservers() { | ||
val version = _version.incrementAndGet() | ||
for (observer in mutationObservers) { | ||
if (!observer.isUnsubscribed) { | ||
observer.onNext(version) | ||
} | ||
} | ||
} | ||
} |
45 changes: 0 additions & 45 deletions
45
node/src/main/kotlin/net/corda/node/internal/MutableClock.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
node/src/main/kotlin/net/corda/node/serialization/NodeClock.kt
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
node/src/main/kotlin/net/corda/node/utilities/DemoClock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package net.corda.node.utilities | ||
|
||
import net.corda.core.internal.until | ||
import net.corda.node.internal.MutableClock | ||
import java.time.Clock | ||
import java.time.LocalDate | ||
import javax.annotation.concurrent.ThreadSafe | ||
|
||
/** A [Clock] that can have the date advanced for use in demos. */ | ||
@ThreadSafe | ||
class DemoClock(delegateClock: Clock) : MutableClock(delegateClock) { | ||
@Synchronized | ||
fun updateDate(date: LocalDate): Boolean { | ||
val currentDate = LocalDate.now(this) | ||
if (currentDate.isBefore(date)) { | ||
// It's ok to increment | ||
delegateClock = Clock.offset(delegateClock, currentDate.atStartOfDay() until date.atStartOfDay()) | ||
notifyMutationObservers() | ||
return true | ||
} | ||
return false | ||
} | ||
} |
49 changes: 0 additions & 49 deletions
49
node/src/main/kotlin/net/corda/node/utilities/TestClock.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters