I wrote a test that spammed a real mailbox until the email service told it to stop
Recently a test suite I wrote sent about two hundred real password reset emails to a colleague’s inbox, and part of a third batch after that. It also used up our staging environment’s hourly email quota twice. While the quota was gone, staging couldn’t send its own sign-up, verification or password reset emails. Nobody stopped the run until a person noticed the mail piling up. This post covers what I got wrong and what I’m changing.
What I was building
We were moving our account emails (sign-up, verification, password reset) onto a managed cloud email service. The test plan was sound in principle. The suite plays back recorded HTTP exchanges with the service, and those recordings are captured by running the same tests in a “live” mode against a real resource. The tests check that:
- a successful send is polled until it finishes,
- a refusal counts as a permanent failure that retrying won’t fix,
- a throttle (HTTP 429) counts as a temporary failure, and the outbox retries it.
The design said every recording had to come from the test itself running against the real service. It didn’t say how to get a real service to throttle you, or what that would cost.
What I did wrong
I filled that gap in the most literal way: keep sending real messages until the service refuses one.
/// The most sends a test makes to meet the resource's throttle.
const MOST_SENDS_TO_THROTTLE: usize = 120;
async fn send_until_throttled(mailer, message) -> ... {
for _ in 0..MOST_SENDS_TO_THROTTLE {
match mailer.send(MessageId::new(), message).await {
Ok(()) => {}
Err(throttle @ MailError::Throttled { .. }) => return Ok(Some(throttle)),
Err(failure) => return Err(failure),
}
}
Ok(None)
}
Two tests used this. The second one also waited for the throttle to lift and then retried, every 30 seconds, up to ten times, so it could prove the outbox eventually delivers. Every message went to one real email address.
I told myself the cap of 120 was a safeguard. It wasn’t. It was a budget for spam.
Two things made it worse:
- I documented the problem as if it were a feature. The README said the throttle tests “may take a burst of messages to the test recipient.” I wrote that sentence and didn’t see it as a warning.
- I never ran it against the real service. My first recordings came from a local stand-in. I designed the burst without ever seeing what it would do. That same stand-in was the right tool for these two tests all along.
When the recordings were later captured for real, the operator knew the service’s limits were roughly 30 messages a minute and 100 an hour. They said the throttle tests might not fit within those limits and that we should change the design if they didn’t. The run went ahead anyway. It hit the hourly quota, and instead of stopping, it scheduled retries to wait for the quota to reset and then sent another burst. The status updates described this as “waiting on the hourly quota,” which sounded routine, and nobody stopped it. It ran for about two hours until someone killed it by hand.
What it cost
- About 200+ real emails in a person’s inbox, each a fake password reset.
- The staging email quota used up twice, which broke staging’s real account emails during those windows.
- Roughly two hours spent waiting on quota windows for a recording that never needed to come from the live service.
- A suite that would do all of this again every time someone re-recorded it, including the two tests that are actually useful.
What I should have done
Design: Some failures a service only gives you under abuse. When the limits are fixed on the provider’s side and can’t be lowered for testing, the throttle recording should be built from the provider’s documented throttle response (status code, Retry-After header, error body) and labelled as synthetic. Only the cheap cases, one successful send and one refusal, should be captured live. “Every recording comes from the real service” can’t apply to every recording.
Implementation:
- A test should never loop on a real, metered, side-effecting operation until the provider says no. The size of the cap doesn’t change that.
- A test that sends real mail sends the fewest messages that prove its point, one per test, and never sends to a person’s inbox in a loop.
- The README states the cost of a recording run in numbers: messages sent, quota used, time taken. If that number is more than a handful, the design is wrong. Writing the number down doesn’t make it acceptable.
- If a provider’s limits can’t be lowered for tests, the throttle path is tested against a synthetic recording or a local stand-in, and the file says so.
How I’ll work from now on
- When reviewing a recording-based suite, I look at every loop that calls an external service and ask who decides when it ends. If the answer is “the provider, when it refuses us,” that’s a bug, whatever the cap.
- Before running a suite live, I read it for side effects: what it sends, to whom, and how many times.
- If a live run’s first result is a used-up quota, I stop the run. I don’t schedule retries around the quota. A quota wait is a reason to stop, not to wait.
- When I hand a run to someone else, I don’t write “change the design if it doesn’t fit the limits.” I say outright: no bursts to a real inbox, and stop at the first quota wait.
The general lesson
This pattern isn’t specific to email. It shows up anywhere a test records a provider’s rate limit or quota response by actually triggering it: send until you get a 429, call until the budget runs out, retry every hour until the window resets. The rule I’m adopting: any test loop that ends only when a provider refuses a real, billed or delivered operation gets rewritten to use a synthetic or stand-in recording before it’s merged. Any recording run that costs more than a few units of a provider’s resource should be rejected outright.
I’m sorry to the colleague whose inbox took the hit, and to anyone who tried to use staging that night. The throttle tests are being rewritten to use synthetic recordings before anyone re-records this suite.