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がスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
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 aWithDeadlinecontext. Trust<-ctx.Done(), notDeadline().SpecContextsatisfiescontext.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. |
SpecTimeoutcan only be more lenient than a node'sNodeTimeout— it caps the sum of all nodes (BeforeEach+It+AfterEach); a per-nodeNodeTimeoutinside it is always more stringent.- Cleanup still runs after a timeout. When
SpecTimeoutfires, Ginkgo cancels the current node, runs anyDeferOnFailurecallbacks (not bound by the expiredSpecTimeout), then runsAfterEach/AfterAll/DeferCleanup(each under its ownNodeTimeout/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/AddReportEntryand pollute a later spec. Always make blocking code respond toctx.Done(). DeferCleanup/DeferOnFailure/DescribeTableentries are interruptible too — give the cleanup/entry func actxfirst arg; don't capture and reuse the parent node'sctx(it's already cancelled by cleanup time).
Interrupting the whole suite
ginkgo --timeout=DURATION— suite-wide budget across all suites (default1h).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, sendSIGINFO/SIGUSR1for 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
コメント
- …
このリポジトリの他のスキル
ci
9.1kエージェントが、GinkgoのテストをCIで確実に動かせるよう設定を支援します。保留やフォーカスの取り残し、不安定なテストを見つける推奨フラグも含みます。
debugging-failures
9.1kエージェントが、要約の一行を読み、JSONレポートから失敗したテストとメッセージを取り出して、Ginkgoのテストが失敗した理由を見つけられるよう支援します。
decorators
9.1kエージェントが、Serial、Ordered、Label、Focus、タイムアウトなどのGinkgoのテストのデコレーターを、それぞれの働きを説明しながら正しく使えるよう支援します。
filtering
9.1kエージェントが、保留、スキップ、フォーカス、ラベル、バージョン条件、ファイルのフィルターを正しい優先順位で使い、Ginkgoのテストの一部だけを実行できるよう支援します。
似ているスキル
test-driven-development
311k機能や修正を作る前に、まず失敗するテストを書き、それが失敗するのを確認してから、合格させる最小のコードを書くようエージェントに教えます。
verification-before-completion
311kタスク、修正、ビルドが完了した、または合格したと主張する前に、実際のチェックを実行し、その出力を読むようエージェントに教えます。
migrate-to-shoehorn
310kテストファイル内の危険な型キャストを、shoehornライブラリに置き換え、テストが部分的なデータを使いつつ型チェックを通るようにします。