mirror of
https://github.com/corda/corda.git
synced 2025-06-01 23:20:54 +00:00
* CORDA-2825 Fix Progress Tracker bug CORDA-2825 Add discriminator CORDA-2825 Fix hashcode CORDA-2825 remove todo CORDA-2825 more tests * CORDA-2825 Address code review comments (cherry picked from commit 685f94bf6659ae4a1b7589e43f518fd9420ef5ce)
This commit is contained in:
parent
8f81b770e0
commit
358d984829
@ -2,6 +2,7 @@ package net.corda.core.utilities
|
||||
|
||||
import net.corda.core.DeleteForDJVM
|
||||
import net.corda.core.internal.STRUCTURAL_STEP_PREFIX
|
||||
import net.corda.core.internal.warnOnce
|
||||
import net.corda.core.serialization.CordaSerializable
|
||||
import rx.Observable
|
||||
import rx.Subscription
|
||||
@ -34,6 +35,10 @@ import java.util.*
|
||||
@DeleteForDJVM
|
||||
class ProgressTracker(vararg inputSteps: Step) {
|
||||
|
||||
private companion object {
|
||||
private val log = contextLogger()
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
@DeleteForDJVM
|
||||
sealed class Change(val progressTracker: ProgressTracker) {
|
||||
@ -55,6 +60,11 @@ class ProgressTracker(vararg inputSteps: Step) {
|
||||
*/
|
||||
@CordaSerializable
|
||||
open class Step(open val label: String) {
|
||||
private fun definitionLocation(): String = Exception().stackTrace.first { it.className != ProgressTracker.Step::class.java.name }.let { "${it.className}:${it.lineNumber}" }
|
||||
|
||||
// Required when Steps with the same name are defined in multiple places.
|
||||
private val discriminator: String = definitionLocation()
|
||||
|
||||
open val changes: Observable<Change> get() = Observable.empty()
|
||||
open fun childProgressTracker(): ProgressTracker? = null
|
||||
/**
|
||||
@ -63,6 +73,17 @@ class ProgressTracker(vararg inputSteps: Step) {
|
||||
* Even if empty the basic details (i.e. label) of the step will be recorded for audit purposes.
|
||||
*/
|
||||
open val extraAuditData: Map<String, String> get() = emptyMap()
|
||||
|
||||
override fun equals(other: Any?) = when (other) {
|
||||
is Step -> this.label == other.label && this.discriminator == other.discriminator
|
||||
else -> false
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = label.hashCode()
|
||||
result = 31 * result + discriminator.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
// Sentinel objects. Overrides equals() to survive process restarts and serialization.
|
||||
@ -89,7 +110,12 @@ class ProgressTracker(vararg inputSteps: Step) {
|
||||
/**
|
||||
* The steps in this tracker, same as the steps passed to the constructor but with UNSTARTED and DONE inserted.
|
||||
*/
|
||||
val steps = arrayOf(UNSTARTED, STARTING, *inputSteps, DONE)
|
||||
val steps = arrayOf(UNSTARTED, STARTING, *inputSteps, DONE).also { stepsArray ->
|
||||
val labels = stepsArray.map { it.label }
|
||||
if (labels.toSet().size < labels.size) {
|
||||
log.warnOnce("Found ProgressTracker Step(s) with the same label: ${labels.groupBy { it }.filter { it.value.size > 1 }.map { it.key }}")
|
||||
}
|
||||
}
|
||||
|
||||
private var _allStepsCache: List<Pair<Int, Step>> = _allSteps()
|
||||
|
||||
@ -137,7 +163,6 @@ class ProgressTracker(vararg inputSteps: Step) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
init {
|
||||
steps.forEach {
|
||||
configureChildTrackerForStep(it)
|
||||
|
@ -1,13 +1,25 @@
|
||||
package net.corda.core.utilities
|
||||
|
||||
import net.corda.core.serialization.internal.checkpointDeserialize
|
||||
import net.corda.core.serialization.internal.checkpointSerialize
|
||||
import net.corda.core.utilities.ProgressTrackerTest.NonSingletonSteps.first
|
||||
import net.corda.core.utilities.ProgressTrackerTest.NonSingletonSteps.first2
|
||||
import net.corda.testing.core.internal.CheckpointSerializationEnvironmentRule
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import java.util.*
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFails
|
||||
import kotlin.test.assertNotEquals
|
||||
|
||||
class ProgressTrackerTest {
|
||||
|
||||
@Rule
|
||||
@JvmField
|
||||
val testCheckpointSerialization = CheckpointSerializationEnvironmentRule()
|
||||
|
||||
object SimpleSteps {
|
||||
object ONE : ProgressTracker.Step("one")
|
||||
object TWO : ProgressTracker.Step("two")
|
||||
@ -320,4 +332,40 @@ class ProgressTrackerTest {
|
||||
fun `cannot assign step not belonging to this progress tracker`() {
|
||||
assertFails { pt.currentStep = BabySteps.UNOS }
|
||||
}
|
||||
|
||||
object NonSingletonSteps {
|
||||
val first = ProgressTracker.Step("first")
|
||||
val second = ProgressTracker.Step("second")
|
||||
val first2 = ProgressTracker.Step("first")
|
||||
fun tracker() = ProgressTracker(first, second, first2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Serializing and deserializing a tracker maintains equality`() {
|
||||
val step = NonSingletonSteps.first
|
||||
val recreatedStep = step
|
||||
.checkpointSerialize(testCheckpointSerialization.checkpointSerializationContext)
|
||||
.checkpointDeserialize(testCheckpointSerialization.checkpointSerializationContext)
|
||||
assertEquals(step, recreatedStep)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can assign a recreated equal step`() {
|
||||
val tracker = NonSingletonSteps.tracker()
|
||||
val recreatedStep = first
|
||||
.checkpointSerialize(testCheckpointSerialization.checkpointSerializationContext)
|
||||
.checkpointDeserialize(testCheckpointSerialization.checkpointSerializationContext)
|
||||
tracker.currentStep = recreatedStep
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Steps with the same label defined in different places are not equal`() {
|
||||
val one = ProgressTracker.Step("one")
|
||||
assertNotEquals(one, SimpleSteps.ONE)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Steps with the same label defined in the same place are also not equal`() {
|
||||
assertNotEquals(first, first2)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user