Skip to content
Skillpedia
Testing9.1k Β· #12309

timeouts-and-async

What it does

This skill explains how to stop Ginkgo tests from running too long and how to test background or delayed work. It is for developers whose Go tests hang or behave inconsistently.

When to use it

  • βœ“When a Ginkgo test hangs or runs past its time limit.
  • βœ“When you need to check a result that takes some time to appear.
  • βœ“When test code runs in goroutines and a failure is not reported.

Skill link

https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/timeouts-and-async/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/timeouts-and-async/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute β†’

Example of what to ask your AI

β€œMy Go test hangs forever when it talks to a background worker, how do I make it stop after a few seconds?”

5 new skills a week. 1 minute to read.

How it works

It shows how to set time limits on tests, pass a cancel signal into the test code, and wait for results with checks that keep retrying.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

Timeouts, interruptible nodes, and async testing

Builds on the failure mental model in ginkgo:overview (failure is a panic Ginkgo recovers; a goroutine that fails needs defer GinkgoRecover()). Docs: https://onsi.github.io/ginkgo/#spec-timeouts-and-interruptible-nodes and https://onsi.github.io/ginkgo/#patterns-for-asynchronous-testing.

Interruptible nodes: take a context, honor cancellation

A setup or subject node becomes interruptible simply by accepting a SpecContext (or plain context.Context) β€” Ginkgo supplies one automatically:

It("likes to sleep in", func(ctx context.Context) {
  select {
  case <-ctx.Done(): return        // honor cancellation and exit promptly
  case <-time.After(time.Hour): ...
  }
}, NodeTimeout(time.Second))

On timeout or interrupt, Ginkgo cancels the context to tell the node to stop. Pass ctx down into every blocking call (libraryClient.SaveBook(ctx, book), exec.CommandContext(ctx, ...)) so the cancellation actually propagates. Only setup/subject nodes are interruptible β€” container nodes are not (they run at construction time).

  • ctx.Deadline() does NOT report the node's deadline. Ginkgo manages cancellation timing itself (to snapshot a progress report first), so it does not use a WithDeadline context. Trust <-ctx.Done(), not Deadline().
  • SpecContext satisfies context.Context; you may wrap it (context.WithValue) and Ginkgo still cancels the result on time.

The timeout decorators (full reference β†’ ginkgo:decorators)

Decorator Scope
NodeTimeout(d) Deadline for one interruptible node.
SpecTimeout(d) Deadline for the whole spec lifecycle β€” It only.
GracePeriod(d) How long Ginkgo waits after cancelling before leaking the node.
  • SpecTimeout can only be more lenient than a node's NodeTimeout β€” it caps the sum of all nodes (BeforeEach+It+AfterEach); a per-node NodeTimeout inside it is always more stringent.
  • Cleanup still runs after a timeout. When SpecTimeout fires, Ginkgo cancels the current node, runs any DeferOnFailure callbacks (not bound by the expired SpecTimeout), then runs AfterEach/AfterAll/DeferCleanup (each under its own NodeTimeout/grace). The timeout is a "mark failed" threshold, not a hard kill.
  • A node that ignores cancellation is "leaked," not killed. After the grace period Ginkgo gives up waiting and moves on. Leaking is deliberately preferred over hanging forever β€” but a leaked goroutine keeps running and can call Fail/AddReportEntry and pollute a later spec. Always make blocking code respond to ctx.Done().
  • DeferCleanup/DeferOnFailure/DescribeTable entries are interruptible too β€” give the cleanup/entry func a ctx first arg; don't capture and reuse the parent node's ctx (it's already cancelled by cleanup time).

Interrupting the whole suite

  • ginkgo --timeout=DURATION β€” suite-wide budget across all suites (default 1h).
  • Abort("reason") β€” end the suite immediately from within a spec (programmatic interrupt).
  • SIGINT/SIGTERM (^C) β€” interrupt: cancel the current interruptible node, run its cleanup + reporting nodes, skip the rest, exit failed. Escalation: a 2nd interrupt skips cleanup (still runs reporting); a 3rd bails immediately. To inspect a suite without stopping it, send SIGINFO/SIGUSR1 for a progress report β†’ ginkgo:debugging-failures.

Async assertions: Eventually / Consistently

Use Gomega's Eventually (polls until the matcher passes or it times out) and Consistently (polls and requires the matcher to hold the whole interval β€” the way to assert something doesn't happen). Three input shapes: bare values (channels, gbytes), functions returning (value[, error]), and functions taking a Gomega.

It("publishes a book", func(ctx SpecContext) {
  buffer := gbytes.NewBuffer()
  c := publisher.Publish(ctx, book, buffer)              // pass ctx so it cancels cleanly
  Eventually(ctx, buffer).Should(gbytes.Say(`Publish complete!`))
  var result publisher.PublishResult
  Eventually(ctx, c).WithTimeout(time.Second).Should(Receive(&result)) // poll, don't <-c
}, SpecTimeout(time.Second*30))

Propagate the spec deadline into the poll with .WithContext(ctx) or the positional Eventually(ctx, ...) β€” now a node timeout/interrupt makes the Eventually exit immediately instead of running its own clock. Gomega also **auto-injec

FAQ

How do I set a time limit for one test?

The skill shows how to add a timeout option to a test so it stops after a set time.

What does cancellation mean here?

It means the test is told to stop early, so the code it calls can end cleanly.

What is Eventually in Gomega?

It is a check that keeps trying for a while until a result becomes true.

Details

Original description by the author

Make Ginkgo specs interruptible and test asynchronous behavior β€” SpecContext/context.Context cancellable nodes, NodeTimeout/SpecTimeout/GracePeriod, the --timeout flag, Abort and SIGINT behavior, Gomega Eventually/Consistently (the func(g Gomega) form, .WithContext), and the defer GinkgoRecover() rule for goroutines. Use when a spec hangs or times out, polls for eventual consistency, tests channels/streams/processes, launches goroutines, or needs a deadline.

repo: onsi/ginkgo

Popularity

GitHub stars
9.1k
Ranking position
#12309
License
MIT
Updated
10/9/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills