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.
C-01: one intent, one booking
Section titled “C-01: one intent, one booking”- 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.
@Testvoid 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()));}
@Testvoid 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);}def test_retrying_one_intent_creates_one_booking(bookings, faults, db, payment_sandbox): intent = bookings.create_intent(CUSTOMER, EVENT, seats=1) faults.drop_next_response() # server commits; client hears nothing with pytest.raises(TimeoutError): bookings.submit(intent)
retry = bookings.submit(intent)
assert db.count("bookings", intent_id=intent.id) == 1 assert len(payment_sandbox.charges_with_key(intent.id)) == 1 assert retry.booking_id == db.booking_id_for(intent.id)
def test_a_deliberate_second_booking_still_works(bookings, payment_sandbox): first = bookings.submit(bookings.create_intent(CUSTOMER, EVENT, seats=1)) second = bookings.submit(bookings.create_intent(CUSTOMER, EVENT, seats=1))
assert second.booking_id != first.booking_id assert len(payment_sandbox.charges_for(CUSTOMER)) == 2-- Any row returned is an intent that produced more than one booking.SELECT intent_id, count(*) AS bookingsFROM bookingsGROUP BY intent_idHAVING count(*) > 1;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.
@Testvoid 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);}
@Testvoid 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);}
@Testvoid confirmationShowsTheAcceptedQuantityNotTheFormsQuantity() { var confirmation = checkoutPage.submit(order(PRODUCT, 3)); assertThat(confirmation.displayedQuantity()).isEqualTo(confirmation.response().quantity());}def test_reusing_a_key_for_a_different_request_is_rejected(orders, faults, db): faults.drop_next_response() with pytest.raises(TimeoutError): orders.place("k-1", order(PRODUCT, quantity=1))
response = orders.place_raw("k-1", order(PRODUCT, quantity=3))
assert response.status in (409, 422) assert db.reserved_quantity(PRODUCT) == 1
def test_a_true_retry_replays_the_original_response(orders, db): first = orders.place("k-2", order(PRODUCT, quantity=1)) again = orders.place("k-2", order(PRODUCT, quantity=1))
assert again == first assert db.count("orders", idempotency_key="k-2") == 1C-03: a cancellation survives reordering
Section titled “C-03: a cancellation survives reordering”- 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();}@pytest.mark.parametrize("sequence", [ ["create", "cancel"], ["cancel", "create"], ["cancel", "create", "create"], ["create", "cancel", "cancel", "create"],])def test_a_canceled_intent_never_becomes_active(sequence, consumer, db, payment_sandbox): intent = new_intent_id() for command in sequence: consumer.deliver(create_booking(intent) if command == "create" else cancel_booking(intent))
assert db.active_booking_for(intent) is None assert payment_sandbox.charges_with_key(intent) == []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.
C-04: one shipment per order
Section titled “C-04: one shipment per order”- 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.
@Testvoid 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);}
@Testvoid replayingYesterdaysEventsCreatesNoShipments() { var before = carrierSandbox.shipmentCount(); broker.republish(OrderPlaced.class, YESTERDAY); worker.drain(); assertThat(carrierSandbox.shipmentCount()).isEqualTo(before);}def test_a_crash_after_the_carrier_call_does_not_ship_twice(orders, faults, broker, carrier_sandbox): order = orders.place_and_publish(ORDER) faults.crash_worker_at("after-carrier-call", times=1)
broker.deliver_until_acknowledged(order.event_id)
assert len(carrier_sandbox.shipments_with_reference(f"ship-{order.id}")) == 1
def test_replaying_yesterdays_events_creates_no_shipments(broker, worker, carrier_sandbox): before = carrier_sandbox.shipment_count() broker.republish("OrderPlaced", day=YESTERDAY) worker.drain() assert carrier_sandbox.shipment_count() == beforeRelated pages
Section titled “Related pages”Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.