Part 02 · Testing & Code Quality · 2.1.08

Messaging, contract và end-to-end testing

Distributed boundary cần test delivery, compatibility và recovery; một happy-path publish/consume không đủ để chứng minh business effect an toàn.


1. Các lớp test cho messaging

Broker container không tự chứng minh exactly-once business effect. Cần test duplicate delivery, crash point và invariant của side effect.

2. Delivery call flow

Producer serialize → broker persist/route → consumer poll/deliver → handler side effect → acknowledgment/offset commit.

Crash giữa side effect và ack tạo redelivery. Vì vậy test phải gửi duplicate hoặc restart consumer tại checkpoint, sau đó assert business invariant cuối cùng thay vì chỉ assert message đã được consume.

Idempotency rule: một message có thể được giao nhiều lần; business effect phải chỉ xảy ra một lần hoặc có cơ chế deduplicate rõ ràng.

3. Retry, backoff và DLQ

Phân biệt transient failure với permanent failure. Test số attempt, backoff, header/metadata, ordering và thời điểm chuyển DLQ. Dùng scheduler cấu hình cho test thay vì chờ delay production thật.

4. Contract testing

Consumer-driven hoặc schema compatibility test phát hiện breaking change trước deployment. Contract trả lời liệu consumer và provider có đồng ý về request/response semantics hay không; nó không chứng minh auth, network policy, data migration hoặc business journey hoàn chỉnh.

Version field, optionality, default, enum evolution và unknown field phải có policy rõ. Contract test nên chạy như quality gate trước khi publish schema/API mới.

Contract tests answer whether services agree on request and response semantics. They reduce the need for broad cross-service environments, but they do not prove that infrastructure, deployment configuration, and the complete business journey work together.

5. E2E có chọn lọc

Chỉ giữ E2E cho critical journey xuyên deployment. Dùng API/data setup thay UI khi có thể, selector ổn định, tenant/data unique và artifact đầy đủ khi fail.

6. Failure modes

7. Checklist tự đánh giá

Nguồn tham khảo