test(inotify): 修复测试清理竞态与防抖用例的时钟敏感

- 清理顺序改为「先停 Run 再 Close watcher」:此前 defer Close 会先关掉唤醒管道,
  Stop 的唤醒丢失,Run 卡在 poll 直到超时(CI 偶发 "Run 未能退出")
- Watcher.Stop/Close 做成幂等且并发安全,Close 前先唤醒 poll
- 防抖用例改用更大窗口(800ms)+ 无间隔连写:CI 上单次写文件耗时波动会让
  连写跨越窗口边界从而触发第 2 次,原断言恰好 1 次因此偶发失败
This commit is contained in:
YuleBest committed 2026-09-12 02:55:34 +08:00
1 parent 04bb0c1248
commit f7cf795773
2 files changed
+26 -16

No files matched your search

+17 -5
View File
@@ -155,13 +155,22 @@ func (w *Watcher) Add(path, letters string) error {
return nil
}
// Close 释放资源。
// Close 释放资源。重复调用安全。
func (w *Watcher) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
// 先唤醒 Run:直接关闭 fd 会让阻塞中的 poll 拿不到退出信号
if w.wakeW >= 0 {
_, _ = unix.Write(w.wakeW, []byte{1})
}
if w.wakeR >= 0 {
_ = unix.Close(w.wakeR)
w.wakeR = -1
}
if w.wakeW >= 0 {
_ = unix.Close(w.wakeW)
w.wakeR, w.wakeW = -1, -1
w.wakeW = -1
}
if w.fd >= 0 {
err := unix.Close(w.fd)
w.fd = -1
@@ -170,10 +179,13 @@ func (w *Watcher) Close() error {
return nil
}
// wake 唤醒正在 poll 的 Run。
// wake 唤醒正在 poll 的 Run。并发/重复调用安全。
func (w *Watcher) wake() {
if w.wakeW >= 0 {
_, _ = unix.Write(w.wakeW, []byte{1})
w.mu.Lock()
fd := w.wakeW
w.mu.Unlock()
if fd >= 0 {
_, _ = unix.Write(fd, []byte{1})
}
}
+9 -11
View File
@@ -80,6 +80,8 @@ func startRunner(t *testing.T, w *Watcher, debounce time.Duration, handler func(
_ = w.NewRunner(debounce, nil).Run(handler, stop)
}()
t.Cleanup(func() {
// 必须先让 Run 退出,再关闭 watcher:反过来会先关掉唤醒管道,
// Stop 的唤醒丢失,Run 会卡在 poll 里直到超时(CI 上偶发)
close(stop)
w.Stop()
select {
@@ -87,6 +89,7 @@ func startRunner(t *testing.T, w *Watcher, debounce time.Duration, handler func(
case <-time.After(2 * time.Second):
t.Error("Run 未能退出")
}
w.Close()
})
return stop
}
@@ -113,7 +116,6 @@ func TestWatchModifyDeliversEvent(t *testing.T) {
if err != nil {
t.Skipf("当前环境不支持 inotify: %v", err)
}
defer w.Close()
if err := w.Add(path, "c"); err != nil {
t.Fatal(err)
}
@@ -144,7 +146,6 @@ func TestCloseWriteLetter(t *testing.T) {
if err != nil {
t.Skipf("当前环境不支持 inotify: %v", err)
}
defer w.Close()
if err := w.Add(path, "w"); err != nil {
t.Fatal(err)
}
@@ -181,7 +182,6 @@ func TestCloseNoWriteLetter(t *testing.T) {
if err != nil {
t.Skipf("当前环境不支持 inotify: %v", err)
}
defer w.Close()
if err := w.Add(path, "0"); err != nil {
t.Fatal(err)
}
@@ -216,25 +216,26 @@ func TestDebounceCoalescesBurst(t *testing.T) {
if err != nil {
t.Skipf("当前环境不支持 inotify: %v", err)
}
defer w.Close()
if err := w.Add(path, "c"); err != nil {
t.Fatal(err)
}
c := &collector{}
startRunner(t, w, 200*time.Millisecond, c.add)
// 防抖窗口取大一些:CI 上单次写文件可能耗时数十毫秒,
// 窗口过短会让连写跨越窗口边界,产生第 2 次触发(测试因此偶发失败)
const debounceWindow = 800 * time.Millisecond
startRunner(t, w, debounceWindow, c.add)
for i := 0; i < 5; i++ {
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
}
if !waitFor(t, 2*time.Second, func() bool { return len(c.snapshot()) > 0 }) {
if !waitFor(t, 3*time.Second, func() bool { return len(c.snapshot()) > 0 }) {
t.Fatal("未收到事件")
}
time.Sleep(300 * time.Millisecond) // 等超过一个防抖窗口
time.Sleep(debounceWindow + 400*time.Millisecond) // 等超过一个防抖窗口
if got := len(c.snapshot()); got != 1 {
t.Fatalf("防抖后应只触发 1 次, 实际 %d 次", got)
}
@@ -251,7 +252,6 @@ func TestDrainPreventsRecursion(t *testing.T) {
if err != nil {
t.Skipf("当前环境不支持 inotify: %v", err)
}
defer w.Close()
if err := w.Add(path, "c"); err != nil {
t.Fatal(err)
}
@@ -287,7 +287,6 @@ func TestStopExitsBlockingRun(t *testing.T) {
if err != nil {
t.Skipf("当前环境不支持 inotify: %v", err)
}
defer w.Close()
if err := w.Add(path, "c"); err != nil {
t.Fatal(err)
}
@@ -317,7 +316,6 @@ func TestAddMissingPathFails(t *testing.T) {
if err != nil {
t.Skipf("当前环境不支持 inotify: %v", err)
}
defer w.Close()
if err := w.Add(filepath.Join(t.TempDir(), "不存在"), "c"); err == nil {
t.Fatal("监听不存在的路径应报错")
}