| 1 | //go:build linux |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | // The wake loop over a real directory and the real inotify watch. Only |
| 6 | // the pass is stood in for: it records why it started, and does what a |
| 7 | // case needs while it "reads the refs". What a real pass does is |
| 8 | // integrator-e2e's. |
| 9 | |
| 10 | import ( |
| 11 | "errors" |
| 12 | "io" |
| 13 | "io/fs" |
| 14 | "os" |
| 15 | "path/filepath" |
| 16 | "strings" |
| 17 | "sync" |
| 18 | "testing" |
| 19 | "time" |
| 20 | ) |
| 21 | |
| 22 | type loopRun struct { |
| 23 | t *testing.T |
| 24 | dir string |
| 25 | stop chan struct{} |
| 26 | done chan error |
| 27 | mu sync.Mutex |
| 28 | starts []passStart |
| 29 | stderr strings.Builder |
| 30 | } |
| 31 | |
| 32 | type passStart struct { |
| 33 | reason passReason |
| 34 | at time.Time |
| 35 | due bool // whether due was there when the pass began |
| 36 | } |
| 37 | |
| 38 | // startLoop runs a wake loop over dir. during, if set, runs inside pass n |
| 39 | // (from 0) and says what the pass returns. |
| 40 | func startLoop(t *testing.T, dir string, interval time.Duration, during func(n int) (bool, error)) *loopRun { |
| 41 | t.Helper() |
| 42 | r := &loopRun{t: t, dir: dir, stop: make(chan struct{}), done: make(chan error, 1)} |
| 43 | l := &wakeLoop{ |
| 44 | dir: dir, |
| 45 | interval: interval, |
| 46 | stderr: lockedWriter{&r.mu, &r.stderr}, |
| 47 | watch: watchDir, |
| 48 | stop: r.stop, |
| 49 | pass: func(reason passReason) (bool, error) { |
| 50 | _, err := os.Lstat(filepath.Join(dir, "due")) |
| 51 | r.mu.Lock() |
| 52 | n := len(r.starts) |
| 53 | r.starts = append(r.starts, passStart{reason, time.Now(), err == nil}) |
| 54 | r.mu.Unlock() |
| 55 | if during != nil { |
| 56 | return during(n) |
| 57 | } |
| 58 | return false, nil |
| 59 | }, |
| 60 | } |
| 61 | go func() { r.done <- l.run() }() |
| 62 | t.Cleanup(func() { |
| 63 | select { |
| 64 | case <-r.stop: |
| 65 | default: |
| 66 | close(r.stop) |
| 67 | } |
| 68 | <-r.done |
| 69 | }) |
| 70 | return r |
| 71 | } |
| 72 | |
| 73 | type lockedWriter struct { |
| 74 | mu *sync.Mutex |
| 75 | w io.Writer |
| 76 | } |
| 77 | |
| 78 | func (l lockedWriter) Write(p []byte) (int, error) { |
| 79 | l.mu.Lock() |
| 80 | defer l.mu.Unlock() |
| 81 | return l.w.Write(p) |
| 82 | } |
| 83 | |
| 84 | func (r *loopRun) passes() []passStart { |
| 85 | r.mu.Lock() |
| 86 | defer r.mu.Unlock() |
| 87 | return append([]passStart(nil), r.starts...) |
| 88 | } |
| 89 | |
| 90 | func (r *loopRun) said() string { |
| 91 | r.mu.Lock() |
| 92 | defer r.mu.Unlock() |
| 93 | return r.stderr.String() |
| 94 | } |
| 95 | |
| 96 | // waitPasses waits until n passes have started, and fails after limit. |
| 97 | func (r *loopRun) waitPasses(n int, limit time.Duration) []passStart { |
| 98 | r.t.Helper() |
| 99 | deadline := time.Now().Add(limit) |
| 100 | for { |
| 101 | p := r.passes() |
| 102 | if len(p) >= n { |
| 103 | return p |
| 104 | } |
| 105 | if time.Now().After(deadline) { |
| 106 | r.t.Fatalf("%d passes after %s, not %d: %v\n%s", len(p), limit, n, p, r.said()) |
| 107 | } |
| 108 | time.Sleep(5 * time.Millisecond) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // still says the loop starts no pass beyond n over d. |
| 113 | func (r *loopRun) still(n int, d time.Duration) { |
| 114 | r.t.Helper() |
| 115 | time.Sleep(d) |
| 116 | if p := r.passes(); len(p) != n { |
| 117 | r.t.Fatalf("%d passes, not %d: %v\n%s", len(p), n, p, r.said()) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func markWake(t *testing.T, dir string) { |
| 122 | f, err := os.OpenFile(filepath.Join(dir, "due"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o440) |
| 123 | if errors.Is(err, fs.ErrExist) { |
| 124 | return |
| 125 | } |
| 126 | if err != nil { |
| 127 | t.Error(err) |
| 128 | return |
| 129 | } |
| 130 | f.Close() |
| 131 | } |
| 132 | |
| 133 | func reasons(p []passStart) []passReason { |
| 134 | var out []passReason |
| 135 | for _, s := range p { |
| 136 | out = append(out, s.reason) |
| 137 | } |
| 138 | return out |
| 139 | } |
| 140 | |
| 141 | func wakeDir(t *testing.T) string { |
| 142 | dir := filepath.Join(t.TempDir(), "wake") |
| 143 | if err := os.Mkdir(dir, 0o770); err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | return dir |
| 147 | } |
| 148 | |
| 149 | // A mark made before the start is removed before the first pass reads. A |
| 150 | // mark made during a pass, after the removal, starts exactly one more pass |
| 151 | // when it ends, and the notification it left starts nothing after that. |
| 152 | func TestAMarkDuringAPassStartsOneMoreAndNoOther(t *testing.T) { |
| 153 | dir := wakeDir(t) |
| 154 | markWake(t, dir) |
| 155 | r := startLoop(t, dir, time.Hour, func(n int) (bool, error) { |
| 156 | if n == 0 { |
| 157 | markWake(t, dir) |
| 158 | } |
| 159 | return false, nil |
| 160 | }) |
| 161 | p := r.waitPasses(2, 5*time.Second) |
| 162 | if p[0].reason != reasonStartup || p[0].due { |
| 163 | t.Fatalf("the first pass was %v, and due was there when it read: %v", p[0].reason, p[0].due) |
| 164 | } |
| 165 | if p[1].reason != reasonDue || p[1].due { |
| 166 | t.Fatalf("the pass after a mark made during the first was %v, due there: %v", p[1].reason, p[1].due) |
| 167 | } |
| 168 | r.still(2, 300*time.Millisecond) |
| 169 | |
| 170 | // Idle, a mark starts a pass promptly. |
| 171 | marked := time.Now() |
| 172 | markWake(t, dir) |
| 173 | p = r.waitPasses(3, 5*time.Second) |
| 174 | if p[2].reason != reasonDue { |
| 175 | t.Fatalf("a mark while idle started a %v pass", p[2].reason) |
| 176 | } |
| 177 | t.Logf("mark to pass start, idle: %s", p[2].at.Sub(marked)) |
| 178 | r.still(3, 300*time.Millisecond) |
| 179 | } |
| 180 | |
| 181 | // A burst of marks while a pass runs is one more pass, and the last pass |
| 182 | // starts after the last mark. |
| 183 | func TestABurstOfMarksIsCoalesced(t *testing.T) { |
| 184 | dir := wakeDir(t) |
| 185 | r := startLoop(t, dir, time.Hour, func(int) (bool, error) { |
| 186 | time.Sleep(100 * time.Millisecond) |
| 187 | return false, nil |
| 188 | }) |
| 189 | r.waitPasses(1, 5*time.Second) |
| 190 | var last time.Time |
| 191 | var wg sync.WaitGroup |
| 192 | var mu sync.Mutex |
| 193 | for i := 0; i < 8; i++ { |
| 194 | wg.Add(1) |
| 195 | go func() { |
| 196 | defer wg.Done() |
| 197 | for j := 0; j < 25; j++ { |
| 198 | markWake(t, dir) |
| 199 | mu.Lock() |
| 200 | last = time.Now() |
| 201 | mu.Unlock() |
| 202 | time.Sleep(time.Millisecond) |
| 203 | } |
| 204 | }() |
| 205 | } |
| 206 | wg.Wait() |
| 207 | time.Sleep(500 * time.Millisecond) |
| 208 | p := r.passes() |
| 209 | if len(p) < 2 || len(p) > 4 { |
| 210 | t.Fatalf("200 marks over ~30ms started %d passes: %v", len(p)-1, reasons(p)) |
| 211 | } |
| 212 | if !p[len(p)-1].at.After(last) { |
| 213 | t.Fatalf("no pass started after the last mark") |
| 214 | } |
| 215 | if entries, _ := os.ReadDir(dir); len(entries) != 0 { |
| 216 | t.Fatalf("the idle directory holds %v", entries) |
| 217 | } |
| 218 | t.Logf("200 marks: %d passes after the first", len(p)-1) |
| 219 | } |
| 220 | |
| 221 | // A pass that lands is followed at once by another, and one that does not |
| 222 | // is followed by nothing until a mark or the timer. |
| 223 | func TestALandingIsFollowedAtOnce(t *testing.T) { |
| 224 | dir := wakeDir(t) |
| 225 | r := startLoop(t, dir, time.Hour, func(n int) (bool, error) { return n == 0, nil }) |
| 226 | p := r.waitPasses(2, 5*time.Second) |
| 227 | if p[1].reason != reasonFollowUp { |
| 228 | t.Fatalf("the pass after a landing was %v", p[1].reason) |
| 229 | } |
| 230 | if _, err := os.Lstat(filepath.Join(dir, "due")); err == nil { |
| 231 | t.Fatalf("the follow-up was marked through the directory") |
| 232 | } |
| 233 | r.still(2, 300*time.Millisecond) |
| 234 | } |
| 235 | |
| 236 | // The interval starts a pass when nothing else does. |
| 237 | func TestTheIntervalIsTheRecoveryPass(t *testing.T) { |
| 238 | dir := wakeDir(t) |
| 239 | r := startLoop(t, dir, 150*time.Millisecond, nil) |
| 240 | p := r.waitPasses(3, 5*time.Second) |
| 241 | if got := reasons(p[:3]); got[1] != reasonTimer || got[2] != reasonTimer { |
| 242 | t.Fatalf("an idle loop passed for %v", got) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // A due that cannot be removed starts the pass it is found at, and then |
| 247 | // nothing until the timer, however often it is marked. |
| 248 | func TestADueThatCannotBeRemovedDoesNotSpin(t *testing.T) { |
| 249 | dir := wakeDir(t) |
| 250 | r := startLoop(t, dir, 400*time.Millisecond, nil) |
| 251 | r.waitPasses(1, 5*time.Second) |
| 252 | // A due that is a directory with something in it cannot be removed. |
| 253 | if err := os.MkdirAll(filepath.Join(dir, "due", "x"), 0o700); err != nil { |
| 254 | t.Fatal(err) |
| 255 | } |
| 256 | p := r.waitPasses(2, 5*time.Second) |
| 257 | if p[1].reason != reasonDue { |
| 258 | t.Fatalf("a new due started a %v pass", p[1].reason) |
| 259 | } |
| 260 | time.Sleep(time.Second) |
| 261 | p = r.passes() |
| 262 | for _, s := range p[2:] { |
| 263 | if s.reason != reasonTimer { |
| 264 | t.Fatalf("a due that could not be removed started %v", reasons(p)) |
| 265 | } |
| 266 | } |
| 267 | if len(p) > 6 { |
| 268 | t.Fatalf("%d passes in about 1.2s at a 400ms interval: %v", len(p), reasons(p)) |
| 269 | } |
| 270 | if !strings.Contains(r.said(), "could not remove") { |
| 271 | t.Fatalf("the failed removal was not said: %s", r.said()) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // A pass that failed is not followed by another before the hold, whatever |
| 276 | // is marked meanwhile. |
| 277 | func TestAFailedPassHoldsOff(t *testing.T) { |
| 278 | dir := wakeDir(t) |
| 279 | r := startLoop(t, dir, time.Hour, func(int) (bool, error) { return false, errors.New("the refs did not read") }) |
| 280 | r.waitPasses(1, 5*time.Second) |
| 281 | for i := 0; i < 5; i++ { |
| 282 | markWake(t, dir) |
| 283 | time.Sleep(100 * time.Millisecond) |
| 284 | } |
| 285 | r.still(1, 0) |
| 286 | if !strings.Contains(r.said(), "the refs did not read") { |
| 287 | t.Fatalf("the failure was not said: %s", r.said()) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // A directory removed under the watch leaves the loop on its timer, and |
| 292 | // the loop watches again once the directory is back, with one pass for |
| 293 | // whatever was marked where nothing watched. |
| 294 | func TestARemovedDirectoryIsWatchedAgain(t *testing.T) { |
| 295 | dir := wakeDir(t) |
| 296 | r := startLoop(t, dir, time.Hour, nil) |
| 297 | r.waitPasses(1, 5*time.Second) |
| 298 | if err := os.RemoveAll(dir); err != nil { |
| 299 | t.Fatal(err) |
| 300 | } |
| 301 | time.Sleep(200 * time.Millisecond) |
| 302 | if !strings.Contains(r.said(), "ended") { |
| 303 | t.Fatalf("the ended watch was not said: %s", r.said()) |
| 304 | } |
| 305 | if err := os.Mkdir(dir, 0o770); err != nil { |
| 306 | t.Fatal(err) |
| 307 | } |
| 308 | p := r.waitPasses(2, 2*watchRetry) |
| 309 | if p[1].reason != reasonWatcher { |
| 310 | t.Fatalf("the pass after the watch came back was %v", p[1].reason) |
| 311 | } |
| 312 | markWake(t, dir) |
| 313 | p = r.waitPasses(3, 5*time.Second) |
| 314 | if p[2].reason != reasonDue { |
| 315 | t.Fatalf("a mark in the directory made again started a %v pass", p[2].reason) |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // A directory missing at the start is said, the start's pass runs, and |
| 320 | // the directory is watched once it exists. |
| 321 | func TestAMissingDirectoryIsWatchedOnceMade(t *testing.T) { |
| 322 | dir := filepath.Join(t.TempDir(), "wake") |
| 323 | r := startLoop(t, dir, time.Hour, nil) |
| 324 | if p := r.waitPasses(1, 5*time.Second); p[0].reason != reasonStartup { |
| 325 | t.Fatalf("the first pass was %v", p[0].reason) |
| 326 | } |
| 327 | if !strings.Contains(r.said(), "not watching") { |
| 328 | t.Fatalf("the missing directory was not said: %s", r.said()) |
| 329 | } |
| 330 | if err := os.Mkdir(dir, 0o770); err != nil { |
| 331 | t.Fatal(err) |
| 332 | } |
| 333 | markWake(t, dir) |
| 334 | p := r.waitPasses(2, 2*watchRetry) |
| 335 | if p[1].reason != reasonWatcher { |
| 336 | t.Fatalf("the first pass once the directory was made was %v", p[1].reason) |
| 337 | } |
| 338 | r.still(2, 300*time.Millisecond) |
| 339 | } |
| 340 | |
| 341 | // A file where the directory should be is no directory to watch. |
| 342 | func TestAFileIsNoWakeDirectory(t *testing.T) { |
| 343 | file := filepath.Join(t.TempDir(), "wake") |
| 344 | if err := os.WriteFile(file, nil, 0o600); err != nil { |
| 345 | t.Fatal(err) |
| 346 | } |
| 347 | if _, err := watchDir(file); err == nil { |
| 348 | t.Fatalf("a file was watched as a wake directory") |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // A timer pass with nothing to say prints nothing; any other pass opens |
| 353 | // with its reason. |
| 354 | func TestATimerPassSaysNothingUnlessItHasSomethingToSay(t *testing.T) { |
| 355 | var b strings.Builder |
| 356 | newPassOutput(&b, reasonTimer) |
| 357 | if b.String() != "" { |
| 358 | t.Fatalf("an idle timer pass said %q", b.String()) |
| 359 | } |
| 360 | io.WriteString(newPassOutput(&b, reasonTimer), "c1 -> refs/heads/main: land\n") |
| 361 | newPassOutput(&b, reasonDue) |
| 362 | if want := "pass: timer\nc1 -> refs/heads/main: land\npass: due\n"; b.String() != want { |
| 363 | t.Fatalf("the passes said %q, not %q", b.String(), want) |
| 364 | } |
| 365 | } |