skip to content

questions

3

When many automated cases send email to one shared test mailbox, how does a case find the email it triggered?

level: juniorimportance: must knowfreq 42%

answer

  1. The mailbox is shared, so filtering matters
  2. The case must own its match key
  3. Generate it before triggering the action
  4. It can ride the subject or a header
  5. Exactly one match, or fail loudly

basics

~20 s

Generate a unique value inside the case and get it into the outgoing email — a subject-line tag or a custom header field — then search the shared mailbox for that exact value rather than for any recent message.

solid answer

~50 s

Give every case execution its own **correlation token**: a random value generated before the action is triggered, which the product then carries into the outgoing email. Two carriers are common — a tag appended to the subject line, or a custom header field the sending code stamps. The case then searches the shared mailbox for the entry carrying that value and asserts on that entry alone. Assert the count too: exactly one match is expected, zero is a real failure, and two usually means the product sent twice. The token does double duty afterwards, because a failure report that quotes it lets a human find the exact email by hand. Prefer a header when a real recipient might read the message and you can change the sender; prefer a subject tag when you can only vary data the product echoes back.

code

pseudocode · 10 lines
pseudocode
token = "case-" + random_hex(8)

# the case supplies a value the product will render into the email
create_account(display_name = token, address = SHARED_TEAM_ADDRESS)

hits = mailbox.find(recipient = SHARED_TEAM_ADDRESS,
                    carries = token)

assert count(hits) == 1, "expected one email carrying " + token
assert hits[0].subject contains "Welcome"

go deeper

for a junior

Be ready to say in one sentence how a case finds its own email when the recipient is shared: the case generates a unique value first, gets the product to carry it into the message, and then searches for that value.

for a middle

Explain the tradeoff between carrying the value in the subject line and carrying it in a header field, and say what you do when the product offers no settable field that the message template renders.

for a senior

Show that you treat the carrier as a contract with the template owners, assert the match count instead of taking the first hit, and put the value into failure output so a human can find the email by hand.

for a principal

Own the decision to make correlation a platform capability rather than each suite's private trick: one identifier stamped from the triggering request, visible in the sending side's logs and in the delivered message.

## Why a shared recipient needs a match key When every automated case sends its notifications to the same long-lived team address, that mailbox is a **shared stream**. Your case's email is one entry among many: entries produced by other cases in your own run, by other runs happening at the same time, by whatever background jobs the environment is running, and by real people who happen to use the address. Nothing in the mailbox records which case caused which email. Position does not encode it, arrival time does not encode it, and the sender address does not encode it either — every case that exercises the same feature sends from the same place. So the case has to bring its own answer. It generates a **correlation token**: a value it invents before it triggers the action, which the product then carries into the message, and which the case later uses to select exactly one entry out of the stream. It is the same idea as a correlation identifier stitching one request across services, applied outward to a message the product emits. Three properties make a token usable: 1. **Unique per execution, not per case.** Derive it from a random value, or from a run identifier plus a per-case counter. A token derived from the case's name collides the moment that case runs twice, or runs on two parallel workers at once. 2. **Known before the trigger.** A value the product invents — an internal record identifier, a delivery reference — is useless for finding the message unless the case learns it from the response it already received. If you can only discover it by reading the mailbox, it is not a match key. 3. **Actually carried into something searchable.** A token placed in a field the message template never renders is a token you cannot search for. ## Where the token rides There are three practical carriers, and the choice is usually forced by how much of the sending side you are able to change. | Carrier | How the token gets there | Choose it when | What it costs | |---|---|---|---| | **Subject-line tag** | the case supplies data the product echoes into the rendered subject | you cannot change the sending code | it appears in text a real recipient reads, and a template redesign can drop it | | **Custom header field** | the sending code stamps an identifier taken from the triggering request | you own the sender and want the copy clean | needs a product change, and intermediaries may not preserve unknown fields | | **A reference the product already generates** | the case reads it from the response to the triggering call | the message already quotes an order or registration reference | you depend on the product continuing to include it | ## Getting the token in without touching the sender Most products leave exactly one seam open to a case that cannot change code: **data the case supplies flows through into the rendered message**. Register an account whose display name is `case-7f3a12` and the greeting line carries it. Create an order with a reference the case chose and the confirmation subject quotes it. Pick a field that is settable by the case, rendered into the message, and validated loosely enough that a random token survives it. That seam is a **contract with the message template**, even though nobody wrote it down. When the template owners redesign and drop the greeting, every messaging case fails at once with "no matching message", which is the least informative failure in the suite. The cheap defence is one small check whose only job is to assert that a token supplied on the way in comes back out in the rendered message. When that breaks, one failure names the cause instead of forty naming the symptom. ## Matching, and only matching The search is deliberately narrow: entries whose subject or header carries the token. Then assert on the size of the match set. - **Exactly one match** — proceed and assert on it. - **Zero matches** — fail, and put the token in the failure text so a human can search the mail store by hand. - **More than one match** — fail as well. Two matches usually means the product sent twice, which is a real defect the search has just detected for free. Quietly taking the first entry hides it. The temptation, when zero comes back, is to widen the filter: drop the token, keep the sender, take the newest. That converts a clear failure into a case that asserts on whatever is lying around, and it is how a suite starts reading other runs' email. ## The token is also the diagnostic A correlation token pays for itself twice. During the run it selects the message. After the run it is the thread that ties the pieces together. If the same value appears in the case's failure output, in the sending side's logs and in the delivered message, then "the email never arrived" becomes answerable: either the send was never attempted, or it was attempted and did not land, and the token tells you which. Teams that stamp an identifier from the triggering request all the way through get that for every message the product sends.

  • What should happen when the search for the case's token returns two emails instead of one?
    Fail the case and report both. Two matches usually means the product sent twice, which is a genuine duplicate-send defect the search has just found for free; the other explanation is that the value was reused across runs, which is also worth knowing. Silently taking the first entry hides both and makes the result depend on ordering. Assert the count, and put the value and the count in the failure text.
  • Why derive the token from a random value rather than from the case's own name?
    A name identifies the case, not the execution. The same case runs again tomorrow, runs on two parallel workers at once, and runs against a mailbox that still holds yesterday's messages, so a name-derived value matches several entries and the case picks one arbitrarily. A random value per execution, optionally prefixed with the run identifier for readability, keeps every match unambiguous.
  • The message template is owned by another team and a redesign drops your subject tag. How do you find out before the whole suite turns red?
    Treat the carrier as a contract and pin it with one small, fast check whose only job is to assert that a value supplied on the way in comes back out in the rendered message. Run it close to the template rather than at the end of a long end-to-end case. When it breaks you get a single failure naming the cause, instead of forty cases reporting 'no matching message' with no clue why.

It is the same trick as labelling a dish you leave in a shared fridge: you do not reach for the newest container, you reach for the one with your name on it.

saying these in an interview costs you the question

  • Takes the most recent entry and assumes it is theirs
  • Derives the match value from the case name, so reruns collide
  • Widens the filter to any recent email when the value is not found
  • Assumes a single match without ever asserting the count
  • Chooses a field the message template never renders
  • Adds the value only afterwards, as a debugging afterthought
open as a page

Two automated suites share one test recipient account and run at once — what breaks first?

level: seniorimportance: should knowfreq 32%

basics

~10 s

The account's single-writer state breaks before the mailbox does: an outstanding single-use code, a verification flag or a notification preference that can hold only one value. The second run silently overwrites the first run's.

open as a page

Why does a case that asserts on the newest email in a shared test mailbox pass alone and fail in parallel?

level: middleimportance: nice to knowfreq 18%

basics

~20 s

Recency is not identity. In a shared mailbox the newest entry belongs to whichever case triggered a send most recently, so the assertion reads a neighbouring run's email — or a second email produced by your own action.

open as a page