跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

notify_windows_test.go

99 行 · 2.5 KB历史下载
1package limaembed
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "path/filepath"
8 "sync/atomic"
9 "testing"
10 "time"
11
12 "github.com/lima-vm/lima/v2/pkg/hostagent/events"
13)
14
15type countedSyncFile struct {
16 *os.File
17 syncs atomic.Int32
18}
19
20func (w *countedSyncFile) Sync() error {
21 w.syncs.Add(1)
22 return w.File.Sync()
23}
24
25// This uses Windows file notifications and the real Lima event watcher. It
26// starts no VM and writes only synthetic events in the test's temp directory.
27func TestWindowsEventWatchFlush(t *testing.T) {
28 dir := t.TempDir()
29 stdoutPath, stderrPath := filepath.Join(dir, "ha.stdout.log"), filepath.Join(dir, "ha.stderr.log")
30 file, err := os.Create(stdoutPath)
31 if err != nil {
32 t.Fatal(err)
33 }
34 defer file.Close()
35 if err := os.WriteFile(stderrPath, nil, 0600); err != nil {
36 t.Fatal(err)
37 }
38 ctx, cancel := context.WithCancel(context.Background())
39 defer cancel()
40 initial, running, done := make(chan struct{}, 1), make(chan struct{}, 1), make(chan error, 1)
41 go func() {
42 done <- events.Watch(ctx, stdoutPath, stderrPath, time.Time{}, false, func(ev events.Event) bool {
43 if ev.Status.Running {
44 running <- struct{}{}
45 return true
46 }
47 select {
48 case initial <- struct{}{}:
49 default:
50 }
51 return false
52 })
53 }()
54 _, err = fmt.Fprintf(file, "{\"time\":%q,\"status\":{}}\n", time.Now().Format(time.RFC3339Nano))
55 if err != nil {
56 t.Fatal(err)
57 }
58 if err := file.Sync(); err != nil {
59 t.Fatal(err)
60 }
61 select {
62 case <-initial:
63 case <-time.After(3 * time.Second):
64 t.Fatal("watcher did not observe initial event")
65 }
66 // Let the real tailer return to waiting at EOF before the measured write.
67 time.Sleep(200 * time.Millisecond)
68 counted := &countedSyncFile{File: file}
69 writer := &syncWriter{w: newEventWatch(counted, "synthetic-test")}
70 start := time.Now()
71 _, err = fmt.Fprintf(writer, "{\"time\":%q,\"status\":{\"running\":true}}\n", time.Now().Format(time.RFC3339Nano))
72 if err != nil {
73 t.Fatal(err)
74 }
75 received := false
76 select {
77 case <-running:
78 received = true
79 case <-time.After(2 * time.Second):
80 }
81 t.Logf("runningReceived=%t latency=%s forwardedSyncCalls=%d", received, time.Since(start), counted.syncs.Load())
82 writer.flush()
83 cancel()
84 file.Close()
85 select {
86 case err := <-done:
87 if err != nil {
88 t.Fatal(err)
89 }
90 case <-time.After(3 * time.Second):
91 t.Fatal("watcher cleanup timed out")
92 }
93 if !received {
94 t.Fatal("hostagent running event did not reach the file watcher")
95 }
96 if counted.syncs.Load() == 0 {
97 t.Fatal("notification wrapper did not forward Sync to the log file")
98 }
99}