Hard fail if listener is implemented wrong
This commit is contained in:
1 parent
d7b1ef5445
commit
624991551b
4 files changed
+168
No files matched your search
@@ -30,6 +30,7 @@ open class EventDispatcher(val eventStore: EventStore) {
|
|||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
validateReferenceId(result, listener)
|
validateReferenceId(result, listener)
|
||||||
|
validateDerivation(result, candidate, effectiveHistory, listener)
|
||||||
eventStore.persist(result)
|
eventStore.persist(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,4 +77,45 @@ open class EventDispatcher(val eventStore: EventStore) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun validateDerivation(
|
||||||
|
producedEvent: Event,
|
||||||
|
sourceEvent: Event,
|
||||||
|
history: List<Event>,
|
||||||
|
listener: EventListener
|
||||||
|
) {
|
||||||
|
val parents = producedEvent.metadata.derivedFromId ?: emptyList()
|
||||||
|
if (parents.isEmpty()) return
|
||||||
|
|
||||||
|
// Lytteren har eksplisitt opt-in → da er alt i historikken lov
|
||||||
|
if (listener.allowDerivativeOnHistoricalEvent()) {
|
||||||
|
// Men vi kan fortsatt nekte Signal/Delete som parent hvis du vil
|
||||||
|
val historyById = history.associateBy { it.eventId }
|
||||||
|
parents.forEach { parentId ->
|
||||||
|
val parent = historyById[parentId]
|
||||||
|
if (parent is SignalEvent || parent is DeleteEvent) {
|
||||||
|
throw HardDispatchException.IllegalDerivationException(
|
||||||
|
parentId = parentId,
|
||||||
|
producedEvent = producedEvent,
|
||||||
|
listener = listener
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default: kun lov å derivere fra sourceEvent
|
||||||
|
parents.forEach { parentId ->
|
||||||
|
if (parentId != sourceEvent.eventId) {
|
||||||
|
throw HardDispatchException.IllegalDerivationException(
|
||||||
|
parentId = parentId,
|
||||||
|
producedEvent = producedEvent,
|
||||||
|
listener = listener
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,34 @@
|
|||||||
package no.iktdev.eventi.events
|
package no.iktdev.eventi.events
|
||||||
|
|
||||||
|
import mu.KotlinLogging
|
||||||
import no.iktdev.eventi.models.Event
|
import no.iktdev.eventi.models.Event
|
||||||
import no.iktdev.eventi.registry.EventListenerRegistry
|
import no.iktdev.eventi.registry.EventListenerRegistry
|
||||||
|
|
||||||
abstract class EventListener: EventListenerImplementation {
|
abstract class EventListener: EventListenerImplementation {
|
||||||
|
private val log = KotlinLogging.logger {}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
EventListenerRegistry.registerListener(this)
|
EventListenerRegistry.registerListener(this)
|
||||||
|
|
||||||
|
if (allowDerivativeOnHistoricalEvent()) {
|
||||||
|
log.warn(
|
||||||
|
"Listener ${this::class.simpleName} has enabled historical derivation. " +
|
||||||
|
"This bypasses strict derivation validation and should be used with caution."
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows this listener to accept events that are derived from events
|
||||||
|
* not present in the current dispatch input.
|
||||||
|
*
|
||||||
|
* WARNING: Enabling this breaks strict derivation guarantees and should
|
||||||
|
* only be used for advanced listeners such as replay, migration or
|
||||||
|
* compensation handlers.
|
||||||
|
*/
|
||||||
|
open fun allowDerivativeOnHistoricalEvent(): Boolean = false
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EventListenerImplementation {
|
interface EventListenerImplementation {
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package no.iktdev.eventi.events
|
||||||
|
|
||||||
|
import no.iktdev.eventi.models.Event
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
sealed class HardDispatchException(
|
||||||
|
message: String,
|
||||||
|
val event: Event? = null,
|
||||||
|
val listener: EventListener? = null
|
||||||
|
) : Exception(message) {
|
||||||
|
|
||||||
|
class IllegalDerivationException(
|
||||||
|
parentId: UUID,
|
||||||
|
producedEvent: Event,
|
||||||
|
listener: EventListener
|
||||||
|
) : HardDispatchException(
|
||||||
|
"Illegal derivation in listener ${listener::class.java.name}: " +
|
||||||
|
"Event ${producedEvent::class.java.name} (${producedEvent.eventId}) " +
|
||||||
|
"claims to be derived from $parentId, but that event is not present in the " +
|
||||||
|
"current dispatch input and historical derivation is not allowed.",
|
||||||
|
event = producedEvent,
|
||||||
|
listener = listener
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import no.iktdev.eventi.serialization.ZDS.toEvent
|
|||||||
import no.iktdev.eventi.serialization.ZDS.toPersisted
|
import no.iktdev.eventi.serialization.ZDS.toPersisted
|
||||||
import no.iktdev.eventi.events.EventDispatcher
|
import no.iktdev.eventi.events.EventDispatcher
|
||||||
import no.iktdev.eventi.events.EventListener
|
import no.iktdev.eventi.events.EventListener
|
||||||
|
import no.iktdev.eventi.events.HardDispatchException
|
||||||
import no.iktdev.eventi.registry.EventTypeRegistry
|
import no.iktdev.eventi.registry.EventTypeRegistry
|
||||||
import no.iktdev.eventi.models.DeleteEvent
|
import no.iktdev.eventi.models.DeleteEvent
|
||||||
import no.iktdev.eventi.models.Event
|
import no.iktdev.eventi.models.Event
|
||||||
@@ -15,10 +16,12 @@ import org.assertj.core.api.Assertions.assertThat
|
|||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertFalse
|
import org.junit.jupiter.api.Assertions.assertFalse
|
||||||
import org.junit.jupiter.api.Assertions.assertNotNull
|
import org.junit.jupiter.api.Assertions.assertNotNull
|
||||||
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
import org.junit.jupiter.api.Assertions.assertTrue
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
import org.junit.jupiter.api.BeforeEach
|
import org.junit.jupiter.api.BeforeEach
|
||||||
import org.junit.jupiter.api.DisplayName
|
import org.junit.jupiter.api.DisplayName
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.assertThrows
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
|
|
||||||
@DisplayName(
|
@DisplayName(
|
||||||
@@ -429,6 +432,84 @@ class EventDispatcherTest : TestBase() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName(
|
||||||
|
"""
|
||||||
|
Når en lytter produserer en derived event fra en historisk hendelse
|
||||||
|
Hvis lytteren ikke tillater historisk derivation
|
||||||
|
Så skal IllegalDerivationException kastes
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
fun shouldThrowWhenDerivingFromHistoricalEventWithoutOptIn() {
|
||||||
|
val dispatcher = EventDispatcher(eventStore)
|
||||||
|
val referenceId = UUID.randomUUID()
|
||||||
|
|
||||||
|
// Historikk
|
||||||
|
val e1 = TriggerEvent().usingReferenceId(referenceId)
|
||||||
|
val e2 = OtherEvent().usingReferenceId(referenceId)
|
||||||
|
val e3 = DummyEvent().usingReferenceId(referenceId)
|
||||||
|
|
||||||
|
// Dispatch-kandidat
|
||||||
|
val e4 = TriggerEvent().usingReferenceId(referenceId)
|
||||||
|
|
||||||
|
// Lytter som produserer en ulovlig derived event
|
||||||
|
object : EventListener() {
|
||||||
|
override fun onEvent(event: Event, history: List<Event>): Event? {
|
||||||
|
if (event == e4) {
|
||||||
|
return DerivedEvent().derivedOf(e2) // ← ulovlig uten opt-in
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val context = listOf(e1, e2, e3, e4)
|
||||||
|
|
||||||
|
assertThrows(HardDispatchException.IllegalDerivationException::class.java) {
|
||||||
|
dispatcher.dispatch(referenceId, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName(
|
||||||
|
"""
|
||||||
|
Når en lytter produserer en derived event fra en historisk hendelse
|
||||||
|
Hvis lytteren tillater historisk derivation
|
||||||
|
Så skal dispatch fullføre uten feil
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
fun shouldAllowHistoricalDerivationWhenOptInIsEnabled() {
|
||||||
|
val dispatcher = EventDispatcher(eventStore)
|
||||||
|
val referenceId = UUID.randomUUID()
|
||||||
|
|
||||||
|
// Historikk
|
||||||
|
val e1 = TriggerEvent().usingReferenceId(referenceId)
|
||||||
|
val e2 = OtherEvent().usingReferenceId(referenceId)
|
||||||
|
val e3 = DummyEvent().usingReferenceId(referenceId)
|
||||||
|
|
||||||
|
// Dispatch-kandidat
|
||||||
|
val e4 = TriggerEvent().usingReferenceId(referenceId)
|
||||||
|
|
||||||
|
// Lytter som tillater historisk derivation
|
||||||
|
object : EventListener() {
|
||||||
|
override fun allowDerivativeOnHistoricalEvent(): Boolean = true
|
||||||
|
|
||||||
|
override fun onEvent(event: Event, history: List<Event>): Event? {
|
||||||
|
if (event == e4) {
|
||||||
|
return DerivedEvent().derivedOf(e2) // ← lovlig med opt-in
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val context = listOf(e1, e2, e3, e4)
|
||||||
|
|
||||||
|
dispatcher.dispatch(referenceId, context)
|
||||||
|
|
||||||
|
val produced = eventStore.all().mapNotNull { it.toEvent() }
|
||||||
|
assertEquals(1, produced.size)
|
||||||
|
assertTrue(produced.first() is DerivedEvent)
|
||||||
|
assertTrue(produced.first().metadata.derivedFromId!!.contains(e2.eventId))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// --- Test helpers ---
|
// --- Test helpers ---
|
||||||
|
|||||||
Reference in new issue
Block a user