Skip to content

fix: catch cause variants in context traps - #31

Draft
mafredri wants to merge 6 commits into
mafredri/contextfrom
mafredri/context-trap-cause
Draft

mafredri wants to merge 6 commits into
mafredri/contextfrom
mafredri/context-trap-cause

Conversation

@mafredri

@mafredri mafredri commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

mClock.Trap().WithTimeout() did not catch WithTimeoutCause() calls, and Trap().WithDeadline() did not catch WithDeadlineCause(). A test that traps WithTimeout misses the call if the code under test switches to the cause variant, even though the context created is the same apart from the cause.

The WithTimeout and WithDeadline trappers now catch both variants, and Trapper.WithTimeoutCause and Trapper.WithDeadlineCause are removed. They were added in #30 and have not been released. Call.Cause still reports the cause, and it is nil for the non-cause variants. The mock clock's log lines still name the exact function that was called. The README's Contexts section now says which traps to use.

A test can no longer trap only the cause variant; a non-nil Call.Cause shows that a cause variant was called.

Stacked on #30.

When squash merging, please replace the commit body with this description.

🤖 This PR was created with the help of Coder Agents.

Trap().WithTimeout() did not catch WithTimeoutCause() calls, and
Trap().WithDeadline() did not catch WithDeadlineCause(), so a test had
to know which variant the code under test calls to trap it.

The cause variants now match their base function's trap, and Call.Cause
still exposes the cause. This removes the separate WithDeadlineCause and
WithTimeoutCause trappers, which are not on main or in any release.
TestWithTimeout_Trap duplicated the WithTimeout row of the cause variant
test. TestTrap_Contexts now has one row per context function, and each
row also checks that the released context expires on Advance, which only
the WithTimeout case checked before.

The negative control for a call without the trap's tags is gone. It
tested generic tag filtering, which Test_MultipleTraps covers, and did
not control for which function a trap catches.
A user reading the Contexts section had no way to learn that
Trap().WithTimeout() and Trap().WithDeadline() also catch the cause
variants, now that the separate cause trappers are gone.
The note now follows the first Contexts example instead of the cancel
paragraph, where #32 and #33 also add text, so the three branches do not
insert at the same place. It also pairs each trap with the calls it
catches in one sentence.
@mafredri
mafredri force-pushed the mafredri/context-trap-cause branch from aa5e14d to e722958 Compare November 4, 2026 19:17
mafredri added a commit that referenced this pull request Nov 4, 2026
The section now follows the Deadline() subsection, so it no longer
inserts text where #31 and #32 do. The timer and ticker paragraph says
what to do instead of how their cleanups work, and the BaseContext
advice says to set it on an unstarted server before Start, as the probe
that showed srv.Close returning did. On main's timer and ticker code, a
reader of C got no tick or value in a cleanup registered before the
ticker or the fire, and a reader that also selected on t.Context()
exited.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant