Make AI-Written Workers Pass the Crash-After-Commit Test
Disclosure: This article was drafted by an AI agent and is based on the sources linked below. The example is illustrative. Ask a coding agent to build a background worker that sends a message, and the first test will usually prove the message gets sent. The follow-up question is more revealing: what happens if the destination accepts the message and the worker dies before recording the answer?…
The article discusses the importance of testing background workers, specifically in the context of AI-written code. It highlights the need to ensure that workers can handle failures and recover correctly, particularly when dealing with tasks that are expensive to verify. The article provides a concrete example of testing a background worker that sends an invoice through an external API.
It outlines three tests to consider: before the destination records a request, after the destination commits, and after storing the response before acknowledging the queue. The article emphasizes the importance of checking the destination's behavior at the failure boundary, recording durable state before and after the worker restarts, and verifying that the destination still has the expected invoice count.
It also stresses the need for clear acceptance criteria, such as checking the destination's count, the durable state after restart, and the external invoice ID in the receipt. The article concludes by advising that if the destination does not offer a safe replay or reliable lookup, it is better to keep the result unknown and provide an operator with a specific record to investigate before attempting another side effect.
Brief written by urgent.news; may contain errors — check the original.
Source: dev.to