Adjusted deletion behaviour
This commit is contained in:
1 parent
4a862d743b
commit
a1229f6793
2 files changed
+153
-15
No files matched your search
@@ -20,25 +20,23 @@ open class EventDispatcher(val eventStore: EventStore, private val lifecycleStor
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
open fun dispatch(referenceId: UUID, history: List<Event>, newEvents: List<Event>) {
|
||||
val deletedEventIds = history.filterIsInstance<DeleteEvent>().map { it.deletedEventId }
|
||||
|
||||
var candidates = newEvents
|
||||
.validEvents(deletedEventIds)
|
||||
.replayCandidates()
|
||||
|
||||
// ⭐ Fallback: kun når ALLE nye events er signaler
|
||||
if (candidates.isEmpty() && newEvents.all { it is SignalEvent }) {
|
||||
candidates = history
|
||||
.validEvents(deletedEventIds)
|
||||
.replayCandidates()
|
||||
}
|
||||
|
||||
val deletedEventIds = history
|
||||
.filterIsInstance<DeleteEvent>()
|
||||
.map { it.deletedEventId }
|
||||
|
||||
val effectiveHistory = history
|
||||
.validEvents(deletedEventIds)
|
||||
.filterNot { it is DeleteEvent }
|
||||
|
||||
//val producedEvents: MutableList<Event> = mutableListOf()
|
||||
var candidates = newEvents
|
||||
.validEvents(deletedEventIds)
|
||||
.replayCandidates()
|
||||
|
||||
// Fallback: kun når alle nye er signaler → bruk historikk som “nytt”
|
||||
if (candidates.isEmpty() && newEvents.all { it is SignalEvent }) {
|
||||
candidates = effectiveHistory
|
||||
.replayCandidates()
|
||||
}
|
||||
|
||||
EventListenerRegistry.getListeners().forEach { listener ->
|
||||
|
||||
@@ -189,8 +187,14 @@ open class EventDispatcher(val eventStore: EventStore, private val lifecycleStor
|
||||
}
|
||||
|
||||
fun List<Event>.validEvents(deletedEventIds: List<UUID>): List<Event> {
|
||||
return this.filter {it.eventId !in deletedEventIds }
|
||||
return this.filter { event ->
|
||||
val id = event.eventId
|
||||
val parents = event.metadata.derivedFromId ?: emptyList()
|
||||
|
||||
id !in deletedEventIds && parents.none { it in deletedEventIds }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates that referenceId (a lateinit) is initialized.
|
||||
|
||||
@@ -521,6 +521,140 @@ class EventDispatcherTest : TestBase() {
|
||||
assertTrue(produced.first() is DerivedEvent)
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Når en event slettes direkte skal den ikke være kandidat eller i historikk")
|
||||
fun directDeleteShouldRemoveEvent() {
|
||||
val ref = UUID.randomUUID()
|
||||
val dispatcher = EventDispatcher(eventStore, lifecycleStore)
|
||||
|
||||
val received = mutableListOf<Event>()
|
||||
|
||||
object : EventListener() {
|
||||
override fun onEvent(event: Event, history: List<Event>): Event? {
|
||||
received += event
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
val a = TriggerEvent().usingReferenceId(ref)
|
||||
val del = object : DeleteEvent(a.eventId) {}.usingReferenceId(ref)
|
||||
|
||||
dispatcher.dispatch(ref, listOf(a, del), listOf(a, del))
|
||||
|
||||
assertFalse(received.contains(a))
|
||||
assertTrue(received.contains(del))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Når en event slettes skal alle dens derived events også fjernes")
|
||||
fun deleteShouldRemoveDerivedChain() {
|
||||
val ref = UUID.randomUUID()
|
||||
val dispatcher = EventDispatcher(eventStore, lifecycleStore)
|
||||
|
||||
val received = mutableListOf<Event>()
|
||||
|
||||
object : EventListener() {
|
||||
override fun onEvent(event: Event, history: List<Event>): Event? {
|
||||
received += event
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
val a = TriggerEvent().usingReferenceId(ref)
|
||||
val b = DerivedEvent().derivedOf(a).usingReferenceId(ref)
|
||||
val c = DerivedEvent().derivedOf(b).usingReferenceId(ref)
|
||||
|
||||
val del = object : DeleteEvent(b.eventId) {}.usingReferenceId(ref)
|
||||
|
||||
dispatcher.dispatch(ref, listOf(a, b, c, del), listOf(a, b, c, del))
|
||||
|
||||
assertTrue(received.contains(a))
|
||||
assertFalse(received.contains(b))
|
||||
assertFalse(received.contains(c))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Når root-event slettes skal hele derivation-treet fjernes")
|
||||
fun deleteRootShouldRemoveEntireTree() {
|
||||
val ref = UUID.randomUUID()
|
||||
val dispatcher = EventDispatcher(eventStore, lifecycleStore)
|
||||
|
||||
val received = mutableListOf<Event>()
|
||||
|
||||
object : EventListener() {
|
||||
override fun onEvent(event: Event, history: List<Event>): Event? {
|
||||
received += event
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
val a = TriggerEvent().usingReferenceId(ref)
|
||||
val b = DerivedEvent().derivedOf(a).usingReferenceId(ref)
|
||||
val c = DerivedEvent().derivedOf(a).usingReferenceId(ref)
|
||||
|
||||
val del = object : DeleteEvent(a.eventId) {}.usingReferenceId(ref)
|
||||
|
||||
dispatcher.dispatch(ref, listOf(a, b, c, del), listOf(a, b, c, del))
|
||||
|
||||
assertFalse(received.contains(a))
|
||||
assertFalse(received.contains(b))
|
||||
assertFalse(received.contains(c))
|
||||
assertTrue(received.contains(del))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Når derived-event slettes skal parent bli replay-kandidat igjen")
|
||||
fun deleteDerivedShouldReplayParent() {
|
||||
val ref = UUID.randomUUID()
|
||||
val dispatcher = EventDispatcher(eventStore, lifecycleStore)
|
||||
|
||||
val received = mutableListOf<Event>()
|
||||
|
||||
object : EventListener() {
|
||||
override fun onEvent(event: Event, history: List<Event>): Event? {
|
||||
received += event
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
val a = TriggerEvent().usingReferenceId(ref)
|
||||
val b = DerivedEvent().derivedOf(a).usingReferenceId(ref)
|
||||
val del = object : DeleteEvent(b.eventId) {}.usingReferenceId(ref)
|
||||
|
||||
dispatcher.dispatch(ref, listOf(a, b, del), listOf(a, b, del))
|
||||
|
||||
assertTrue(received.contains(a)) // replayed
|
||||
assertFalse(received.contains(b))
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("SignalEvent skal ikke være kandidat selv om den slettes")
|
||||
fun deleteSignalShouldNotMakeItCandidate() {
|
||||
val ref = UUID.randomUUID()
|
||||
val dispatcher = EventDispatcher(eventStore, lifecycleStore)
|
||||
|
||||
val received = mutableListOf<Event>()
|
||||
|
||||
class S : SignalEvent()
|
||||
|
||||
EventTypeRegistry.register(listOf(S::class.java))
|
||||
|
||||
object : EventListener() {
|
||||
override fun onEvent(event: Event, history: List<Event>): Event? {
|
||||
received += event
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
val sig = S().usingReferenceId(ref)
|
||||
val del = object : DeleteEvent(sig.eventId) {}.usingReferenceId(ref)
|
||||
|
||||
dispatcher.dispatch(ref, listOf(sig, del), listOf(sig, del))
|
||||
|
||||
assertFalse(received.contains(sig))
|
||||
assertTrue(received.contains(del))
|
||||
}
|
||||
|
||||
|
||||
|
||||
// --- Test helpers ---
|
||||
|
||||
Reference in new issue
Block a user