Test Hang Watchdog logo

Test Hang Watchdog

Community
niki914
test-hang-watchdog

Prevent and diagnose hanging or long-running Gradle unit tests (> 30s) in Android/Kotlin coroutines codebases. Use when running tests, when a test execution takes unexpectedly long, when background test tasks appear frozen, or when taking thread dumps (jstack) to unblock coroutine deadlocks.

Overview

Publisherniki914
Repositoryzafiro
Skill nametest-hang-watchdog
Stars
305
Forks
50
Bundled files
Instructions only
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by niki914 on GitHub. Read the source before you install it.

Installation

Install the Test Hang Watchdog AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/niki914/zafiro/tree/main/.agents/skills/test-hang-watchdog
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/niki914/zafiro.git /tmp/zafiro
mkdir -p .claude/skills
cp -r /tmp/zafiro/.agents/skills/test-hang-watchdog .claude/skills/test-hang-watchdog
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Test Hang Watchdog in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Test Hang Watchdog on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Test Hang Watchdog is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Test Hang Watchdog

在本项目中,针对特定类的单元测试(testDebugUnitTest --tests "...")正常执行时间为 2 ~ 8 秒;全模块单测执行时间通常在 15 秒以内。

任何单测执行超过 30 秒,100% 存在死锁、挂起或桩缺失。 无限等待是零拦截力且严重浪费人机协同时间的行为。


1. 运行看门狗协议(主动介入,杜绝傻等)

运行单元测试命令时,必须遵循以下看门狗守则:

  1. 背景任务倒计时:
    • 若 ./gradlew testDebugUnitTest 转入后台任务,严禁无限期挂起等待。
    • 任务运行达到 15 秒 时主动查看一次进度与输出;
    • 任务运行达到 30 秒 且仍在运行,判定为异常挂起,立即触发下文的「现场解剖 SOP」,绝不等待用户提醒。
  2. 测试超时防御:
    • 涉及挂起函数(suspend)、CompletableDeferred、Channel、Flow 收集的用例,尽量在 runTest 中使用 backgroundScope 或以 withTimeout(2_000) 保护,杜绝裸 runBlocking 无界等待。

2. 现场解剖 SOP(30 秒内定位挂死点)

一旦测试任务超过 30 秒未结束,按以下步骤定位并熔断:

Step 1: 定位 Test Worker 进程

bash
jps -v | grep GradleWorkerMain

(输出第一列即为运行当前测试用例的 JVM Worker 进程 PID,例如 82065)

Step 2: 抓取线程快照

bash
jstack <worker-pid> | head -n 45

Step 3: 读取案发现场调用栈

在 jstack 输出的头部直接寻找 "Test worker" 线程(通常为 #3),查看第 20 ~ 30 行栈帧:

text
"Test worker" #3 prio=5 os_prio=31 ... waiting on condition
   at jdk.internal.misc.Unsafe.park(Native Method)
   - parking to wait for <0x...> (a kotlinx.coroutines.BlockingCoroutine)
   ...
   at com.niki914.zafiro.business.agent.AgentManagerImplTest.switchTo_reusesExistingActiveAgent(AgentManagerImplTest.kt:50)
  • 定位被测用例:栈顶清晰指示出是哪个测试类、哪个 @Test 方法。
  • 定位挂起原语:
    • LockSupport.parkNanos / CompletableDeferred.await:协程 deferred 从未被 complete;
    • awaitSettingsGateway():缺少依赖注入/全局桩环境;
    • runBlockingImpl:主线程等待子协程终态,子协程未退出。

Step 4: 果断熔断

立即终止挂起任务与 Worker 进程,避免占用 Gradle Daemon 槽位:

bash
manage_task(Action='kill', TaskId='<task-id>')
kill -9 <worker-pid>

3. 四大典型挂死病灶与速治清单

通过调用栈定位到测试方法与具体行后,对照以下四大病灶对号入座:

病灶 ①:自引用状态死锁(Self-Referential Job State)

  • 现象:测试在等待某组任务全部完成,例如 jobs.values.all { it.isCompleted }。
  • 病理:在协程体执行内部去查询自身的 Job 状态——当前协程体未退出前,自身的 job.isCompleted 恒为 false,形成闭环死锁。
  • 解法:解耦状态。使用独立的并发数据结构(如 ConcurrentHashMap<Key, Result>)显式记录各任务的结果,不要用未退出的 Job 生命周期代替语义结果。

病灶 ②:运行时测试桩缺失(Missing Test Doubles / Gateway)

  • 现象:栈停在 RuntimeEnvironment.awaitSettingsGateway() 或其他 await* 延迟装配门禁。
  • 病理:生产代码依赖全局环境(如 Application 启动时安装的 Bridge/Gateway),而新写的单元测试 @Before setUp() 中仅 mock 了局部 Store,漏掉了运行时 Gateway,导致协程无限等待 Bridge 安装。
  • 解法:在测试的 @Before 中安装测试专用 Stub(如 StubRuntimeSettingsGateway),并在 @After tearDown() 中显式清理。

病灶 ③:废弃契约反向挂起(Dead Contract Mismatch)

  • 现象:改动业务逻辑后,旧单测突然挂死在 agent.doSomething()。
  • 病理:业务契约已经重构(例如:后台无 Approver 时不再立即判 Deny,而是挂起等待前台切回)。而旧单测依然在验证「立即返回 Deny」这一废弃契约,导致其在 runBlocking 中合法挂起却等不到返回。
  • 解法:依照 test-triage 准则,被测契约改变后,旧测试必须重写或直接删除,严禁留着死契约。若需测试挂起等待行为,必须在独立的协程中延时提供触发事件或验证挂起状态后取消。

病灶 ④:无界流收集泄露(Unbounded Flow Collector)

  • 现象:测试执行完断言后依然不退出,等待协程清理。
  • 病理:测试中对冷/热 Flow 启动了无限监听(flow.collect { }),而未绑定测试生命周期或未在 finally 中 cancel。
  • 解法:
    • 使用 kotlinx-coroutines-test 的 runTest 并在 backgroundScope.launch { ... } 中收集流;
    • 或者使用 take(1) / first() 获取单次发射。

4. 验证与闭环标准

修复代码后,按照以下阶梯验证:

  1. 单点隔离验证:仅运行此前卡住的单一测试:
    bash
    ./gradlew :module:testDebugUnitTest --tests "com.package.TargetTest.targetMethod"
    目标耗时:5 秒以内通过。
  2. 类级别回归:
    bash
    ./gradlew :module:testDebugUnitTest --tests "com.package.TargetTest"
  3. 全工程验证:
    bash
    ./gradlew testDebugUnitTest

Frequently asked questions

What does the Test Hang Watchdog AI skill do?

Prevent and diagnose hanging or long-running Gradle unit tests (> 30s) in Android/Kotlin coroutines codebases. Use when running tests, when a test execution takes unexpectedly long, when background test tasks appear frozen, or when taking thread dumps (jstack) to unblock coroutine deadlocks.

Why use Test Hang Watchdog on TypingMind?

Because you install it once and use it with any model. Test Hang Watchdog is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Test Hang Watchdog in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/niki914/zafiro/tree/main/.agents/skills/test-hang-watchdog. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Test Hang Watchdog?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Test Hang Watchdog?

As many as you like. As long as a model supports skills, you can use Test Hang Watchdog with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Test Hang Watchdog AI skill free?

Yes. It is published on GitHub by niki914 under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇