0Pricing
Microservices Communication Patterns (Saga, Circuit Breaker) · บทเรียน

การทดสอบซากาแบบควบคุมจากศูนย์กลาง

เรียนรู้การทดสอบซากาที่อาศัยการควบคุมจากศูนย์กลาง ครอบคลุมการทดสอบหน่วยสำหรับเส้นทางชดเชย การทดสอบการผสานรวมด้วยความล้มเหลวของบริการจำลอง และการตรวจสอบตั้งแต่ต้นจนจบว่าซากาเสร็จสมบูรณ์

การทดสอบซากาแบบควบคุมจากศูนย์กลาง เป็นบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Microservices Communication Patterns (Saga, Circuit Breaker) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Test Sagas?

An orchestration saga coordinates multiple services through a central orchestrator. A bug in the orchestrator can leave the system in an inconsistent state across services.

Testing sagas is harder than testing a single function because you must verify both the happy path and every compensation path.

  • Did each step execute in order?
  • Did failures trigger the right compensations?
  • Is the final state consistent?

The Testing Pyramid for Sagas

Apply the classic testing pyramid:

  • Unit tests — the orchestrator's decision logic in isolation.
  • Integration tests — the orchestrator talking to real or stubbed services.
  • End-to-end tests — the full saga across all services.

Most tests should be unit tests because they are fast and deterministic.

Unit Testing the Orchestrator

The orchestrator is often a state machine. You can unit test its transitions by feeding events and asserting the next command.

def next_command(state, event):
    if state == 'STARTED' and event == 'PAYMENT_OK':
        return 'RESERVE_STOCK'
    if state == 'STARTED' and event == 'PAYMENT_FAILED':
        return 'ABORT'
    return 'NOOP'

print(next_command('STARTED', 'PAYMENT_OK'))
print(next_command('STARTED', 'PAYMENT_FAILED'))

Mocking Service Calls

In unit tests, replace real service calls with mocks or stubs. This lets you control exactly what each step returns.

For example, force the payment service to return a failure so you can assert the orchestrator issues a compensation command.

class FakePayment:
    def __init__(self, ok):
        self.ok = ok
    def charge(self, amount):
        return 'PAYMENT_OK' if self.ok else 'PAYMENT_FAILED'

print(FakePayment(False).charge(100))

Testing Compensation Paths

The riskiest part of any saga is compensation. For each forward step, write a test that:

  • Executes steps up to a chosen point.
  • Forces a failure.
  • Asserts that every prior step was compensated in reverse order.
executed = ['reserve_stock', 'charge_card']
compensations = list(reversed(executed))
print('Compensate in order:', compensations)

Integration Tests with Test Containers

Integration tests run the orchestrator against real dependencies in disposable containers (databases, message brokers).

Tools like Testcontainers spin up a real broker (e.g. Kafka or RabbitMQ) so message flow is exercised exactly as in production.

Simulating Service Failures

To test resilience, inject failures: make a downstream service return errors, time out, or crash mid-saga.

  • HTTP 500 from a step
  • Timeout (no response)
  • Duplicate event delivery

Each scenario should leave the system consistent.

Testing Timeouts

Orchestrators often set a timeout for each step. If a step does not respond, the saga should trigger compensation.

import time

def wait_for_step(timeout, elapsed):
    if elapsed > timeout:
        return 'TIMEOUT -> COMPENSATE'
    return 'OK'

print(wait_for_step(5, 7))
print(wait_for_step(5, 3))

Asserting Final State Consistency

The most important assertion is that the system ends in a valid state. After a failed saga, no money should be charged and no stock reserved.

Query each service and verify the invariants hold across them.

Replaying Events in Tests

Because messages can be delivered more than once, replay the same event twice in your tests and assert the saga state is unchanged the second time. This verifies idempotency at the orchestrator level.

processed = set()

def handle(event_id):
    if event_id in processed:
        return 'IGNORED (duplicate)'
    processed.add(event_id)
    return 'PROCESSED'

print(handle('e1'))
print(handle('e1'))

Observability in Tests

Add assertions on emitted logs, metrics, and trace spans. A well-tested saga should produce a clear audit trail so that, when a real failure happens in production, you can reconstruct exactly what occurred.

Quick Check

Which test type is the best place to verify the orchestrator's transition logic quickly and deterministically?

Recap

You learned how to test orchestrated sagas:

  • Use the testing pyramid: many unit tests, fewer integration and E2E tests.
  • Mock services to force happy and failure paths.
  • Always test compensation order and final state consistency.
  • Replay events to confirm idempotency, and inject timeouts and failures.

Thorough saga testing is what keeps distributed transactions trustworthy.

คำถามที่พบบ่อย

บทเรียน “การทดสอบซากาแบบควบคุมจากศูนย์กลาง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การทดสอบซากาแบบควบคุมจากศูนย์กลาง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบซากาแบบควบคุมจากศูนย์กลาง”

เรียนรู้การทดสอบซากาที่อาศัยการควบคุมจากศูนย์กลาง ครอบคลุมการทดสอบหน่วยสำหรับเส้นทางชดเชย การทดสอบการผสานรวมด้วยความล้มเหลวของบริการจำลอง และการตรวจสอบตั้งแต่ต้นจนจบว่าซากาเสร็จสมบูรณ์ คุณปฏิบัติ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Microservices Communication Patterns (Saga, Circuit Breaker) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Microservices Communication Patterns (Saga, Circuit Breaker) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การทดสอบซากาแบบควบคุมจากศูนย์กลาง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) นี้ได้ไหม

ได้ บทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การออกแบบตัวประสานงาน Saga
  2. เครื่องจักรสถานะสำหรับการประสานงาน
  3. การนำไปใช้ด้วยเครื่องมือกระบวนงาน
  4. การทดสอบซากาแบบควบคุมจากศูนย์กลาง
← กลับไปที่ Microservices Communication Patterns (Saga, Circuit Breaker)