본문으로 건너뛰기
Skillpedia
테스트9.1k · #12309

timeouts-and-async

하는 일

이 스킬은 Ginkgo 테스트가 너무 오래 실행되는 것을 막는 방법과, 백그라운드나 지연된 작업을 테스트하는 방법을 설명합니다. Go 테스트가 멈추거나 일관되지 않게 동작하는 개발자를 위한 것입니다.

사용하는 경우

  • ✓Ginkgo 테스트가 멈추거나 제한 시간을 넘길 때.
  • ✓몇 초 뒤에 나타나는 결과를 확인해야 할 때.
  • ✓테스트 코드가 고루틴에서 실행되고 실패가 보고되지 않을 때.

스킬 링크

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

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

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.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“제 Go 테스트가 백그라운드 워커와 통신할 때 영원히 멈춥니다. 몇 초 뒤에 멈추게 하려면 어떻게 해야 하나요?”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

이 스킬은 테스트에 제한 시간을 설정하고, 테스트 코드에 취소 신호를 전달하며, 결과가 나올 때까지 계속 다시 확인하는 검사로 기다리는 방법을 보여 줍니다.

기술 내용 (원본 SKILL.md)

작성자의 영어 원문을 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

자주 묻는 질문

테스트 하나에 제한 시간을 어떻게 정하나요?

이 스킬은 테스트에 시간 제한 옵션을 추가해서 정해진 시간 뒤에 멈추게 하는 방법을 보여 줍니다.

여기서 취소는 무엇을 뜻하나요?

테스트가 일찍 멈추도록 신호를 받는다는 뜻이며, 그러면 호출하는 코드가 깔끔하게 끝날 수 있습니다.

Gomega의 Eventually는 무엇인가요?

결과가 참이 될 때까지 일정 시간 계속 시도하는 검사입니다.

상세 정보

작성자의 원본 설명

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

인기도

GitHub 스타
9.1k
순위
#12309
라이선스
MIT
업데이트
2026. 10. 9.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬