Skip to content

Checks: requests, retries and side effects

Templates for the four requests, retries and side effects stories. The hooks they assume are described in about the checks.

  • Proves: retrying the same booking intent creates one booking and one charge, while a deliberate second booking still works.
  • Fails for: per-attempt request IDs. The reservation that succeeded twice.
  • Harness needs: drop the next response after the server commits; count charges at the payment sandbox.
@Test
void retryingOneIntentCreatesOneBooking() {
var intent = bookings.createIntent(CUSTOMER, EVENT, 1);
faults.dropNextResponse(); // server commits; client hears nothing
assertThatThrownBy(() -> bookings.submit(intent)).isInstanceOf(TimeoutException.class);
var retry = bookings.submit(intent);
assertThat(db.count("bookings", "intent_id = ?", intent.id())).isEqualTo(1);
assertThat(paymentSandbox.chargesWithKey(intent.id())).hasSize(1);
assertThat(retry.bookingId()).isEqualTo(db.bookingIdFor(intent.id()));
}
@Test
void aDeliberateSecondBookingStillWorks() {
var first = bookings.submit(bookings.createIntent(CUSTOMER, EVENT, 1));
var second = bookings.submit(bookings.createIntent(CUSTOMER, EVENT, 1));
assertThat(second.bookingId()).isNotEqualTo(first.bookingId());
assertThat(paymentSandbox.chargesFor(CUSTOMER)).hasSize(2);
}

C-02: a changed request can’t borrow an old answer

Section titled “C-02: a changed request can’t borrow an old answer”
  • Proves: reusing an idempotency key with a different request is rejected, a true retry still replays, and the confirmation shows what the server actually accepted.
  • Fails for: key-only lookup. The idempotency key that hid a changed order.
  • Harness needs: drop the next response.
@Test
void reusingAKeyForADifferentRequestIsRejected() {
faults.dropNextResponse();
assertThatThrownBy(() -> orders.place("k-1", order(PRODUCT, 1))).isInstanceOf(TimeoutException.class);
var response = orders.placeRaw("k-1", order(PRODUCT, 3));
assertThat(response.status()).isIn(409, 422);
assertThat(db.reservedQuantity(PRODUCT)).isEqualTo(1);
}
@Test
void aTrueRetryReplaysTheOriginalResponse() {
var first = orders.place("k-2", order(PRODUCT, 1));
var again = orders.place("k-2", order(PRODUCT, 1));
assertThat(again).isEqualTo(first);
assertThat(db.count("orders", "idempotency_key = ?", "k-2")).isEqualTo(1);
}
@Test
void confirmationShowsTheAcceptedQuantityNotTheFormsQuantity() {
var confirmation = checkoutPage.submit(order(PRODUCT, 3));
assertThat(confirmation.displayedQuantity()).isEqualTo(confirmation.response().quantity());
}
  • Proves: once a cancellation is acknowledged, a create for the same intent can’t activate it, in any delivery order.
  • Fails for: treating not found as canceled. The cancellation that arrived before the booking.
  • Harness needs: deliver messages to the consumer directly, in a chosen order.
static Stream<List<String>> deliverySequences() {
return Stream.of(
List.of("create", "cancel"),
List.of("cancel", "create"),
List.of("cancel", "create", "create"),
List.of("create", "cancel", "cancel", "create"));
}
@ParameterizedTest
@MethodSource("deliverySequences")
void aCanceledIntentNeverBecomesActive(List<String> sequence) {
var intent = IntentId.random();
for (var command : sequence) {
consumer.deliver(command.equals("create") ? createBooking(intent) : cancelBooking(intent));
}
assertThat(db.activeBookingFor(intent)).isEmpty();
assertThat(paymentSandbox.chargesWithKey(intent.value())).isEmpty();
}

Every sequence ends with the intent canceled, because a cancellation is acknowledged in each one. If your business rule lets a later create override a cancellation, write that rule into the test explicitly.

  • Proves: a crash after the carrier accepted a shipment, followed by redelivery, doesn’t create a second shipment, and replaying old events creates none.
  • Fails for: local deduplication only. The outbox that shipped two packages.
  • Harness needs: crash the worker at a named point; count shipments in the carrier’s sandbox.
@Test
void aCrashAfterTheCarrierCallDoesNotShipTwice() {
var order = orders.placeAndPublish(ORDER);
faults.crashWorkerAt("after-carrier-call", 1); // crash once, at that point
broker.deliverUntilAcknowledged(order.eventId());
assertThat(carrierSandbox.shipmentsWithReference("ship-" + order.id())).hasSize(1);
}
@Test
void replayingYesterdaysEventsCreatesNoShipments() {
var before = carrierSandbox.shipmentCount();
broker.republish(OrderPlaced.class, YESTERDAY);
worker.drain();
assertThat(carrierSandbox.shipmentCount()).isEqualTo(before);
}

Written and edited by Abhinav Taduka, enterprise software architect.