Skip to content

Checks: events and replay

Templates for the four events and replay stories. The hooks they assume are described in about the checks.

  • Proves: rebuilding a read model from history reproduces it exactly and sends no email, while live events during the rebuild are still handled once.
  • Fails for: one handler that projects and sends. The replay that emailed every customer again.
  • Harness needs: a fake mail sink; the ability to rebuild a projection into a new copy.
@Test
void rebuildingTheOrderHistorySendsNothing() {
eventLog.append(sampleOrders(500));
projections.catchUp("order-history");
var original = projections.snapshot("order-history");
mailSink.clear();
projections.rebuildInto("order-history", "order-history-v2");
assertThat(projections.snapshot("order-history-v2")).isEqualTo(original);
assertThat(mailSink.messages()).isEmpty();
}
@Test
void aLiveOrderDuringTheRebuildGetsExactlyOneEmail() {
eventLog.append(sampleOrders(500));
var rebuild = projections.rebuildIntoAsync("order-history", "order-history-v2");
pausePoints.await("rebuild-halfway");
var live = orders.place(NEW_ORDER);
pausePoints.release("rebuild-halfway");
rebuild.join();
assertThat(mailSink.messagesAbout(live.id())).hasSize(1);
assertThat(projections.contains("order-history-v2", live.id())).isTrue();
}

A cheap structural guard to add next to it: fail the build if any handler registered as a projector depends on a mail, HTTP or message-publishing client.

  • Proves: a 30-second hold expires at 30 seconds in every supported producer and consumer pairing.
  • Fails for: schema-only compatibility checks. The compatible schema that changed the meaning.
  • Harness needs: the producer versions and consumer versions you support, runnable in one test.
static Stream<Arguments> pairings() {
return SUPPORTED_PRODUCERS.stream()
.flatMap(p -> SUPPORTED_CONSUMERS.stream().map(c -> Arguments.of(p, c)));
}
@ParameterizedTest(name = "producer {0} -> consumer {1}")
@MethodSource("pairings")
void aThirtySecondHoldExpiresAtThirtySeconds(String producerVersion, String consumerVersion) {
var heldAt = Instant.parse("2026-11-21T10:00:00Z");
var event = producers.version(producerVersion).seatHeld("S-1", heldAt, Duration.ofSeconds(30));
var expiresAt = consumers.version(consumerVersion).expiryOf(event);
assertThat(expiresAt).isEqualTo(heldAt.plusSeconds(30));
}

The test asks the producer for a duration and the consumer for an instant, so neither side’s unit leaks into the test. That’s the point: the test states the meaning, and each side proves it honors it.

C-07: stale events can’t reopen a closed record

Section titled “C-07: stale events can’t reopen a closed record”
  • Proves: redriving an older event leaves a canceled order canceled, explains the skip, and still applies a genuinely missing event.
  • Fails for: blind redrive. The dead-letter redrive that reopened a canceled order.
  • Harness needs: deliver events to the consumer directly; read the redrive report.
@Test
void redrivingAnOldCreateLeavesACanceledOrderClosed() {
consumer.deliver(orderCanceled("O-1", 2));
var report = consumer.redrive(List.of(orderCreated("O-1", 1)));
assertThat(fulfillment.status("O-1")).isEqualTo(CANCELED);
assertThat(fulfillment.pickList()).doesNotContain("O-1");
assertThat(report.skipped()).singleElement()
.satisfies(s -> assertThat(s.reason()).contains("version 1", "version 2"));
}
@Test
void everyDeliveryOrderGivesTheSameFinalState() {
var events = List.of(orderCreated("O-2", 1), orderUpdated("O-2", 2), orderCanceled("O-2", 3));
for (var permutation : permutations(events)) {
fulfillment.reset();
permutation.forEach(consumer::deliver);
assertThat(fulfillment.status("O-2")).as(permutation.toString()).isEqualTo(CANCELED);
}
}
  • Proves: rebuilding invoices after a rate change reproduces every issued invoice exactly, and an intentional recalculation produces separate, identifiable records.
  • Fails for: looking up current configuration during replay. The replay that calculated a different invoice.
  • Harness needs: a controllable rate table and clock.
@Test
void rebuildingAfterARateChangeReproducesIssuedInvoices() {
rates.set(DESTINATION, "0.0825", LocalDate.parse("2026-01-01"));
clock.set("2026-03-10");
var issued = invoices.issueFor(shipOrder("O-3", "100.00"));
rates.set(DESTINATION, "0.0850", LocalDate.parse("2026-07-01"));
clock.set("2026-09-01");
projections.rebuild("invoices");
assertThat(invoices.get(issued.id()).contentHash()).isEqualTo(issued.contentHash());
assertThat(invoices.get(issued.id()).total()).isEqualByComparingTo("108.25");
}
@Test
void aRecalculationIsRecordedSeparately() {
var issued = invoices.issueFor(shipOrder("O-4", "100.00"));
var adjustment = invoices.recalculate(issued.id(), "0.0800", "rate correction");
assertThat(invoices.get(issued.id()).contentHash()).isEqualTo(issued.contentHash());
assertThat(adjustment.references()).isEqualTo(issued.id());
}

Written and edited by Abhinav Taduka, enterprise software architect.