How would you test an event consumer for duplicate delivery and idempotency?
Answer
Deliver the same event identifier more than once, concurrently and after consumer restart, then verify that business side effects occur at most once while acknowledgments and metrics remain correct. Test duplicates before and after partial failure, expired deduplication state and semantically repeated events with different IDs. Use authoritative persisted state as the oracle, not only consumer logs.
Strong answer includes
- tests duplicate delivery across restarts and concurrency
- covers partial failure around side effect and acknowledgment
- uses persisted business state as the oracle
Practical example
Killing the consumer process right after it charges a customer's card but before it acknowledges the message causes the broker to redeliver the same event on restart; the test asserts the persisted payment record shows exactly one charge, not that the consumer looks idempotent from its logs alone, because the logs would show two processing attempts either way.