| 1 | package limaembed |
| 2 | |
| 3 | import ( |
| 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 | |
| 15 | type countedSyncFile struct { |
| 16 | *os.File |
| 17 | syncs atomic.Int32 |
| 18 | } |
| 19 | |
| 20 | func (w *countedSyncFile) Sync() error { |
| 21 | w.syncs.Add(1) |
| 22 | return w.File.Sync() |
| 23 | } |
| 24 | |
| 25 | |
| 26 | |
| 27 | func 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 | |
| 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 | } |