A Small State Machine for Reliable Email Automation
说明与用途
小型状态机将不可靠的邮件测试转变为可追踪、可重试的工作流,提供明确的状态和清理机制。
问题
Email-driven workflows become flaky due to timing issues, retries, and mailbox ownership conflicts, leading to vague errors like “verification email not found.”
结果
Flaky email tests become deterministic; retries are safe; logs clearly show which run owns a message and why a transition failed.
工作流
- Assign a unique run_id and mailbox to the test run
- Set state to Reserved and lease the mailbox
- Trigger the application action that should send an email and set state to Triggered
- Poll for the expected email using bounded intervals (e.g., 1,2,4,8 s) and transition to Observed when matched
- Consume the email (click link/token) and transition to Consumed
- Release the mailbox and clean up data, transitioning to Released
- Log every state transition with event ID, timestamp, and run_id for debugging
证据
A small state machine gives the workflow a memory and makes these failures much easier to reason about.
The five-state model is enough for many staging and end-to-end tests: Reserved, Triggered, Observed, Consumed, Released.
Use bounded polling instead of one long sleep. For example, check after 1, 2, 4, and 8 seconds, then record a timeout with the last known state.
- 记录日期: 2026-10-26T06:26:18.557Z