mirror of
https://github.com/YuleBest/LuminPro.git
synced 2026-12-04 00:54:42 +08:00
feat(go): 移植 lumipro 为 internal/inotify 包
- 基于 x/sys/unix 原始 inotify,事件字母与 toybox 全量兼容 (含 fsnotify 不支持的 CLOSE_WRITE 'w' / CLOSE_NOWRITE '0') - 防抖 + handler 期间排空积压事件,逻辑与旧 lumipro 一致 - 唤醒管道使 Run 可被立即停止;host 集成测试覆盖事件投递、 防抖合并、排空防递归、Stop 唤醒
This commit is contained in:
1 parent
25c59677e7
commit
c2efc6e7e8
4 files changed
+675
-1
No files matched your search
@@ -1,3 +1,5 @@
|
|||||||
module github.com/YuleBest/LuminPro/go
|
module github.com/YuleBest/LuminPro/go
|
||||||
|
|
||||||
go 1.24
|
go 1.26.0
|
||||||
|
|
||||||
|
require golang.org/x/sys v0.48.0
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||||
|
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||||
@@ -0,0 +1,346 @@
|
|||||||
|
// Package inotify 封装原始 inotify,事件字母与 toybox inotifyd 完全兼容。
|
||||||
|
//
|
||||||
|
// 为什么不用 fsnotify:fsnotify 不暴露 IN_CLOSE_WRITE / IN_CLOSE_NOWRITE,
|
||||||
|
// 而部分机型需要监听 `w` / `0` 事件才能触发亮度提升。
|
||||||
|
package inotify
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Event 是一次监听事件。
|
||||||
|
type Event struct {
|
||||||
|
Letters string // 事件字母串,如 "c"、"0"、"cw"
|
||||||
|
Path string // 触发事件的被监听路径
|
||||||
|
}
|
||||||
|
|
||||||
|
// LettersToMask 把事件字母串转换为 inotify watch mask(与 toybox 兼容)。
|
||||||
|
func LettersToMask(letters string) uint32 {
|
||||||
|
var mask uint32
|
||||||
|
for _, c := range letters {
|
||||||
|
switch c {
|
||||||
|
case 'a':
|
||||||
|
mask |= unix.IN_ACCESS
|
||||||
|
case 'c':
|
||||||
|
mask |= unix.IN_MODIFY
|
||||||
|
case 'e':
|
||||||
|
mask |= unix.IN_ATTRIB
|
||||||
|
case 'w':
|
||||||
|
mask |= unix.IN_CLOSE_WRITE
|
||||||
|
case '0':
|
||||||
|
mask |= unix.IN_CLOSE_NOWRITE
|
||||||
|
case 'r':
|
||||||
|
mask |= unix.IN_OPEN
|
||||||
|
case 'n':
|
||||||
|
mask |= unix.IN_CREATE
|
||||||
|
case 'd':
|
||||||
|
mask |= unix.IN_DELETE
|
||||||
|
case 'D':
|
||||||
|
mask |= unix.IN_DELETE_SELF
|
||||||
|
case 'M':
|
||||||
|
mask |= unix.IN_MOVE_SELF
|
||||||
|
case 'm':
|
||||||
|
mask |= unix.IN_MOVED_TO
|
||||||
|
case 'y':
|
||||||
|
mask |= unix.IN_MOVED_FROM
|
||||||
|
case '*':
|
||||||
|
mask |= unix.IN_ALL_EVENTS
|
||||||
|
case 'u', 'o', 'x':
|
||||||
|
// 内核产生的事件,不设置 watch mask
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mask
|
||||||
|
}
|
||||||
|
|
||||||
|
// MaskToLetters 把事件 mask 转回字母串(顺序与旧 lumipro 一致)。
|
||||||
|
func MaskToLetters(mask uint32) string {
|
||||||
|
var s []byte
|
||||||
|
if mask&unix.IN_ACCESS != 0 {
|
||||||
|
s = append(s, 'a')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_MODIFY != 0 {
|
||||||
|
s = append(s, 'c')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_ATTRIB != 0 {
|
||||||
|
s = append(s, 'e')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_CLOSE_WRITE != 0 {
|
||||||
|
s = append(s, 'w')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_CLOSE_NOWRITE != 0 {
|
||||||
|
s = append(s, '0')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_OPEN != 0 {
|
||||||
|
s = append(s, 'r')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_MOVED_FROM != 0 {
|
||||||
|
s = append(s, 'y')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_MOVED_TO != 0 {
|
||||||
|
s = append(s, 'm')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_CREATE != 0 {
|
||||||
|
s = append(s, 'n')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_DELETE != 0 {
|
||||||
|
s = append(s, 'd')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_DELETE_SELF != 0 {
|
||||||
|
s = append(s, 'D')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_MOVE_SELF != 0 {
|
||||||
|
s = append(s, 'M')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_UNMOUNT != 0 {
|
||||||
|
s = append(s, 'u')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_Q_OVERFLOW != 0 {
|
||||||
|
s = append(s, 'o')
|
||||||
|
}
|
||||||
|
if mask&unix.IN_IGNORED != 0 {
|
||||||
|
s = append(s, 'x')
|
||||||
|
}
|
||||||
|
if len(s) == 0 {
|
||||||
|
return "?"
|
||||||
|
}
|
||||||
|
return string(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Watcher 是一个 inotify 实例。
|
||||||
|
type Watcher struct {
|
||||||
|
fd int
|
||||||
|
wakeR int
|
||||||
|
wakeW int
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
watches map[int32]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// New 创建 watcher。
|
||||||
|
func New() (*Watcher, error) {
|
||||||
|
fd, err := unix.InotifyInit1(unix.IN_NONBLOCK | unix.IN_CLOEXEC)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("inotify_init 失败: %w", err)
|
||||||
|
}
|
||||||
|
// 唤醒管道:让 Run 能在阻塞 poll 期间被 Stop 立即唤醒
|
||||||
|
var pipe [2]int
|
||||||
|
if err := unix.Pipe2(pipe[:], unix.O_NONBLOCK|unix.O_CLOEXEC); err != nil {
|
||||||
|
_ = unix.Close(fd)
|
||||||
|
return nil, fmt.Errorf("创建唤醒管道失败: %w", err)
|
||||||
|
}
|
||||||
|
return &Watcher{
|
||||||
|
fd: fd,
|
||||||
|
wakeR: pipe[0],
|
||||||
|
wakeW: pipe[1],
|
||||||
|
watches: map[int32]string{},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add 监听路径,letters 为事件字母串。
|
||||||
|
func (w *Watcher) Add(path, letters string) error {
|
||||||
|
mask := LettersToMask(letters)
|
||||||
|
wd, err := unix.InotifyAddWatch(w.fd, path, mask)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("监听 %s 失败: %w", path, err)
|
||||||
|
}
|
||||||
|
w.mu.Lock()
|
||||||
|
w.watches[int32(wd)] = path
|
||||||
|
w.mu.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close 释放资源。
|
||||||
|
func (w *Watcher) Close() error {
|
||||||
|
w.mu.Lock()
|
||||||
|
defer w.mu.Unlock()
|
||||||
|
_ = unix.Close(w.wakeR)
|
||||||
|
_ = unix.Close(w.wakeW)
|
||||||
|
w.wakeR, w.wakeW = -1, -1
|
||||||
|
if w.fd >= 0 {
|
||||||
|
err := unix.Close(w.fd)
|
||||||
|
w.fd = -1
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// wake 唤醒正在 poll 的 Run。
|
||||||
|
func (w *Watcher) wake() {
|
||||||
|
if w.wakeW >= 0 {
|
||||||
|
_, _ = unix.Write(w.wakeW, []byte{1})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// poll 等待 inotify fd 可读或唤醒管道触发。timeoutMs 为 -1 表示永久阻塞。
|
||||||
|
func (w *Watcher) poll(timeoutMs int) (ready, woken bool, err error) {
|
||||||
|
fds := []unix.PollFd{
|
||||||
|
{Fd: int32(w.fd), Events: unix.POLLIN},
|
||||||
|
{Fd: int32(w.wakeR), Events: unix.POLLIN},
|
||||||
|
}
|
||||||
|
n, err := unix.Poll(fds, timeoutMs)
|
||||||
|
if err != nil {
|
||||||
|
if err == unix.EINTR {
|
||||||
|
return false, false, nil
|
||||||
|
}
|
||||||
|
return false, false, err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return false, false, nil
|
||||||
|
}
|
||||||
|
if fds[1].Revents&unix.POLLIN != 0 {
|
||||||
|
return false, true, nil
|
||||||
|
}
|
||||||
|
return fds[0].Revents&unix.POLLIN != 0, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadEvents 非阻塞读取当前可用的全部事件;无数据时返回空切片。
|
||||||
|
func (w *Watcher) ReadEvents() ([]Event, error) {
|
||||||
|
var events []Event
|
||||||
|
buf := make([]byte, 4096)
|
||||||
|
evSize := int(unsafe.Sizeof(unix.InotifyEvent{}))
|
||||||
|
|
||||||
|
for {
|
||||||
|
n, err := unix.Read(w.fd, buf)
|
||||||
|
if err != nil {
|
||||||
|
if err == unix.EAGAIN || err == unix.EWOULDBLOCK {
|
||||||
|
return events, nil
|
||||||
|
}
|
||||||
|
if err == unix.EINTR {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return events, err
|
||||||
|
}
|
||||||
|
if n <= 0 {
|
||||||
|
return events, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for offset := 0; offset+evSize <= n; {
|
||||||
|
raw := (*unix.InotifyEvent)(unsafe.Pointer(&buf[offset]))
|
||||||
|
// 事件结构后跟 name(已 NUL 结尾),本程序按 wd 定位路径,忽略 name
|
||||||
|
offset += evSize + int(raw.Len)
|
||||||
|
|
||||||
|
w.mu.Lock()
|
||||||
|
path, ok := w.watches[raw.Wd]
|
||||||
|
w.mu.Unlock()
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
events = append(events, Event{
|
||||||
|
Letters: MaskToLetters(raw.Mask),
|
||||||
|
Path: path,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Runner 把 watcher 包装成「防抖 + 执行 handler + 排空积压」的事件循环。
|
||||||
|
type Runner struct {
|
||||||
|
w *Watcher
|
||||||
|
debounce time.Duration
|
||||||
|
debugf func(format string, args ...any)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRunner 创建事件循环。debugf 可为 nil。
|
||||||
|
func (w *Watcher) NewRunner(debounce time.Duration, debugf func(string, ...any)) *Runner {
|
||||||
|
if debounce <= 0 {
|
||||||
|
debounce = 300 * time.Millisecond
|
||||||
|
}
|
||||||
|
return &Runner{w: w, debounce: debounce, debugf: debugf}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Runner) debug(format string, args ...any) {
|
||||||
|
if r.debugf != nil {
|
||||||
|
r.debugf(format, args...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run 持续监听并触发 handler,直到 stop 被关闭。
|
||||||
|
// handler 执行期间积压的事件会被全部丢弃(防止自身写入节点造成递归)。
|
||||||
|
func (r *Runner) Run(handler func(Event), stop <-chan struct{}) error {
|
||||||
|
var pending *Event
|
||||||
|
var triggeredAt time.Time
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stop:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := -1
|
||||||
|
if pending != nil {
|
||||||
|
elapsed := time.Since(triggeredAt)
|
||||||
|
if elapsed >= r.debounce {
|
||||||
|
timeout = 0
|
||||||
|
} else {
|
||||||
|
timeout = int((r.debounce - elapsed).Milliseconds())
|
||||||
|
if timeout < 0 {
|
||||||
|
timeout = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if timeout != 0 {
|
||||||
|
ready, woken, err := r.w.poll(timeout)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if woken {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if ready {
|
||||||
|
events, err := r.w.ReadEvents()
|
||||||
|
if err != nil {
|
||||||
|
r.debug("读取事件失败: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, ev := range events {
|
||||||
|
ev := ev
|
||||||
|
pending = &ev
|
||||||
|
triggeredAt = time.Now()
|
||||||
|
r.debug("event: %s path: %s", ev.Letters, ev.Path)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if pending == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ev := *pending
|
||||||
|
pending = nil
|
||||||
|
r.debug("debounce fired → handler: %s %s", ev.Letters, ev.Path)
|
||||||
|
handler(ev)
|
||||||
|
|
||||||
|
// 丢弃 handler 执行期间(亮度渐变写入)积压的全部事件
|
||||||
|
drained := 0
|
||||||
|
for {
|
||||||
|
events, err := r.w.ReadEvents()
|
||||||
|
if err != nil || len(events) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
drained += len(events)
|
||||||
|
}
|
||||||
|
if drained > 0 {
|
||||||
|
r.debug("drain: discarded %d event(s)", drained)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop 唤醒 Run 使其退出(配合关闭 stop channel 使用)。
|
||||||
|
func (w *Watcher) Stop() { w.wake() }
|
||||||
|
|
||||||
|
// WatchFileModify 是给单文件(如 config.json)做变更监听的便捷封装。
|
||||||
|
func (w *Watcher) WatchFileModify(path string) error {
|
||||||
|
if _, err := os.Stat(path); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return w.Add(path, "cw")
|
||||||
|
}
|
||||||
@@ -0,0 +1,324 @@
|
|||||||
|
package inotify
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLettersToMaskMapping(t *testing.T) {
|
||||||
|
cases := map[string]uint32{
|
||||||
|
"a": unix.IN_ACCESS,
|
||||||
|
"c": unix.IN_MODIFY,
|
||||||
|
"e": unix.IN_ATTRIB,
|
||||||
|
"w": unix.IN_CLOSE_WRITE,
|
||||||
|
"0": unix.IN_CLOSE_NOWRITE,
|
||||||
|
"r": unix.IN_OPEN,
|
||||||
|
"n": unix.IN_CREATE,
|
||||||
|
"d": unix.IN_DELETE,
|
||||||
|
"D": unix.IN_DELETE_SELF,
|
||||||
|
"M": unix.IN_MOVE_SELF,
|
||||||
|
"m": unix.IN_MOVED_TO,
|
||||||
|
"y": unix.IN_MOVED_FROM,
|
||||||
|
"*": unix.IN_ALL_EVENTS,
|
||||||
|
}
|
||||||
|
for letter, want := range cases {
|
||||||
|
if got := LettersToMask(letter); got != want {
|
||||||
|
t.Errorf("LettersToMask(%q) = %#x, 期望 %#x", letter, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 内核产生的事件字母不应设置 mask
|
||||||
|
for _, letter := range []string{"u", "o", "x", "z"} {
|
||||||
|
if got := LettersToMask(letter); got != 0 {
|
||||||
|
t.Errorf("LettersToMask(%q) 应为 0, 实际 %#x", letter, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if got := LettersToMask("cw"); got != unix.IN_MODIFY|unix.IN_CLOSE_WRITE {
|
||||||
|
t.Errorf("多字母组合错误: %#x", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaskToLettersOrder(t *testing.T) {
|
||||||
|
var mask uint32 = unix.IN_MODIFY | unix.IN_CLOSE_WRITE | unix.IN_CLOSE_NOWRITE
|
||||||
|
if got := MaskToLetters(mask); got != "cw0" {
|
||||||
|
t.Fatalf("MaskToLetters = %q, 期望 cw0", got)
|
||||||
|
}
|
||||||
|
if got := MaskToLetters(0); got != "?" {
|
||||||
|
t.Fatalf("空 mask 应为 ?, 实际 %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// collect 在后台运行 Runner,收集事件。
|
||||||
|
type collector struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
events []Event
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *collector) add(ev Event) {
|
||||||
|
c.mu.Lock()
|
||||||
|
c.events = append(c.events, ev)
|
||||||
|
c.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *collector) snapshot() []Event {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
return append([]Event(nil), c.events...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func startRunner(t *testing.T, w *Watcher, debounce time.Duration, handler func(Event)) chan struct{} {
|
||||||
|
t.Helper()
|
||||||
|
stop := make(chan struct{})
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
_ = w.NewRunner(debounce, nil).Run(handler, stop)
|
||||||
|
}()
|
||||||
|
t.Cleanup(func() {
|
||||||
|
close(stop)
|
||||||
|
w.Stop()
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Error("Run 未能退出")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return stop
|
||||||
|
}
|
||||||
|
|
||||||
|
func waitFor(t *testing.T, timeout time.Duration, cond func() bool) bool {
|
||||||
|
t.Helper()
|
||||||
|
deadline := time.Now().Add(timeout)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
if cond() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
|
return cond()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWatchModifyDeliversEvent(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "brightness")
|
||||||
|
if err := os.WriteFile(path, []byte("100"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w, err := New()
|
||||||
|
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, 30*time.Millisecond, c.add)
|
||||||
|
|
||||||
|
if err := os.WriteFile(path, []byte("200"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !waitFor(t, 2*time.Second, func() bool { return len(c.snapshot()) > 0 }) {
|
||||||
|
t.Fatal("未收到 MODIFY 事件")
|
||||||
|
}
|
||||||
|
ev := c.snapshot()[0]
|
||||||
|
if !strings.Contains(ev.Letters, "c") || ev.Path != path {
|
||||||
|
t.Fatalf("事件内容不正确: %+v", ev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关键兼容性测试:fsnotify 不暴露 CLOSE_WRITE,必须能收到 'w'。
|
||||||
|
func TestCloseWriteLetter(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "node")
|
||||||
|
if err := os.WriteFile(path, []byte("1"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w, err := New()
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("当前环境不支持 inotify: %v", err)
|
||||||
|
}
|
||||||
|
defer w.Close()
|
||||||
|
if err := w.Add(path, "w"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c := &collector{}
|
||||||
|
startRunner(t, w, 30*time.Millisecond, c.add)
|
||||||
|
|
||||||
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0o644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := f.WriteString("2"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !waitFor(t, 2*time.Second, func() bool { return len(c.snapshot()) > 0 }) {
|
||||||
|
t.Fatal("未收到 CLOSE_WRITE 事件")
|
||||||
|
}
|
||||||
|
if got := c.snapshot()[0].Letters; !strings.Contains(got, "w") {
|
||||||
|
t.Fatalf("事件字母应为 w, 实际 %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCloseNoWriteLetter(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "node")
|
||||||
|
if err := os.WriteFile(path, []byte("1"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w, err := New()
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("当前环境不支持 inotify: %v", err)
|
||||||
|
}
|
||||||
|
defer w.Close()
|
||||||
|
if err := w.Add(path, "0"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c := &collector{}
|
||||||
|
startRunner(t, w, 30*time.Millisecond, c.add)
|
||||||
|
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_, _ = f.Read(make([]byte, 1))
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !waitFor(t, 2*time.Second, func() bool { return len(c.snapshot()) > 0 }) {
|
||||||
|
t.Fatal("未收到 CLOSE_NOWRITE 事件")
|
||||||
|
}
|
||||||
|
if got := c.snapshot()[0].Letters; !strings.Contains(got, "0") {
|
||||||
|
t.Fatalf("事件字母应为 0, 实际 %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDebounceCoalescesBurst(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "node")
|
||||||
|
if err := os.WriteFile(path, []byte("1"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w, err := New()
|
||||||
|
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)
|
||||||
|
|
||||||
|
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 }) {
|
||||||
|
t.Fatal("未收到事件")
|
||||||
|
}
|
||||||
|
time.Sleep(300 * time.Millisecond) // 等超过一个防抖窗口
|
||||||
|
if got := len(c.snapshot()); got != 1 {
|
||||||
|
t.Fatalf("防抖后应只触发 1 次, 实际 %d 次", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handler 自身写入被监听文件时,积压事件必须被丢弃,避免递归。
|
||||||
|
func TestDrainPreventsRecursion(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "node")
|
||||||
|
if err := os.WriteFile(path, []byte("1"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w, err := New()
|
||||||
|
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, 100*time.Millisecond, func(ev Event) {
|
||||||
|
c.add(ev)
|
||||||
|
// 模拟亮度渐变写入
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
_ = os.WriteFile(path, []byte("step"), 0o644)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := os.WriteFile(path, []byte("trigger"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !waitFor(t, 2*time.Second, func() bool { return len(c.snapshot()) > 0 }) {
|
||||||
|
t.Fatal("未收到事件")
|
||||||
|
}
|
||||||
|
time.Sleep(400 * time.Millisecond)
|
||||||
|
if got := len(c.snapshot()); got != 1 {
|
||||||
|
t.Fatalf("渐变写入产生的事件应被丢弃, handler 应只执行 1 次, 实际 %d 次", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStopExitsBlockingRun(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "node")
|
||||||
|
if err := os.WriteFile(path, []byte("1"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w, err := New()
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("当前环境不支持 inotify: %v", err)
|
||||||
|
}
|
||||||
|
defer w.Close()
|
||||||
|
if err := w.Add(path, "c"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stop := make(chan struct{})
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
done <- w.NewRunner(time.Second, nil).Run(func(Event) {}, stop)
|
||||||
|
}()
|
||||||
|
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
close(stop)
|
||||||
|
w.Stop() // 唤醒阻塞中的 poll
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-done:
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Run 返回错误: %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("Stop 未能唤醒 Run")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddMissingPathFails(t *testing.T) {
|
||||||
|
w, err := New()
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("当前环境不支持 inotify: %v", err)
|
||||||
|
}
|
||||||
|
defer w.Close()
|
||||||
|
if err := w.Add(filepath.Join(t.TempDir(), "不存在"), "c"); err == nil {
|
||||||
|
t.Fatal("监听不存在的路径应报错")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user