| 1 | //go:build darwin && cgo |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "runtime" |
| 12 | "strings" |
| 13 | "sync" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "github.com/fsnotify/fsnotify" |
| 18 | "golang.org/x/sys/unix" |
| 19 | "reasonix/internal/agent" |
| 20 | "reasonix/internal/config" |
| 21 | "reasonix/internal/provider" |
| 22 | ) |
| 23 | |
| 24 | const darwinLowNoFileChild = "REASONIX_DARWIN_LOW_NOFILE_CHILD" |
| 25 | |
| 26 | func TestDarwinWorkspaceWatcherReportsDeepFileOperations(t *testing.T) { |
| 27 | root := canonicalWorkspaceRoot(t.TempDir()) |
| 28 | w := newDarwinWatcherForTest(t) |
| 29 | if err := w.Add(root, true); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | |
| 33 | deep := filepath.Join(root, "目录 with spaces", "nested") |
| 34 | if err := os.MkdirAll(deep, 0o700); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | path := filepath.Join(deep, "文件.txt") |
| 38 | if err := os.WriteFile(path, []byte("created"), 0o600); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | waitForDarwinWorkspaceEvent(t, w, func(event fsnotify.Event) bool { |
| 42 | return filepath.Clean(event.Name) == path && event.Op&fsnotify.Create != 0 |
| 43 | }) |
| 44 | |
| 45 | if err := os.WriteFile(path, []byte("modified"), 0o600); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | waitForDarwinWorkspaceEvent(t, w, func(event fsnotify.Event) bool { |
| 49 | return filepath.Clean(event.Name) == path && event.Op&fsnotify.Write != 0 |
| 50 | }) |
| 51 | |
| 52 | renamed := filepath.Join(deep, "renamed 文件.txt") |
| 53 | if err := os.Rename(path, renamed); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | waitForDarwinWorkspaceEvent(t, w, func(event fsnotify.Event) bool { |
| 57 | name := filepath.Clean(event.Name) |
| 58 | return (name == path || name == renamed) && event.Op&fsnotify.Rename != 0 |
| 59 | }) |
| 60 | |
| 61 | if err := os.Remove(renamed); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | waitForDarwinWorkspaceEvent(t, w, func(event fsnotify.Event) bool { |
| 65 | return filepath.Clean(event.Name) == renamed && event.Op&fsnotify.Remove != 0 |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | func TestDarwinWorkspaceWatcherHonorsRecursiveModeAndRemove(t *testing.T) { |
| 70 | root := canonicalWorkspaceRoot(t.TempDir()) |
| 71 | nested := filepath.Join(root, "child", "grandchild") |
| 72 | if err := os.MkdirAll(nested, 0o700); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | w := newDarwinWatcherForTest(t) |
| 76 | if err := w.Add(root, false); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | |
| 80 | direct := filepath.Join(root, "direct.txt") |
| 81 | if err := os.WriteFile(direct, []byte("direct"), 0o600); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | waitForDarwinWorkspaceEvent(t, w, func(event fsnotify.Event) bool { |
| 85 | return filepath.Clean(event.Name) == direct |
| 86 | }) |
| 87 | |
| 88 | deep := filepath.Join(nested, "deep.txt") |
| 89 | if err := os.WriteFile(deep, []byte("ignored"), 0o600); err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | assertNoDarwinWorkspaceEvent(t, w, deep, 400*time.Millisecond) |
| 93 | |
| 94 | if err := w.Remove(root); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | removedWatchPath := filepath.Join(root, "after-remove.txt") |
| 98 | if err := os.WriteFile(removedWatchPath, []byte("ignored"), 0o600); err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | assertNoDarwinWorkspaceEvent(t, w, removedWatchPath, 400*time.Millisecond) |
| 102 | |
| 103 | if err := w.Add(root, true); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | if err := os.WriteFile(deep, []byte("recursive"), 0o600); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | waitForDarwinWorkspaceEvent(t, w, func(event fsnotify.Event) bool { |
| 110 | return filepath.Clean(event.Name) == deep |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | func TestDarwinWorkspaceWatcherMapsFlagsAndDegradesHub(t *testing.T) { |
| 115 | op, overflow := darwinWorkspaceEvent(darwinFSEventItemRemoved | darwinFSEventItemRenamed | darwinFSEventItemCreated | darwinFSEventItemModified | darwinFSEventItemXattr) |
| 116 | if overflow || op != fsnotify.Remove|fsnotify.Rename|fsnotify.Create|fsnotify.Write { |
| 117 | t.Fatalf("mapped event = (%v, %v), want all operation bits without overflow", op, overflow) |
| 118 | } |
| 119 | if op, overflow = darwinWorkspaceEvent(darwinFSEventHistoryDone); op != 0 || overflow { |
| 120 | t.Fatalf("HistoryDone mapped to (%v, %v), want ignored", op, overflow) |
| 121 | } |
| 122 | for name, flags := range map[string]uint32{ |
| 123 | "must-scan": darwinFSEventMustScanSubDirs, |
| 124 | "user-dropped": darwinFSEventUserDropped, |
| 125 | "kernel-dropped": darwinFSEventKernelDropped, |
| 126 | "ids-wrapped": darwinFSEventEventIDsWrapped, |
| 127 | "mount": darwinFSEventMount, |
| 128 | "unmount": darwinFSEventUnmount, |
| 129 | } { |
| 130 | t.Run(name, func(t *testing.T) { |
| 131 | if op, overflow := darwinWorkspaceEvent(flags); op != 0 || !overflow { |
| 132 | t.Fatalf("mapped event = (%v, %v), want overflow only", op, overflow) |
| 133 | } |
| 134 | }) |
| 135 | } |
| 136 | if op, overflow = darwinWorkspaceEvent(darwinFSEventRootChanged); op&fsnotify.Rename == 0 || !overflow { |
| 137 | t.Fatalf("RootChanged mapped to (%v, %v), want rename and overflow", op, overflow) |
| 138 | } |
| 139 | |
| 140 | root := t.TempDir() |
| 141 | app := &App{tabs: map[string]*WorkspaceTab{"a": {ID: "a", WorkspaceRoot: root}}} |
| 142 | app.workspaceHub = newWorkspaceChangeHub(app) |
| 143 | t.Cleanup(func() { app.workspaceHub.close() }) |
| 144 | if state := app.WorkspaceRevisionForTab("a").WatchState; state != "active" { |
| 145 | t.Fatalf("initial watch state = %s, want active", state) |
| 146 | } |
| 147 | key := canonicalWorkspaceRoot(root) |
| 148 | app.workspaceHub.mu.Lock() |
| 149 | r := app.workspaceHub.roots[key] |
| 150 | watcher, watcherOK := r.watcher.(*darwinWorkspaceWatcher) |
| 151 | app.workspaceHub.mu.Unlock() |
| 152 | if !watcherOK { |
| 153 | t.Fatalf("watcher type = %T, want Darwin FSEvents watcher", r.watcher) |
| 154 | } |
| 155 | watcher.mu.Lock() |
| 156 | sub := watcher.watches[key] |
| 157 | watcher.mu.Unlock() |
| 158 | if sub == nil { |
| 159 | t.Fatal("workspace root subscription is missing") |
| 160 | } |
| 161 | // RootChanged mapping above proves the combined Rename+overflow contract. |
| 162 | // Inject an overflow-only native flag here so the Hub degradation assertion |
| 163 | // is deterministic and does not also remove the watched root. |
| 164 | sub.publish(key, darwinFSEventMustScanSubDirs) |
| 165 | |
| 166 | deadline := time.Now().Add(time.Second) |
| 167 | for time.Now().Before(deadline) { |
| 168 | app.workspaceHub.mu.Lock() |
| 169 | degraded := r.state == "degraded" && r.allPaths |
| 170 | app.workspaceHub.mu.Unlock() |
| 171 | if degraded { |
| 172 | return |
| 173 | } |
| 174 | time.Sleep(5 * time.Millisecond) |
| 175 | } |
| 176 | t.Fatal("dropped FSEvents notification did not degrade the hub with allPaths invalidation") |
| 177 | } |
| 178 | |
| 179 | func TestDarwinWorkspaceWatcherCoalescesChannelOverflow(t *testing.T) { |
| 180 | w := &darwinWorkspaceWatcher{ |
| 181 | events: make(chan fsnotify.Event, 1), |
| 182 | errors: make(chan error, 1), |
| 183 | watches: make(map[string]*darwinWorkspaceSubscription), |
| 184 | } |
| 185 | event := fsnotify.Event{Name: "/tmp/file", Op: fsnotify.Write} |
| 186 | w.sendEvent(event) |
| 187 | w.sendEvent(event) |
| 188 | w.sendEvent(event) |
| 189 | if err := <-w.errors; !errors.Is(err, fsnotify.ErrEventOverflow) { |
| 190 | t.Fatalf("overflow error = %v", err) |
| 191 | } |
| 192 | select { |
| 193 | case err := <-w.errors: |
| 194 | t.Fatalf("duplicate overflow before recovery: %v", err) |
| 195 | default: |
| 196 | } |
| 197 | <-w.events |
| 198 | w.sendEvent(event) |
| 199 | <-w.events |
| 200 | w.sendEvent(event) |
| 201 | w.sendEvent(event) |
| 202 | if err := <-w.errors; !errors.Is(err, fsnotify.ErrEventOverflow) { |
| 203 | t.Fatalf("overflow after successful recovery = %v", err) |
| 204 | } |
| 205 | w.closed.Store(true) |
| 206 | close(w.events) |
| 207 | close(w.errors) |
| 208 | w.sendEvent(event) |
| 209 | w.sendOverflow() |
| 210 | } |
| 211 | |
| 212 | func TestDarwinWorkspaceWatcherKeepsWorkspaceAndExternalGitScope(t *testing.T) { |
| 213 | base := t.TempDir() |
| 214 | root := filepath.Join(base, "workspace") |
| 215 | gitDir := filepath.Join(base, "external git") |
| 216 | if out, err := exec.Command("git", "init", "--separate-git-dir", gitDir, root).CombinedOutput(); err != nil { |
| 217 | t.Fatalf("git init --separate-git-dir: %v: %s", err, out) |
| 218 | } |
| 219 | generated := filepath.Join(root, "node_modules", "pkg") |
| 220 | if err := os.MkdirAll(generated, 0o700); err != nil { |
| 221 | t.Fatal(err) |
| 222 | } |
| 223 | |
| 224 | app := &App{tabs: map[string]*WorkspaceTab{"a": {ID: "a", WorkspaceRoot: root}}} |
| 225 | app.workspaceHub = newWorkspaceChangeHub(app) |
| 226 | t.Cleanup(func() { app.workspaceHub.close() }) |
| 227 | view := app.WorkspaceRevisionForTab("a") |
| 228 | if view.WatchState != "active" { |
| 229 | t.Fatalf("watch state = %s, want active", view.WatchState) |
| 230 | } |
| 231 | beforeContent := view.Revisions.Content |
| 232 | for index := range 64 { |
| 233 | path := filepath.Join(generated, fmt.Sprintf("generated-%03d.js", index)) |
| 234 | if err := os.WriteFile(path, []byte("generated"), 0o600); err != nil { |
| 235 | t.Fatal(err) |
| 236 | } |
| 237 | } |
| 238 | time.Sleep(300 * time.Millisecond) |
| 239 | view = app.WorkspaceRevisionForTab("a") |
| 240 | if view.Revisions.Content != beforeContent || view.WatchState != "active" { |
| 241 | t.Fatalf("generated churn changed view: before=%d after=%+v", beforeContent, view) |
| 242 | } |
| 243 | |
| 244 | beforeGit := view.Revisions.GitMeta |
| 245 | ref := filepath.Join(gitDir, "refs", "heads", "watch-test") |
| 246 | if err := os.WriteFile(ref, []byte("0000000000000000000000000000000000000000\n"), 0o600); err != nil { |
| 247 | t.Fatal(err) |
| 248 | } |
| 249 | afterGit := waitForDarwinGitRevision(t, app, beforeGit) |
| 250 | afterGit = waitForDarwinGitRevisionToSettle(t, app, afterGit) |
| 251 | objectDir := filepath.Join(gitDir, "objects", "ff") |
| 252 | if err := os.MkdirAll(objectDir, 0o700); err != nil { |
| 253 | t.Fatal(err) |
| 254 | } |
| 255 | if err := os.WriteFile(filepath.Join(objectDir, "ignored"), []byte("object"), 0o600); err != nil { |
| 256 | t.Fatal(err) |
| 257 | } |
| 258 | time.Sleep(300 * time.Millisecond) |
| 259 | if got := app.WorkspaceRevisionForTab("a").Revisions.GitMeta; got != afterGit { |
| 260 | t.Fatalf("Git objects churn advanced metadata revision: before=%d after=%d", afterGit, got) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | func TestDarwinWorkspaceWatcherConcurrentLifecycle(t *testing.T) { |
| 265 | base := t.TempDir() |
| 266 | paths := make([]string, 24) |
| 267 | for index := range paths { |
| 268 | paths[index] = filepath.Join(base, fmt.Sprintf("root-%02d", index)) |
| 269 | if err := os.Mkdir(paths[index], 0o700); err != nil { |
| 270 | t.Fatal(err) |
| 271 | } |
| 272 | } |
| 273 | w, err := newWorkspaceWatcher() |
| 274 | if err != nil { |
| 275 | t.Fatal(err) |
| 276 | } |
| 277 | for _, path := range paths[:8] { |
| 278 | if err := w.Add(path, true); err != nil { |
| 279 | t.Fatal(err) |
| 280 | } |
| 281 | } |
| 282 | start := make(chan struct{}) |
| 283 | errCh := make(chan error, len(paths)) |
| 284 | var wg sync.WaitGroup |
| 285 | for index, path := range paths { |
| 286 | wg.Add(1) |
| 287 | go func(index int, path string) { |
| 288 | defer wg.Done() |
| 289 | <-start |
| 290 | if err := w.Add(path, index%2 == 0); err != nil && !errors.Is(err, fsnotify.ErrClosed) { |
| 291 | errCh <- err |
| 292 | return |
| 293 | } |
| 294 | if err := w.Remove(path); err != nil { |
| 295 | errCh <- err |
| 296 | } |
| 297 | }(index, path) |
| 298 | } |
| 299 | close(start) |
| 300 | if err := w.Close(); err != nil { |
| 301 | t.Fatal(err) |
| 302 | } |
| 303 | wg.Wait() |
| 304 | close(errCh) |
| 305 | for err := range errCh { |
| 306 | t.Errorf("concurrent watcher lifecycle: %v", err) |
| 307 | } |
| 308 | if err := w.Close(); err != nil { |
| 309 | t.Fatalf("second Close: %v", err) |
| 310 | } |
| 311 | darwin := w.(*darwinWorkspaceWatcher) |
| 312 | darwin.mu.Lock() |
| 313 | remaining := len(darwin.watches) |
| 314 | darwin.mu.Unlock() |
| 315 | if remaining != 0 { |
| 316 | t.Fatalf("remaining subscriptions after Close = %d", remaining) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | func TestDarwinWorkspaceWatcherCloseWaitsForConcurrentRemove(t *testing.T) { |
| 321 | watcher, err := newWorkspaceWatcher() |
| 322 | if err != nil { |
| 323 | t.Fatal(err) |
| 324 | } |
| 325 | w := watcher.(*darwinWorkspaceWatcher) |
| 326 | root := canonicalWorkspaceRoot(t.TempDir()) |
| 327 | stopEntered := make(chan struct{}) |
| 328 | allowStop := make(chan struct{}) |
| 329 | sub := &darwinWorkspaceSubscription{ |
| 330 | watcher: w, |
| 331 | path: root, |
| 332 | stopNative: func() { |
| 333 | close(stopEntered) |
| 334 | <-allowStop |
| 335 | }, |
| 336 | } |
| 337 | w.mu.Lock() |
| 338 | w.watches[root] = sub |
| 339 | w.mu.Unlock() |
| 340 | |
| 341 | removeDone := make(chan error, 1) |
| 342 | go func() { removeDone <- w.Remove(root) }() |
| 343 | <-stopEntered |
| 344 | closeDone := make(chan error, 1) |
| 345 | go func() { closeDone <- w.Close() }() |
| 346 | deadline := time.Now().Add(time.Second) |
| 347 | for !w.closed.Load() && time.Now().Before(deadline) { |
| 348 | runtime.Gosched() |
| 349 | } |
| 350 | if !w.closed.Load() { |
| 351 | t.Fatal("Close did not start") |
| 352 | } |
| 353 | select { |
| 354 | case err := <-closeDone: |
| 355 | t.Fatalf("Close returned before concurrent Remove stopped: %v", err) |
| 356 | case <-time.After(50 * time.Millisecond): |
| 357 | } |
| 358 | |
| 359 | close(allowStop) |
| 360 | if err := <-removeDone; err != nil { |
| 361 | t.Fatal(err) |
| 362 | } |
| 363 | if err := <-closeDone; err != nil { |
| 364 | t.Fatal(err) |
| 365 | } |
| 366 | if _, ok := <-w.Events(); ok { |
| 367 | t.Fatal("event channel remained open after Close") |
| 368 | } |
| 369 | if _, ok := <-w.Errors(); ok { |
| 370 | t.Fatal("error channel remained open after Close") |
| 371 | } |
| 372 | if err := w.Close(); err != nil { |
| 373 | t.Fatalf("idempotent Close: %v", err) |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | func TestDarwinWorkspaceWatcherLowFileDescriptorLimit(t *testing.T) { |
| 378 | if os.Getenv(darwinLowNoFileChild) == "1" { |
| 379 | runDarwinLowNoFileChild(t) |
| 380 | return |
| 381 | } |
| 382 | cmd := exec.Command(os.Args[0], "-test.run=^TestDarwinWorkspaceWatcherLowFileDescriptorLimit$", "-test.count=1", "-test.v") |
| 383 | cmd.Env = append(os.Environ(), darwinLowNoFileChild+"=1") |
| 384 | output, err := cmd.CombinedOutput() |
| 385 | if err != nil { |
| 386 | t.Fatalf("low RLIMIT_NOFILE child failed: %v\n%s", err, output) |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | func runDarwinLowNoFileChild(t *testing.T) { |
| 391 | base := t.TempDir() |
| 392 | root := filepath.Join(base, "large workspace") |
| 393 | deep := filepath.Join(root, "one", "two", "three") |
| 394 | if err := os.MkdirAll(deep, 0o700); err != nil { |
| 395 | t.Fatal(err) |
| 396 | } |
| 397 | for index := range 512 { |
| 398 | path := filepath.Join(root, fmt.Sprintf("root-file-%03d.txt", index)) |
| 399 | if err := os.WriteFile(path, []byte("fixture"), 0o600); err != nil { |
| 400 | t.Fatal(err) |
| 401 | } |
| 402 | } |
| 403 | projectConfig := filepath.Join(root, "reasonix.toml") |
| 404 | if err := os.WriteFile(projectConfig, []byte("[agent]\nmemory_compiler = { enabled = true, verbosity = \"compact\" }\n"), 0o600); err != nil { |
| 405 | t.Fatal(err) |
| 406 | } |
| 407 | userConfig := config.UserConfigPath() |
| 408 | if err := os.MkdirAll(filepath.Dir(userConfig), 0o700); err != nil { |
| 409 | t.Fatal(err) |
| 410 | } |
| 411 | if err := os.WriteFile(userConfig, []byte("[agent]\nmemory_compiler = \"compact\"\n"), 0o600); err != nil { |
| 412 | t.Fatal(err) |
| 413 | } |
| 414 | |
| 415 | var oldLimit unix.Rlimit |
| 416 | if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &oldLimit); err != nil { |
| 417 | t.Fatal(err) |
| 418 | } |
| 419 | limit := oldLimit |
| 420 | if limit.Max < 256 { |
| 421 | t.Fatalf("hard RLIMIT_NOFILE = %d, need at least 256", limit.Max) |
| 422 | } |
| 423 | limit.Cur = 256 |
| 424 | if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &limit); err != nil { |
| 425 | t.Fatal(err) |
| 426 | } |
| 427 | t.Cleanup(func() { _ = unix.Setrlimit(unix.RLIMIT_NOFILE, &oldLimit) }) |
| 428 | |
| 429 | app := &App{tabs: map[string]*WorkspaceTab{"a": {ID: "a", WorkspaceRoot: root}}} |
| 430 | app.workspaceHub = newWorkspaceChangeHub(app) |
| 431 | t.Cleanup(func() { app.workspaceHub.close() }) |
| 432 | view := app.WorkspaceRevisionForTab("a") |
| 433 | if view.WatchState != "active" { |
| 434 | t.Fatalf("watch state under RLIMIT_NOFILE=256 = %s", view.WatchState) |
| 435 | } |
| 436 | if fds := darwinOpenFDCount(t); fds >= 128 { |
| 437 | t.Fatalf("FSEvents watcher opened too many descriptors: %d", fds) |
| 438 | } |
| 439 | |
| 440 | deepFile := filepath.Join(deep, "external-write.txt") |
| 441 | beforeContent := view.Revisions.Content |
| 442 | if err := os.WriteFile(deepFile, []byte("external"), 0o600); err != nil { |
| 443 | t.Fatal(err) |
| 444 | } |
| 445 | waitForDarwinContentRevision(t, app, beforeContent) |
| 446 | |
| 447 | changed, err := config.MigrateLegacyMemoryCompilerForRoot(root) |
| 448 | if err != nil || !changed { |
| 449 | t.Fatalf("config migration changed=%v err=%v", changed, err) |
| 450 | } |
| 451 | for _, path := range []string{userConfig, projectConfig} { |
| 452 | raw, err := os.ReadFile(path) |
| 453 | if err != nil { |
| 454 | t.Fatal(err) |
| 455 | } |
| 456 | if strings.Contains(string(raw), "memory_compiler") { |
| 457 | t.Fatalf("legacy memory_compiler remains in %s", path) |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | subagents := filepath.Join(base, "sessions", "subagents") |
| 462 | if err := os.MkdirAll(subagents, 0o700); err != nil { |
| 463 | t.Fatal(err) |
| 464 | } |
| 465 | if cleaned, err := agent.NewSubagentStore(subagents).CleanupStaleRunning(); err != nil || cleaned != 0 { |
| 466 | t.Fatalf("subagent cleanup cleaned=%d err=%v", cleaned, err) |
| 467 | } |
| 468 | |
| 469 | session := agent.NewSession("system") |
| 470 | session.Add(provider.Message{Role: provider.RoleUser, Content: "hello"}) |
| 471 | sessionPath := filepath.Join(base, "sessions", "low-limit.jsonl") |
| 472 | if err := session.SaveSnapshot(sessionPath); err != nil { |
| 473 | t.Fatalf("save session snapshot: %v", err) |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | func newDarwinWatcherForTest(t *testing.T) workspaceWatcher { |
| 478 | t.Helper() |
| 479 | w, err := newWorkspaceWatcher() |
| 480 | if err != nil { |
| 481 | t.Fatal(err) |
| 482 | } |
| 483 | t.Cleanup(func() { _ = w.Close() }) |
| 484 | return w |
| 485 | } |
| 486 | |
| 487 | func waitForDarwinWorkspaceEvent(t *testing.T, watcher workspaceWatcher, match func(fsnotify.Event) bool) fsnotify.Event { |
| 488 | t.Helper() |
| 489 | timer := time.NewTimer(5 * time.Second) |
| 490 | defer timer.Stop() |
| 491 | for { |
| 492 | select { |
| 493 | case event, ok := <-watcher.Events(): |
| 494 | if !ok { |
| 495 | t.Fatal("watcher event channel closed") |
| 496 | } |
| 497 | if match(event) { |
| 498 | return event |
| 499 | } |
| 500 | case err, ok := <-watcher.Errors(): |
| 501 | if !ok { |
| 502 | t.Fatal("watcher error channel closed") |
| 503 | } |
| 504 | t.Fatalf("watcher error: %v", err) |
| 505 | case <-timer.C: |
| 506 | t.Fatal("timed out waiting for FSEvents notification") |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | func assertNoDarwinWorkspaceEvent(t *testing.T, watcher workspaceWatcher, path string, duration time.Duration) { |
| 512 | t.Helper() |
| 513 | timer := time.NewTimer(duration) |
| 514 | defer timer.Stop() |
| 515 | for { |
| 516 | select { |
| 517 | case event, ok := <-watcher.Events(): |
| 518 | if !ok { |
| 519 | return |
| 520 | } |
| 521 | if filepath.Clean(event.Name) == path { |
| 522 | t.Fatalf("unexpected event after non-recursive filter/removal: %v", event) |
| 523 | } |
| 524 | case err, ok := <-watcher.Errors(): |
| 525 | if ok { |
| 526 | t.Fatalf("watcher error: %v", err) |
| 527 | } |
| 528 | case <-timer.C: |
| 529 | return |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | func waitForDarwinContentRevision(t *testing.T, app *App, before uint64) uint64 { |
| 535 | t.Helper() |
| 536 | deadline := time.Now().Add(5 * time.Second) |
| 537 | for time.Now().Before(deadline) { |
| 538 | if got := app.WorkspaceRevisionForTab("a").Revisions.Content; got > before { |
| 539 | return got |
| 540 | } |
| 541 | time.Sleep(10 * time.Millisecond) |
| 542 | } |
| 543 | t.Fatalf("content revision did not advance beyond %d", before) |
| 544 | return before |
| 545 | } |
| 546 | |
| 547 | func waitForDarwinGitRevision(t *testing.T, app *App, before uint64) uint64 { |
| 548 | t.Helper() |
| 549 | deadline := time.Now().Add(5 * time.Second) |
| 550 | for time.Now().Before(deadline) { |
| 551 | if got := app.WorkspaceRevisionForTab("a").Revisions.GitMeta; got > before { |
| 552 | return got |
| 553 | } |
| 554 | time.Sleep(10 * time.Millisecond) |
| 555 | } |
| 556 | t.Fatalf("Git metadata revision did not advance beyond %d", before) |
| 557 | return before |
| 558 | } |
| 559 | |
| 560 | func waitForDarwinGitRevisionToSettle(t *testing.T, app *App, last uint64) uint64 { |
| 561 | t.Helper() |
| 562 | stableSince := time.Now() |
| 563 | deadline := time.Now().Add(3 * time.Second) |
| 564 | for time.Now().Before(deadline) { |
| 565 | time.Sleep(10 * time.Millisecond) |
| 566 | got := app.WorkspaceRevisionForTab("a").Revisions.GitMeta |
| 567 | if got != last { |
| 568 | last = got |
| 569 | stableSince = time.Now() |
| 570 | continue |
| 571 | } |
| 572 | if time.Since(stableSince) >= 150*time.Millisecond { |
| 573 | return got |
| 574 | } |
| 575 | } |
| 576 | t.Fatal("Git metadata revision did not settle") |
| 577 | return last |
| 578 | } |
| 579 | |
| 580 | func darwinOpenFDCount(t *testing.T) int { |
| 581 | t.Helper() |
| 582 | var limit unix.Rlimit |
| 583 | if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &limit); err != nil { |
| 584 | t.Fatal(err) |
| 585 | } |
| 586 | maxFD := min(limit.Cur, 4096) |
| 587 | open := 0 |
| 588 | for fd := range maxFD { |
| 589 | if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); err == nil { |
| 590 | open++ |
| 591 | } |
| 592 | } |
| 593 | return open |
| 594 | } |
| 595 |